 title "Pager Reset for MOOS Mooring"
;*********************************************************************
; Program: Pager Reset for MOOS Mooring
; Date: 10/10/2002
; Programmer: Lance McBride
; Target: PIC16F627 PDIP
;
; This will act as a state machine for incoming pages for reseting
; the controller
; Internal RC Oscillator: 4.00MHz (EACH INSTRUCTION=1.0us)
;*********************************************************************
 LIST P=16F627, F=INHX32, R=HEX
 errorlevel 0,-305
 INCLUDE "c:\apps\mplab\P16f627.inc"

; Register addresses
j equ 0x20;
k equ 0x21;
i equ 0x22;
test equ 0x23;
test2 equ 0x24;

 __CONFIG _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _MCLRE_ON & _PWRTE_ON

 PAGE
; Mainline of FIRST

 org	0
 goto   Initialize		; Initialize the system

 org	4			; interrupt handler (NOT ENABLED)
 goto RCVD			; on wake up, goto RCVD state

Initialize:
; initialize registers
 movlw 0x00
 movwf i
 movlw 0x00
 movwf j
 movlw 0x00
 movwf k
 movlw 0x00
 movwf test
 
; starts in bank 0 

 movlw 	0x00			; set port A output latch to 
 movwf	PORTA			; all pins to logic low
 bcf 	STATUS, RP1		; goto bank 1 to set port direction
 bsf    STATUS, RP0            
 movlw	0x1C			; set up direction on PORTA<1:0> as 
 movwf	TRISA			; all out, PORTA<2:4> as input
 bcf 	STATUS, RP1		; return to bank 0 
 bcf    STATUS, RP0            

 movlw 	0x00			; set port B output latch to 
 movwf	PORTB			; all pins to logic low
 bcf 	STATUS, RP1		; goto bank 1 to set port direction
 bsf    STATUS, RP0            
 movlw	0xFF			; set up direction on PORTB as 
 movwf	TRISB			; all inputs
 bcf 	STATUS, RP1		; return to bank 0 
 bcf    STATUS, RP0            

; Initialize Ports
 movlw 	0x00			; set all PORTA pins to logic 0
 movwf	PORTA

INIT_PAGER:
; Initialize Pager by reseting pager
 bsf	PORTA, 0		;  - turn off pager
 call  	Delay			;  - wait ~5 seconds
 bcf	PORTA, 0		;  - turn on pager
 
StartStateMachineLoop:

RST_Start:
 btfsc PORTB,0			; wait for reset signal to start
 goto RST_Start
RST_End:
 btfss PORTB,0			; wait for reset signal to end
 goto RST_End

BUOY_RESET:			; reset buoy
 bsf	PORTA, 1		
 call  	Delay
 bcf	PORTA, 1

WAIT:
; sleep				; wait for page
 nop
 btfsc PORTB,0			; wait for reset signal to start
 goto WAIT

RCVD:
 btfss PORTB,0			; wait for page to end
 goto RCVD

OFF:				; reset pager
 bsf	PORTA, 0		;  - turn off pager
 call  	Delay			;  - wait ~5 seconds
 bcf	PORTA, 0		;  - turn on pager

 goto StartStateMachineLoop

; PROCEDURE: Simple multiple instruction delay
; Total procedure = 5,010,034 cycles
; Time/cycle @ 4MHz = 1us
; Total delay time = 5.010034 seconds
Delay:

 movlw  0x13
 movwf  i
DelayLoop1:      ; 19 * (6 + 263680)
 nop

  movlw  0xFF
  movwf  j
DelayLoop2:      ; 256 * (6 + 1024)
  nop
 
  movlw	 0xFF
  movwf	 k
DelayLoop3:      ; 256 * 4
   nop
    decfsz  k			; reduce loop counter
    goto DelayLoop3
   decfsz j
   goto DelayLoop2
  decfsz i
 goto DelayLoop1
 
 return
 
Finished:			; now, just spin forever
 goto $

 end
