Ardunio Due 中斷
/*
The code implement Timer Interrupt for Arduino Due, to toggle LED every
second and send the durationin millisecond to PC via Serial port.
*/
int led = 13;
volatile boolean ledon;
volatile unsigned long lasttime;
volatile unsigned long NOW;
int FREQ_1000Hz = 1000; // 中斷頻率1k Hz
void TC3_Handler(){
TC_GetStatus(TC1, 0);
NOW = micros();
digitalWrite(led, ledon = !ledon);
Serial.println(NOW - lasttime); //間隔時間(micro-sec)
lasttime = NOW;
}
void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency){
//Enable or disable write protect of PMC registers.
pmc_set_writeprotect(false);
//Enable the specified peripheral clock.
pmc_enable_periph_clk((uint32_t)irq);
TC_Configure(tc, channel, TC_CMR_WAVE|TC_CMR_WAVSEL_UP_RC|TC_CMR_TCCLKS_TIMER_CLOCK4);
uint32_t rc = VARIANT_MCK/128/frequency;
TC_SetRA(tc, channel, rc/2);
TC_SetRC(tc, channel, rc);
TC_Start(tc, channel);
tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
tc->TC_CHANNEL[channel].TC_IDR = ~TC_IER_CPCS;
NVIC_EnableIRQ(irq);
}
void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
startTimer(TC1, 0, TC3_IRQn, FREQ_1000Hz);
lasttime = 0;
}
void loop() {
}
本文程式碼內容轉載自 Implement Timer Interrupt for Arduino Due
有關於普通的 Arduino 中斷可參考 Arduino – 中斷功能