본문 바로가기
Programming/Embedded

[Arduino Due]-micro periodic library test code

by No Brainer 2022. 2. 19.

/*

 * 아두이노 라이브러리에서 제공하는 함수 micro()를 이용하여 timer interrupt를 사용하지 않고 

 * 주기적인 연산을 수행하는 방법을 살펴보자. 아두이노 듀에 보드의 13번(PB27)을 주기적으로 ON/OFF

 * 제어하는 것을 구성으로 한다. 

 * NVIC를 제공하지 않는 보드(ATMEGA시리즈)에서 타이머 인터럽트를 사용하여 처리하며 

 * 타이머 인터럽트보다 발생빈도가 높은 인터럽트를 같이 사용할 경우,

 * 타이머인터럽트가 ISR을 실행하고 있을때 발생빈도가 더 높은 인터럽트가 발생해도 처리할 수

 * 없게 된다. 이런 경우 타이머인터럽트를 사용하지 않는 micro()를 사용할 수 있다

 */

 

float sample_time = 0.5;

uint32_t MicrosSampleTime;//마이크로초 단위로 환산한 sample time

uint32_t start_time, end_time;//마이크로초 단위의, 주기적 연산의 시작시간과 종료시간

 

void setup() 

{

  pmc_enable_periph_clk(ID_PIOB);//PIOB를 사용하기 위해 PIOB로 가는 CLOCK을 활성화한다

 

  PIOB->PIO_PER = PIO_PB27;  //PB27(pin 13)

  PIOB->PIO_IDR = PIO_PB27;  //interrupt disable

  PIOB->PIO_OER = PIO_PB27;  //set as output

 

  MicrosSampleTime = (uint32_t)(sample_time*1e6); //us로 치환

  start_time = micros();

  end_time = start_time + MicrosSampleTime;

}

 

void loop() {

  static uint8_t flag=0;

 

  if(flag==0)

  {

    PIOB->PIO_CODR =PIO_PB27;  //LED on

    flag=1;

  }

  else

  {

    PIOB->PIO_SODR =PIO_PB27;  //LED off

    flag=0;

  }

 

  //**

  while(!((end_time - micros()) & 0x80000000));  //0x800... : 음수

  end_time += MicrosSampleTime;

 

}