avrctc

How to set compare value in AVR CTC mode


I'm trying to learn how to use the CTC mode in AVR and I'm trying to figure out why I should minus 1 when I set the comparing value. For instance,now I'm working to send the working time of the AVR every 200 ms and with the calculation I find out the value should be 50000,however in a video about this program,the man claims that it should be 50000-1,why is that?


Solution

  • It's because the underlying counter starts at zero and ends at "TOP" as it's referred to in the various AVR datasheets. Without know the exact part you're using, I can't refer you to a datasheet, but they all use basically the same terminology in their matrix of timer modes.

    So if you want the counter to count exactly 50000 times, you have to subtract one. CTC means "timer on compare match" and starting at zero means you need to be "off by one." Simple as that.

    Really good discussion of this and other AVR topics at AVRGeeks