Pages: [1]   Go Down
Author Topic: Two Half Bytes to a Whole Byte  (Read 458 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm shifting out 6 half bytes to three 595's. But it's just doing the first half byte then finishing the rest of the byte with 0. How do I stick together two half bytes to make a whole byte?

Code:
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte Dig[] = {
  B0000,
  B0001,
  B0010,
  B0011,
  B0100,
  B0101,
  B0110,
  B0111,
  B1000,
  B1001};
int i = 0;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
i++;

if (i==10)i=0;
  digitalWrite(latchPin, LOW);

  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);

  digitalWrite(latchPin, HIGH);
  delay(1000);
}

Code, just in case its not clear what im trying to say. for example, with i = 4 i want to output "00110011,00110011,00110011" but instead im getting "00110000,00110000,00110000" then i would assume it does this twice since it has already gone through all three. But Im not 100% clear on how the 595 behaves...

anyway, yeah, just want to stick two half bytes together for output.
Logged

California
Offline Offline
Sr. Member
****
Karma: 2
Posts: 433
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
byte Digit = B00000000;
bitWrite(Digit,0,bitRead(Dig[i],0));
bitWrite(Digit,1,bitRead(Dig[i],1));
bitWrite(Digit,2,bitRead(Dig[i],2));
bitWrite(Digit,3,bitRead(Dig[i],3));
bitWrite(Digit,4,bitRead(Dig[i],0));
bitWrite(Digit,5,bitRead(Dig[i],1));
bitWrite(Digit,6,bitRead(Dig[i],2));
bitWrite(Digit,7,bitRead(Dig[i],3));

This works, obviously only for repeating each half byte twice, it would be slightly modified for actual use. Is there an easier way, or is this the way to get it done?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/HighByte
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Manchester, New Hampshire
Offline Offline
Edison Member
*
Karma: 1
Posts: 1283
Propmaker
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

byte a, b, c;

c = (b << 4) | a;

This assumes bits 4 thru 7 of a are not set.

How it works:

If b = 00001111, b << 4 shifts those bits four bits to the left, so b = 11110000.
If a = 00001001 and you OR that with b, and place the result in c, then if a bit in a OR b is 1, the bit in c will be 1 as well, and c = 11111001.
« Last Edit: April 08, 2011, 08:45:52 am by scswift » Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm shifting out 6 half bytes to three 595's.


1. Each time you call shiftOut()  It always shifts out eight bits.

2. So: if you have three '595 devices connected in the usual manner, you need to call shiftOut three times.

3. On Arduino, a variable whose data type is byte always has eight bits.

4. So: if you want to shift the bits , say, 00110011, you can create a byte that has those bits and then call shiftOut.  (See Footnote.)

5. If you have a byte whose bits are 00000011 and you want to make upper 4 bits equal to lower four bits:
Code:
   byte x = B0011; // Same as if you had written byte x = B00000011;
.
.
.
    x = (x << 4) | x; // Assumes upper four bits are zero
.
.
.
   // If you don't know whether the upper four bits are zero and you want to make sure to throw them away
   x = (x << 4) | (x & B00001111);

Regards,

Dave

Footnote:
Arrays always start with index equal to zero, so your array elements have the following values:
Code:
Dig[0] = B0000; // Same as B00000000 or, simply, 0
Dig[1] = B0001; // Same as B00000001 or, simply, 1
Dig[2] = B0010; // Same as B00000010 or, simply, 2
DIg[3] = B0011; // Same as B00000011 or, simply, 3
.
.
.
Dig[9] = B1001; // Same as B00001001 or, simply, 9

So you don't need an array at all for these values, since the value of Dig[n] is, simply, n for n = 0, 1, ..., 9

However...

If you want to eliminate the necessity of having to use logical operations to duplicate the lower bits into the upper bits, you can define the  elements of the array like this:

Code:
byte Dig[] = {
  B00000000,
  B00010001,
  B00100010,
  B00110011,
  B01000100,
  B01010101,
  B01100110,
  B01110111,
  B10001000,
  B10011001
};

Then in the loop, shift Dig[ i ] three times to fill the three shift registers with that byte:
Code:

  digitalWrite(latchPin, LOW)
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, Dig[i]);
  digitalWrite(latchPin, HIGH)

So, with i equal to 3, you will be sending B00110011 to each of the three shift registers.
« Last Edit: April 08, 2011, 10:32:47 am by davekw7x » Logged

Pages: [1]   Go Up
Jump to: