Continue to Site

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.

  • 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.

AVR 2313 Accurate 1 second clock

Status
Not open for further replies.

cleoestrada

New Member
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
 
cleoestrada: You can visit bastoc.com and pull the RVKBASIC free version (Atmel BASIC compiler) and see if it is work correctly?
 
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.
 
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.
 
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
 
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.
 
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.
 
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.
 
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:
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.

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.
 
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top