Hello Everyone,
In my project, I use Quad Timer (TMR4_CH2) to count up to a value (TMR4_COMP12) and then trigger an interrupt. In the interrupt, I create a PWM signal. I am successful in doing so, however I still have questions regarding some of the registers. My (working) code is below, it outputs a 23.42Hz signal at 80% duty.
In the code I have an LED blinking for a heartbeat, within the loop() function.
The question is... when do I enable interrupts using TMR4_CSCTRL2 vs TMR4_SCTRL2? Within the function Setup_TMR4_CH2(), I have the line with TMR4_SCTRL2 commented out in the working software, because if I do include this line, then the system will hang up, the PWM signals is whacky, and the heartbeat function within loop() does not work.
Does anyone know why this is? Why is it that within TMR4_CSCTRL2 I have to enable TCF1EN (Timer Compare 1 Interrupt Enable) but I am not supposed to enable TCFIE (Timer Compare Flag Interrupt Enable) within TMR4_SCTRL2?
Also, just FYI, within the ISR, I must clear the TCF flag of TMR4_SCTRL2, otherwise the system will jam up.
Also - I have tried manipulating TMR4_CMPLD12, thinking that when the ISR is invoked, this value would override TMR4_COMP12, however this is not the case. This is why I directly change TMR4_COMP12 within my ISR. Does anyone know why this is such?
Thank You!
In my project, I use Quad Timer (TMR4_CH2) to count up to a value (TMR4_COMP12) and then trigger an interrupt. In the interrupt, I create a PWM signal. I am successful in doing so, however I still have questions regarding some of the registers. My (working) code is below, it outputs a 23.42Hz signal at 80% duty.
In the code I have an LED blinking for a heartbeat, within the loop() function.
Code:
const byte pin_LED = 13;
const byte pinOut_OutputSig = 11;
boolean LED_st = 1;
const unsigned long LED_time_ms = 500;
boolean OutputSig_st = 1;
int i = 0;
unsigned long t1,t2;
///////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
pinMode(pin_LED,OUTPUT);
pinMode(pinOut_OutputSig,OUTPUT);
digitalWrite(pin_LED,HIGH);
digitalWrite(pinOut_OutputSig,OutputSig_St);
delay(25);
Setup_TMR4_CH2();
t1 = millis();
t2 = t1;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
t1 = millis();
if(((t1-t2) >= LED_time_ms) || (t2 > t1))
{
// Heartbeat function
LED_st = !LED_st;
digitalWrite(pin_LED,LED_st);
Serial.print(t1/1000);
Serial.println();
t2 = t1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
void Dimitri_ISR()
{
//if((TMR4_SCTRL2 & TMR_SCTRL_TCF) != 0)
//if((TMR4_SCTRL2 & TMR_SCTRL_TOF) != 0)
OutputSig_St = !OutputSig_St;
digitalWrite(pinOut_OutputSig,OutputSig_St);
if(OutputSig_St == 1)
{
TMR4_COMP12 = 40000;
}
else
{
TMR4_COMP12 = 10000;
}
TMR4_CSCTRL2 &= ~(TMR_CSCTRL_TCF1); // clear
__asm volatile ("dsb");
}
///////////////////////////////////////////////////////////////////////////////////////////////
void Setup_TMR4_CH2()
{
TMR4_CTRL2 = 0; // stop
TMR4_LOAD2 = 0; // start val after compare
TMR4_COMP12 = 0xF000; // count up to this val, interrupt, and start again
TMR4_CMPLD12 = 0xF000;
TMR4_CTRL2 = TMR_CTRL_CM(1) | TMR_CTRL_PCS(8 + 7) | TMR_CTRL_LENGTH ; // prescale
attachInterruptVector(IRQ_QTIMER4, Dimitri_ISR);
TMR4_CSCTRL2 &= ~(TMR_CSCTRL_TCF1); // clear
TMR4_CSCTRL2 |= TMR_CSCTRL_TCF1EN; // enable interrupt
//TMR4_SCTRL2 |= TMR_SCTRL_TCFIE;
NVIC_ENABLE_IRQ(IRQ_QTIMER4);
}
The question is... when do I enable interrupts using TMR4_CSCTRL2 vs TMR4_SCTRL2? Within the function Setup_TMR4_CH2(), I have the line with TMR4_SCTRL2 commented out in the working software, because if I do include this line, then the system will hang up, the PWM signals is whacky, and the heartbeat function within loop() does not work.
Does anyone know why this is? Why is it that within TMR4_CSCTRL2 I have to enable TCF1EN (Timer Compare 1 Interrupt Enable) but I am not supposed to enable TCFIE (Timer Compare Flag Interrupt Enable) within TMR4_SCTRL2?
Also, just FYI, within the ISR, I must clear the TCF flag of TMR4_SCTRL2, otherwise the system will jam up.
Also - I have tried manipulating TMR4_CMPLD12, thinking that when the ISR is invoked, this value would override TMR4_COMP12, however this is not the case. This is why I directly change TMR4_COMP12 within my ISR. Does anyone know why this is such?
Thank You!