Hello Folks,
I want to use Timer interrupt in Arduino Due and I am referring 2manyProjects Tutorial and found this code, Please find the code below:
// These are the clock frequencies available to the timers /2,/8,/32,/128
// 84Mhz/2 = 42.000 MHz
// 84Mhz/8 = 10.500 MHz
// 84Mhz/32 = 2.625 MHz
// 84Mhz/128 = 656.250 KHz
//
// 42Mhz/44.1Khz = 952.38
// 10.5Mhz/44.1Khz = 238.09
// 2.625Hmz/44.1Khz = 59.5
// 656Khz/44.1Khz = 14.88 // 131200 / 656000 = .2 (.2 seconds)
// 84Mhz/44.1Khz = 1904 instructions per tick
const int led_pin = 13;
int state = false;
int interruptCtr = 1;
int S = 0;
void setup()
{
pinMode(led_pin, OUTPUT);
/* turn on the timer clock in the power management controller */
pmc_set_writeprotect(false); // disable write protection for pmc registers
pmc_enable_periph_clk(ID_TC7); // enable peripheral clock TC7
/* we want wavesel 01 with RC */
TC_Configure(/* clock */TC2,/* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
TC_SetRC(TC2, 1, 131200);
TC_Start(TC2, 1);
// enable timer interrupts on the timer
TC2->TC_CHANNEL[1].TC_IER=TC_IER_CPCS; // IER = interrupt enable register
TC2->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS; // IDR = interrupt disable register
/* Enable the interrupt in the nested vector interrupt controller */
/* TC4_IRQn where 4 is the timer number * timer channels (3) + the channel number (=(1*3)+1) for timer1 channel1 */
NVIC_EnableIRQ(TC7_IRQn);
}
void loop()
{
// do nothing timer interrupts will handle the blinking;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERRUPT HANDLERS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TC7_Handler()
{
// We need to get the status to clear it and allow the interrupt to fire again
TC_GetStatus(TC2, 1);
state = !state;
digitalWrite(led_pin, state);
if( interruptCtr++ >= 6 )
{
interruptCtr = 1;
S = !S; // are we flashing S or O
if( S ) // set time till next interrupt
TC_SetRC(TC2, 1, 131200); // 131200 / 656000 = .2 seconds
else
TC_SetRC(TC2, 1, 656000); // 656000/ 656000 = 1 second
}
}
I have basic understanding of Timer Interrupt. Now when I was referring the tutorial many doubts came into my mind
- In the above project why it has selected /128 (I know that 84Mhz/128=656Khz)and why not /2,/8,/32?
2)"// 656Khz/44.1Khz = 14.88 // 131200 / 656000 = .2 (.2 seconds)" I mean how does he got this calculation what does this means?
- In TC_Configure Which conditions are taken into consideration selection of TC0,TC1,TC2 and their respective channels and what are the criterias for Clocks are assigned to Timer Counters?
Please Help.