;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;											;
;	ADC Calibration Program	ver 1.0			;
;											;
;	By Brad Slattery 2009					;
;	www.bradsprojects.com					;
;	brad@bradsprojects.com					;
;											;
;	'Designing electronic projects,			;
;	to spread the name of Jesus'			;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


	LIST p=16f648a 				;	tell assembler what chip we are using (if you are using the 16f628a, then
	include "P16f648a.inc"		;	make sure you change this line and the previous line to read p16f628a
	__config h'3f18'			;	sets the configuration settings - internal oscillator, watchdog timer OFF
								;	Power Up Timer DISABLED, Master Clear DISABLED, Brown Out Detect DISABLED,
								;	Low Voltage Programming DISABLED, Data EE Read Protect Disabled,
								;	Code Protect OFF. (this will remain the same for all tutorials)


PC equ h'02'					;	The program counter will be refered to as PC


	cblock h'20'				;	Start to define variables
	digital_count				;	Used to store our binary representation of the analogue input.
	endc						;	finished defining variables



	org h'0000'					;	Start the program from 0000 hex


	movlw h'07'					;	This will turn the comparators OFF.	
	movwf CMCON 				;	(we just want to use the ports as digital ports)


	bsf STATUS, RP0 			;	select bank 1 (to enable us to change the Input / Output status of our ports)
	movlw b'00000000' 			;	set PORTB all outputs (A '0' means output, A '1' means input. We can set each
	movwf TRISB					;	We can set each bit individualy. Each port having 8-bits or 8 pins.
	movlw b'00100000' 			;	set PORTA all outputs except for bit 5. Bit 5 is an input ONLY pin.
	movwf TRISA 				;	It cannot be set to an output!
	bcf STATUS, RP0 			;	select bank 0




setup							;	get our PORTS and variables setup, ready for the main program
	
	clrf PORTB					;	ensure all LED's are off to start with
	movlw b'00000001'			;	This will ensure that the capacitor will charge each time PORTA pin 0 is an output.
	movwf PORTA


begin							;	This is where our main program starts.
	clrf digital_count			;	reset our digital counter, ready for a new sample
	call charge_cap				;	call the routine to charge the capacitor
	call discharge_cap			;	and now call the routine to discharge the capacitor
	movf digital_count, w		;	now that we have the digital data, we want to see it
	movwf PORTB					;	we do this by sending it to the 8 LED's connected to PORTB
	goto begin					;	and now go and do it all again.



charge_cap
	bsf STATUS, RP0 			;	select bank 1 (to enable us to change the Input / Output status of our ports)
	bcf TRISA, 0				;	make PORTA pin 0 an output (this will now charge the capacitor
								;	you will recall that we always have a logic 1 on PORTA pin 0 whenever it set to
								;	an output?
	bcf STATUS, RP0 			;	select bank 0
	return


discharge_cap					;	This routine takes care of discharging the capacitor and counting how long it
								;	took to discharge.
	bsf STATUS, RP0 			;	select bank 1 (to enable us to change the Input / Output status of our ports)
	bsf TRISA, 0				;	make PORTA pin 0 an input
	bcf STATUS, RP0 			;	select bank 0
keep_checking					;	we keep coming back here IF PORTA pin 0 is still a logic 1.
	incf digital_count, f		;	increment our counter
	btfsc PORTA, 0				;	has the capacitor discharged yet?
		goto keep_checking		;	no, then go back, increment the counter and then check the PORT again
	return						;	otherwise, we can finish counting and return to the main program


	end							;	must have this here!
