Jump to content
43oh

Sending 32 bits to cascaded 8 bit registers


Recommended Posts

I'm using the 74hc164 shift register. The schematic I'm following is attached.

I first started controlling all of the pins by writing 8 bits of data at a time to the shift registers.

Then, I decided that I wanted to be able to send more than 8 bits of data a time to them.

 

This is how i sent 8 bits at a time:

int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  P1DIR |= (DATA + CLOCK);  // Setup pins as outputs
 int Fisrt = 0x40;	//Last register
 int Second = 0xff;	//third register
 int Third = 0xff;	//Second register
 int Fourth = 0xff;	//First register
  shiftOut(one);
  shiftOut(two);
  shiftOut(three);
  shiftOut(four);

}
----------------------------------------------
.....
---------------------------------------------
// ---------->>>>And Im using this to send the bits:
 
/***********************************************************/
// 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)
{


  unsigned int i = 0;

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

 

I was able to modify that to send 16 bits at a time. All I had to do was to change this 4 lines:

 

 int Fisrt = 0x40;	//Last register int Second = 0xff;	//third register int Third = 0xff;	//Second register int Fourth = 0xff;	//First register  shiftOut(one);  shiftOut(two);  shiftOut(three);  shiftOut(four);

To

 

 int Fisrt = 0xff40;	//Last register int Second = 0xffff;	//third register //int Third = 0xff;	//Second register //int Fourth = 0xff;	//First register  shiftOut(one);  shiftOut(two);  //shiftOut(three);  //shiftOut(four);

 

and this line:

 

 for (i = 0; i < 8; i++)  {

to 

 

 for (i = 0; i < 16; i++)  {

--> I was now able to send 16 bits of data at a time.

 

Now my question here is, Would it be possible to send 32 bits of data at a time???

I tried changing my data declaration and command to this:

 

 long Fisrt = 0xffffff40;	//Last register //int Second = 0xffff;	//third register

 

  shiftOut(one);  \\shiftOut(two);

and changed this 16 

 

for (i = 0; i < 16; i++)  {

To 32

 

for (i = 0; i < 32; i++)  {

 

--->It did not work, I stepped through the code, it seems that only the first 16 bits are being sent, after that everything dies.

Any suggestions???

Thanks

 

 

post-29793-0-97054700-1357349990_thumb.jpg

Link to post
Share on other sites

The problem you ran into is that "(val & (1 << i))" would translate "1 << i" to a single 16 bit integer, which would be ANDed with val. So changling "i" to be an unsigned long would also have fixed the problem.

On the other hand, as the passed argument is a copy anyways, you could just as well use it in a destructive way and shift val right by 1, this is as the solution oPossum suggested.

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