PARALLEL2SERIAL					;using 74HC166 IC
;input, serial_byte4, serial_byte3, serial_byte2, serial_byte1, serial_byte0
;output, display bytes from 74LS595 serial to parallel logic IC
#define p2s_serial_data_in	PORTA,0
#define p2s_serial_clock	PORTA,1
#define p2s_latch_enable	PORTA,2
	_SWITCH_BANK 0
	BSF		p2s_serial_clock		;initialise the serial clock
	BSF		p2s_latch_enable		;initialise the latch clock

	;load data into 74HC166 IC
	BCF		p2s_latch_enable		;enable parallel data to be latched into the IC
	BCF		p2s_serial_clock		;
	BSF		p2s_serial_clock		;data serial clock, the parallel data into the IC
	BSF		p2s_latch_enable		;disable latching into the IC
									;LSB bit is now already at the output Q7 pin13

	;{loop rountine to loop through all the bits in the defined number of byte
		;{loop rountine to do left rotate for all the bytes to check starting from the MSB bit
		MOVLW	SERIAL_BYTE_SIZE	;counting loop to determine the number of bytes that need to have the bits shifted
		BANKSEL serial_bytesize_count	;bank select for direct addressing
		MOVWF	serial_bytesize_count
		CLRW
		P2S_SERIAL_BYTE_COUNT0
		ADDLW	0x08
		DECFSZ	serial_bytesize_count,F
		GOTO	P2S_SERIAL_BYTE_COUNT0	;counting loop back
		;}
;	ADDLW	d'1'					;add in one more loop to rotate data back to their original memory location
	MOVWF	serial_bitsize_count	;compute the number of bits required to rotate

	P2S_SERIAL_BIT_COUNT
	;{loop rountine to do left rotate for all the bytes to check starting from the MSB bit
		MOVLW	serial_byte0+SERIAL_BYTE_SIZE-1		;find out the base address
		MOVWF	FSR					;load base address
		MOVLW	SERIAL_BYTE_SIZE	;counting loop to determine the number of bytes that need to have the bits shifted
		BANKSEL serial_bytesize_count	;bank select for direct addressing
		MOVWF	serial_bytesize_count

		_SWITCH_BANK 0
		BCF		STATUS,0			;loading the data bit into the carry flag for rotation
		BTFSS	p2s_serial_data_in	;inverting the active low input data
		BSF		STATUS,0

		P2S_SERIAL_BYTE_COUNT1
		BANKISEL serial_byte0		;bank select for indirect addressing
		RRF		INDF,F
		DECF	FSR,F
		BANKSEL	serial_bytesize_count
		DECFSZ	serial_bytesize_count,F
		GOTO	P2S_SERIAL_BYTE_COUNT1	;counting loop back
	;}
	DECFSZ	serial_bitsize_count,F
	GOTO	PARALLEL2SERIAL_SERIALCLOCK	;proceed to do serial clock
	GOTO	PARALLEL2SERIAL_END			;last rotation, rotates the data back to their location and does not need any serial clocking
PARALLEL2SERIAL_SERIALCLOCK
	_SWITCH_BANK 0
	BCF		p2s_serial_clock
	BSF		p2s_serial_clock		;data serial clocking in the next bit of data
	GOTO	P2S_SERIAL_BIT_COUNT	;counting loop back
	;}
PARALLEL2SERIAL_END
RETURN

