개발자/Arduino

nano 33 iot 보드 타이머 인터럽트 예제

지구빵집 2020. 10. 20. 11:14
반응형

 

 

모듈식 신세사이저 클럭-Modular synth clock. 원래 게시글

BPM 및 듀티 사이클은 두 개의 로터리 엔코더를 사용하여 업데이트됩니다. 출력은 Eurorack Modular 신디사이저를 제어하기 위해 잭 소켓을 통해 0-3.3v입니다. 배경: 저는 NANO 33 IoT로 작업 해 왔으며 UNO / MEGA에서 코드를 전송할 때 타이머에 여러 문제가있었습니다. 이 때문에 SAM D21 플랫폼에서 내부 타이머 (TC)를 배우고 사용하는 방법을 배울 때라고 생각합니다. 이 스레드의 게시물 # 8에서 제공한 Markus Bader 코드는 많은 도움이되었습니다 (아래 링크). 

BPM이 120 (4/4)이고 듀티 사이클이 50 % 인 SAMD21 TC 카운트 기능의 기본 예를 누구나 공유 할 수 있습니까? BPM 및 듀티 사이클 값을 모두 업데이트 할 수 있습니다.

  • -분당 박동수 : 120BPM
  • -초당 비트 : 2Hz
  • -1 비트 길이 : 0.5 초
  • -1 bar 길이 : 2 초
  • 참조 링크 : 아래 참고 사이트 링크 

또한 공식 문서 (Atmel-42123-SAM-Timer-Counter- (TC) -Driver_ApplicationNote_AT03263)는 읽을 가치가 있으며 손에 넣어야합니다. 이제 일부 용어와 방법을 이해했지만 여전히 혼란스럽습니다.

내 프로젝트는 CV (제어 전압) 출력이있는 SM Tik Tak BPM 클록을 기반으로합니다. 로터리 인코더와 OLED 디스플레이가 추가되었습니다. 이것이 효과적으로 작동하려면 타이머 카운트가 기본 기능이어야하며 다른 활동 (인코더, 디스플레이 업데이트 등)에 의해 차단 될 수 없습니다. 원래 프로젝트는 타이머 라이브러리를 사용했지만 SAM D21 플랫폼과 호환되지 않습니다. 

이것은 여러 Eurorack 모듈의 기초로 사용할 수있는 간단한 프로젝트입니다.

회로 구성 요소 :

  • -NANO 33 IoT
  • -BPM 로터리 엔코더
  • -듀티 사이클 로터리 엔코더
  • -BPM 출력, 3.5mm 잭 소켓
  • -반분 할 출력, 잭 소켓
  • -1/4 분할 출력, 잭 소켓
  • -8 분할 출력, 잭 소켓
  • -OLED 디스플레이 (BPM, Duty Cycle, Divisions) 

 

/*
  
   Testing with Arduino NANO 33 IoT
   @author Markus Bader
   @brief this program shows how to use the TC timer with interrupts on an Arduino Zero board
   @email markus.bader@tuwien.ac.at

   https://forum.arduino.cc/index.php?topic=332275.0
   Post: #8
*/

int pin_ovf_led = 2;  // debug pin for overflow led
int pin_mc0_led = 5;  // debug pin for compare led
unsigned int loop_count = 0;
unsigned int irq_ovf_count = 0;

void setup() {

  pinMode(pin_ovf_led, OUTPUT);   // for debug leds
  digitalWrite(pin_ovf_led, LOW); // for debug leds
  pinMode(pin_mc0_led, OUTPUT);   // for debug leds
  digitalWrite(pin_mc0_led, LOW); // for debug leds

  // Enable clock for TC
  REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_TCC2_TC3) ;
  while ( GCLK->STATUS.bit.SYNCBUSY == 1 ); // wait for sync

  // The type cast must fit with the selected timer mode
  TcCount16* TC = (TcCount16*) TC3; // get timer struct

  TC->CTRLA.reg &= ~TC_CTRLA_ENABLE;   // Disable TC
  while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync

  TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT16;  // Set Timer counter Mode to 16 bits
  while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
  TC->CTRLA.reg |= TC_CTRLA_WAVEGEN_NFRQ; // Set TC as normal Normal Frq
  while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync

  TC->CTRLA.reg |= TC_CTRLA_PRESCALER_DIV256;   // Set perscaler (1,2,4,8,64,256,1024)
  //TC->CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1024;   // Set perscaler
  while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync

  // TC->PER.reg = 0xFF;   // Set counter Top using the PER register but the 16/32 bit timer counts allway to max
  // while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync

  TC->CC[0].reg = 0xFFF;
  while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync

  // Interrupts
  TC->INTENSET.reg = 0;              // disable all interrupts
  TC->INTENSET.bit.OVF = 1;          // enable overfollow
  TC->INTENSET.bit.MC0 = 1;          // enable compare match to CC0

  // Enable InterruptVector
  NVIC_EnableIRQ(TC3_IRQn);

  // Enable TC
  TC->CTRLA.reg |= TC_CTRLA_ENABLE;
  while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync

}

void loop() {
  // dummy
  delay(500);
}

void TC3_Handler()
{
  TcCount16* TC = (TcCount16*) TC3; // get timer struct

  if (TC->INTFLAG.bit.OVF == 1) {  // A overflow caused the interrupt
    digitalWrite(pin_ovf_led, irq_ovf_count % 2); // for debug leds
    digitalWrite(pin_mc0_led, HIGH); // for debug leds
    TC->INTFLAG.bit.OVF = 1;    // writing a one clears the flag ovf flag
    irq_ovf_count++;                 // for debug leds
  }

  if (TC->INTFLAG.bit.MC0 == 1) {  // A compare to cc0 caused the interrupt
    digitalWrite(pin_mc0_led, LOW);  // for debug leds
    TC->INTFLAG.bit.MC0 = 1;    // writing a one clears the flag ovf flag
  }

}

 

 

 

참고사이트

BPM calculator 

Arduino ZERO timer interrupts 

Modular synth clock module DIY ARDUINO, "SM Tik-Tak" 

Smoothly Changing a Timer’s Frequency on the Arduino Zero 

위 프로젝트의 코드가 있는 곳 Arduino Zero Timer Demo 

 

 

nano 33 iot 센서값 전송

 

 

 

 

반응형