please dont rip this site

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.


#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
	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

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

	bsf	    SRCLK	        ; strobe active rising edge
	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
	bcf	    SRLAT




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; 
         DRDAT = 0; 
      SRCLK = 1;                              // Clock the data
      SRCLK = 0; 
   SRLAT = 1;                                  // Latch the data 
   SRLAT = 0; 


See also:

file: /Techref/microchip/16x-74hc595-lg.htm, 4KB, , updated: 2011/2/20 10:22, local time: 2020/5/28 08:50,


<A HREF=""> PIC Microcontroller Output Method - Expand 3 IO pins into 8 outputs with a 74HC595</A>


Link? Put it here: 
if you want a response, please enter your email address: 
Did you find what you needed?


Welcome to!


Welcome to!