Re: Long delays
2006-03-19 by Robert Rademacher
> Date: Sun, 19 Mar 2006 13:52:28 +1100
> From: "OWEN-A" <oldboot@bigpond.com>
> Subject: Long delays
>
> I need to generate delays of up to 24 hours on a Mega16 has anyone
> any suggestions, I could use an RTC but there may be another option
as
> strict timekeeping is not necessary for this application.
> Thanking You,
>
> Owen.
Hi Owen,
Use the timer1 - see the actual code below I wrote.
Robert J Rademacher
//TIMER1 initialisation - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Sec
// actual value: 1.000Sec (0.0%)
// Crystal: 14.7456Mhz
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x1F; //setup
TCNT1L = 0x01;
OCR1AH = 0xE0;
OCR1AL = 0xFF;
OCR1BH = 0xE0;
OCR1BL = 0xFF;
ICR1H = 0xE0;
ICR1L = 0xFF;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x1F; //reload counter high value
TCNT1L = 0x01; //reload counter low value
//global - unsigned int OneSecCounter;
//global - unsigned int OneHourCounter;
//OneSecCounter can reaches 65535 as the max
//total seconds, which would be 18.2041 hour, which is
//not practical when counting hours.
//Best method would be relying on 1 hour counter based
//on counting the seconds to reach 3600 secs.
//Example.
//One hour = 60 secs x 60 mins (3600 secs)
//Make sure you reset OneSecCounter & OneHourCounter
//when starting the OneSecCounter/OneHourCounter timers.
//This OneHourCounter can reach 65,5535 hours, which is
//more than sufficient.
OneSecCounter++;
if (OneSecCounter >= 3600) {
OneHourCounter++;
OneSecCounter = 0;//reset
}
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
//
void main(void)
{
init_devices();
OneSecCounter = 0;
OneHourCounter = 0;
for (;;) {
if (OneHourCounter == 1) printf("One Hour Timer Elasped!\n");
if (OneHourCounter == 12) printf("Twelve Hours Timer Elasped!\n");
if (OneHourCounter == 24) printf("Twenty-Four Hour Timer
Elasped!\n");
}
}
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com