 title "Mud Power Cell Capacitor Interface Board"
;*********************************************************************
; Program: Mud Power Cell Capacitor Interface Board
; Date: 05/16/2005
; Programmer: Lance McBride
; Target: PIC16F676 DIP-14
;
; This switches between supercap banks 
; Internal RC Oscillator: 4.0000MHz (EACH INSTRUCTION CYCLE=1us)
;*********************************************************************
; .LIST P=16F676, F=INHX32, R=HEX
 INCLUDE "c:\program files\microchip\mpasm suite\P16F676.inc"


; Register addresses
j     equ 0x20;
k     equ 0x21;
i     equ 0x22;
test  equ 0x23;
test2 equ 0x24;
swA   equ 0;
swB   equ 1; 
swC   equ 2;
swD   equ 3;
swE   equ 4;
swF   equ 5;

 org	0x0000          ; processor reset vector
 goto   Initialize		; Initialize the system

 org	0x0004		    ; interrupt handler
; CLRWDT				; clear watchdog timer
; bcf	INTCON, INTF		; clear port RB0/INT interupt flag
; bcf	INTCON, RBIF		; clear RB Port Change interrupt flag
; RETFIE				; on wake up, goto RCVD state reenabling all interrupts

 org    0x0010
Initialize:
; initialize registers
 movlw 0x00
 movwf i
 movlw 0x00
 movwf j
 movlw 0x00
 movwf k
 movlw 0x00
 movwf test

; starts in bank 0 
; Set PORTC direction
 movlw 	0x00			; set port C output latch to 
 movwf	PORTC			; all pins to logic low
 bcf 	STATUS, RP1		; goto bank 1 to set port direction
 bsf    STATUS, RP0            
 movlw	0x00			; set up direction on PORTC as 
                        ; 1 = input
                        ; 0 = output
 movwf	TRISC			; all out
 bcf 	STATUS, RP1		; return to bank 0 
 bcf    STATUS, RP0            
; Set PORTA direction
 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	0x1F			; set up direction on PORTA<0:4> as 
 movwf	TRISA			; all inputs, and PORTA<5:7> as outputs
                        ; 1 = input
                        ; 0 = output
 bcf 	STATUS, RP1		; return to bank 0 
 bcf    STATUS, RP0            


; Initialize Ports
 movlw 	0x00			; set all PORTC pins to logic 0
 movwf	PORTC
 bsf    PORTA, 5        ; turn PIC LED on

;; Initialize watchdog timer
; bcf 	STATUS, RP1		; goto bank 1 to set port direction
; bsf    STATUS, RP0            
; bsf	OPTION_REG, PS0		; set watchdog division ratio to 1:128
; bsf	OPTION_REG, PS1		; for a 2.3 second time-out period
; bsf	OPTION_REG, PS2		; 
; bsf    OPTION_REG, PSA		; assign prescaler to watchdog timer
; bcf 	STATUS, RP1		; return to bank 0 
; bcf    STATUS, RP0            
; 
;; Initialize Interrupts
; bcf 	STATUS, RP1		; goto bank 1 to set port direction
; bsf    STATUS, RP0            
; bcf	OPTION_REG, INTEDG	; set RB0/INT interrupt to falling edge triggered
; bcf 	STATUS, RP1		; return to bank 0 
; bcf    STATUS, RP0            
; bcf	INTCON, INTF		; clear port RB0/INT interupt flag
; bcf	INTCON, RBIF		; clear RB Port Change interrupt flag
; bsf	INTCON, INTE		; enable RB0/INT Interrupt
; bsf    INTCON, RBIE		; enable port B change interrupt

main:

 bsf    PORTA, 5        ; turn PIC LED on
 call   Delay
 bcf    PORTA, 5        ; turn PIC LED on
 call   Delay
; call TestRelayControls
 call   EnableBank0
 call   Delay
 call   Delay
 call   Delay
 call   EnableBank1
 call   Delay
 call   Delay
 call   Delay

 goto main

;************************************************************************************
; PROCEDURE: Tests the relay control lines
;************************************************************************************
TestRelayControls:
 bsf    PORTC, swA ; Test relays on contol line A
 call   Delay
 bcf    PORTC, swA ; disable relay switch
 call   Delay

 bsf    PORTC, swB ; Test relays on contol line B
 call   Delay
 bcf    PORTC, swB ; disable relay switch
 call   Delay
 
 bsf    PORTC, swC ; Test relays on contol line C
 call   Delay
 bcf    PORTC, swC ; disable relay switch
 call   Delay
 
 bsf    PORTC, swD ; Test relays on contol line D
 call   Delay
 bcf    PORTC, swD ; disable relay switch
 call   Delay

 bsf    PORTC, swE ; Test relays on contol line E
 call   Delay
 bcf    PORTC, swE ; disable relay switch
 call   Delay

 bsf    PORTC, swF ; Test relays on contol line F
 call   Delay
 bcf    PORTC, swF ; disable relay switch
 call   Delay

 return

;************************************************************************************
; PROCEDURE: Enable Bank 0
;************************************************************************************
EnableBank0:

 bsf    PORTC, swA ; parallel ->
 call   ShortDelay
 bsf    PORTC, swD ; -> serial
 call   ShortDelay
 bsf    PORTC, swE ; bank 1 -> bank 0
 call   ShortDelay
 bcf    PORTC, swA ; disable relay switch
 bcf    PORTC, swD ; disable relay switch
 bcf    PORTC, swE ; disable relay switch

 return

;************************************************************************************
; PROCEDURE: Enable Bank 1
;************************************************************************************
EnableBank1:

 bsf    PORTC, swB ; parallel ->
 call   ShortDelay
 bsf    PORTC, swC ; -> serial
 call   ShortDelay
 bsf    PORTC, swF ; bank 0 -> bank 1
 call   ShortDelay
 bcf    PORTC, swB ; disable relay switch
 bcf    PORTC, swC ; disable relay switch
 bcf    PORTC, swF ; disable relay switch

 return


;************************************************************************************
; PROCEDURE: Simple multiple instruction delay
; Total procedure = 2,109,488 cycles
; Time/cycle @ 4MHz = 1us
; Total delay time = 2.109488 seconds
;************************************************************************************
Delay:

 movlw  0x13
; movlw  0x01
 movwf  i
DelayLoop1:      ; 8 * (6 + 263680)
 CLRWDT

  movlw  0xFF
  movwf  j
DelayLoop2:      ; 256 * (6 + 1024)
  CLRWDT
 
  movlw	 0xFF
  movwf	 k
DelayLoop3:      ; 256 * 4
   CLRWDT			; clear watchdog timer
    decfsz  k			; reduce loop counter
    goto DelayLoop3
   decfsz j
   goto DelayLoop2
  decfsz i
 goto DelayLoop1
 
 return

;************************************************************************************

;************************************************************************************
; PROCEDURE: Simple multiple instruction delay
; roughly 1ms
;************************************************************************************
ShortDelay:

 movlw  0x01
 movwf  i
ShortDelayLoop1:      ; 8 * (6 + 263680)
 CLRWDT

  movlw  0x10
  movwf  j
ShortDelayLoop2:      ; 256 * (6 + 1024)
  CLRWDT
 
  movlw	 0xFF
  movwf	 k
ShortDelayLoop3:      ; 256 * 4
   CLRWDT			; clear watchdog timer
    decfsz  k			; reduce loop counter
    goto ShortDelayLoop3
   decfsz j
   goto ShortDelayLoop2
  decfsz i
 goto ShortDelayLoop1
 
 return

;************************************************************************************

 
Finished:			; now, just spin forever
 goto $

 end
