
#include <p16c5x.inc>
	
;***** VARIABLE DEFINITIONS
temp_var        udata
result		res	1	;result variable declaration
count		res	1	;result variable declaration
dig0            res	1	;dig0 variable declaration
dig1            res	1	;dig1 variable declaration
dig2            res	1	;dig2 variable declaration
dig3            res	1	;dig3 variable declaration
control         res	1	;control variable declaration

;**********************************************************************
reset_vector    code   0x1ff         ;processor reset vector
		goto   start

MAIN            CODE   0x000

start	
                clrw                  ;clear accumulator
                tris   PORTB          ;Set PORT B as output
	        movwf  count	      ;move accumulator to count
loop		
	        incfsz count,1        ;increment count,skip if 0	
	        goto   conversion     ;call binary to gray conversion subroutine	

repeat          goto   repeat	      ;infinite loop

conversion

;writing binary to hpdl 1414
	

                 movlw   0x00         ;move literal value to accumulator
                 tris    PORTB        ;Set PORT B as output

                 movlw   0x00         ;move literal value to accumulator
                 tris    PORTC        ;Set PORT C as output

;  display  of DIG2


 ;separation of second  digit

                  movlw   0x0f         ;move literal value to accumulator
                  andwf   count,0      ;AND count with accumulator and output in accumulator
                  movwf   dig2         ;move accumulator to dig2

;check digit is grater than 9


                  movlw   0x09         ;move literal value to accumulator
                  subwf   dig2,0       ;subtract 9 from dig2 and result in accumulator

                  btfss   STATUS, 0    ;skip if carry flag  is 1
                  goto    nochange2    ;goto nochange2

                  btfsc   STATUS,2     ;skip if zero flag is zero
                                       ;ie result is non zero
                  goto    nochange2    ;goto nochange2

                  movlw   0x09         ;move literal value to accumulator
                  subwf   dig2,1       ;subtract 9 from dig2 and result in dig2

                  movlw   0x18         ;move literal value to accumulator
                  movwf   control      ;move control to accumulator

                  goto    porting2     ;goto porting 2

nochange2         movlw   0x16         ;move literal value to accumulator
                  movwf   control      ;move control to accumulator

porting2          movf    dig2,0       ;move dig2 value to accumulator
                  movwf   PORTB        ;move accumulator to portB

                  movf    control,0    ;move control value to accumulator
                  movwf   PORTC        ;move accumulator to port C

                  movlw   0xff         ;move literal value to accumulator
                  movwf   control      ;move control to accumulator

                  movf    control,0    ;move control value to accumulator
                  movwf   PORTC        ;move accumulator to port C


;second digit   complete
;third digit

;separation of  third  digit

                  movlw   0xf0          ;move literal value to accumulator
                  andwf   count,0       ;AND count with accumulator and output in accumulator
                  movwf   dig3          ;move accumulator to dig3
                  swapf   dig3,1        ;swap and result in dig3


;check digit is grater than 9

                  movlw    0x09         ;move literal value to accumulator
                  subwf    dig3,0       ;subtract 9 from dig3 and result in accumulator

                  btfss    STATUS, 0    ;skip if carry flag  is 1
                  goto     nochange3    ;goto nochange3

                  btfsc    STATUS,2     ;skip if zero flag is zero
                                        ;ie result is non zero
                  goto     nochange3    ;goto nochange3

                  movlw    0x09         ;move literal value to accumulator
                  subwf    dig3,1       ;subtract 9 from dig3 and result in dig3

                  movlw    0x08          ;move literal value to accumulator
                  movwf    control       ;move control to accumulator

                  goto     porting3      ;goto porting 3

nochange3         movlw    0x06          ;move literal value to accumulator
                  movwf    control       ;move control to accumulator

porting3          movf     dig3,0        ;move dig3 value to accumulator
                  movwf    PORTB         ;move accumulator to portB

                  movf     control,0     ;move control value to accumulator
                  movwf    PORTC         ;move accumulator to port C

                  movlw    0xff          ;move literal value to accumulator
                  movwf    control       ;move control to accumulator

                  movf     control,0     ;move control value to accumulator
                  movwf    PORTC         ;move accumulator to port C

 ;third digit   complete
 ;convwersion algoritham

		bcf 	 STATUS,0	;clear carry flag
		rrf 	 count,0	;shift binary to right and place the result in accumulator
		xorwf 	 count,0	;xor binary with shifted 1 and store result in accumulator
            	movwf 	 result		;move accumulator to result

		
;writing gray to  HPDL1414

; display  of DIG0

;separation of zeroth  digit

                  movlw  0x0f         ;move literal value to accumulator
                  andwf  result,0     ;AND result with accumulator and
                                      ;output in accumulator
                  movwf  dig0         ;move accumulator to dig0

;check digit is greater than 9

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig0,0       ;subtract 9 from dig0 and result in accumulator

                  btfss  STATUS, 0    ;skip if carry flag  is 1
                  goto   nochange     ;goto nochange

                  btfsc  STATUS,2     ;skip if zero flag is zero
                                      ;ie result is non zero
                  goto   nochange     ;goto nochange

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig0,1       ;subtract 9 from dig0 and result in dig0

                  movlw  0x38         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  goto   porting0     ;goto porting 0

nochange          movlw  0x36         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

porting0          movf   dig0,0       ;move dig0 value to accumulator
                  movwf  PORTB        ;move accumulator to portB

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C

                  movlw  0xff         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C



;zeroth  digit complete

;first  digit

;separation of  first  digit

                  movlw  0xf0          ;move literal value to accumulator
                  andwf  result,0      ;AND result with accumulator and output in accumulator
                  movwf  dig1          ;move accumulator to dig1
                  swapf  dig1,1        ;swap and result in dig1

;check digit is greater than 9

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig1,0       ;subtract 9 from dig1 and result in accumulator

                  btfss  STATUS, 0    ;skip if carry flag  is 1
                  goto   nochange1    ;goto nochange1

                  btfsc  STATUS,2     ;skip if zero flag is zero
                                      ;ie result is non zero
                  goto   nochange1    ;goto nochange1

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig1,1       ;subtract 9 from dig1 and result in dig1

                  movlw  0x28         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  goto   porting1     ;goto porting 1

nochange1         movlw  0x26         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

porting1          movf   dig1,0       ;move dig1 value to accumulator
                  movwf  PORTB        ;move accumulator to portB

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C

                  movlw  0xff         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C
		
                  goto   loop         ;goto loop
		
                  end                 ;directive 'end of program'

