Full Version : Long Delay using Timer Interrupt
avr >>BEGINNERS & BUTTERFLIES >>Long Delay using Timer Interrupt


[Bennyck2004]- 09-09-2006
I used this code to generate long delay, i.e. 1 hour at 11,0592 MHz XTAL, using Timer0 interrupt.

This routine assign the AVR to toggle the LED when reach timeout.


CODE

include "m8535def.inc"

.def smtr = r16
.def menuju1dtk = r17
.def cacahdtk = r18
.def cacahmnt = r19

.cseg
.org 0
rjmp main
.org 0x009
rjmp melimpah

;------------------------ISR-------------------------------
melimpah:
     inc menuju1dtk
     cpi menuju1dtk, 43
     in smtr, SREG
     sbrc smtr, 1
     rcall sdh1dtk
     
     reti

;---------------------------------
sdh1dtk:
     clr menuju1dtk
     inc cacahdtk
     cpi cacahdtk, 60
     in smtr, SREG
     sbrc smtr, 1
     rcall sdh1mnt
     ret

;---------------------------------
sdh1mnt:
     clr cacahdtk
     inc cacahmnt
     cpi cacahmnt, 60
     in smtr, SREG
     sbrc smtr, 1
     rcall timeout
     ret

;----------------------------------
timeout:
     clr cacahdtk                    ;the delay reach timeout
     clr cacahmnt                   ; you can assign other
     in smtr, PORTD
     com smtr
     out PORTD, smtr
     ret

;-----------------------------------------------------------------------

main:
     ldi smtr, high(RAMEND)
     out SPH, smtr
     ldi smtr, low(RAMEND)
     out SPL, smtr

;aktifkan timer
     clr menuju1dtk
     clr cacahdtk
     clr cacahmnt
     ldi smtr, 0b00000101
     out TCCR0, smtr

     ldi smtr, 1
     out TIMSK, smtr
    ;ldi smtr, 250
    ;out TCNT0, smtr
     sei
     
     ser smtr                
     out DDRD, smtr
     ldi smtr, 0xF0
     out PORTD, smtr

loop:
     rjmp loop      ;this is your main routine


The important values are :

43 in cpi menuju1dtk, 43 -> obtained by calculate 1 second / tme needed to overflow of Timer0

60 in cpi cacahdtk, 60 -> to obtain 1 minute delay (60 seconds)

60 in cpi cacahmnt, 60 -> to obtain 1 hour delay (60 seconds)

The delay can be longer if you change the above values with the higher number.




Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.