--- In AVR-Chat@yahoogroups.com, "bobby cossum" <nbronski@...> wrote:
>
> i'm trying to get a 30Hz pulse out of timer 1. i initialize it as
> follows...
> //
> // set up timer 1, ca 30Hz, fast pwm mode 15, prescale = 64, top =
> 8333, no interrupts
> //
> TCCR1B = 0; // stop timer 1
> TIMSK1 = 0; // disable all interrupts on
> timer 1
> TCCR1A = (1 << WGM11) | (1 << WGM10); // disconnect output compare
> pins
> OCR1A = 8333; // freq = 16M / 64 / 8333,
> ca 30Hz
> TCCR1B = (1 << CS11) | (1 << CS10) | // start timer one with
> prescale = 64 and ...
> (1 << WGM13) | (1 << WGM12); // ... fast pwm mode 15
>
> then to get a 1 Hz pulse on PD7 i test TOV1 in my main loop as follows
>
>
> if (TIFR1 & (1 << TOV1)) { // timer1 overflow ?
> TIFR1 &= ~(1 << TOV1); // clear timer1 overflow
> if (++t1Count == 15) {
> PORTD ^= 0x80;
> t1Count = 0;
> }
> }
>
> instead i get a very rapid flicker on the led i have connected. i
> suspect my timer math, but i've gone over it again and again and don't
> see what's wrong.
>
ok. it was two problems. first the backwards reset of the TOV1 flag.
you guys claim it makes sense, so far be it from me to argue.
second. apparently setting the timer mode clears the high byte of the
16 bit register holding the TOP value for modes 14 and 15. i,
foolishly felt like i should load the top value before starting the
timer and figured why write TCCRB1 twice, so i wrote the WGM and clock
select bits all at once.
now, i write the WGM bits to set mode 15, then write my top value to
OCR1A and finally write the clock select bits to start the timer.
appears to work. i can check the frequency tomorrow when i get home
to my meter.
after getting something that seems to work this morning i spent all
afternoon looking at the datasheet trying to find this explained, but
no joy. is it documented somewhere? if so, where?
thanks anyone.Message
Re: timer 1 question
2008-03-01 by bobby cossum
Attachments
- No local attachments were found for this message.