1

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 ?

2
  • This question comes from the other question: arduino.stackexchange.com/questions/44847/…
    – Jot
    Sep 19 '17 at 18:31
  • @Jot Thanks for redirecting. The ram is not full and I am aware that Timer2 has nothing to do with the library. I assume it could be delay within the library.
    – flowian
    Sep 20 '17 at 5:46
1

You're getting interrupt every 16th CPU clock cycle. The ISR handler might be even longer than that and your code and lower priority ISRs suffers by "starvation" as MCU can't do anything else than processing ISR.

If you use prescaler value 8, then you have 1/2us resolution of TCNT2 and overflow interrupt (in normal mode) occurs every 128us, so there will be enough time to do something else. However, if you want atomic value of TCNT2 and counted value from ISR handler, you'll have to stop Timer2 for a while.

1
  • How to set timer2 for overflow interrupt at 1/2 us ? I have not used TCNT2 before for ISR.
    – flowian
    Sep 19 '17 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.