;File: Wdt842.asm
;Author: Eckart Hartmann Date:17/10/2003
;Description of Software: Demonstrates Watchdog timer functions.
;Development progress: Wdt834.df
;
extrn CODE (_WdtCfg) ; _WdtCfg in ADuC834.lib
extrn CODE (_WdtKk) ; _WdtKk )
extrn CODE (_PllDly) ; _PllDly )

NAME WDT834
$NOMOD51
 $IC(..kei842.inc) ; Parameter passing registers for Keil .
 $IC(..kei842.dat) ; SFR definition for Keil .
;
	CSEG at 0000h
rstorg:	ljmp	amain
;
;Function Start======================================================Function Start
;==========Compiler Specifics:
;
;WdtInt==========Watchdog interrupt entry.
;C Function prototype: interrupt void WdtInt(void);
;Description of Function: On interrupt it calls user watchdog handler.
;User interface: User watchdog handler must be at WdtUInt.
;Robustness: No known problems.
;Side effects: Uses 2 stack levels.
;
	CSEG at 0005bh		;WDT interrupt vector.
WdtInt:	lcall	WdtUint		;Jump to user watchdog interrupt handler.
	reti
;
;WdtUint==========Watchdog user handler.
;C Function prototype: char WdtUint(void);
;Description of Function: User defined.
;User interface: User defined.
;Robustness: User defined.
;Side effects: User defined.
;
WdtUint:;insert watchdog handler here.
	cpl	P3.3		;To show interrupt occured.
;setb	WDS
	ret
;
;Function End==========================================================Function End
;Assembler main program.===========================================================
;
;	org 00140h
;
WdWas:	lcall	flick		;Flicker for 4s to indicate
	lcall	flick		; watchdog reset restart.
	mov	a,HOUR		;If HOUR=5
	cjne	a,#5,WdFrc
WdEnd:	lcall	flick		; we are done.
	mov	ip1l,#200	;200ms delay should do nothing.
	mov	ip1h,#0
	lcall	_PllDly
	sjmp	WdEnd
WdFrc:	mov	HOUR,#5		;Else set HOUR as marker immune to reset.
	clr	P3.4
	mov	ip1l,#0b8h	;3s delay should do nothing.
	mov	ip1h,#00bh
	lcall	_PllDly
	mov	cp1l,#082h	;Force reset.
	lcall	_WdtCfg
	nop
	ljmp	WdErr		;If no reset occured then error stop.
;
amain:	clr	P3.3		;Mark start.
	mov	ip1l,#0b8h	;3s delay should do nothing.
	mov	ip1h,#00bh
	lcall	_PllDly
	clr	P3.4		;Signal 3s delay over.
	jb	WDS,WdWas	;If reset by watchdog, go to WdWas.
	mov	HOUR,#0		;else mark for first boot.
	mov	MIN,#0
	mov	TIMECON,#1	;Start timer else HOUR is lost by resets.
	mov	cp1l,#06ah	;Start watchdog for 1s interrupt.
	lcall	_WdtCfg
	mov	ip1l,#058h	;Repeat 5 times: wait 600ms
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4		; flash 100ms
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk		; kick watchdog before interrupt.
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#04ch	;If in 1.1s
	mov	ip1h,#004h
	lcall	_PllDly
;	lcall	WdtRd		; no interrupt occured
mov	cp1l,WDCON
mov	a,cp1l
	cjne	cp1l,#06ah,WdEr1	; then error stop.
;	cjne	cp1l,#06eh,WdEr1	; then error stop.
	sjmp	WdFl1
WdEr1:	ljmp	WdErr
WdFl1:	lcall	flick		;else flicker.
	mov	cp1l,#062h	;Start watchdog for 1s reset.
	lcall	_WdtCfg
	mov	ip1l,#058h	;Repeat 5 times: wait 600ms
	mov	ip1h,#00h
	lcall	_PllDly
	setb	P3.4		; flash 100ms
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk		; kick watchdog before interrupt.
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#058h
	mov	ip1h,#002h
	lcall	_PllDly
	setb	P3.4
	mov	ip1l,#100
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	lcall	_WdtKk
	mov	ip1l,#04ch	;If in 1.1s
	mov	ip1h,#004h
	lcall	_PllDly
	ljmp	WdErr		; no reset occured then error stop.
;
WdErr:	setb	P3.4		;Signal error
	mov	ip1l,#30	; 100ms delay.
	mov	ip1h,#0
	lcall	_PllDly
	clr	P3.4
	mov	ip1l,#0		; ~2s delay.
	mov	ip1h,#8
	lcall	_PllDly
	sjmp	WdErr		; continuously.
;
flick:	mov	r2,#33		;Flicker P3.4 for 2 seconds.
flickL:	mov	ip1l,#30
	mov	ip1h,#00
	lcall	_PllDly
	setb	P3.4
	lcall	_WdtKk
	mov	ip1l,#30
	mov	ip1h,#00
	lcall	_PllDly
	clr	P3.4
	djnz	r2,flickL
	ret
END


