Jump to content
43oh

Code for 14hc164 shift register


Recommended Posts

Hello,

I am asking for your help.

I want to be able to control a 74hc164 shift register using the launchpad with a msp430g2553.

I found some, the one for the LCD, but I am not understanding it.

 

I would like to receive some help to develop code, (or be provided some simple code) to control one 74hc164 shift register.

Any help would be greatly appreciated.

I need to use the 74hc164, because we have a bunch of them at school and I don't have the money to buy others.

Thank You

Link to post
Share on other sites

Here are the important parts from the LCD code

#define DATAPIN BIT6
#define CLOCKPIN BIT5
void send(char data);
void main(void) {
    WDTCTL = WDTPW + WDTHOLD;
    P1OUT &= ~(CLOCKPIN + DATAPIN);
    P1OUT |= ENABLEPIN;
    P1DIR |= ENABLEPIN + CLOCKPIN + DATAPIN;
    while(1);
}
void send(char data) {
    char bitCounter = 0;
    while(bitCounter < 8) {
        (data & BIT7) ? (P1OUT |= DATAPIN) : (P1OUT &= ~DATAPIN);
        data <<= 1;
        P1OUT |= CLOCKPIN;
        P1OUT &= ~CLOCKPIN;
        bitCounter++;
    }
}
Link to post
Share on other sites

hi !    -  I'm pretty new to this environment too, but I have a background in hardware and other microcontrollerts.

 

 

The 74hc164 does not have a 2nd latch as does the '595.

So you only need to send it clock and the 8 data bits - this means two output witres from the 430 Launchpad.

Although the ouput pins of the 164 will change while data is being shifted out, it happens so quickly that you would never see it in the case where you are driving LEDs.

 

Here is a page that has a project which could be done in Energia :

 

http://www.instructables.com/id/The-74HC164-Shift-Register-and-your-Arduino/

 

 

I am using Energia version 8.

There is an instruction as follows:

shiftOut(dataPin, clockPin, bitOrder, value)

... this sends clock and data to the 164
... and this is used in the arduino axample in the link.

 

 

have fun!

Link to post
Share on other sites

Thanks for the links and examples. After going through them I was able to modify the code for the 74ls595 example.

 

Now that I had one 74hc164 working I decided to kick it up a notch and cascade four of them. 

I came up with the following code:

//***************************************************************************************
// MSP430 Driver for 74HC164 Shift Register
//
//Drives five(5) levels of LED's. Each level consists of a matrix of 25 LED's
//------------5x5x5 LED Cube---------------
// MSP430g2553
//
//Adapted from example by Andrew Morton posted at http://processors.wiki.ti.com/index.php/MSP430_Launchpad_Shift_Register
//***************************************************************************************
#include <msp430G2553.h>

//Define our pins
#define DATA BIT0 // DS -> 1.0
#define CLOCK BIT4 // SH_CP -> 1.4

// Declare functions
void pulseClock ( void );
void shiftOut ( unsigned char );
void pinWrite ( unsigned int, unsigned char );


int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  P1DIR |= (DATA + CLOCK);  // Setup pins as outputs

  int First = 0xfc;
  int Second = 0X80+0x40+0x20+0x10+0x08+0x04+0x02+0x01;	// 0xFF
  int Third = 0X80+0x40+0x20+0x10+0x08+0x04+0x02+0x01;	// 0xFF
  int Fourth = 0xff;									// 0X80+0x40+0x20+0x10+0x08+0x04+0x02+0x01

  shiftOut(First);
  shiftOut(Second);
  shiftOut(Third);
  shiftOut(Fourth);
}
/***********************************************************/

// 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)
{


  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();
  }
}
/***********************************************************/
void pinWrite( unsigned int bit, unsigned char val )
{
  if (val){
    P1OUT |= bit;
  } else {
    P1OUT &= ~bit;
  }
}
/***********************************************************/

 

 

Now Im wondering if any of you could help me on making my code a little better. Especially on this part:

 

  int First = 0xfc;
  int Second = 0X80+0x40+0x20+0x10+0x08+0x04+0x02+0x01;	// 0xFF
  int Third = 0X80+0x40+0x20+0x10+0x08+0x04+0x02+0x01;	// 0xFF
  int Fourth = 0xff;	// 0X80+0x40+0x20+0x10+0x08+0x04+0x02+0x01

  shiftOut(First);
  shiftOut(Second);
  shiftOut(Third);
  shiftOut(Fourth)

 

 

 

As you can see I am sending a byte for each of the 74hc164's I'm using (4).

Could there be a way of sending all of this like a word. (all together????)

 

Thanks

(If I don't make sense, please ask.)

Link to post
Share on other sites

Yes you can send it all at once.

 

 

in shiftOut() change the bit count to how many bits you want to send (EG 16, instead of 8)

and change your int to ((long?) I think) or a type that fits all your data

 

There are probably a few other ways to do it, like put all your data into an array, and use the array size to for your bit count....but that would be more advanced but would give you a nice way to send dynamic data......but that should be for another lesson. 

Link to post
Share on other sites

hi,

 

what is your IDE / compiler / tool / development environment for your code that you wrote ?

nice, clear code you wrote, btw.

 

i see that your hardware is LP with 2553 and using P1.0 and P1.4 to drive the 4 74hc164's.

 

For developing my code I am using the latest version of code composer studio.

I tried energia, but its too simple and doesn't have the debugging tools that code composer has. I also use CCS, because energia (arduino stuff), is not allowed at my school.

Link to post
Share on other sites

Energia does ship with the command line program called msp430-gdb which can be used to attach to mspdebug in gdb mode.  You can use that to debug your code. Granted, it isn't a GUI based tool but it works great.  This is one of the big advantages the Energia + Launchpad combo has over the Arduino.  Our hardware debugger is included in $4.30. On the Arduino, if you purchased an avrdragon debugger,  it would set you back about $50.

 

-rick

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...