Yahoo Groups archive

AVR-Chat

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

Thread

timer 1 question

timer 1 question

2008-02-20 by bobby cossum

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.

Re: timer 1 question

2008-02-25 by bobby cossum

--- In AVR-Chat@yahoogroups.com, "bobby cossum" <nbronski@...> wrote:
> 
> 
> if (TIFR1 & (1 << TOV1)) {             // timer1 overflow ?
>   TIFR1 &= ~(1 << TOV1);               // clear timer1 overflow
>   if (++t1Count == 15) {
>     PORTD ^= 0x80;
>     t1Count = 0;
>   }
> }


ok, so i have learned that to reset TOV1 i must set it.  very zen. 
(exactly how far north were the guys who designed this chip?  bet it
was summer.  couldn't sleep on account of 24 hour daylight.)

it finally sunk in on the hundredth reading of the datasheet.  now i
can sleep.  or could if my timer was working as desired.

i want TOV1 set every 0.0333333 seconds.  my mega168 is running at
16MHz.  i put timer1 in fast pwm mode 15, WGM13:0 = 15.  i select a
prescaler of 64, CS12:0 = 5.  i set OCR1A = 8333.

so i figure 16000000 / 64 = 250000.

and 250000 / 8333 is as close to 30 as i'm going to get.

it's close enough.  delays getting to the bit of code where i check
the flag and a fairly busy timer2 ISR might just nudge it towards
greater accuracy.

it would be close enough, that is, but my timer math still appears to
be off.  the flicker on my led has slowed, but is still too fast.

any advise greatly appreciated.

Re: [AVR-Chat] Re: timer 1 question

2008-02-25 by David VanHorn

> ok, so i have learned that to reset TOV1 i must set it.  very zen.
> (exactly how far north were the guys who designed this chip?  bet it
> was summer.  couldn't sleep on account of 24 hour daylight.)

This is very nice in that you can clear all the flags simply by
reading the flag byte and writing it back.
Small price to pay.  Clear any one flag by writing that bit to a one.


> it would be close enough, that is, but my timer math still appears to
> be off.  the flicker on my led has slowed, but is still too fast.
>
> any advise greatly appreciated.

When getting started with the timers, it really helps to just do a
little snip of code and run them in studio simulation.
It's much easier to change the settings there, and observe what's happening.

Re: timer 1 question

2008-02-25 by bobby cossum

--- In AVR-Chat@yahoogroups.com, "David VanHorn" <microbrix@...> wrote:

> This is very nice in that you can clear all the flags simply by
> reading the flag byte and writing it back.
> Small price to pay.  Clear any one flag by writing that bit to a one.
> 

ok. so my feeble little TIFR1 &= ~(1 << TOV1) was resetting all the
flags except the one i wanted.  as i said i read it about 100 times,
but was too stupid to glom onto the actual sense of it.  maybe i'm
getting better...

> 
> little snip of code and run them in studio simulation.
>

well i've reduced things to a little snip of code, but, alas, no
windows here.  i'm off visiting family with nought but a little asus
eeepc on which i managed to install avr-gcc and build avrdude.  good
thing i don't get paid for this.  i'd be in a heap of trouble.

Re: [AVR-Chat] Re: timer 1 question

2008-02-25 by John Samperi

At 01:54 AM 26/02/2008, you wrote:
>ok, so i have learned that to reset TOV1 i must set it.  very zen.
>(exactly how far north were the guys who designed this chip?

This is standard procedure with other brands of processors too,
not just AVR.

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Re: timer 1 question

2008-03-01 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.  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.

Re: [AVR-Chat] Re: timer 1 question

2008-03-01 by John Samperi

At 03:07 PM 1/03/2008, you wrote:
>--- In AVR-Chat@yahoogroups.com, "bobby cossum" <nbronski@...> wrote:
>first the backwards reset of the TOV1 flag.
>  you guys claim it makes sense,

Don't know whether it makes sense or not, this is just the
way that avr and other companies like Motorola (Freescale)
do it.

Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Re: timer 1 question

2008-03-01 by bobby cossum

--- In AVR-Chat@yahoogroups.com, John Samperi <samperi@...> wrote:
>
> At 03:07 PM 1/03/2008, you wrote:
> >--- In AVR-Chat@yahoogroups.com, "bobby cossum" <nbronski@> wrote:
> >first the backwards reset of the TOV1 flag.
> >  you guys claim it makes sense,
> 
> Don't know whether it makes sense or not, this is just the
> way that avr and other companies like Motorola (Freescale)
> do it.

sorry didn't mean to get testy.  spent yesterday in a car travelling
from dallas to houston with a couple of people older and crankier than
myself by a full generation, then a couple of hours at a twelve year
old's birthday party in a facility with noise level i would have
previously thought unattainable without an electric guitar.

i am still curious to know if anyone knows where the loss of the high
order half of the TOP value when setting the WGM13:0 bits to mode 14
or 15 is documented.  or if i'm imagining it.

Re: [AVR-Chat] Re: timer 1 question

2008-03-02 by James Wagner

On Mar 1, 2008, at 3:08 PM, bobby cossum wrote:

> --- In AVR-Chat@yahoogroups.com, John Samperi <samperi@...> wrote:
> >
> > At 03:07 PM 1/03/2008, you wrote:
> > >--- In AVR-Chat@yahoogroups.com, "bobby cossum" <nbronski@> wrote:
> > >first the backwards reset of the TOV1 flag.
> > > you guys claim it makes sense,
> >
> > Don't know whether it makes sense or not, this is just the
> > way that avr and other companies like Motorola (Freescale)
> > do it.
>
> sorry didn't mean to get testy. spent yesterday in a car travelling
> from dallas to houston with a couple of people older and crankier than
> myself by a full generation, then a couple of hours at a twelve year
> old's birthday party in a facility with noise level i would have
> previously thought unattainable without an electric guitar.
>
> i am still curious to know if anyone knows where the loss of the high
> order half of the TOP value when setting the WGM13:0 bits to mode 14
> or 15 is documented. or if i'm imagining it.
>
>
> 
Check that you are loading the dual 8-bit registers in the correct  
ORDER. There IS a required order so that an internal "temp" register  
can load both registers in a single operation. Do a doc search for 16  
bit register or something like that.

Jim




[Non-text portions of this message have been removed]

Re: timer 1 question

2008-03-02 by bobby cossum

--- In AVR-Chat@yahoogroups.com, James Wagner <wagnerj@...> wrote:

> Check that you are loading the dual 8-bit registers in the correct  
> ORDER. There IS a required order so that an internal "temp" register  
> can load both registers in a single operation. Do a doc search for 16  
> bit register or something like that.
> 
> Jim
>

nope.  wrote a little piece of code that loaded the OCR1A register,
then set WGM11:0 and CS12:0 in TCCR1B, then dumped OCR1A out the USART
and the high order byte was cleared.

did it again except set WGM11:0 in TCCR1B, then loaded the OCR1A
register, then set CS12:0 in TCCR1B, then dumped OCR1A out the USART
and this time the high order byte was as i had set it.

in both cases WGM13:0 was set to 15, CS12:0 to 5, and OCR1A to 8333 in
an attempt to get TOV1 set at about 30Hz.  register access was taken
care of by gcc.

i is well and truly perplexed.

Re: [AVR-Chat] Re: timer 1 question

2008-03-02 by Roy E. Burrage

Which controller are you using?


REB


bobby cossum wrote:
> --- In AVR-Chat@yahoogroups.com, James Wagner <wagnerj@...> wrote:
>
>   
>> Check that you are loading the dual 8-bit registers in the correct  
>> ORDER. There IS a required order so that an internal "temp" register  
>> can load both registers in a single operation. Do a doc search for 16  
>> bit register or something like that.
>>
>> Jim
>>
>>     
>
> nope.  wrote a little piece of code that loaded the OCR1A register,
> then set WGM11:0 and CS12:0 in TCCR1B, then dumped OCR1A out the USART
> and the high order byte was cleared.
>
> did it again except set WGM11:0 in TCCR1B, then loaded the OCR1A
> register, then set CS12:0 in TCCR1B, then dumped OCR1A out the USART
> and this time the high order byte was as i had set it.
>
> in both cases WGM13:0 was set to 15, CS12:0 to 5, and OCR1A to 8333 in
> an attempt to get TOV1 set at about 30Hz.  register access was taken
> care of by gcc.
>
> i is well and truly perplexed.
>
>   


[Non-text portions of this message have been removed]

Re: timer 1 question

2008-03-02 by bobby cossum

--- In AVR-Chat@yahoogroups.com, "Roy E. Burrage" <RBurrage@...> wrote:
>
> Which controller are you using?
> 
> 
> REB
> 
> 

mega168.  16MHz oscillator.

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.

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.