I'm a novice and I'm looking for help on a two hour delay timer for a project I'm working on.
I'm using a RS08KA2 MC and it doesn't have a RTC so I've nested loops to create a long delay but it isn't very accurate. Does anyone know how to implement a long delay in the KA2 that may be easier to implement and more accurate? I've looked at AN3413 Low Cost digital timer but it ties in a ADC and I can't seem to understand the code.
Below is what I've done to get a long delay and it works but seems like a very bad way to do it.
I included all the code since it's such a small program.
Any advice would be appreciated,
Thanks,
Rod
ORG RAMStart
counter: DS.B 1
counter2: DS.B 1
counter3: DS.B 1
time2: DS.B 1
; Const Section
ConstSection: SECTION
time: DC.B $FF
; Code Section
ORG ROMStart
;*******************************************************************************************
; Peripheral Initialization
;*******************************************************************************************
init:
;CONFIGURES SYSTEM CONTROL
mov #HIGH_6_13(SOPT), PAGESEL
mov #$03, MAP_ADDR_6(SOPT) ; Disables COP, enables BKGD (PTA3) and RESET (PTA2)
;CONFIGURES CLOCK (FEI Operation Mode)
mov #HIGH_6_13(NV_ICSTRM),PAGESEL
lda MAP_ADDR_6(NV_ICSTRM)
sta ICSTRM ; Sets trimming value
clr ICSC1 ; Selects FLL as clock source and disables it in stop mode
clr ICSC2 ; ICSOUT = DCO output frequency
wait_clock:
brset CLKST,ICSSC,wait_clock ; Waits until FLL is engaged
;CONFIGURES TIMER
mov #$70, MTIMSC ; Enables interrupt, stops and resets timer counter
mov #$FF, MTIMMOD ; MTIM modulo = 256 counts before interrupt
mov #$08, MTIMCLK ; Selects fBUS as reference clock (8 MHz)
;CONFIGURES I/O CONTROL PORT
mov #$31, PTADD ; Configures PTA0, PTA4 and PTA5 as output,PTA1 as an Input
mov #HIGH_6_13(PTAPE), PAGESEL
mov #$04, MAP_ADDR_6(PTAPE) ; Enables Pull UP on PTA2
;CONFIGURES KEYBOARD INTERRUPTS (KBI)
mov #$00, KBIES ; Selects Falling Edge/Low on Pin
mov #$02, KBIPE ; PTA1 as KBI input
mov #$06, KBISC ; Clears any false interrupts
clr PTAD ; Clears PTA port
rts
;*******************************************************************************************
; Entry Point
;*******************************************************************************************
_Startup:
main:
bsr init
clr counter
clr counter2
clr counter3
main_loop:
loopRelay:
mov #HIGH_6_13(SIP1),PAGESEL
brset 4,MAP_ADDR_6(SIP1),RelayOn ;If button pushed goto Relay on
bra loopRelay
;No push button continue to wait
RelayOn:
bset 2, KBISC ;Clear KBI Interrupt
bset 5, PTAD ;Turn on Relay LED
bset 4,PTAD ;Turn on Relay
;BELOW IS THE NESTED DELAY LOOPS I USED
StartDelay:
mov #$60,MTIMSC
bclr 4,MTIMSC ; Start MTIM counter
wait
lda counter
cbeqa #255,Delay2
inc counter
bra StartDelay
Delay2:
clr counter
lda counter2
cbeqa #55,Delay3
inc counter2
bra StartDelay
Delay3:
clr counter
clr counter2
lda counter3
cbeqa #57, RelayOff
inc counter3
bra StartDelay
RelayOff
clr counter
clr counter2
clr counter3
bclr 5,PTAD
bclr 4,PTAD
bra loopRelay
Hello Rod,
Using the MTIM for such a long time delay, I might suggest -
With these settings, the MTIM clock frequency should be 62.5 Hz, assuming the reference clock is properly trimmed. Now for a free-running MTIM, the overflow period should be 4.096 seconds. However, if the MTIMMOD value were to be set to decimal 249, the overflow period should be exactly 4.0 seconds, which may be more convenient to work with.
A two-hour timeout period now requires that decimal 1800 overflows should occur, and this requires a 16-bit counter. Consider the following untested code snippet:
DELAY EQU 1800
count DS.W 1
...
StartDelay:
mov #(DELAY/256),count ; High byte
mov #(DELAY%256),count+1 ; Low byte
mov #$60,MTIMSC ; Start MTIM counter
mov #HIGH_6_13(SIP1),PAGESEL
loop1:
wait
brclr 2,MAP_ADDR_6(SIP1),loop1 ; Wait for MTIM overflow
mov #HIGH_6_13(MTIMSC),PAGESEL
bclr 7,MAP_ADDR_6(MTIMSC) ; Clear flag
lda count+1 ; Decrement word value
bne next1
dec count
next1:
dec count+1
lda count ; Test for timeout
ora count+1
bne loop1 ; Loop if not timeout
; Do timeout action here
Regards,
Mac
Thanks Mac,
I'll give that a try.
Have a great day,
Rod