Continue to Site

# Need help: AVR 8-bit timer

Status
Not open for further replies.

#### Don_dody

##### Full Member level 1
Can anybody tell me how to calculate OCR0 & OCR2 in timer0 and timer 2?

I've been searching and just found formula to calculate TCNT and OCR1A.

Why is the calculation of OCR0 any different from OCR1A ?
You also haven't mentioned the mcu model

oh, so you mean calculation of OCR0 is the same as OCR1A?

Actually I just need the formula, but if you want to help:
I use AVR ATmega8535
12Mhz
prescaler 1024.

I need, 0.5 ms and 9 ms time interval.

The calculation is the same for any mcu.

First find the frequency of the timer, in your case 12000000/1024 =11718.75Hz

Then the period to see the time it takes for each tick of the timer 1/11718.75Hz= 85.3us

Nor calculate how many ticks you need for required delay.
For 0.5ms 0.0005/0.0000853 = 5.86 , the closest integer is 6 , the compare match value you should use is the result -1 so 6-1=5 (I assume CTC mode , if not you should wait for 6 ticks to get the delay you want)

Status
Not open for further replies.