;**********************************************************
;  KiteCamera.asm
;
;  A simple timer to take pictures at a slow (90 second) rate.
;  The picture output is active-low.
;  The low signal should be held for 3 seconds (?).
;
;  This program relies on the factory calibration being left
;  in address 0x3FF - do not erase or overwrite this
;  program location.
;
;  An external oscillator at a very low rate would be best,
;  but for simplicity we're using the 4 MHz internal clock
;  (talk about overkill!).  To reduce power, we go to sleep
;  and have the watch dog timer wake us up at a rate
;  of the active period (3 seconds).  Note that the watch
;  dog timer is not very accurate.
;
;  PIN ASSIGNMENT (PIC12F675)
;
;  VCC---1   8---GND
;    X---2   7---X
;  GP4---3   6---X
;    X---4   5---X
;
; GP4 => Output
;
;**********************************************************

	list p=12f675
	include "p12f675.inc"

; Timing
; 
; 1 second nominal (?) cycle period

WAIT_TO_PRESS_CYCLES equ .87 
WAIT_TO_RELEASE_CYCLES equ .3 

; Testing only
;WAIT_TO_PRESS_CYCLES equ .10 
;WAIT_TO_RELEASE_CYCLES equ .1 


; Configuration bits
;
; BG1 BG0 | - - - CPD | CP BODEN MCLRE PWTRE | WDTE FOSC2 FOSC1 FOSC0
;  *   *    1 1 1  1    1    0     0     0      0     1     0     0
;    F(?)      F                 8                     4
; CPD - Data Code Protection = 1 (disabled)
; CP - Code Protection = 1 (disabled)
; BODEN - Brown Out Detect = 1 (enabled)
; MCLRE - External Reset pin enable = 0 (GP3 = input, MCLR internally tied to VDD)
; PWRTE - Power on Timer Enable = 0 (enabled)
; WDTE - Watchdog = 1 enabled
; FOSC2:FOSC0 = Oscillator Selection = 100 (INTOSC-Internal oscillator, CLKOUT disabled)
; 
;	__config H'3F84' 

	__CONFIG  _CPD_OFF & _CP_OFF & _BODEN_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_ON & _INTRC_OSC_NOCLKOUT


;*********************************
; Macro:		SelectBank0	  
; Description:	Selects the first bank of registers in
;               preparation for direct access.
;
; Arguments:    none
;
; Requires:     none
;
; Uses:         none
;
; Returns:      none
;                                                             
SelectBank0 macro
	bcf 	STATUS, RP0
	endm

;*********************************
; Macro:		SelectBank1	  
; Description:	Selects the second bank of registers in
;               preparation for direct access.
;
; Arguments:    none
;
; Requires:     none
;
; Uses:         none
;
; Returns:      none
;                                                             
SelectBank1 macro
	bsf 	STATUS, RP0
	endm

; Register definitions
SHUTTER_BIT		equ 	4
SHUTTER_FILE	equ 	GPIO

;*********************************
; Macro:		Press/ReleaseShutter                                   		  
; Description:	Activates/deactivates the camera shutter button input
;
; Arguments:    none
;
; Requires:     Bank 0 selected
;
; Uses:         none
;
; Returns:      none
;                                                             
ReleaseShutter macro
	bsf SHUTTER_FILE,SHUTTER_BIT
	endm

PressShutter macro
	bcf SHUTTER_FILE,SHUTTER_BIT
	endm

; Memory Register designations (Bank0 Free Ram:  20-5Fh)
; Replace with (res)
cycleCount		equ 20h

	; Start at the reset vector
Reset_Vector  	org 0x000
	goto 	Start
	nop		

	; Start application beyond vector area
Start 	org 0x005		

	SelectBank0

	clrf 	GPIO ;Init GPIO
	; Before we activate any outputs - turn the shutter off
	ReleaseShutter

	SelectBank1

	; Make sure that GPIO2 is configured as an I/O pin not an A/D pin

	; ANSEL (BANK1)
	; 7 6 5 4 3 2 1 0
	; - X X X Y Y Y Y  X=ADCS<2:0> = 001 for 2us conversions (2us ok too)
    ;                  Y=ANS3:ANS0 - 0000; 1=Analog Input, 0=Digital Input
	movlw 	010h ; Set ADCS<2:0>=001, ANS3:ANS0=0000 
	movwf 	ANSEL


	; Turn on any outputs
	;
	; TRISIO (BANK1)
	; 7 6 5 4 3 2 1 0
	; - - X X X X X X  X=GPN = 1 for input
	; 0 0 1 1 1 0 1 1
;	movlw 	03Bh; Set all but GP2 as inputs
	movlw 	000h; Set all but GP2 as inputs
	movwf	TRISIO

	; Nominal WDT at 5V:  17ms
	; Nominal WDT at 3.3V: 
    ;
    ; If 17 msec, 64 * 17 is about 1 second
	; OPTION_REG (BANK1)
	;
	;   7     6     5    4    3   2   1   0
	; GPPUB INTEDG T0CS T0SE PSA PS2 PS1 PS0
	;   1     1     1    1    1   1   1   0 = 0xFE
	;
	; GPPUB:  1 = pull ups disabled
	; INTEDG: 1 = rising edge (don't care - using default)
	; TOCS:   1 = GP2 timer source (don't care - using default)
	; TOSE:	  1 = high-to-low clock edge (don't care - using default)
	; PSA:    1 = Prescalar assigned to the WDT
	; PS2:PS0 110 = 1:64 WDT Prescalar
	;
	movlw	0xFE
	movwf	OPTION_REG

	SelectBank0

Use_Factory_Oscal_Calibration
	bsf 	STATUS, RP0 ;Bank 1
	call 	3FFh ;Get the cal value
	movwf 	OSCCAL ;Calibrate
	bcf 	STATUS, RP0 ;Bank 0

goto Press_The_Shutter

Wait_To_Press
	movlw WAIT_TO_PRESS_CYCLES
	movwf cycleCount
Wait_To_Press_Loop
	clrwdt	; Tickle the Watchdog
	sleep	; Sleep until the watchdog timer wakes us up
	decfsz cycleCount, f
	goto Wait_To_Press_Loop
Press_The_Shutter
	PressShutter
	goto Wait_To_Release

Wait_To_Release
	movlw WAIT_TO_RELEASE_CYCLES
	movwf cycleCount
Wait_To_Release_Loop
	clrwdt	; Tickle the Watchdog
	sleep	; Sleep until the watchdog timer wakes us up
	decfsz cycleCount, f
	goto Wait_To_Release_Loop
Release_The_Shutter
	ReleaseShutter
	goto Wait_To_Press

ProgramLimit 	org 0x300
	nop ; Warn if any code overlaps this boundary close to the end
		; of the program space

	end	

	



