How to use a parallel to serial shift register using a micro

Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

How to use a parallel to serial shift register using a micro

Post by Shane » Fri Jun 24, 2011 7:03 am

Though the Arduino has 14 Digital Input/Output pins and 6 Analog Input pins, it is not difficult to design a project that exceeds those capabilities, either with too few outputs or too few inputs (or both). This tutorial introduces the parallel to serial shift register.
1.jpg
1.jpg (166.85 KiB) Viewed 2938 times

Code: Select all

//PARALLEL TO SERIAL SHIFT REGISTER (SN74LS165 8-BIT)

//DEFINE VARIABLES

//DEFINE SERIAL IN/OUT PINS
 int clockPin = 7;
 int latchPin = 8;
 int dataPin = 9;

//DEFINE SWITCH DATA VARIABLES
 int switchValue = 0; //variable to hold value of switch states
 int switchState = 0; //variable to read state of each switch


 void setup(){
 //start serial and set baud rate for serial window
 Serial.begin(9600);

 //define pin modes
 pinMode(clockPin, OUTPUT);
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, INPUT);
 }

void loop(){
 //set latchPin low and then high for shift register to collect latest parallel data
 digitalWrite (latchPin, LOW);
 //wait for the shift register to read data
 delayMicroseconds (10);
 //set latchPin high to end parallel read and begin serial transmission
 digitalWrite (latchPin, HIGH);

 //with the latch pin low, collect serial data
 switchValue = 0; //clear switchValue variable
 for (int i = 0; i <8; i ++){
 digitalWrite (clockPin, LOW); //set clock Pin low to prepare to read data
 switchState = digitalRead (dataPin); //read dataPin value
 switchValue = (switchValue << 1); //bitshift one to the left to open LSB to write switchState
 switchValue = switchValue | (!switchState); //add switchState value to LSB of switchValue
 digitalWrite (clockPin, HIGH); //complete BIT data transmission; prepare for next
 }

 Serial.println(switchValue, BIN); //debugging in serial window of switches 1 through 8)
 Serial.println(switchValue);
 Serial.println(switchValue, HEX);
 Serial.println();
 delay (2000); //wait to avoid overloading serial window 
 }
2.jpg
2.jpg (225.1 KiB) Viewed 2938 times
Courtecy of UMASS AMHERST
Post Reply

Return to “Arduino”