ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Arduino digitalWrite() 반응속도
    아두이노 2021. 3. 31. 15:58

    01. Basics_Blink 예제

     

    Blink | Arduino

     

    Blink

    Open-source electronic prototyping platform enabling users to create interactive electronic objects.

    www.arduino.cc

    Blink 예제 프로젝트는 아두이노 보드 외부에 LED 를 연결하여 깜빡이는 것을 보기 위한 예제이다.

    // the setup function runs once when you press reset or power the board
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    }
    
    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);                       // wait for a second
    }

    pinMode() 를 사용하여 어떤 핀을 LED 에 연결할 것인지 결정한다.

    여기서는 LED_BUILTIN 을 OUTPUT 으로 사용하겠다고 정의하였다.

     

    LED_BUILTIN 은 어디에 정의 되어있느냐??

    C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h 에 정의되어 있다. 

    LED_BUILTIN 은 핀번호 13번으로 정의되어 있는데 읽기전용이라 수정이 불가능하다.

    만약 핀번호를 바꾸고 싶다면 핀번호를 다시 정의해서 사용하는게 좋겠다.

    #define LED_BUILTIN 12
    
    // the setup function runs once when you press reset or power the board
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    }

    12번 핀에 LED 를 연결하면 깜빡이는 것을 볼 수 있다.

     

     

     

    digitalWrite() 의 반응속도는 어느정도일까?

     

    delay() 가 없이 digitalWrite() 만 사용할 경우 얼마나 빨리 값을 변화시킬 수 있을까??

    그래서 한번 실험을 해보았다.

     

    // the setup function runs once when you press reset or power the board
    void setup() {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    }
    
    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      //delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      //delay(1000);                       // wait for a second
    }

    Oscilloscope 로 확인해보자.

     

    digitalWrite() 실행시간 = 5.5 us 

     

    clock frequency = 16 Mhz ( 62.5 ns )

    5.5 us / 62.5 ns = 88 개

     

    digitalWrite() 를 처리하기 위해 88 개의 명령을 처리하는 시간이 필요하다.

    digitalWrite() 를 사용하여 delay() 없이 사용했을때 최대 180Khz pulse 를 생성 가능하다. 

    또한 중간중간 interrupt 처리등으로 5.5 us 보다 길어질 때도 있다.

     

    Microchip Studio - digitalWrite() 정의 

     

    궁금해서 digitalWrite() 정의를 찾아보았다.

    void digitalWrite(uint8_t pin, uint8_t val)
    {
    uint8_t timer = digitalPinToTimer(pin);
    uint8_t bit = digitalPinToBitMask(pin);
    uint8_t port = digitalPinToPort(pin);
    volatile uint8_t *out;
    
    if (port == NOT_A_PIN) return;
    
    // If the pin that support PWM output, we need to turn it off
    // before doing a digital write.
    if (timer != NOT_ON_TIMER) turnOffPWM(timer);
    
    out = portOutputRegister(port);
    
    uint8_t oldSREG = SREG;
    cli();
    
    if (val == LOW) {
    *out &= ~bit;
    } else {
    *out |= bit;
    }
    
    SREG = oldSREG;
    }

    이런저런 일들을 처리한다고 88 cycle 이 필요한가보다.......

    '아두이노' 카테고리의 다른 글

    Touch Sense Key Matrix (HW-136 , 8229BSF)  (0) 2021.04.06
    Arduino digitalRead() 반응속도  (0) 2021.04.01
    Arduino Mega 2560 회로정리  (0) 2021.03.31
Designed by Tistory.