;**********************************************************************
;	Idle Current Reduction Firmware for THSTEP25 v1.0
;	www.thsengineering.com
;
;	Written under MPLAB IDE 6.12.0.0 
;	Compiled with MPASM 3.20.08
;		Case Sensitivity DISABLED
;
;	Copyright THS Engineering 2003
;	
;	Last Updated on: 2/11/03
;**********************************************************************
;
;	Program Description:
;
;	Initially, the GP4 pin is set high, bypassing R13 and supplying
;	VR1 with 5V.  
;
;	The program polls the INT pin interrupt flag in order to detect
;	a rising edge on the STEP signal.  If no activity on the step
;	signal is detected for 1 second, the GP4 pin is set to an input
;	(high-z), allowing the vref volatage to decrease.  GP4 goes high
;	again as soon as the next rising edge is detected on the STEP 
;	signal. 
;	
;	The GP0 pin is also polled.  If GP0 is pulled low, GP4 will
;	stay at 5V.  
;
;	Weak pullups are enabled on all pins except GP4.  The DIRECTION
;	and ENABLE signals are connected only to take advantage of the
;	pullups and are not used by the program.  
;
;**********************************************************************

	list      p=12f629           ; list directive to define processor
	#include <p12f629.inc>       ; processor specific variable definitions

	errorlevel  -302             ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 


;***** VARIABLE DEFINITIONS
delay_l			equ	0x22
delay_h			equ	0x23





;**********************************************************************
		ORG     0x000        ; processor reset vector
		goto    init         ; go to beginning of program
	

		ORG     0x00c

init:
		call    0x3FF        ; retrieve factory calibration value
		bsf     status,rp0   ; set file register bank to 1 
		movwf   osccal       ; update register with factory cal value 
		bcf     status,rp0   ; set file register bank to 0

		bcf 	status,rp0 		;Bank 0
		clrf 	gpio 				;Init GPIO
		movlw 07h 				;Set GP<2:0> to
		movwf cmcon 			;digital IO
		bsf 	status,rp0 		;Bank 1
		movlw b'00111111' 	;Set 0,1,2,3, 4 and 5 as inputs
		movwf trisio 			;
		bcf	wpu, 4			;disable wpu on gp4 
		bcf	option_reg, not_gppu		;global enable weak pullups 
		bcf 	status,rp0    	;Bank 0



fullcurrent:
		bsf	gpio, 4			;set GP4 high
		bsf	status, rp0 	;bank 1
		bcf	trisio, 4		;make GP4 output
		bcf	status, rp0 	;bank 0

clear:
		bcf	intcon, intf	;clear flag bit
		clrf	delay_l			;and delay counters
		clrf	delay_h

loop:
		btfsc	intcon, intf	;check for rising edge on step pin
		goto clear				;and clear
		btfss	gpio,0			;check for jp3 shorted to gnd
		goto clear				;and clear

		movlw	d'1'				;increment delay counters
		addwf	delay_l, f
		btfsc	status, c
		addwf	delay_h, f
		btfsc	status, c		;jump to idle when times out 
		goto idle				;(256x256x16=~1sec)

		nop
		nop
		nop
		nop
		goto loop



idle:	
		bsf	status, RP0 ;bank 1
		bsf	trisio, 4	;make GP4 high-z to activate current reduction
		bcf	status, RP0 ;bank 0

idleloop:
		btfsc	intcon, intf	;check for rising edge on step pin
		goto fullcurrent
		btfss	gpio,0			;check for jp3 shorted to gnd
		goto fullcurrent
		goto idleloop


		END                       

