i am trying use TCB1 in "pulse width meaurement" mode, with the pulse being routed from pin D3 to EVENT SYSTEM PERIPHERAL via Channel 4 to end user of event TCB1. the TCB1 is configured for Positive edge trigger start
I am generating the pulse manually and hence the negative edge of the pulse might take longer to occur and the TCB1.CNT is bound to overflow... but, here the Software crashes and has to be reset to start again. i have taken care of the overflow case also via ISR, but, still have the problem of crash. Any help is appreciated.
#include <megaAVR_TimerInterrupt.h>
#include <megaAVR_TimerInterrupt.hpp>
#include <megaAVR_ISR_Timer.h>
#include <megaAVR_ISR_Timer.hpp>
#include <SoftwareSerial.h>
// variables , FLAGS ;
static bool timerb0_OVRFL_FLAG = false ;
static bool timerb1_OVRFL_FLAG = false ;
static bool timerb2_OVRFL_FLAG = false ;
unsigned int loopycounty = 0 ;
static unsigned int timerb1FREQ_CNT ;
static bool Status_Flag_b1_freqcnt ;
static unsigned int timerb1DUTY_CNT ;
static bool Status_Flag_b1_dutycnt ;
void setup()
{
// put your setup code here, to run once:
PORTA.DIR = 0x01 ;
PORTA.OUT = 0x0 ;
PORTB.DIR = 0x07 ;
PORTB.OUT = 0x0 ;
PORTC.DIR = 0x40 ;
PORTC.OUT = 0x0 ;
PORTD.DIR = 0x07 ;
PORTD.OUT = 0x0 ;
// port E all are i/ps so no initial value needed
PORTF.DIR = 0x10 ;
PORTF.OUT = 0x0 ;
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
/* Disable interrupts */
cli();//stop interrupts
// Timers TCB1
TCB1.CTRLB = 0x04 ;
TCB1.EVCTRL = 0x01 ;
TCB1.INTCTRL = 0x01 ;
TCB1.CNT = 0 ;
// EVSYS Drivers
EVSYS.CHANNEL4 = 0x4D ;
EVSYS.USERTCB1 = 0x05 ; // TCB1 to CH4
sei();//allow interrupts
}
void loop()
{
// put your main code here, to run repeatedly:
TCB1.CTRLA = 0x01 ;
do
{
if(timerb1_OVRFL_FLAG)
{
timerb1_OVRFL_FLAG = false ;
Serial.print("\n\t Timer B1 overflowed");
}
if((Status_Flag_b1_freqcnt) && (Status_Flag_b1_dutycnt))
{
Status_Flag_b1_freqcnt = false ;
Status_Flag_b1_dutycnt = false ;
//Serial.print("\n\t Timer B1 Freq - ") ;
//Serial.print(timerb1FREQ_CNT) ;
Serial.print("\n\t Timer B1 Duty - ") ;
Serial.print(timerb1DUTY_CNT) ;
TCB1.INTCTRL = 0x01 ;
}
delay(1000) ;
loopycounty += 1 ;
Serial.print("\n\t ARDUINO NANO EVERY is looping now ");
Serial.print(loopycounty) ;
}while (1) ;
}
ISR(TCB1_OVF_vect)
{
TCB1.INTCTRL = 0x00 ;
timerb1_OVRFL_FLAG = true ;
PORTA.OUT = 0x01 ; // USING THIS PIN to signal interrupt occurrence for debug only
}
ISR(TCB1_CAPT_vect)
{
TCB1.INTCTRL = 0x00 ;
//timerb1FREQ_CNT = TCB1.CNT ;
timerb1DUTY_CNT = TCB1.CCMP ;
PORTA.OUT = 0x01 ; // USING THIS PIN to signal interrupt occurrence for debug only
Status_Flag_b1_freqcnt = true ;
Status_Flag_b1_dutycnt = true ;
}