If I set timer2 to Compare Match mode, my LCD display stops working. If I use Overflow Interrupt it works fine.
Here's my test code. I have a loop() function which does its thing, and a timer interrupt which increments another variable, just to show it's working.
#include <LiquidCrystal.h>
#include <avr/interrupt.h>
#include <avr/io.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); //Pins for the freetronics 16x2 LCD shield. LCD:
int i;
volatile int j;
void setup()
{
i=0;
j=0;
//button adc input
pinMode( A0, INPUT ); //ensure A0 is an input
digitalWrite( A0, LOW ); //ensure pullup is off on A0
//lcd backlight control
pinMode( 10, OUTPUT ); //D10 is an output
digitalWrite( 10, HIGH ); //backlight control pin D10 is high (on)
//set up the LCD number of columns and rows:
lcd.begin( 16, 2 );
// setup timer interrupt
cli(); //disable global interrupts
TCCR1A = 0; //set tccr1a and tccr1b to 0
TCCR1B = 0;
//set cs10 and cs12 bit so timer runs at clock iSpeed/ 1024
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// Compare Match Mode **Doesn't work**:
//set compare match register
OCR1A = 15624;
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// enable Timer1 compare interrupt
TIMSK1 = (1 << OCIE1A);
// Overflow mode **Works**
// enable overflow interrupt
// TIMSK1 = (1 << TOIE1);
//enable global interrupt
sei();
}
void loop()
{
lcd.setCursor( 0, 0 ); //top left
lcd.print(i);
lcd.setCursor(0,1); //bottom left
lcd.print(j);
delay(500);
lcd.clear();
i++;
}
ISR(TIMER1_OVF_vect)
{
j++;
}
If I use overflow interrupts it works fine, the loop() counter is displayed on the top line and the interrupt counter on the second, but by using overflow interrupts I'm limited to the whatever timing the various prescalers give me.
If I use compare match the LCD display just shows faint flickering squares on its first line, like it's doing something but too quickly for the LCD to display.
I looked at the source for liquidcrystal.cpp, and it doesn't seem to use any timer or interrupts, so I'm completely mystified.