avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] Problem with timing


From: Bjarne Laursen
Subject: Re: [avr-gcc-list] Problem with timing
Date: Wed, 04 Feb 2004 09:13:25 +0100

This code works for me.

int16_t GetTimer10Ms(void)
{
    int16_t s;
    cli();
    s=timer10ms;   // timer10ms is incremented in interrupt
    sei();
    return s;
}

void Wait10Ms(int16_t ms10)
{
    ms10+= GetTimer10Ms();
    while (GetTimer10Ms()-ms10 <0)
    {
        wdt_reset();
    }
}





At 21:18 03-02-2004, you wrote:
Where is ms_timer set and reset to 0?  Shouldn't your code be:

        while( ms_timer < koniec );
        ms_timer = 0 ;
}

Keith


On Tue, 3 Feb 2004, Juraj wrote:

> Hi all..
>
> Perhaps, it is trivial problem, but I cannot find the bug.
> Please help.
>
> I have LED, connected to AVR. I want to get blinking LED, with 1:1
> alternation:
> LED_ON - 500ms delay - LED_OFF - 500ms delay - LED_ON ...
>
> Problem is - times are not always 500ms, some of periodes are shorter.
>
>
> Thank you very much.
>
> Juraj.
>
>
>
>
>
>
>
>
> So here is part of main programm:
>
> #include "timer.h"
>
> int main(void) {
> .
> .
>       timer0_init();
>       while(1){
>               LED_ON;         //sets the output
>               ms_delay(500);
>               LED_OFF;        //clears the output
>               ms_delay(500);
>       }
> .
> .
> }
>
>
> *******************************************************************************************
>
> And here is timing module - timer.c:
>
> #include <avr/timer.h>
> #include <avr/interrupt.h>
> #include <avr/signal.h>
> #include <inttypes.h>
> #include "casovac.h"
>
> volatile uint32_t ms_timer = 0;
>
>
> void timer0_init(void)
> {
>       timer_enable_int( BV(TOIE0) );
>       timer0_source(CK64);
>       TCNT0 = PRESCALE_LO;
> }
>
>
> SIGNAL(SIG_OVERFLOW0)                  // signal handler for tcnt0
> overflow interrupt
> {
>       ms_timer++;                    // increment timer counter
>       TCNT0 = PRESCALE_LO;
> }
>
>
> void ms_delay(uint16_t ms )
> {
> uint32_t koniec = ms_timer + (uint32_t)ms;
>
>       while( ms_timer < koniec );
> }
>
> *******************************************************************************************
>
> And timer.h:
>
> #define       F_CPU           8000000
> #define       PRESCALE_HI     64
> #define       PRESCALE_LO     ( 256 - ( (F_CPU/PRESCALE_HI) / 1000 ) )
>
> extern volatile uint32_t ms_timer;
>
> void timer0_init(void);
> void ms_delay(uint16_t ms );
>
> *******************************************************************************************
>
>
>
>
> --
>
> ----------------------------
> email:        address@hidden
>       address@hidden
> http://       durec.szm.sk



reply via email to

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