mnpumar:
[quote author=Sebastian Vik link=topic=130423.msg984328#msg984328 date=1352148520]
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:
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/IRQ | TC | Channel | Due pins |
| - | - | - | - |
| TC0 | TC0 | 0 | 2, 13 |
| TC1 | TC0 | 1 | 60, 61 |
| TC2 | TC0 | 2 | 58 |
| TC3 | TC1 | 0 | none <- this line in the example above |
| TC4 | TC1 | 1 | none |
| TC5 | TC1 | 2 | none |
| TC6 | TC2 | 0 | 4, 5 |
| TC7 | TC2 | 1 | 3, 10 |
| TC8 | TC2 | 2 | 11, 12 |
Noob question here, could someone please explain the inputs to the startTimer function? What is channel, and how do we use the frequency input? Also, what are the pins in the table for?
[/quote]
Guys! can anyone kindly explain me why this code does not work for the frequencies upper than 3 KHZ?
I have tested 1Hz to 3000Hz for the frequency but seems further frequencies it has problem.
What's the acceptable range of frequency?
We did a loop as follows.
void loop() {
startTimer(TC1, 0, TC3_IRQn, 1);
Serial.println("1Hz");
delay(5000);
startTimer(TC1, 0, TC3_IRQn, 4000); // LIne 6
Serial.println("4KHz");
delay(3000);
}
This frequency is used to control the speed of motor.
When we set the second frequency (Line 6 ) to 4000HZ it won't return to the 1HZ as it is supposed to.
We tried 1 to 3KHZ and it works but more than 4KHZ it goes weird!
Any solution please?