Show Posts
Pages: [1]
1  Products / Arduino Due / Re: Timer Interrupts on Due on: May 06, 2013, 02:17:03 pm
I got frequency between 1-21KHz to work, but I found another problem. I can't get 2 timers to run simultaneously. I ran the startTimer function twice with different handlers, but they refuse to work. Anyone know how to fix this problem?
2  Products / Arduino Due / Re: Timer Interrupts on Due on: April 20, 2013, 03:29:31 pm
Using this code, you can set a timer for any of the ISRs TC0_Handler through TC8_Handler, see table of parameters below. It is possible to use the timers without a physically mapped pin, such as TC1 channel 0 (TC3_Handler) shown here:

Code:
volatile boolean l;

//TC1 ch 0
void TC3_Handler()
{
        TC_GetStatus(TC1, 0);
        digitalWrite(13, l = !l);
}

void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
        pmc_set_writeprotect(false);
        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; //128 because we selected TIMER_CLOCK4 above
        TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        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(13,OUTPUT);
        startTimer(TC1, 0, TC3_IRQn, 4); //TC1 channel 0, the IRQ for that channel and the desired frequency
}

void loop(){
}

Here is the table of parameters:
ISR/IRQTC        ChannelDue pins
TC0TC002, 13
TC1TC0160, 61
TC2TC0258
TC3TC10none  <- this line in the example above
TC4TC11none
TC5TC12none
TC6TC204, 5
TC7TC213, 10
TC8TC2211, 12


I'm a bit confused about the code for the startTimer function.

-What is the purpose of TC3_Handler() and how does it interact with startTimer()?
-Once startTimer() is called, how do I stop the timer(how do I disable PWM)?
-There's a few lines that I don't understand in startTimer():


Code:

        TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4); <--- How does TC_Configure() work? Where can I find reference about this?
        uint32_t rc = VARIANT_MCK/128/frequency; <---------- is VARIANT_MCK the frequency of arduino DUE(84MHz)? What does rc do?
        TC_SetRA(tc, channel, rc/2); //50% high, 50% low<----sets the duty cycle?
        TC_SetRC(tc, channel, rc);<---------- what is this line for?
        TC_Start(tc, channel);<---------------enables PWM? How does it work?
        tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;<----------What does this line do?
        tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;<--------What does this line do?
        NVIC_EnableIRQ(irq); <-----enables interrupt?

Pages: [1]