;********************************************************************
;
; Author        : ADI - Apps            www.analog.com/MicroConverter
;
; Date          : October 2003
;
; File          : PSMonS.asm
;
; Hardware      : ADuC842/ADuC843
;
; Description   : Demonstrates use of on-chip power supply monitor.
;                 In normal operation, this code flashes the LED at
;                 approximately 5Hz.  When Vdd drops below the user
;                 specified trip-point (here 4.63V) the PSM interrupt
;                 is executed.  once inside this interrupt service
;                 routine, this code waits until the PSM interrupt
;                 bit becomes zero again, indicating that the power
;                 supply is again above the trip point and has been
;                 there for at least 256ms.  at this point, a RETI
;                 instruction is executed, and normal code execution
;                 is resumed, indicated by the flashing LED.
;
;********************************************************************
$MOD842

LED     EQU     P3.4            ; P3.4 drives red LED on eval board

;____________________________________________________________________
                                                  ; BEGINNING OF CODE
CSEG

ORG 0000h

        JMP     MAIN            ; jump to main program
;____________________________________________________________________
                                             ; INTERRUPT VECTOR SPACE
ORG 0043h ;  (PSM ISR)

        SETB     LED             ; turn off the LED to indicate fault

; most often, a routine would here be called to store critical values
; in user Flash/EE space and wait in a "safe" state of code execution
; until the PSM interrupt bit becomes zero indicating that adequate
; power supply voltage has returned.

CHECK:  MOV     A,PSMCON        ; PSMCON.5 is the PSM interrupt bit..
        JB      ACC.5,CHECK     ; ..it is cleared only when Vdd has
                                ; remained above the trip point for
                                ; 256ms or more.
	RETI                    ; return only when "all's well"

;====================================================================
                                                       ; MAIN PROGRAM
ORG 0060h                       ; start program above interrupts

MAIN:

        MOV     PSMCON, #041H   ; enable PSM with 
				;  Vdd 4.63V threshold
        MOV     IEIP2, #22H     ; enable PSM interrupt
				; high priority for PSM interrupt
        SETB    EA              ; enable interrupts
	MOV	A,#10
FLASH:  	                ; Main Routine would go here
        CPL     LED             ; blink LED indicating norm operation
        CALL    DELAY           ; delay 100ms
        JMP     FLASH           ; loop

;____________________________________________________________________
                                                         ; SUBROUTINE
                                                        ; 10ms DELAY
DELAY:					; Delays by 100ms 
					; 10mSec based on 2.097152MHZ 
					; Core Clock 
					; i.e. default ADuC842 Clock

		MOV	R1,A		
 DLY0:	        MOV    	R2,#01Bh	; Set up delay loop0
 DLY1:		MOV	R3,#0FFh	; Set up delay loop1
		DJNZ	R3,$		; Dec R3 & Jump here until R3 is 0
		DJNZ	R2,DLY1         ; Dec R2 & Jump DLY1 until R2 is 0
		DJNZ	R1,DLY0
		RET			; Return from subroutine

;____________________________________________________________________

END
