Yahoo Groups archive

AVR-Chat

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

Thread

Timer1 Problems

Timer1 Problems

2005-07-06 by Aaron

This AVR newbie is using a tiny2313 and studio 4.11.

I have timer1 configured for CTC with OCR1A.   OCIE1A is set in TIMSK.  
Global interrupts are enabled.  Stepping thru the code in the simulator 
shows OCF1A being set for 1 cycle but my interrupt vector is not being 
jumped to.   My code is not clearing OCF1A and the datasheet says that 
OCF1A will not be cleared until execution of the interrup vector.  Any 
thoughts on what I am doing wrong?  Code below.

Thanks,
Aaron

; configure Timer/Counter1
    ldi temp1,  0b00000000
                ; 00------  ; Compare Output Mode for Channel A, Normal 
Port Operation, OC1A & OC1B disconnected
                ; --00----  ; Compare Output Mode for Channel B, Normal 
Port Operation, OC1A & OC1B disconnected
                ; ----00--  ; reserved
                ; ------00  ; 00 = clear timer on compare match with OCR1A
    out TCCR1A, temp1

    ldi temp1,  0b00001001
                ; 0-------  ; 0 = input capture noise canceler disabled
                ; -0------  ; 0 = trigger capture on falling edge of IC1
                ; --0-----  ; reserved
                ; ---01---  ; 01 = clear timer on compare match with OCR1A
                ; -----001  ; 001 = no prescaler, internal clock
                ; -----011  ; 011 = 1:64 prescaler, internal clock
    out TCCR1B, temp1

    ldi temp1,  0b00000000
                ; 0-------  ; 1 = force output compare for channel A
                ; -0------  ; 1 = force output compare for channel B
                ; --000000  ; reserved
    out TCCR1C, temp1
   
    cli                     ; clear interrupts for 16-bit register access
   
    ldi temp1, 0x1F         ; 8Mhz clock -> 125 ns instruction cycle
    ldi temp2, 0x40         ; ...for a 1 ms delay we need 8000 
instruction cycles
    out OCR1AH, temp1       ; ...8000 decimal = 0x1F40
    out OCR1AL, temp2
   
    sei                     ; enable interrupts after 16-bit register access

    ldi temp1,  0b01000000
                ; 0-------  ; 0 = Timer/Counter1 Overflow Interrupt Disabled
                ; -1------  ; 1 = Timer/Counter1 Output Compare A Match 
Interrupt Enabled
                ; --0-----  ; 0 = Timer/Counter1 Output Compare B Match 
Interrupt Disabled
                ; ---0----  ; reserved
                ; ----0---  ; 0 = Timer/Counter1 Input Capture Interrupt 
Disabled
                ; -----0--  ; 0 = Timer/Counter0 Output Compare B Match 
Interrupt Disabled
                ; ------0-  ; 0 = Timer/Counter0 Overflow Interrupt Disabled
                ; -------0  ; 0 = Timer/Counter0 Output Compare A Match 
Interrupt Disabled

    out TIMSK, temp1

Loop:
   nop
   nop
   nop
   nop
   nop
   rjmp Loop

Re: [AVR-Chat] Timer1 Problems

2005-07-06 by James Hatley

Here is how I do timer startup ... (hope I got this correct...)

initialization of timer

... stop interrupts (CLI)
... stop timer TCCR1B  = 0
... set for CTC etc in TCCR1A
... set OCR1A as required
... set TCNT as required
... start timer TCCR1B = 9 (or whatever required)
... set TIMSK as necessary
... enable interrupts (SEI)

and maybe you just didn't include it but I think you must load your
interrupt service routine (ISR) address into the appropriate interrupt
vector and you must have an ISR routine.

As I understand it the flag should be cleared when the program jumps to that
ISR address.

Good Luck

Jim

----- Original Message ----- 
From: "Aaron" <aaron.groups@gmail.com>
To: <avr-chat@yahoogroups.com>
Sent: Wednesday, July 06, 2005 12:54 PM
Subject: [AVR-Chat] Timer1 Problems


> This AVR newbie is using a tiny2313 and studio 4.11.
>
> I have timer1 configured for CTC with OCR1A.   OCIE1A is set in TIMSK.
> Global interrupts are enabled.  Stepping thru the code in the simulator
> shows OCF1A being set for 1 cycle but my interrupt vector is not being
> jumped to.   My code is not clearing OCF1A and the datasheet says that
> OCF1A will not be cleared until execution of the interrup vector.  Any
> thoughts on what I am doing wrong?  Code below.
>
> Thanks,
> Aaron
>
> ; configure Timer/Counter1
>     ldi temp1,  0b00000000
>                 ; 00------  ; Compare Output Mode for Channel A, Normal
> Port Operation, OC1A & OC1B disconnected
>                 ; --00----  ; Compare Output Mode for Channel B, Normal
> Port Operation, OC1A & OC1B disconnected
>                 ; ----00--  ; reserved
>                 ; ------00  ; 00 = clear timer on compare match with OCR1A
>     out TCCR1A, temp1
>
>     ldi temp1,  0b00001001
>                 ; 0-------  ; 0 = input capture noise canceler disabled
>                 ; -0------  ; 0 = trigger capture on falling edge of IC1
>                 ; --0-----  ; reserved
>                 ; ---01---  ; 01 = clear timer on compare match with OCR1A
>                 ; -----001  ; 001 = no prescaler, internal clock
>                 ; -----011  ; 011 = 1:64 prescaler, internal clock
>     out TCCR1B, temp1
>
>     ldi temp1,  0b00000000
>                 ; 0-------  ; 1 = force output compare for channel A
>                 ; -0------  ; 1 = force output compare for channel B
>                 ; --000000  ; reserved
>     out TCCR1C, temp1
>
>     cli                     ; clear interrupts for 16-bit register access
>
>     ldi temp1, 0x1F         ; 8Mhz clock -> 125 ns instruction cycle
>     ldi temp2, 0x40         ; ...for a 1 ms delay we need 8000
> instruction cycles
>     out OCR1AH, temp1       ; ...8000 decimal = 0x1F40
>     out OCR1AL, temp2
>
>     sei                     ; enable interrupts after 16-bit register
access
>
>     ldi temp1,  0b01000000
>                 ; 0-------  ; 0 = Timer/Counter1 Overflow Interrupt
Disabled
>                 ; -1------  ; 1 = Timer/Counter1 Output Compare A Match
> Interrupt Enabled
>                 ; --0-----  ; 0 = Timer/Counter1 Output Compare B Match
> Interrupt Disabled
>                 ; ---0----  ; reserved
>                 ; ----0---  ; 0 = Timer/Counter1 Input Capture Interrupt
> Disabled
>                 ; -----0--  ; 0 = Timer/Counter0 Output Compare B Match
> Interrupt Disabled
>                 ; ------0-  ; 0 = Timer/Counter0 Overflow Interrupt
Disabled
Show quoted textHide quoted text
>                 ; -------0  ; 0 = Timer/Counter0 Output Compare A Match
> Interrupt Disabled
>
>     out TIMSK, temp1
>
> Loop:
>    nop
>    nop
>    nop
>    nop
>    nop
>    rjmp Loop
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by James Hatley

Here I am replying to my own message ...

Anyway, in re-reading what I wrote, I should have added that the vectors
must be "rjmp address", not just address as I put in the message. ( from my
Motorola assembler habits I guess. )

I wrote a simple version of the method below and it does work in the
simulator correctly.

Jim
----- Original Message ----- 
From: "James Hatley" <james.hatley@comcast.net>
To: <AVR-Chat@yahoogroups.com>
Sent: Wednesday, July 06, 2005 1:40 PM
Subject: Re: [AVR-Chat] Timer1 Problems


> Here is how I do timer startup ... (hope I got this correct...)
>
> initialization of timer
>
> ... stop interrupts (CLI)
> ... stop timer TCCR1B  = 0
> ... set for CTC etc in TCCR1A
> ... set OCR1A as required
> ... set TCNT as required
> ... start timer TCCR1B = 9 (or whatever required)
> ... set TIMSK as necessary
> ... enable interrupts (SEI)
>
> and maybe you just didn't include it but I think you must load your
> interrupt service routine (ISR) address into the appropriate interrupt
> vector and you must have an ISR routine.
>
> As I understand it the flag should be cleared when the program jumps to
that
> ISR address.
>
> Good Luck
>
> Jim
>
> ----- Original Message ----- 
> From: "Aaron" <aaron.groups@gmail.com>
> To: <avr-chat@yahoogroups.com>
> Sent: Wednesday, July 06, 2005 12:54 PM
> Subject: [AVR-Chat] Timer1 Problems
>
>
> > This AVR newbie is using a tiny2313 and studio 4.11.
> >
> > I have timer1 configured for CTC with OCR1A.   OCIE1A is set in TIMSK.
> > Global interrupts are enabled.  Stepping thru the code in the simulator
> > shows OCF1A being set for 1 cycle but my interrupt vector is not being
> > jumped to.   My code is not clearing OCF1A and the datasheet says that
> > OCF1A will not be cleared until execution of the interrup vector.  Any
> > thoughts on what I am doing wrong?  Code below.
> >
> > Thanks,
> > Aaron
> >
> > ; configure Timer/Counter1
> >     ldi temp1,  0b00000000
> >                 ; 00------  ; Compare Output Mode for Channel A, Normal
> > Port Operation, OC1A & OC1B disconnected
> >                 ; --00----  ; Compare Output Mode for Channel B, Normal
> > Port Operation, OC1A & OC1B disconnected
> >                 ; ----00--  ; reserved
> >                 ; ------00  ; 00 = clear timer on compare match with
OCR1A
> >     out TCCR1A, temp1
> >
> >     ldi temp1,  0b00001001
> >                 ; 0-------  ; 0 = input capture noise canceler disabled
> >                 ; -0------  ; 0 = trigger capture on falling edge of IC1
> >                 ; --0-----  ; reserved
> >                 ; ---01---  ; 01 = clear timer on compare match with
OCR1A
> >                 ; -----001  ; 001 = no prescaler, internal clock
> >                 ; -----011  ; 011 = 1:64 prescaler, internal clock
> >     out TCCR1B, temp1
> >
> >     ldi temp1,  0b00000000
> >                 ; 0-------  ; 1 = force output compare for channel A
> >                 ; -0------  ; 1 = force output compare for channel B
> >                 ; --000000  ; reserved
> >     out TCCR1C, temp1
> >
> >     cli                     ; clear interrupts for 16-bit register
access
Show quoted textHide quoted text
> >
> >     ldi temp1, 0x1F         ; 8Mhz clock -> 125 ns instruction cycle
> >     ldi temp2, 0x40         ; ...for a 1 ms delay we need 8000
> > instruction cycles
> >     out OCR1AH, temp1       ; ...8000 decimal = 0x1F40
> >     out OCR1AL, temp2
> >
> >     sei                     ; enable interrupts after 16-bit register
> access
> >
> >     ldi temp1,  0b01000000
> >                 ; 0-------  ; 0 = Timer/Counter1 Overflow Interrupt
> Disabled
> >                 ; -1------  ; 1 = Timer/Counter1 Output Compare A Match
> > Interrupt Enabled
> >                 ; --0-----  ; 0 = Timer/Counter1 Output Compare B Match
> > Interrupt Disabled
> >                 ; ---0----  ; reserved
> >                 ; ----0---  ; 0 = Timer/Counter1 Input Capture Interrupt
> > Disabled
> >                 ; -----0--  ; 0 = Timer/Counter0 Output Compare B Match
> > Interrupt Disabled
> >                 ; ------0-  ; 0 = Timer/Counter0 Overflow Interrupt
> Disabled
> >                 ; -------0  ; 0 = Timer/Counter0 Output Compare A Match
> > Interrupt Disabled
> >
> >     out TIMSK, temp1
> >
> > Loop:
> >    nop
> >    nop
> >    nop
> >    nop
> >    nop
> >    rjmp Loop
> >
> >
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
> >
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by Dave VanHorn

At 09:35 PM 7/6/2005, James Hatley wrote:
>Here I am replying to my own message ...
>
>Anyway, in re-reading what I wrote, I should have added that the vectors
>must be "rjmp address", not just address as I put in the message. ( from my
>Motorola assembler habits I guess. )

Check the DS for the particular processor, some use JMP, and if 
you're using RJMP in the vector table you will get some really ODD results.

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by James Hatley

Good point. And for most processors I assume that the JMP would be more
appropriate.

However, the little 2313 only has a RJMP. I couldn't find any other
instruction that fit for interrupt vectors.

I wrote some code and it ran just fine with the RJMP. (on the simulator
anyway).

Jim

----- Original Message ----- 
From: "Dave VanHorn" <dvanhorn@dvanhorn.org>
To: <AVR-Chat@yahoogroups.com>; <AVR-Chat@yahoogroups.com>
Sent: Wednesday, July 06, 2005 8:33 PM
Subject: Re: [AVR-Chat] Timer1 Problems


> At 09:35 PM 7/6/2005, James Hatley wrote:
> >Here I am replying to my own message ...
> >
> >Anyway, in re-reading what I wrote, I should have added that the vectors
> >must be "rjmp address", not just address as I put in the message. ( from
my
> >Motorola assembler habits I guess. )
>
> Check the DS for the particular processor, some use JMP, and if
> you're using RJMP in the vector table you will get some really ODD
results.
Show quoted textHide quoted text
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by Dave VanHorn

At 09:20 AM 7/7/2005, James Hatley wrote:
>Good point. And for most processors I assume that the JMP would be more
>appropriate.
>
>However, the little 2313 only has a RJMP. I couldn't find any other
>instruction that fit for interrupt vectors.
>
>I wrote some code and it ran just fine with the RJMP. (on the simulator
>anyway).

Be sure which is right for your box.
I made the mistake once, of copying the vector table from a lower AVR 
(with rjmps) into a mega, which uses JMP, and the results were most 
amusing, but depending on how much code there is, and how it's 
structured, it might have worked, which is more scary, to me anyway.

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by James Hatley

> At 09:20 AM 7/7/2005, James Hatley wrote:
> >Good point. And for most processors I assume that the JMP would be more
> >appropriate.
> >
> >However, the little 2313 only has a RJMP. I couldn't find any other
> >instruction that fit for interrupt vectors.
> >
> >I wrote some code and it ran just fine with the RJMP. (on the simulator
> >anyway).
>
> At 07:42 AM 7/7/2005 Dave VanHorn wrote:
> Be sure which is right for your box.
> I made the mistake once, of copying the vector table from a lower AVR
> (with rjmps) into a mega, which uses JMP, and the results were most
> amusing, but depending on how much code there is, and how it's
> structured, it might have worked, which is more scary, to me anyway.
>
Excellent advice for all starting out (or continuing) to write assembler (or
C or whatever for that matter), namely, that it is essential to know the
processor that you are writing code for. The only way to get that is to
study, write code, build something, and get your hands dirty. Atmel, as much
as we complain at times, does write a pretty good manual on their products
but they do take serious study and that takes time and perseverance, for
sure.

Jim

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by Aaron

Aaron wrote:

>This AVR newbie is using a tiny2313 and studio 4.11.
>
>I have timer1 configured for CTC with OCR1A.   OCIE1A is set in TIMSK.  
>Global interrupts are enabled.  Stepping thru the code in the simulator 
>shows OCF1A being set for 1 cycle but my interrupt vector is not being 
>jumped to.   My code is not clearing OCF1A and the datasheet says that 
>OCF1A will not be cleared until execution of the interrup vector.  Any 
>thoughts on what I am doing wrong?  Code below.
>  
>
Hello all,

Thanks to all those who responded both on and off list.

The original code was fine -- it was a bug in the Studio 4.11's 
simulator.  I downloaded and installed service pack 3 for studio 4.11 
and the problem went away.   (Release notes for SP3 don't even mention 
this fix.)

About makes this newbie want to go back to PICs.  At least I've never 
had an issue with MPLAB's simulator...

Aaron

Re: [AVR-Chat] Timer1 Problems

2005-07-07 by John Samperi

At 12:42 AM 8/07/2005, you wrote:
>I made the mistake once, of copying the vector table from a lower AVR
>(with rjmps) into a mega, which uses JMP, and the results were most
>amusing,

For which I'm thankful :-) Since then I copy the vectors table
directly from the PDF data sheet (Copy and paste) so I can blame
Atmel if things are not right. I would have done the same thing
with my new board (M64/M128) and pulling out the last strand of
hair I have when things would not work.


Regards

John Samperi

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

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.