i have atmega16 with freq. 1MHZ and prescaler 1024 how to calculate OCR0 in compare mode with timer0 to toggle a led for half second
The "cheat" that most people use is a tool called "avrcalc". years ago there used to only be one utility (written by Jack Tidwell I think?) called "avrcalc" but over the years several different people have done tools that perform pretty much the same function. I think there may even be an Android app?
You basically enter the CPU frequency, the period you want to achieve and it will tell you the prescaler and the OCR value to achieve that (or it will say that you can't achieve it - like trying to get a 5s period on an 8bit timer at 16MHz or something!)
I'll see if I can remind myself of the download links for the tools....
(of course one could attempt to do this by hand if you are a masochist).
OK so I found the old avrcalc here:
When I plug in your numbers:
So it it is saying a compare value of 0x3D0. That is 976. That's a bit unfortunate as you can't put a value above 0xFF into an 8 bit compare register. What you can get is:
So if you were willing to settle for 100ms instead of 1000ms you can achieve that using a compare value of 0x61. Note that it's really 100.68ms not a totally accurate 100ms
You would then need to separately count 10 lots of 100ms to make the entire 1000ms you require.
All this kind of shows why an 8bit timer is probably not a great choice for such long periods, even on a lowly 1MHz. You could easily achieve 1000ms with a 16 bit timer:
So that is saying use a 16 bit timer with a \64 prescaler and using 0x3D09 as the compare value you can get exactly 1000ms
(I seem to remember that this old avrcalc did not do the -1 adjustment to account for the 0 could so it may be 0x3D08. In fact 0x3D08 is 15624 which would give 15625 steps. 1MHz/64 is 15.625kHz
Here's the AVRCALC I use by Kevin Rosenberg.
I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie
"The critical shortage here is not stuff, but time." - Johan Ekdahl
"Step N is required before you can do step N+1!" - ka7ehk
"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman
"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?" - Lee "theusch"
Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-) - Source Unknown
Please Read: Code-of-Conduct
Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user
© 2022 Microchip Technology Inc.