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: 2018/3/23 14:24,

 ©2018 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE! / 

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

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2018 contributors:
o List host: MIT, Site host, Top posters @20180323 RussellMc, Isaac M. Bavaresco, Sean Breheny, Van Horn, David, Neil, alan.b.pearce, Bob Blick, David C Brown, John Gardner, Brent Brown,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!