#INCLUDE "P16F873A.INC"			; INCLUDE THE LIBRARY OF THE PIC84

	__CONFIG _RC_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF

#DEFINE	DISPLAY_PORT	PORTB
#DEFINE	D1_SEL	PORTC, 7
#DEFINE	D2_SEL	PORTC, 6
#DEFINE	D3_SEL	PORTC, 5
#DEFINE	D4_SEL	PORTC, 3
#DEFINE	D5_SEL	PORTC, 2
#DEFINE	D6_SEL	PORTC, 4


	CBLOCK 0X020	
	A1, A2, A3, VOLTAGE_1, VOLTAGE_2, VOLTAGE_1_TEMP, VOLTAGE_2_TEMP
	D1_VALUE, D2_VALUE, D3_VALUE, D4_VALUE, D5_VALUE, D6_VALUE, VALUE_ON_DISPLAY, COUNTER
	READ_COUNTER	
	ENDC					

	ORG		0X000				
	GOTO	MAIN
	ORG		0X004 		
	

	RETFIE								
	
MAIN
	CALL	SETUP

	CALL	DISPLAY_NAME		; A- EID
	CLRF	PORTC
	CLRF	PORTB
	CALL	DELAY_200MS

	CALL	DISPLAY_NAME		; A- EID
	CLRF	PORTC
	CLRF	PORTB
	CALL	DELAY_200MS

	CALL	DISPLAY_NAME		; A- EID
	CLRF	PORTC
	CLRF	PORTB
	CALL	DELAY_200MS

	CALL	DISPLAY_NAME		; A- EID

	CLRF	PORTC
	CLRF	PORTB
	CALL	DELAY_200MS

	CALL	DISPLAY_NAME		; A- EID

	CLRF	PORTC
	CLRF	PORTB
	CALL	DELAY_200MS
	
	CLRF	D1_VALUE
	CLRF	D2_VALUE
	CLRF	D3_VALUE
	CLRF	D4_VALUE
	CLRF	VOLTAGE_1
	CLRF	VOLTAGE_2

	MOVLW	D'20'
	MOVWF	READ_COUNTER

REPEAT	
	MOVF	VOLTAGE_1, W
	MOVWF	VOLTAGE_1_TEMP
	CALL	CONVERT_VOLTAGE_1
	CALL	SHOW_VOLTAGE_1

	MOVF	VOLTAGE_2, W
	MOVWF	VOLTAGE_2_TEMP
	CALL	CONVERT_VOLTAGE_2
	CALL	SHOW_VOLTAGE_2		

	DECFSZ	READ_COUNTER, F
	GOTO	REPEAT
	
	MOVLW	D'200'
	MOVWF	READ_COUNTER
	CALL	A2D_CHANEL_0
	MOVWF	VOLTAGE_1
	CALL	A2D_CHANEL_1
	MOVWF	VOLTAGE_2
	GOTO	REPEAT

		
					
CONVERT_VOLTAGE_1							
	CLRF	D1_VALUE
	CLRF	D2_VALUE
	CLRF	D3_VALUE	
	
	MOVLW	D'100'
	SUBWF	VOLTAGE_1_TEMP, W
	BTFSS	STATUS, C
	GOTO	$+4
	MOVWF	VOLTAGE_1_TEMP
	INCF	D1_VALUE, F
	GOTO	$-6

	MOVLW	D'10'						
	SUBWF	VOLTAGE_1_TEMP, W
	BTFSS	STATUS, C				
	GOTO	$+4						
	MOVWF	VOLTAGE_1_TEMP		
	INCF	D2_VALUE, F	
	GOTO	$-6	
																		
	MOVF	VOLTAGE_1_TEMP, W	
	MOVWF	D3_VALUE
	RETURN


CONVERT_VOLTAGE_2							
	CLRF	D4_VALUE
	CLRF	D5_VALUE
	CLRF	D6_VALUE	
	
	MOVLW	D'100'
	SUBWF	VOLTAGE_2_TEMP, W
	BTFSS	STATUS, C
	GOTO	$+4
	MOVWF	VOLTAGE_2_TEMP
	INCF	D4_VALUE, F
	GOTO	$-6

	MOVLW	D'10'						
	SUBWF	VOLTAGE_2_TEMP, W
	BTFSS	STATUS, C				
	GOTO	$+4						
	MOVWF	VOLTAGE_2_TEMP		
	INCF	D5_VALUE, F	
	GOTO	$-6	
																		
	MOVF	VOLTAGE_2_TEMP, W	
	MOVWF	D6_VALUE
	RETURN

SHOW_VOLTAGE_1
	MOVF	D1_VALUE, W	
	CALL	SHOW_ON_DISPLAY		
	MOVWF	DISPLAY_PORT			
	BSF		D1_SEL		
	CALL	DELAY_10MS				
	BCF		D1_SEL

	MOVF	D2_VALUE, W	
	CALL	SHOW_ON_DISPLAY		
	MOVWF	DISPLAY_PORT			
	BSF		D2_SEL		
	CALL	DELAY_10MS				
	BCF		D2_SEL

	MOVF	D3_VALUE, W	
	CALL	SHOW_ON_DISPLAY		
	MOVWF	DISPLAY_PORT			
	BSF		D3_SEL		
	CALL	DELAY_10MS				
	BCF		D3_SEL
	RETURN


SHOW_VOLTAGE_2
	MOVF	D4_VALUE, W	
	CALL	SHOW_ON_DISPLAY		
	MOVWF	DISPLAY_PORT			
	BSF		D4_SEL		
	CALL	DELAY_10MS				
	BCF		D4_SEL

	MOVF	D5_VALUE, W	
	CALL	SHOW_ON_DISPLAY		
	MOVWF	DISPLAY_PORT			
	BSF		D5_SEL		
	CALL	DELAY_10MS				
	BCF		D5_SEL

	MOVF	D6_VALUE, W	
	CALL	SHOW_ON_DISPLAY		
	MOVWF	DISPLAY_PORT			
	BSF		D6_SEL		
	CALL	DELAY_10MS				
	BCF		D6_SEL	
	RETURN

SHOW_ON_DISPLAY					; THIS FUNCTION CONVERT DATA FROM DECIMAL TO 
	CLRF	DISPLAY_PORT		; CHRAR IN ORDER TO BE DISPLAYED CORRECTLY
	MOVWF	VALUE_ON_DISPLAY
	MOVF	VALUE_ON_DISPLAY, W	; IF THE VALUE IS 0 THEN 
	XORLW	D'0'
	BTFSC	STATUS, Z
	RETLW	B'01000000'				; RETURN AND PUT THIS VALUE IN W
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'1'
	BTFSC	STATUS, Z
	RETLW	B'11011110'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'2'
	BTFSC	STATUS, Z
	RETLW	B'00100010'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'3'
	BTFSC	STATUS, Z
	RETLW	B'00001010'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'4'
	BTFSC	STATUS, Z
	RETLW	B'00011100'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'5'
	BTFSC	STATUS, Z
	RETLW	B'00001001'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'6'
	BTFSC	STATUS, Z
	RETLW	B'00000001'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'7'
	BTFSC	STATUS, Z
	RETLW	B'01011010'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'8'
	BTFSC	STATUS, Z
	RETLW	B'00000000'
	MOVF	VALUE_ON_DISPLAY, W
	XORLW	D'9'
	BTFSC	STATUS, Z
	RETLW	B'00001000'

DISPLAY_NAME
	MOVLW	D'255'
	MOVWF	COUNTER
REP_NAME
	MOVLW	B'00010000'
	MOVWF	PORTB
	BSF		D2_SEL		
	CALL	DELAY_10MS				
	BCF		D2_SEL


	MOVLW	B'00100001'
	MOVWF	PORTB
	BSF		D4_SEL		
	CALL	DELAY_10MS				
	BCF		D4_SEL

	MOVLW	B'01011111'
	MOVWF	PORTB

	BSF		D5_SEL		
	CALL	DELAY_10MS				
	BCF		D5_SEL

	MOVLW	B'00000110'
	MOVWF	PORTB

	BSF		D6_SEL		
	CALL	DELAY_10MS				
	BCF		D6_SEL

	DECFSZ	COUNTER, F
	GOTO	REP_NAME
	RETURN


A2D_CHANEL_0
	MOVLW	B'01000001'		; Fosc/8, and select CHANNEL 0
	MOVWF	ADCON0	
	CALL	A2D_DELAY
	BSF     ADCON0,GO       ;Start A/D conversion
	BTFSC   ADCON0,GO       ;Wait for conversion to complete
    GOTO    $-1
	MOVF    ADRESH,W		; PUT ADRESH IN W
    RETURN

A2D_CHANEL_1
	MOVLW	B'01001001'		; Fosc/8, and select CHANNEL 0
	MOVWF	ADCON0	
	CALL	A2D_DELAY
	BSF     ADCON0,GO       ;Start A/D conversion
	BTFSC   ADCON0,GO       ;Wait for conversion to complete
    GOTO    $-1
	MOVF    ADRESH,W		; PUT ADRESH IN W
    RETURN

A2D_DELAY					; AQUISITION TIME	~20US
	MOVLW	D'10'
	MOVWF	A1
	DECFSZ	A1, F
	GOTO	$-1
	RETURN

DELAY_200MS		; NOT ACCURATE
    MOVLW    D'10'
    MOVWF    A3
    MOVLW    D'95'
    MOVWF    A2
    MOVLW    D'139'
    MOVWF    A1
    DECFSZ   A1, F
    GOTO     $-1
    DECFSZ   A2, F
    GOTO     $-5
    DECFSZ   A3, F
    GOTO     $-9
    RETURN
	
DELAY_10MS 
	MOVLW	D'1'
	MOVWF	A3
	MOVLW	D'15'
	MOVWF	A2
	MOVLW	D'33'
	MOVWF	A1
	DECFSZ 	A1,F
	GOTO	$-1	
	DECFSZ	A2,F
	GOTO	$-5
	DECFSZ	A3, F
	GOTO	$-9
	RETURN		

SETUP
	CLRF	PORTA
	CLRF	PORTB
	CLRF	PORTC
	BSF 	STATUS, RP0
	MOVLW	B'00000100'
	MOVWF	ADCON0
	MOVLW	B'00000111'
	MOVWF	CMCON
	MOVLW	B'00111111'
	MOVWF	TRISA
	MOVLW	B'00000000'
	MOVWF	TRISB
	MOVLW	B'00000000'
	MOVWF	TRISC
	BCF 	STATUS, RP0
	RETURN			
	END
