Please, check my watchdog timer code on atmega48

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi,

I choose to use the watchdog timer in the atmega48. 64ms would be enough time and with an interrupt before system reset.
Few things wonders me. Clearing and polling WDRF- and WDIF- flag... is it necessary to do so? I placed those lines just to make sure that the WDT is ready to use.
Other thing is an interrupt, should it work just like any other interrupt?

If you have different kind of a approach, please send it here. I would be interested to see. Bottom line, I need a robust WDT which works all the time.

here is the init section:

;***************** Initial of the watchdogtimer ***************************

cli					; Turn off global interrupt

WDT_Prescaler_Change:
;--------------------------------------------------------------------------
wdr					; Reset Watchdog Timer
;--------------------------------------------------------------------------
lds temp, WDTCSR	; Start timed sequence
ori temp, (1<<WDCE) | (1<<WDE)
;--------------------------------------------------------------------------
sts WDTCSR, temp	; Set new prescaler(time-out) value = 64ms
ldi temp, (1<<WDE) | (1<<WDP1)
sts WDTCSR, temp
;--------------------------------------------------------------------------
ldi temp, (1<<WDIE)	; Watchdog Interrupt Enable
sts WDTCSR, temp
;--------------------------------------------------------------------------

WDRF_flag:
;--------------------------------------------------------------------------
wdr
in temp, mcusr		; clear Watchdog System Reset Flag
cbr temp, (1<<WDRF)
out mcusr, temp
nop
nop
nop
nop
;--------------------------------------------------------------------------
in temp, mcusr		; test flag
sbrs temp, 3
rjmp WDRF_flag
;--------------------------------------------------------------------------

WDIF_flag:
;--------------------------------------------------------------------------
wdr
lds temp, WDTCSR	; clear Watchdog Interrupt Flag
sbr temp, (1<<WDIF)
sts WDTCSR, temp
nop
nop
nop
nop
;--------------------------------------------------------------------------
sbrc temp, WDIF		; test flag
rjmp WDIF_flag
;--------------------------------------------------------------------------


sei					; Turn on global interrupt

...and here is an interrupt code:

.org 0x006	; watchdog timer int.
rjmp WDT_TIMER
;************************** WDT Timer *************************************
WDT_TIMER:

;--------------------------------------------------------------------------
waiteeprom_wdt:	; alarm_control to eeprom
sbic EECR,EEPE
rjmp waiteeprom_wdt

ldi temp, 0x00
out EEARH, temp
ldi temp, 0x07
out EEARL, temp
out EEDR, alarm_control

sbi EECR,EEMPE
sbi EECR,EEPE
;--------------------------------------------------------------------------
reti

Regards
heguli