;************************************************** ; Pic by example ; WDT.ASM ; Watch Dog Timer example ; ; (c) 2004, Sergio Tanzilli ; http://www.tanzilli.com ;************************************************** PROCESSOR 16F84A RADIX DEC INCLUDE "P16F84A.INC" ERRORLEVEL -302 ;Setup of chip flags ;Enable watch dog timer ;Enable power up timer ;XT oscillator ;Disable code protect __CONFIG 3FF5H SWITCH1 EQU 0 SWITCH2 EQU 1 LED1 EQU 2 ORG 0CH ;16 bit counter used in the delay subroutine Count RES 2 ;Reset Vector ;Start point at CPU reset ORG 00H ;Jump to main body of program. goto Start ;********************************************************************** ; Interrupt vector ; Start point for every interrupt handler ;********************************************************************** ORG 04H ;********************************************************************** ; Interrupt handler ;********************************************************************** bcf INTCON,INTF ;Reset INTF flag retfie ;Return to the main body ;********************************************************************** ; Main body ;********************************************************************** Start: bsf STATUS,RP0 ;Swap to data bank 1 ;I/O lines definition on port A (0=output, 1=input) movlw 00011111B ;Definition of port a movwf TRISA ;I/O lines definition on port B (0=output, 1=input) bsf TRISB,SWITCH1 ;Switch 1 bsf TRISB,SWITCH2 ;Switch 2 bcf TRISB,LED1 ;Led 1 ;Set to 0 the INTEDG bit on OPTION register ;to have an interrupt on the falling edge of RB0/INT bcf OPTION_REG,INTEDG ;Assign the PRESCALER to Watch dog timer bsf OPTION_REG,PSA ;Set the PRESCALER to 1:128 bsf OPTION_REG,PS0 bsf OPTION_REG,PS1 bsf OPTION_REG,PS2 bcf STATUS,RP0 ;Swap to data bank 0 bsf INTCON,GIE ;Enables interrupts bsf INTCON,INTE ;Enables RB0/INT interrupt bcf PORTB,LED1 ;Turn off LED1 ;********************************************************************** ; Main loop ;********************************************************************** MainLoop btfss PORTB,SWITCH2 ;If switch2 is down enter in StopLoop goto StopLoop ;Stops CPU clrwdt ;Clear wtahc dog timer call Delay ;Software delay ;If LED1 in on then turn it off and viceversa btfss PORTB,LED1 ;Led on ? goto TurnOnLed1 ;No, turn it on goto TurnOffLed1 ;Yes, turn it off ;Turn LED1 on TurnOnLed1 bsf PORTB,LED1 goto MainLoop ;Turn LED1 off TurnOffLed1 bcf PORTB,LED1 goto MainLoop ;********************************************************************** ; Software delay ;********************************************************************** Delay clrf Count clrf Count+1 DelayLoop decfsz Count,1 goto DelayLoop decfsz Count+1,1 goto DelayLoop return END