Hello all,
I am having problem of setting timer interrupt frequency in M0 board. I read the timer function in SAMD21 datasheet and on the internet i found the following example code which generates interrupt on specific time. But i dont know this calculation. How to generate an interrupt at every one seconds using this.
long x;
void setup()
{
//Serial5.begin(9600);
delay(100);
REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | 0x1B) ;
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
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 = 250;
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() {
// put your main code here, to run repeatedly:
}
void TC3_Handler()
{
TcCount16* TC = (TcCount16*) TC3; // get timer struct
if (TC->INTFLAG.bit.MC0 == 1)
{
++x;
//Serial5.println(TARGETSTEPS1);
TC->INTFLAG.bit.MC0 = 1; // writing a one clears the flag ovf flag
}
}