	
	;BEE COUNTER
	;Left IR detects then increments count on right detection
	;   12F629.asm
	;  8-1-2012 


	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Dont complain about BANK 1 Registers during assembly

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.


temp1		equ 20h	;
temp2		equ 21h	;
temp3		equ 22h	;
units		equ	23h	; 
tens	    equ	24h	;
flags		equ	25h
File5mS		equ	26h
FileXmS	    equ	27h
del_x    	equ	28h
del_y    	equ	29h
tempunits   equ	2Ah
temptens    equ	2Bh

;****************************************************************
;Equates
;****************************************************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
GPIO 		equ 0x05
			



status		equ	03h
option_reg	equ 81h


						; bits on GPIO
				
pin7		equ	0	;GP0  left IR detector - input
pin6	 	equ	1	;GP1  goes low to produce HIGH signal - output
pin5		equ	2	;GP2  Sw input  sends count to beeper - input
pin4		equ	3	;GP3  right IR detector - input
pin3		equ	4	;GP4  beeper  - output
pin2		equ	5	;GP5     
 

						;bits
				
rp0		equ	5			;bit 5 of the status register



;****************************************************************
;Beginning of program
;****************************************************************
Start	org		0x00	;reset vector address
		nop
		nop
		nop
		nop		;NOPs to get past reset vector address
		nop
		nop
								;set up to allow counting from external oscillator

SetUp	bsf		status, rp0 	;Bank 1			
       	movlw	b'11001001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw   07h         	;Set up W to turn off Comparator ports
        movwf   CMCON       	;must be placed in bank 0  
		clrf 	GPIO       		;Clear GPIO of junk		
		clrf	flags	
		clrf	units	    	;initialise count = 0
		clrf	tens     		;initialise count = 0
		goto 	Main	



;****************************************************************
;* Delays 
;****************************************************************

			
		
_10mS	movlw	0Ah
		movwf	temp2
D_a		nop
		decfsz temp1,1
		goto D_a
		decfsz temp2,1
		goto D_a	
		retlw 00
		

	;Delay 0.25 sec

D_250mS	movlw 	01h
		movwf 	temp3	
DelX	decfsz 	temp1,1	
		goto 	DelX	
		decfsz 	temp2,1		
		goto 	DelX
		decfsz 	temp3,1
		goto 	DelX
		retlw 	00

;****************************
;* Sub-routines		*
;****************************

    ;output produces long beeps for tens and short beeps for units to signify count.

output	movf	units,0
		movwf   tempunits
		movf	tens,0
		movwf   temptens
		movf	temptens,1   ;check for zero
		btfsc	status,2	;zero flag Will be set if file is zero
		goto    $+4		
		call    tensbeep
		decfsz	temptens,1
		goto	$-2		
		call	D_250mS	
		call	D_250mS	
		call	D_250mS	
		call	D_250mS	
		movf	tempunits,1   ;check for zero
		btfsc	status,2	;zero flag Will be set if file is zero
		retlw	00		
		call    unitsbeep
		decfsz	tempunits,1
		goto	$-2			
		retlw	00
		
		
		;produces "beep" to indicate bee has moved and outputs to output.
			
shortbeep	
		movlw	0ffh
		movwf	del_y
		movlw	.45
		movwf	del_x
		nop
		decfsz	del_x,1
		goto	$-2
		movlw	b'00010000'
		xorwf	gpio,1		;toggle GP4		
		decfsz	del_y,1
		goto	$-8
		call	D_250mS		
		retlw	00
		
		
		
		;short beep to indicate units
		
unitsbeep	
		movlw	80h
		movwf	del_y
		movlw	.45
		movwf	del_x
		nop
		decfsz	del_x,1
		goto	$-2
		movlw	b'00010000'
		xorwf	gpio,1		;toggle GP4		
		decfsz	del_y,1
		goto	$-8
		call	D_250mS
		call	D_250mS
		retlw	00			
		
		
		
		
		
leftbeep	
		movlw	80h
		movwf	del_y
		movlw	.45
		movwf	del_x
		nop
		decfsz	del_x,1
		goto	$-2
		movlw	b'00010000'
		xorwf	gpio,1		;toggle GP4		
		decfsz	del_y,1
		goto	$-8
		call	D_250mS
		call	D_250mS
		retlw	00	
		
					
		
		;long beep to indicate tens
		
tensbeep
		movlw	0ffh
		movwf	del_y
		movlw	0ffh
		movwf	del_x
		nop
		decfsz	del_x,1
		goto	$-2
		movlw	b'00010000'
		xorwf	gpio,1		;toggle GP4		
		decfsz	del_y,1
		goto	$-8
		call	D_250mS
		call	D_250mS	
		retlw	00		
		
		
		
				
		
rightbeep
		movlw	0ffh
		movwf	del_y
		movlw	0ffh
		movwf	del_x
		nop
		decfsz	del_x,1
		goto	$-2
		movlw	b'00010000'
		xorwf	gpio,1		;toggle GP4		
		decfsz	del_y,1
		goto	$-8
		call	D_250mS
		call	D_250mS	
		retlw	00	
		
		
Up		incf	units,1
		movlw	0Ah			;put 10 into w
		xorwf	units,0		;compare units file with 10
		btfss	status,2	;zero flag in status file. Will be set if units is 10
		retlw	00
		clrf	units
		incf	tens,1
		movlw	0Ah			;put 10 into w
		xorwf	tens,0		;compare units file with 10
		btfss	status,2	;zero flag in status file. Will be set if tens is 10
		retlw	00			;
		clrf	tens
		bsf     flags,0     ;set lowest bit to indicate 100 has been reached		
		retlw	00
		
			
;****************************************************************
;* Main 							*
;****************************************************************

					
			

Main	bsf		status, rp0 	;Bank 1			
       	movlw	b'11101101'		;switch and IR receivers GP0 GP2 GP3 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		bsf     GPIO,1			;turn off output LED
		
		call	D_250mS
		btfss	GPIO,2	        ;input will be LOW when sw pressed
		call    output
		nop		
		btfss	GPIO,0	        ;input will be HIGH when bee detected
		goto	$-5 			;Left IR not detecting
		call    leftbeep
		call	D_250mS
		btfss	GPIO,2	        ;input will be LOW when sw pressed
		call    output
		nop
		btfss	GPIO,3	        ;input will be HIGH when bee detected
		goto	$-5 			;right IR not detecting
		call    Up				;increment count
		call    rightbeep
		bcf		gpio,1
		call    _10mS
		bsf		gpio,1
		goto	$-18
		
			

		end
		

	
	