0
\$\begingroup\$

I'm using an ATSAM4E16C microcontroller and i need to get a 1 millisecond tick. I want to implement this using the timer counter module to trigger an interrupt. The clock frequency is 96 MHz, so using a prescaler of 128 and a compare of 750 i should get 1 millisecond. Problem is if i try to activate the interrupt, when the program reaches the delay_ms function if freezes (i think), and the debugger stops working (still connected)

void timer_init(void)
{   
    sysclk_enable_peripheral_clock(ID_TC0);
    //init TC0 channel 0 with a prescaler of 128 and WAVEFORM
    tc_init(TC0, 0, TC_CMR_TCCLKS_TIMER_CLOCK4 
                |TC_CMR_WAVE
                |TC_CMR_WAVSEL_UP_RC);
    tc_write_rc(TC0, 0, 750);

    // trigger interrupt on RA compare
    tc_enable_interrupt(TC0, 0, TC_IER_CPCS);

}

int main (void)
{
    wdt_disable(WDT);
    sysclk_init();
    board_init();

    irq_initialize_vectors();
    cpu_irq_enable();

    NVIC_DisableIRQ(TC0_IRQn);
    NVIC_ClearPendingIRQ(TC0_IRQn);
    NVIC_SetPriority(TC0_IRQn, 0);
    NVIC_EnableIRQ(TC0_IRQn);

    timer_init();
    tc_start(TC0, 0);

    delay_ms(10);
    thermocouple_init(&thermocouple);
    delay_ms(300);
    thermocouple_read(&thermocouple);
    set_led(true);
}

I've tried also with different interrupt priorities but still the same result. Any ideas as to why this doesn't work? Thanks

\$\endgroup\$
  • \$\begingroup\$ First you probably already have a 1 mS systick interrupt running, so this is probably redundant. As for your actual problem, you may be trying to use a timer that is required by the millisecond delay function. Another issue is that you don't show any handler for the interrupt you are trying to activate. \$\endgroup\$ – Chris Stratton Dec 27 '18 at 22:06
  • \$\begingroup\$ I tried chaning the value for the counter to match, for example 1 second, and still the same problem. Also the handler doesn't matter, it has nothing in it. \$\endgroup\$ – user3808318 Dec 27 '18 at 22:26
  • \$\begingroup\$ On the contrary, the handler does matter - we need to see that you validly wrote and linked it, otherwise you may be going to some random invalid address, or falling through to some default fault handler which the linker has substituted via a weak symbol when it didn't find your not-quite-correctly-defined intended handler. And of course beware that interrupts can make debugging quite messy, depending on how your debugger is configured. \$\endgroup\$ – Chris Stratton Dec 27 '18 at 22:31
  • \$\begingroup\$ void TC0_Handler(void) { toggle_led(); }, this is the handler. Actually i just discovered that it is executed once (or atleast the toggle function is called) and then no more, as the led stays on forever, but it does turn on after the specified time \$\endgroup\$ – user3808318 Dec 27 '18 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.