MSP430 Launchpad Shift Register
From Texas Instruments Embedded Processors Wiki
by Andrew Morton
This is a quick example of using an 8-bit shift register to drive eight output pins with only three outputs of the MSP430. By chaining the serial out to serial in, and tieing the clock and latch pins, one can add even more shift registers, and more outputs, while still using just three pins.
Contents |
Quick Start
- Get external components.
- 1 Resistor of 1KOhm
- 8 Resistors of 270Ohm
- 1 Capacitor of .1uF (may optionally be added between the latch pin and ground to stabilize the signal)
- 8 LED's:Just about any normal LED will do
- 1 IC: 74HC595 Shift 'Register' or equivalent chip
- Small breadboard and jumper cables
- Create new project in CCS/IAR and copy/paste code
- Build circuit.
Development
This example drives eight LED's in a "ping-pong" pattern using the 74HC595 shift register, on P1.0, P1.4, P1.5, and optionally, P1.6. If you do opt not to control the OE pin(13), then tie it directly to GND
Schematic
Fig. 1: Schematic for Shift Register Circuit
Link to video showing the breadboarded and running circuit: www.youtube.com/watch
Code
//*************************************************************************************** // MSP430 Driver for 74HC595 Shift Register // // Description; Drives 8 LED's with 3 digital pins of the MSP430, via a shift register // // MSP430x2xx // //*************************************************************************************** #include <msp430x20x2.h> //Define our pins #define DATA BIT0 // DS -> 1.0 #define CLOCK BIT4 // SH_CP -> 1.4 #define LATCH BIT5 // ST_CP -> 1.5 #define ENABLE BIT6 // OE -> 1.6 // The OE pin can be tied directly to ground, but controlling // it from the MCU lets you turn off the entire array without // zeroing the register // Declare functions void delay ( unsigned int ); void pulseClock ( void ); void shiftOut ( unsigned char ); void enable ( void ); void disable ( void ); void init ( void ); void pinWrite ( unsigned int, unsigned char ); int main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; P1DIR |= (DATA + CLOCK + LATCH + ENABLE); // Setup pins as outputs enable(); // Enable output (pull OE low) int i; //Do a "ping-pong" effect back and forth for(;;){ for ( i = 0 ; i < 8 ; i++ ){ shiftOut(1 << i); delay(50); } for ( i = 7 ; i >= 0 ; i-- ){ shiftOut(1 << i); delay(50); } } } // Delays by the specified Milliseconds // thanks to: // http://www.threadabort.com/archive/2010/09/05/msp430-delay-function-like-the-arduino.aspx void delay(unsigned int ms) { while (ms--) { __delay_cycles(1000); // set for 16Mhz change it to 1000 for 1 Mhz } } // Writes a value to the specified bitmask/pin. Use built in defines // when calling this, as the shiftOut() function does. // All nonzero values are treated as "high" and zero is "low" void pinWrite( unsigned int bit, unsigned char val ) { if (val){ P1OUT |= bit; } else { P1OUT &= ~bit; } } // Pulse the clock pin void pulseClock( void ) { P1OUT |= CLOCK; P1OUT ^= CLOCK; } // Take the given 8-bit value and shift it out, LSB to MSB void shiftOut(unsigned char val) { //Set latch to low (should be already) P1OUT &= ~LATCH; char i; // Iterate over each bit, set data pin, and pulse the clock to send it // to the shift register for (i = 0; i < 8; i++) { pinWrite(DATA, (val & (1 << i))); pulseClock(); } // Pulse the latch pin to write the values into the storage register P1OUT |= LATCH; P1OUT &= ~LATCH; } // These functions are just a shortcut to turn on and off the array of // LED's when you have the enable pin tied to the MCU. Entirely optional. void enable( void ) { P1OUT &= ~ENABLE; } void disable( void ) { P1OUT |= ENABLE; }
Future Ideas
- Add support for chained shift registers (see Talk Page)
- Take different/larger datatypes for larger arrays
References
Leave a CommentComments
Comments on MSP430 Launchpad Shift Register


As for the idea of supporting multiple chained shift registers, one step further in this direction is simply separating the latch on / latch off logic from the shiftOut() function. I created two separate functions "latchOn()" and "latchOff()", so that if I have let's say three daisy chained shift registers, I just have to latchOff, shiftOut three times, then latchOn. Here is my code:
// set latch off to enable Shifting bits in void latchOff( void ) { //Set latch to low (should be already) P1OUT &= ~LATCH; } // set latch on to set bits in output void latchOn( void ) { // Pulse the latch pin to write the values into the storage register P1OUT |= LATCH; P1OUT &= ~LATCH; } // Take the given 8-bit value and shift it out, LSB to MSB void shiftOut(unsigned char val) { char i; // Iterate over each bit, set data pin, and pulse the clock to send it // to the shift register for (i = 0; i < 8; i++) { pinWrite(DATA, (val & (1 << i))); pulseClock(); } }As for the rest I pretty much kept it as is... In this way I can simply do something like this:
And here is a youtube video demonstrating 5 daisy-chained M74HC595B Shift Registers hooked up to the MSP430 Launchpad controlling 40 leds with only three wires. In this specific example I also hooked up 5 ULN2803s (darlington transistor arrays) so that I can add two more sets of 40 leds to the load.
--Lwangaman 07:45, 5 August 2012 (CDT)