ATMega328P Timer0 in TCNT mode
Quote from Christian on February 7, 2026, 6:36 pmI am not sure what to think about this ; but whith an OCRO0A value @ 9, the frequency should be 12,7Hz ; instead Simulide gives 127 Hz. Worse it stays at 127Hz whatever OCRO0A value is given (127 in the picture below....
The program works as expected with real Arduino Nano and oscilloscope.
Attached the firmware source code.
I am not sure what to think about this ; but whith an OCRO0A value @ 9, the frequency should be 12,7Hz ; instead Simulide gives 127 Hz. Worse it stays at 127Hz whatever OCRO0A value is given (127 in the picture below....
The program works as expected with real Arduino Nano and oscilloscope.
Attached the firmware source code.

Quote from arcachofo on February 7, 2026, 9:59 pmThank you. Already fixed the problem.
By now you can clear TCNT0 right before restarting the timer and it should work ok:
delay_timer0: ;0.64 ms delay via Timer0 ;--------------------------------------------------------- LDI R20,9 OUT OCR0A, R20 ;OCR0 = 9 LDI R20, 0b00000010 OUT TCCR0A, R20 ;timer0: CTC mode CLR R20 ; <<------------------------ OUT TCNT0, R20 ;initialize timer0 with count=0 <<------------------------ LDI R20, 0b00000101 OUT TCCR0B, R20 ;prescaler 1024 ;---------------------------------------------------------
Thank you. Already fixed the problem.
By now you can clear TCNT0 right before restarting the timer and it should work ok:
delay_timer0: ;0.64 ms delay via Timer0
;---------------------------------------------------------
LDI R20,9
OUT OCR0A, R20 ;OCR0 = 9
LDI R20, 0b00000010
OUT TCCR0A, R20 ;timer0: CTC mode
CLR R20 ; <<------------------------
OUT TCNT0, R20 ;initialize timer0 with count=0 <<------------------------
LDI R20, 0b00000101
OUT TCCR0B, R20 ;prescaler 1024
;---------------------------------------------------------
Quote from Christian on February 8, 2026, 10:26 amThank you for your fast answer! Confirm the work around is doing the job.
Thank you for your fast answer! Confirm the work around is doing the job.
