# AVR 10ms timer with 8MHz clock

I want to generate a 10ms timer using ATMEGA32. The crystal used is 8MHz. Is this calculation correct TCNT0 value is correct.The prescaler value i used is 1024.

is the calculation and the value 78 for TCNT0 is correct?

• Seems correct to me. – Eugene Sh. Jul 27 '16 at 17:15
• It is 0.000128 seconds not microseconds. But all the same, you have calculated TC as if it were 0.000128 seconds. – Bence Kaulics Jul 27 '16 at 18:19

If you want to get periodic timer, you shall use CTC mode.

Check the datasheet page 99:

http://www.atmel.com/Images/Atmel-7766-8-bit-AVR-ATmega16U4-32U4_Datasheet.pdf

The formula is: F = F_CPU / (2 * N * (1 + OCR0))

OCR0 = (F_CPU / (F * 2 * N)) - 1 = (8000000 /(100 * 2 * 1024)) - 1 = 38.06 = 38

Timer 0 CTC mode (page 106): WGM02=0 WGM01=1 WGM00=0

To use TIMER0_COMP_vect interrupt, OCF0 & OCIE0 flags must be set.

• I was trying to implement delay without CTC mode – ganeshredcobra Jul 27 '16 at 18:07
• Oh, then TCNT0 = F_CPU/N/F = 8000000/1024/100 = 78.125 = 78 so it is right. – Flanker Jul 27 '16 at 18:35
• F_CPU/N/F what is that F which is substituted as 100? – ganeshredcobra Jul 28 '16 at 4:30
• F is the timer frequency, if period is 10ms (0.01s) then 1/0.01=100Hz – Flanker Jul 28 '16 at 7:45