I want to set up real time counter along with lcd functions from LiquidCrystal library. But they don't work together. However, they work fine separately.
The code is working by itself, but by implementing counter function, LCD just freezes with other functionality.
Part of code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7, 8, 9, 13, 12, 10);
volatile boolean timerFlag = false;
volatile unsigned long int tmr2Count = 0;
void setup(){
timer2Init();
}
void loop(){
checkState();
}
//everything works by commenting out ISR and timer2Init
//===================================================
ISR (TIMER2_COMPA_vect){
if (timerFlag)
tmr2Count++;
}
void timerStart(void){
if ( !timerFlag )
timerFlag = true;
}
void timerReset(void){
timerFlag = false;
tmr2Count = 0;
}
void timer2Init(void){
//Timer 2 interrupt service routine CTC settings, 1 uS:
TCCR2A = 0;
TCCR2B = 0;
//set CTC mode
TCCR2A |= (1 << WGM21);
//prescaler 1 for timer2
TCCR2B |= (1 << CS20);
// prescaler for 1 uS.
OCR2A = 15;
//set compare match for register OCRA
TIMSK2 |= ( 1 << OCIE2A);
}
//========================================================
// menu
void checkState(void)
{
// if button is pressed without menu being displayed
if (userSettings.currentState == STATE_IDLE && encButton == true){
userSettings.currentState = MENU_CHOICE;
lcd.clear();
lcd.setCursor(3,1);
lcd.print("<Settings>");
encButton = false;
}
int menuState = userSettings.currentState;
switch(menuState)
{
case STATE_IDLE:
timeDisplay();
dateDisplay();
break;
case MENU_CHOICE:
menuSettingDisplay();
break;
case MENU_CONTRAST:
contrastSetting();
break;
case MENU_TIME:
timeSetting();
break;
case MENU_DATE:
setDate();
break;
case TIME_DISPLAY:
timeDisplayOnOff();
break;
case DATE_DISPLAY:
dateDisplay();
break;
default:
break;
}
}
I digged in LiquidCrystal.cpp and found that there is DelayMicroseconds function, I'm thinking that it could be the cause. If it's not, what can be done otherwise ?