MSP430 Launchpad Shift Register

From Texas Instruments Embedded Processors Wiki

Jump to: navigation, search
Translate this page to   

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

  1. 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
  2. Create new project in CCS/IAR and copy/paste code
  3. 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


 

              74HC595.png                  

                                                             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

References

  1. MSP430x2xx Family User Guide
  2. MSP430G2x31 Datasheet
E2e.jpg For technical support on MSP430 please post your questions on The MSP430 Forum. Please post only comments about the article MSP430 Launchpad Shift Register here.
Hyperlink blue.png Links
ARM Microcontroller MCU ARM Processor Digital Media Processor Digital Signal Processing Microcontroller MCU Multi Core Processor
Ultra Low Power DSP 8 bit Microcontroller MCU 16 bit Microcontroller MCU 32 bit Microcontroller MCU

Leave a Comment

Comments

Comments on MSP430 Launchpad Shift Register


Lwangaman said ...

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:


latchOff();
shiftOut(0x80);
shiftOut(0x01);
latchOn();

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)

Personal tools
Namespaces
Variants
Actions
Navigation
Print/export
Toolbox