Jump to content
43oh

Recommended Posts

hello i am trying to use multiple shift registers and am having a hard time.

 

this is the code i have to start with, but i am unsure how to modify it to use 3 shift registers

 

//***************************************************************************************
// MSP430 Driver for 74HC595 Shift Register
//
// Description; Drives 8 LED's with 3 digital pins of the MSP430, via a shift register
//
// MSP430x2xx
//
//***************************************************************************************
#include 

//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;
}

Link to post
Share on other sites
hello i am trying to use multiple shift registers and am having a hard time.

 

u need to specify whether

 

1. you want to daisy chain 3 595s to shift 24 bits of data.

 

or

 

2. you want to turn on (chip select / enable) 3 595s successively.

 

for (1) daisy chaining, u could just tie Q7* (pin9) to DS (pin14) of the next stage / chip.

Link to post
Share on other sites
i am daisy chaining them. i have the sout of one tied to the sin of the next. but with the code i posted i am only able to shift out a number as high as 255

 

why only 8 bit (255), u could have 16/32 bit variables.

 

may be try

   for ( i = 0 ; i < 24 ; i++ ){
     shiftOut(1UL << i);
     delay(50); 
   }

Link to post
Share on other sites

For 24 bits, you need to use long, so how about this (untested):

 

    //***************************************************************************************
   // MSP430 Driver for 74HC595 Shift Register
   //
   // Description; Drives 8 LED's with 3 digital pins of the MSP430, via a shift register
   //
   // MSP430x2xx
   //
   //***************************************************************************************
   #include 

   //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 < 24 ; i++ ){
         shiftOut(1 << i);
         delay(50);
       }
       for ( i = 23 ; 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 long 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 long 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 < 24; 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;
   }

Link to post
Share on other sites

than you very much for the help.

 

the code that RobG posted almost worked. but there was a little mistake,

void shiftOut ( unsigned char ); and void pinWrite ( unsigned int, unsigned char );

should be

void shiftOut ( unsigned long ); and void pinWrite ( unsigned int, unsigned long );

 

here is the code with the fixes for anyone who wants it.

 

 

    //***************************************************************************************
   // MSP430 Driver for 74HC595 Shift Register
   //
   // Description; Drives 8 LED's with 3 digital pins of the MSP430, via a shift register
   //
   // MSP430x2xx
   //
   //***************************************************************************************
   #include 

   //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 long );
   void enable ( void );
   void disable ( void );
   void init ( void );
   void pinWrite ( unsigned int, unsigned long );


   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 < 24 ; i++ ){
         shiftOut(1 << i);
         delay(50);
       }
       for ( i = 23 ; 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 long 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 long 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 < 24; 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;
   }

Link to post
Share on other sites
  • 3 weeks later...

I tried the code and it was working, but i think it's blinking too fast, and if i increase the delay my Shiftreg. begins to act strange and not blink some of the LED's randomly.

 

Any idea what might be wrong with what i'm doing?

 

it's a 74HC595N shiftreg.

I'm using a 7805 as power regulator (with 0.33uF and 0.1uF capacitors)

The LED's are rated at ~3.4V 20mA

 

Side-note: I have worked with a 8051 before where pull-up resistors was needed to get a good signal, is it the same with the MSP430?

 

//Cube2

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...