This is my project's outline:
Clock : 8Mhz Internal with calibration code
Peripherials: ADC, LCD, Keys
Using default values stored in internal EEPROM, monitor input current (ADC value). If this input current crosses the SET limit, then a formula will tell us, in how much TIME, relay should be OFF.
All and all a simple looking project.
But was HORIBLE experience due to MISLEADING datasheet of ATmega8.
I used Timer 2 in ASYNC mode CTC with external 32.768KHz crystal.
Every thing was fine and simple, except the DELAY counting code.
I wrote an ISR, which will increment a global variable and compare it with the value calculated using the formula.
The BIGGEST Problem:
Timer 2 works fine in stand alone application. It is very accurate.
But if LCD Code is mixed with the Timer 2, then timer was BAD. Used to count 670ms instead of 1400, an example of error.
I refered abcminiuser, and other freaks users. But all in vain... no one could POINT out the PROBLEM. abcminiuser has helped me a lot during this problem and guided me to a large extent to get going.
At last, today, I got a reply from "theusch", a freaks user.
As per the reply, I simply connected two 22pf disc cap to crystal and grounded it's other legs. Every thing started working fine.
In Timer2 Async CTC mode, Always use 22pf disc with crystal of 32.768 Khz, to avoid "glitches" in crystal's frequency. EVEN THOUGH DATASHEET SAYS NOT TO DO IT
Hope this will be sticky message forever.......