Hi,
I am trying to use input capture (shown in code below) feature along with TFT display (1.8inch TFT display). Now when i individually test the input capture code it works as expected(results observed on serial port). But when i initialise the TFT library and then call InitTimer5(). the code gets stuck.
void setup() {
Serial.begin(115200);
DEBUG(1);
digitalWrite(LCD_LED,HIGH);
DEBUG(2);
TFTscreen.begin();
TFTscreen.setRotation(90);
TFTscreen.background(0, 0, 0);
//ICP configuarion setup
DEBUG(3);
InitTimer5();
DEBUG("timer initialized");
StartTimer5();
ChannelSelection('r'); //For glucose RED led will be used// For proto model only
}
Is timer 5 also used by the TFT library??
#define DEBUG(x) Serial.println(x)
typedef enum {
CAPTURE_1,
CAPTURE_2,
WAIT
} timer_state_t;
volatile timer_state_t flag = WAIT;
// J:This is a 16-bit timer, so these values will always fit into an unsigned int
volatile unsigned int Capt1, Capt2, CaptOvr;
// J:Mind as well make this unsigned and give it 2x range since it can never be negative.
volatile unsigned long T5Ovs;
unsigned char doubleOverflowError=0;
void InitTimer5(void)
{
//Set Initial Timer value
// J:All measurements against TCNT are relative, so no need to reset
// TCNT1=0;
// J: Note we need to set up all the timer control bits because we do not know what state they are in
// J: If, for example, the WGM bits are set to a PWM mode then the TCNT is going to be resetting out from under us rather than monotonically counting up to MAX
TCCR5A = 0x00;
//First capture on rising edge
TCCR5B =(1<<ICES5);
//Enable input capture and overflow interrupts
//Timer interrupt mask register (Input capture intrrupt and overflow interrupt enable)
TIMSK5|=(1<<ICIE5)|(1<<TOIE5);
}
// J: Note that it would be ok to start the timer when we assign TCCR1B in InitTimer since nothing will happen when the ISR is called until we set flag to CAPTURE1
void StartTimer5(void)
{
//Start timer without prescaler
// J: Note that we know that the other CS bits are 0 becuase of the Assignment in InitTimer
TCCR5B |= (1<<CS50);
//Enable global interrutps
// J: Interrupts are turned on by Arduino platform startup code
// sei();
}
ISR(TIMER5_CAPT_vect) {
switch(flag) {
case CAPTURE_1:
Capt1 = ICR5;
// J: Reset the overflow to 0 each time we start a measurement
T5Ovs=0;
doubleOverflowError=0;
flag = CAPTURE_2;
break;
case CAPTURE_2:
Capt2 = ICR5;
// J: Grab a snap shot of the overflow count since the timer will keep counting (and overflowing);
CaptOvr = T5Ovs;
flag = WAIT;
//J: Generally bad to print in ISRs
//Serial.println(flag);
break;
}
}
ISR(TIMER5_OVF_vect)
{
T5Ovs++;
// J: Just to be correct, check for overflow of the overflow, otherwise if it overflows we would get an incorrect answer.
if (!T5Ovs) {
doubleOverflowError=1;
}
}
unsigned long sensor_freq()
{
unsigned long count=0;
unsigned long frequency=0;
// J: No need to bracket this set with cli() becuase the counter will not be counting until wait is updated
flag = CAPTURE_1;
while (flag != WAIT); //Need to figure way to eliminate this wait time.
// J: Parenthesis and explicit cast for good luck! ( and to ensure correct size and order for operations)
count=( (unsigned long) (Capt2) + (CaptOvr * 0x10000UL) )-Capt1;
frequency= 0.0625*count; //16Mhz ^-1 = 0.0625 e-6
frequency=1000000/frequency;
return(frequency);
}