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.

Questions about delays

Status
Not open for further replies.

cosmonavt

Member
Hello,

I was wondering that in order to make a delay of 1 second, why so we say _delay_ms(100); and not _delay_ms(1000); ?

Secondly, I want to make a delay of 24 hours using a tiny AVR. I need to measure these 24 hours very accurately. First off, I am using AVR Studio 5, how do I set the f_CPU value to 10MHZ? Secondly, _delay_ms has a max value of 262.14, how do I go about measuring 24 hours?

I thought of a way to use the time period over frequency formula. This would give me the number of oscillations required to reach 24 hours. Then I can use this value in a for loop to measure the delay.

The number would be very large of course. How do I go about this?
 
Hello,

I was wondering that in order to make a delay of 1 second, why so we say _delay_ms(100); and not _delay_ms(1000); ?

Secondly, I want to make a delay of 24 hours using a tiny AVR. I need to measure these 24 hours very accurately. First off, I am using AVR Studio 5, how do I set the f_CPU value to 10MHZ? Secondly, _delay_ms has a max value of 262.14, how do I go about measuring 24 hours?

I thought of a way to use the time period over frequency formula. This would give me the number of oscillations required to reach 24 hours. Then I can use this value in a for loop to measure the delay.

The number would be very large of course. How do I go about this?

First:
I would say _delay_ms(1000); to create 1 second delay.

Second:
To create 24 hour clock, you can setup a Timer/Counter to create interrupts at constant intervals. Use a 32 bit integer calculate the number of interrupts. If the interval is 10ms, then 24 hours is 8 640 000 "ticks". Google "avr timer interrupt tutorial".
How accurate is "very accurate". Do you mean something like a Chinese wristwatch, or more like an atomic clock?
 
Last edited:
OK my bad luck, the MCU im using is ATTiny13 which has only one 8 bit timer. Means its maximum count is 255. With a frequency of 10MHz, the time period is 1 x 10^-7 second. To get a delay of 24 hours, I need 8.64 x 10^11 cycles. Would I have to run the timer again and again? Is that possible? If I reduce the frequency, then I guess the accuracy will be affected. But since I need a 24 hour delay, how much accuracy would I need?

Anyways, dividing 8.64 x 10^11 by 255 gives 3388235294. Would this work?

Code:
for ( i = 0, i < 3388235294,  ) {
        ++i;
}
 
Yes I have a prescaler. Isn't the largest value of the prescaler 1024? If I set F_CPU value to 400KHz, and prescaler to 1024, then I have a freq of 390.625. This means that it has a time period of 2.56*10^-3 which is a much greater value. How do I generate an hour long delay with this now?
 
OK I generated something like this:

Code:
#define F_CPU 400UL
#include <avr/io.h>
#include <util/delay.h>

void timer0_init(){
	TCCR0B = (1<<CS00)|(0<<CS01)|(1<<CS02);
	TCNT0 = 0;
}

void main(){
	DDRC = 0x01;
	timer0_init();
	
	while(1){
		if (TCNT0 > 34560000){
			while(1)
			{
				PORTC = 0x01;
				_delay_ms(1000);
				PORTC = 0x00;
				_delay_ms(1000);
			}
		}	
	}		
}

When the counter reaches 34560000, the LED on Pin C0 should start blinking forever.

Is this code correct? The device has an 8 bit timer, the max it can count to is 255. What do I do about the value 34560000? Ive reduced the frequency to a mere 400 Hz to make the time period bigger, but I can't seem to get the counter value within the range for an 8bit timer.

BTW the prescaler is set to 1024.
 
Last edited:
Does decreasing the frequency reduce the power consumption? For example, I set the frequency to a mere 400 Hz by

define F_CPU 400UL

Would it increase the amp hours of the battery?
 
F_CPU is not a setting. It just tells the compiler your MCU frequency.. it does not set it.

in other words:

#define F_CPU 400UL

does nothing to your MCU frequency.

But, decreasing the frequency does reduce power consumption.
 
If you have your prescaler at 1024 and you allow the timer to overflow, it will happen every 1024*256 = 262144 cycles and will set the interrupt flag for the timer.

In your code do,
Code:
    if(InterruptFlag==1){
        InterruptFlag=0;
        count+=262144;
        if(count>10000000){    // = clock speed
             count-=10000000;
             seconds++;
             // here do if(seconds==60) etc
        }
    }
You now are counting seconds and so it is easy to extend to count minutes and hours.

Mike.
 
Last edited:
I see that ATTiny 13A has two selectable frequencies, one is 4.8 MHz, the other is 9.6 MHz. If I use teh lower frequency and the maximum prescaler (1024), then I get a frequency of 4687.5 and a time period of 2.13*10^-4.

24 hours will then take (24*60*60) / (2.13*10^-4) = 405,000,000 cycles.

Now isn't it possible for me to create a timer where the initial counter is first set to zero, then since I have an 8 bit timer, use a compare value of 250 (when it exceeds this, 250 cycles are completed) and use a for loop to loop this process 405,000,000/250 = 1620,000 times?

How do I set the frequency to 4.8MHz? There are two possible CKSEL values. 0b10 will give 9.6MHz while 0b01 will give 4.8 MHz. I can't seem to set this frequency...
 
I assumed your frequency was set by your crystal.

The code above should count seconds with complete accuracy.

Edit, your original post stated 10MHz.

Mike.
 
Last edited:
In the datasheet of ATTiny13, there are two frequencies mentioned. One is 4.8MHz and the other is 9.6 MHz. How exactly do I know which one is it running at? Or how do I modify the clock select fuses (CKSEL) which are needed to decide between 4.8MHz and 9.6MHz.

P.S. please throw some light on the code you posted above.

Thanks.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top