This link shows how we do the timer interrupts on Arduino Uno or any board with ATMEL 328/168. Following this instruction, now I can change the interrupt frequency at whatever I want.
However, now I want to do this on Arduino Due, and I searched for some time but got nothing. Could anyone can teach me how I can do the same things on Due or show me where I can learn this with some examples...
Thanks very much!
Search in the DUE sub forum for Timer Counter or PWM programming. TC_lib or PWM_lib from antodom are a good starting point plus there are numerous example sketches with direct register programing either for Timer Counters or PWM.
Hi, many thanks for ard_newbie. I have also found your code for another question.
/******************************************************************/
/* Timer Counter 2 Channel 2, namely TC8, 1 MHz frequency */
/******************************************************************/
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
tc_setup();
}
void loop() {
}
void tc_setup() {
PMC->PMC_PCER1 |= PMC_PCER1_PID35; // TC8 power ON : Timer Counter 2 channel 2 IS TC8 - see page 38
//PIOD->PIO_PDR |= PIO_PDR_P7; // Set the pin to the peripheral
//PIOD->PIO_ABSR |= PIO_PD7B_TIOA8; // Peripheral type B
TC2->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 // MCK/2 = 42 M Hz, clk on rising edge
| TC_CMR_WAVE // Waveform mode
| TC_CMR_WAVSEL_UP_RC; // UP mode with automatic trigger on RC Compare
// | TC_CMR_ACPA_CLEAR // Clear TIOA2 on RA compare match
// | TC_CMR_ACPC_SET; // Set TIOA2 on RC compare match
TC2->TC_CHANNEL[2].TC_RC = 42; //<********************* Frequency = (Mck/2)/TC_RC Hz
//TC2->TC_CHANNEL[2].TC_RA = 21; //<******************** Any Duty cycle in between 1 and TC_RC
TC2->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;//TC_IER_CPAS;
NVIC_EnableIRQ(TC8_IRQn);
TC2->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable
}
void TC8_Handler() {
static uint32_t Count;
TC2->TC_CHANNEL[2].TC_SR; // Read and clear status register
if (Count++ == 1000000) {
Count = 0;
PIOB->PIO_ODSR ^= PIO_ODSR_P27; // Toggle LED every 1 Hz
}
}
Now, I came up with some new questions.... If you can answer it or anyone else, I will very appreciate it.
MC->PMC_PCER1 |= PMC_PCER1_PID35; // TC8 power ON : Timer Counter 2 channel 2 IS TC8 - see page 38
- Whether I change number 35 to 27, the TC0 will be switched on? The instance ID of TC0 is 27 based on the datasheet.
TC2->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 // MCK/2 = 42 M Hz, clk on rising edge
- Whether the reason why MCK is divided by 2 is [2]? If I would like to change it, should I change that?
Moreover, I don't quite understand the TC2 and CLCOK1 here... Can they be changed?
TC2->TC_CHANNEL[2].TC_RC = 42; //<********************* Frequency = (Mck/2)/TC_RC Hz
3. I assume this is a kind of Compare Match. Can we change it? What is the maximum value? Is the maximum value the same as other TC0, TC1, TC2, etc.?
- Finally question. I assume that TC is counting for a specific value to have a timer interrupt. Suppose I would like to interrupt this timer interrupt and let it restart counting from 0. How do I do that?
Many many many thanks!
Topics on the same subject merged
Why did you start a new topic with a follow-on question ?