 title "Sample Application for Mud Power System"
;*********************************************************************
; Program: Mud Power Cell Sample Application
; Date: 11/16/2004
; Programmer: Lance McBride
; Target: PIC10F206 SOT23-6
;
; This switches between supercaps and blinks an LED
; Internal RC Oscillator: 4.0000MHz (EACH INSTRUCTION CYCLE=1us)
;*********************************************************************
; .LIST P=10F206, F=INHX32, R=HEX
 INCLUDE "c:\program files\microchip\mpasm suite\P10F206.inc"


; Register addresses
i equ 0x10	; assigns register address 0x20 to variable j
j equ 0x11
k equ 0x12
m equ 0x13
n equ 0x14
o equ 0x15
p equ 0x16
q equ 0x17
r equ 0x18
s equ 0x19
t equ 0x1A

;__CONFIG _CP_OFF & _WDT_OFF

;text
; Mainline of FIRST

 org	0

; global __reset

; initialize registers.
; These (i, j, k) control the delay. The contents of each register;
; counts down until it hits zero, so the starting value of the
; register controls how long the delay is.

reset:
 clrwdt
 movlw #20			; moves 8-bit literal to working register 0 (W0)
 movwf i			; moves contents of W0 to i
 movlw #0xFF
 movwf j
 movlw #0xFF
 movwf k
 movlw #0xFF
 movwf m
 movlw #0xFF
 movwf n
 movlw #0xFF
 clrwdt
 movwf o
 movlw #0xFF
 movwf p
 movlw #0xFF
 movwf q
 movlw #0xFF
 movwf r
 movlw #0xFF
 movwf s
 movlw #0xFF
 movwf t

 bcf CMCON0, 3		; disable comparator
 movlw #0xDF        ; set timer to transition on internal clock cycle
 OPTION				

; starts in bank 0 

 movlw 	#0x00			; set port D output latch to 
 movwf	GPIO			; all pins to logic low
 clrwdt	
            
 movlw	#0x08			; set up direction on GPIO0, GPIO1, GPIO2 as all out
 TRIS   GPIO			; '1'=input  '0'=output	 

StartLoop:
 clrwdt
 movlw B'001'      ; turn on Relay coil 0
 movwf GPIO
 call ShortDelay   ; wait a short bit
 movlw B'000'      ; turn off Relay coil 0
 movwf GPIO
; call ShortDelay   ; wait a short bit
 call Delay        ; wait about 0.55 hours

 clrwdt
 movlw B'010'      ; turn on Relay coil 1
 movwf GPIO
 call ShortDelay   ; wait a short bit
 movlw B'000'      ; turn off Relay coil 1
 movwf GPIO
; call ShortDelay   ; wait a short bit
 call Delay        ; wait about 0.55 hours

 goto StartLoop

; PROCEDURE: Simple multiple instruction delay
; Time/cycle @ 4MHz = 1us
; Total delay time = 0.55hours
Delay:
; movlw D'1'           ; 1 us
; movwf i              ; 1 us
;DelayLoop1:           ; total loop 1 delay 73802.660622 seconds
;  nop                 ; 1 us
;  movlw D'1'         ; 1 us
;  movwf j             ; 1 us
;DelayLoop2:           ; total loop 2 delay 36901.33 seconds
;   nop                ; 1 us
   movlw D'3'        ; 1 us
   movwf k            ; 1 us
DelayLoop3:           ; total loop 3 delay 2460.089seconds
     nop              ; 1 us
     bsf GPIO, 2      ; 1 us  (Turn ON LED)
     call ThreeSecondDelay  ; 2 us + 3.004420s
     bcf GPIO, 2      ; 1 us  (Turn OFF LED)
;     call ThreeSecondDelay  ; 2 us + 3.004420s (FOR DEBUGGING ONLY)
     call TwoMinDelay ; 2 us + 120.000004s
     decfsz k,1       ; 1 us
     goto DelayLoop3  ; 2 us

   clrwdt             ; 1 us
;   decfsz j, 1        ; 1 us
;   goto DelayLoop2    ; 2 us
;
;  decfsz i, 1         ; 1 us
;  goto DelayLoop1     ; 2 us

 RETLW 0              ; 2 us
 
; PROCEDURE: Simple multiple instruction delay
; Time/cycle @ 4MHz = 1us
; Total delay time = 261.889ms
; Revised total delay time 5.02ms
ShortDelay:

;  movlw D'255'            ; 1 us
  movlw D'5'              ; 1 us
  movwf m                 ; 1 us
ShortDelayLoop2:          ; total loop2 = 261.885ms
   nop                    ; 1 us
   movlw D'255'           ; 1 us
   movwf n                ; 1 us
ShortDelayLoop3:          ; total loop3 = 1.02ms
     nop                  ; 1 us
     decfsz n,1           ; 1 us
     goto ShortDelayLoop3 ; 2 us

   clrwdt                 ; 1 us
   decfsz m, 1            ; 1 us
   goto ShortDelayLoop2   ; 2 us

 RETLW 0                  ; 2 us

; PROCEDURE: Simple multiple instruction delay
; Time/cycle @ 4MHz = 1us
; Total delay time = 120.000004s
TwoMinDelay:
 movlw D'255'              ; 1 us
 movwf o                   ; 1 us
TwoMinDelayLoop1:          ; total loop1 = 120.000000seconds
  nop                      ; 1 us
  nop                      ; 1 us
  nop                      ; 1 us
  nop                      ; 1 us
  nop                      ; 1 us
  nop                      ; 1 us
  nop                      ; 1 us
  nop                      ; 1 us
  movlw D'255'             ; 1 us
  movwf p                  ; 1 us
TwoMinDelayLoop2:          ; total loop2 = 468.736ms
   nop                     ; 1 us
   nop                     ; 1 us
   nop                     ; 1 us
   nop                     ; 1 us
   nop                     ; 1 us
   nop                     ; 1 us
   nop                     ; 1 us
   movlw D'201'            ; 1 us
   movwf q                 ; 1 us
TwoMinDelayLoop3:          ; total loop3 = 1.818ms
     nop                   ; 1 us
     nop                   ; 1 us
     nop                   ; 1 us
     nop                   ; 1 us
     nop                   ; 1 us
     clrwdt                ; 1 us
     decfsz q,1            ; 1 us
     goto TwoMinDelayLoop3 ; 2 us

   clrwdt                  ; 1 us
   decfsz p, 1             ; 1 us
   goto TwoMinDelayLoop2   ; 2 us

  nop                      ; 1 us
  decfsz o, 1              ; 1 us
  goto TwoMinDelayLoop1    ; 2 us

 RETLW 0                   ; 2 us

; PROCEDURE: Simple multiple instruction delay
; Time/cycle @ 4MHz = 1us
; Total delay time = 3.004420s
ThreeSecondDelay:               
 movlw D'255'                   ; 1 us
 movwf r                        ; 1 us
ThreeSecondDelayLoop1:          ; total loop1 = 3.004416s
  movlw D'254'                  ; 1 us
  movwf s                       ; 1 us
ThreeSecondDelayLoop2:          ; total loop2 = 11.73ms
   nop                          ; 1 us
   nop                          ; 1 us
   nop                          ; 1 us
   nop                          ; 1 us
   movlw D'8'                   ; 1 us
   movwf t                      ; 1 us
ThreeSecondDelayLoop3:          ; total loop3 = 36us
     clrwdt                     ; 1 us
     decfsz t,1                 ; 1 us
     goto ThreeSecondDelayLoop3 ; 2 us

   clrwdt                       ; 1 us
   decfsz s, 1                  ; 1 us
   goto ThreeSecondDelayLoop2   ; 2 us

  decfsz r, 1                   ; 1 us
  goto ThreeSecondDelayLoop1    ; 2 us

 RETLW 0                        ; 2 us


Finished:			; now, just spin forever
 goto $

 end

