Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: timer 1 question

2008-03-02 by bobby cossum

--- 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.  here's the answer thanks to a guy named lars on avrfreaks.

setting...

TCCR1B = 0;

stops the timer.  then setting ...

TCCR1A = (1 << WGM11) | (1 << WGM10);

puts the timer in mode 3 with a fixed 10 bit top.  now, according to
page 123 of the datasheet "when using fixed TOP values the unused bits
are masked to zero when any of the OCR1x Registers are written."  thus
setting WGM13 and WGM12 before or after saving OCR1A makes all the
difference.

hope this saves someone some time somewhere down the line.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.