Timer2 ctc mode problem

My query aimed at knowing the affect of the placement of TC2's start code. Thank you for doing the experiment on my behalf as currently I don't have ATmega8A setup which I had few years ago.

Please, go back to your previous setup/sketch where it works fine for you and load OCR2 with 31.

How can you tell? Have you added serial output? Please show your current sketch.

I compared it with a clock it's fine 1hz
but it freezes randomly, I noticed that the led randomly light for two seconds instead of one second.
I worry if the unsigned long variable is the problem
because I read that 8bit microcontroller has problems when doing operations with 32bit variable in ISR

I tried to make the variable byte , but same issue :frowning:

What unsigned long variable?

oh sorry, my code above was a test, my real program which I want to get persice clock use this code:

volatile unsigned  long sec;
ISR(TIMER2_COMP_vect) {
  sec++;
}

I've changed it to volatile byte but still same problem

it stucks for a second every few minutes, could my else code affect the processor interrupt ? or it must happens whatever the processor do ?
my code is large a little and complicated so I don't want to make the problem forked.
I want the theoretical information

Post your complete sketch which you have not done in post #1. Your variable sec has appeared just now. now. Also, please state the use of 1 Hz signal that you are deriving from 32.768 kHz oscillator. are you planning to build a clock.

it's really huge and complicated, yes I'm trying to make 24h timer without using external RTC because I have the PCB done already and cannot add external RTC now.

What 'else code'?

If you are reading the variable sec in the main function (or some other function), then protect as critical section so that ISR() cannot access it until the current process/access is done.

I mean the rest code in the project, sorry for bad expression

but that's not a solution.. I want the clock to keep going in the real time, It's not a good idea to prevent interrupt from increase value if it's being read.

Or you mean that this way interrupt will wait until finish reading value in loop() then be executed ? will I lose seconds in this way ?

OK! There is not much chance to update the sec counter by ISR() while it (sec counter) is being accessed in the main() function as the interrupt interval is too long (1-sec). So, protection of the critical section is not important here.

Have you used the Arduino IDE to develop your ATmega8A based sketch? If yes, then try to disable TC0's interrupt logic. This is to ensure that millis() and micros() functions are not active which works on TC0's interrupt. If TC0's interrupt happens (which disables global interrupt logic) and during that time OCF2 flag becomes active, then the flag will not be able to generate interrupt; as a result, the sec counter will not be updated.

1 Like

It's as important as is the proper operation of the code.

If your interrupt interval is very very small, then you must protect the critical section by creating fence around it as follows. This is to ensure that the user program reads the last updated correct value of the variable.

noInterrupts();
//code to access multi-byte wide variable
interrupts();

Yes, it could. If you want some help figuring out what is going wrong you will have to show the rest of the code OR as much of the code as is needed to reproduce the problem.

Even with all the code, it will be hard for anyone else to reproduce the problem if it requires an ATmega8a processor set up for an async 32.768k crystal clock. With luck, someone will be able to look at your code and point out suspicious items (like the FOC bit being set) that you can try.

I think you've put your finger on the best solution, I'm trying it now I think this will work, how didn't I noticed that
you're great thank you ! I'll tell you the result

I've just disabled TIMER0 interrupt, is that enough ? or I must disable timer0 entirely ?

You're amazing ! about an hour and 15 minutes and the clock is going precisely, thank you alot

1 Like

7 hours left and no one second losed, that's great!

now I have a question, can I make interrupt priority ? I want to keep timer0 running, to use delay()s and millis()s
I know I can remake them with timer2 but I'm searching for simpler solution.

I would suggest not use those built-in functions of the IDE; rather, you use hardware of TC0/TC1 on polling strategy to get your other time ticks.