1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

AVR 2313 Accurate 1 second clock

Discussion in 'AVR' started by cleoestrada, May 31, 2006.

  1. cleoestrada

    cleoestrada New Member

    Joined:
    May 25, 2006
    Messages:
    4
    Likes:
    0
    Hi,

    I am trying to make a 1 sec period using AVR AT90S2313 but the timing is not accurate - 1 second is sometimes fast. Can some one show me a code in BASCOM AVR or assembly to create very accurate 1 sec clock, pls.

    Thx

    Cleo
     
  2. mramos1

    mramos1 Active Member

    Joined:
    Sep 15, 2005
    Messages:
    2,403
    Likes:
    9
    Location:
    Boynton Beach, Florida USA
    cleoestrada: You can visit bastoc.com and pull the RVKBASIC free version (Atmel BASIC compiler) and see if it is work correctly?
     
  3. Papabravo

    Papabravo Well-Known Member

    Joined:
    Mar 9, 2006
    Messages:
    2,889
    Likes:
    25
    Location:
    Michigan, USA (GMT-5)
    If the processor's clock is off by a little bit then your one second time can be off by quite a bit. Start with the processors time base, then check your algorithm and peripherals for counting the correct number of cycles. Lastly, if you are using interrupts, dont forget to account for the interrupt response latency.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. bibinjohn

    bibinjohn New Member

    Joined:
    May 7, 2006
    Messages:
    47
    Likes:
    0

    void delay_ms(unsigned char time_ms)
    {
    unsigned short delay_count = F_CPU / 4000;

    unsigned short cnt;
    asm volatile ("\n"
    "L_dl1%=:\n\t"
    "mov %A0, %A2\n\t"
    "mov %B0, %B2\n"
    "L_dl2%=:\n\t"
    "sbiw %A0, 1\n\t"
    "brne L_dl2%=\n\t"
    "dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
    :"r"(time_ms), "r"((unsigned short) (delay_count))
    );
    }


    This is an accurate delay generator.
     
  6. johnsmith123

    johnsmith123 Member

    Joined:
    Sep 24, 2005
    Messages:
    141
    Likes:
    2
    avr 1 second delay

    Why don't you just use the simulator in avr studio?
    here is a routine from avr240.asm (slightly modified)
    ;1 second delay on 4 MHz clock
    delay:
    ldi R20,21
    delay1:
    ldi R19,252
    delay2:
    ldi R18,251
    delay3:
    dec R18
    brne delay3
    dec R19
    brne delay2
    dec R20
    brne delay1
    ret
     
  7. mramos1

    mramos1 Active Member

    Joined:
    Sep 15, 2005
    Messages:
    2,403
    Likes:
    9
    Location:
    Boynton Beach, Florida USA
    Johnsmith123.. Thanks, Nabbed a copy for myself. Thank, will mosy likely use that in my next Atmel project (just inline ASM it)..

    Seems like I need 1 second delay lately. The pause 1000 is fine with a EXT OSC, will see if it is better for the little extra work.
     
  8. cleoestrada

    cleoestrada New Member

    Joined:
    May 25, 2006
    Messages:
    4
    Likes:
    0
    RE: 1 Sec time delay

    Thank you all for your kind help.

    Cleo
     
  9. johnsmith123

    johnsmith123 Member

    Joined:
    Sep 24, 2005
    Messages:
    141
    Likes:
    2

    Attached Files:

  10. agent420

    agent420 New Member

    Joined:
    Jun 1, 2006
    Messages:
    73
    Likes:
    0
    Location:
    3rd stoned from the sun
    Check out this RTC tutorial over at avrFreaks; it explains the how to configure the timers for accurate delays and how to make a Real Time Clock using the system clock frequency.

    The examples are in C but should be easily ported to Bascom.

    You might need to register (free) at avrFreaks, but it's a great resource for all things avr.
     
  11. Rakesh hasija

    Rakesh hasija New Member

    Joined:
    Aug 22, 2008
    Messages:
    2
    Likes:
    0
    help me out

    Plz anybuddy help me out,I want to generate 1sec delay using timers of atmega32 in assembly lang,i m using avr studio.plz anybuddy reply me with code.
     
  12. CCD

    CCD New Member

    Joined:
    Jul 27, 2008
    Messages:
    74
    Likes:
    1
    Location:
    Chennai, India
  13. philba

    philba New Member

    Joined:
    Mar 12, 2006
    Messages:
    1,886
    Likes:
    8
    Location:
    Seattle
    I think it's very hard to build an RTC using delays. While it is easy to create precise delays, you have to factor in the code that calls the delays. In addition, you will have different execution paths depending on program logic and it becomes very difficult to take into account every cycle. Even an instruction or two off starts to add up.

    It's much better to use timers to make the time measured independent of the code execution path. Care needs to be taken to ensure that no clock cycles are lost.
     
    Last edited: Sep 30, 2008
  14. Rakesh hasija

    Rakesh hasija New Member

    Joined:
    Aug 22, 2008
    Messages:
    2
    Likes:
    0
    actuaaly i m using timer1 which is 16 bit of atemega 32 and having external crystal of 10mhz.but i can't have idea abt prescalers,i want to generate 1 sec delay after one second my overflag should get set.u have code for that plz reply as sun as possible.
     
  15. philba

    philba New Member

    Joined:
    Mar 12, 2006
    Messages:
    1,886
    Likes:
    8
    Location:
    Seattle
    I don't have code for that. If I were you, I would take a close look at the datasheet and do some experimenting if it is not obvious.

    Another approach is to accumulate smaller units like 100 mS. Accumulate 10 of those to make 1 second. There are many ways to do what you want.
     

Share This Page