PIC Microcontroller Output Method

Expand 3 IO pins into 8 outputs with a 74HC595

Lawrence Glaister VE7IT says:

Code used to expand I/O on pic16xxx series. I used a 74hc595 to get 8 extra output bits on a pic16f627. The 595's can be cascaded for higher i/o counts with no extra lines used. In another project, I used 3 595's.

Also:


#DEFINE SRCLK   PORTB,4     ; 74hc595 clock output
#DEFINE SRDAT   PORTB,5     ; 74hc595 data output  
#DEFINE SRLAT   PORTB,3     ; 74hc595 output latch 

;----------------------------------------------------------------------
;  set relays to match global variable "relay"             
;  8 bits are clocked out to 74hct595 and then strobed to output register
;----------------------------------------------------------------------
set_relays
	movlw 	d'8'		    ; 8 hi bits to shift out
	movwf 	sr_tmp
    movf    relay,w
    movwf   sr_sr           ; save a temp copy of relay image

sr_001
	bcf	    SRDAT	        ; start with bit low
	rlf 	sr_sr,f		    ; rotate bits through c
	btfsc	STATUS,C
	bsf	    SRDAT   	    ; flip data if needed
	nop

	bsf	    SRCLK	        ; strobe active rising edge
	nop
	bcf	    SRCLK

	decfsz	sr_tmp,f
	goto	sr_001		    ; loop for rest of bits in byte	
    
	bsf	    SRLAT	        ; xfer data from sr to output latch
	nop
	bcf	    SRLAT

	return

Questions:

Comments:

Here is one possible C version of this code.


void OutPORT(unsigned char data) 

{ 
   unsigned char bits; 
   for (bits=0x80; bits!=0; bits >>= 1) 
   { 
      if ((bits & data) == bits)            // send Data 
         SRDAT = 1; 
      else 
         DRDAT = 0; 
      SRCLK = 1;                              // Clock the data
      SRCLK = 0; 
   } 
   SRLAT = 1;                                  // Latch the data 
   SRLAT = 0; 
}