I am unable to get the interrupts to work on my Arduino Due while using the Timer Counter to generate a Waveform. I tried to enable both RA and RC Compare, but neither is being triggered. I would like the frequency generated to remain at the 1.7MHz it is currently at, but I need to implement some instructions on the edges.
Any help would be appreciated!
/******************************************************************************/
/*** 1.7MHz/ 50% Duty cycle PWM thru TIOA0 ***/
/******************************************************************************/
#define led_pin 13
int interruptCtr = 1;
void setup() {
Serial.begin(115200);
pinMode(led_pin, OUTPUT);
Serial.println("code began");
/************* Timer Counter 0 Channel 0 to generate PWM pulses thru TIOA0 ************/
pmc_set_writeprotect(false); // disable write protection for pmc registers
TC0->TC_CHANNEL[0].TC_IER=TC_IER_CPAS; //enable RA compare interrupt
TC0->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; //enable RC compare interrupt
TC0->TC_CHANNEL[0].TC_IDR=~TC_IER_CPAS; // IDR = interrupt disable register
TC0->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS; // IDR = interrupt disable register
TC0->TC_CHANNEL[0].TC_IMR=TC_IMR_CPAS; //enable RA compare interrupt
TC0->TC_CHANNEL[0].TC_IMR=TC_IMR_CPCS; //enable RC compare interrupt
PMC->PMC_PCER0 |= PMC_PCER0_PID27; // Timer Counter 0 channel 0 IS TC0, TCO power ON
PMC->PMC_PCER0 |= PMC_PCER0_PID12; // PIOB power ON, page 38
PIOB->PIO_PDR |= PIO_PDR_P25;
PIOB->PIO_ABSR |= PIO_ABSR_P25; // PB25 is driven by the TC, peripheral type B, page 858
TC0->TC_CHANNEL[0].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 // MCK/2, 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 TIOA0 on RA compare match
| TC_CMR_ACPC_SET; // Set TIOA0 on RC compare match
TC0->TC_CHANNEL[0].TC_RC = 24; //<********************* Frequency = (Mck/2)/TC_RC = 1.7 MHz
TC0->TC_CHANNEL[0].TC_RA = 12; //<******************** Duty cycle = (TC_RA/TC_RC) * 100 = 50 %
TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC0 counter and enable
NVIC_EnableIRQ(TC0_IRQn);
interrupts();
}
ISR(TIMER0_COMPA_vect)
{
digitalWrite(led_pin,digitalRead(led_pin)^1); //toggle led pin
Serial.println("interrupted RA");
}
ISR(TIMER0_COMPC_vect)
{
digitalWrite(led_pin,digitalRead(led_pin)^1); //toggle led pin
Serial.println("interrupted RC");
}
void loop() {
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.