[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Problem with timer0 overflow interrupt in c language.

From: David Kelly
Subject: Re: [avr-gcc-list] Problem with timer0 overflow interrupt in c language..
Date: Thu, 6 Dec 2007 17:17:28 -0600
User-agent: Mutt/

On Tue, Dec 04, 2007 at 11:21:23PM -0800, Sach wrote:
> Hi friends,
> I m programming with WinAVR for ATMega32 8-bit RISC from atmel.
> I have tried to write timer overflow interrupt and LED toggle
> everytime when timer0 overflows. In this code i have not got the
> output result. So please help me with this issue. Here the code that i
> have written is given below:

Needs more comments in your code.

> #include <avr\io.h>
> #include <avr\interrupt.h>

Avr-gcc is a Unix tool. Proper path separation symbol is "/". Only
"happens" to work the other way on Windows hosts.

> void main(void)
> {
>       SP    = 0x085F;

Why are you setting the stack pointer? Let the avr-libc startup code
handle it. Off the top of my head I'd guess this is the most wrong thing
you have done. 2nd guess is the frequency you toggle the LED.

>       TCCR0 = (1<<CS02)|(1<<CS00);

Dividing by 1024.

>       TIFR = 1<<TOV0;

Clear a possible (probable) pending interrupt. Thats OK. Also OK to
leave it be as it will be dispatched immediately after sei() below.

>       TIMSK = 1<<TOIE0;

Enable the interrupt.

>       DDRB = 0xFF;

All of PORTB is now output.

>       sei();
>       while(1);
> }
> ISR (TIMER0_OVF_vect)
> {     
>       cli();

Clear interrupt enable is unnecessary, they are already off.

>       PORTB = ~PORTB; // Toggle pins on Port B

What is your CPU oscillator running at? For 8 MHz divided by 1024 divided
by 256 you will hit this code 25.95 times per second. Can you see that

Next you are counting on reading the state of PORTB to toggle its
output. With many CPUs reading an output port reads the actual pin
states not the value previously written. I don't recall the AVR acts
that way, but there is an easier way to toggle an AVR output pin: Write
a 1 to its input. Like this:

        PINB = 0xff;    //  toggle all output pins on PORTB

> }

While you can't slow the timer any more than you have, you can put a
software divider in your code. This should stretch it to 3.8 seconds per

        static uint8_t divider;

        if( divider ) {
        } else {
                divider = 100;  //  whatever you want to reset
                PINB = 0xff;    //  toggle output bits

David Kelly N4HHE, address@hidden
Whom computers would destroy, they must first drive mad.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]