Go Down

### Topic: how do i split one bit into 3 or more bytes?? (Read 3525 times)previous topic - next topic

#### rlwilliams

##### Apr 05, 2011, 12:39 pm
hi i am trying to use more than 3 74hc595 shift registers to power LED's directly. i have two shift registers working by
splitting the bit into 2 bytes as shown below:

byte registerOne = lowByte(bitsToSend);
byte registerTwo = highByte(bitsToSend);

I am wondering what code i need to have this as 3 or more bytes?

any help would be great! thanks

#### tomm

#1
##### Apr 05, 2011, 12:49 pm
I'm not sure what you're doing with the shift registers exactly, but is there some reason you aren't daisy chaining them?

#### rlwilliams

#2
##### Apr 05, 2011, 01:10 pm
hi derby,

i am daisy chaining the shift registers with the data running through all of them in the chain, however the problem i have is getting them to react individually to the data sent.

eg  byte registerOne = lowByte(bitsToSend);
byte registerTwo = highByte(bitsToSend);
byte registerThree = highByte(bitsToSend);

Highlighted byte will be triggered identical to register two.

do you see what i mean?

thanks

#### AWOL

#3
##### Apr 05, 2011, 01:55 pm
Assuming (because you haven't shown us) that "bitsToSend" is an "int", then the answer is that there aren't any more bytes.
An "int" is 16 bits, or two bytes long.
Or, if it's a "long", simply shift right 16 bits and mask with 0xFF.

#### Korman

#4
##### Apr 05, 2011, 02:06 pm
If you just have two bytes, you can get away with using one 16-bit integer value and splitting it with highbyte() and lowbyte(). For more than two, things become impractical, in that case you  better define a byte array and shift that out.

So your code will look something like this:
Code: [Select]
`const int daisylength = 3;uint8_t daisydata[daisylength];void loop() {...// Setting some bitsdaisydata[2] |= 0x43;...// Send out datafor (int i = 0; i < daisylength; i++) {    shiftOut(dataPin, clock, LSBFIRST, daisydata[i]);}// Latch etc.....}`

Then you can increase your daisy-chain by simply increasing the value of daisylength and adding the necessary hardware.

Korman

#### rlwilliams

#5
##### Apr 05, 2011, 04:41 pm
ok thats great!

one question however

When setting the bits

i am having problems with the constant before the = sign

Whenever i use a different symbol than | the software says it is missing a ; before the contant.

any ideas??

#### AWOL

#6
##### Apr 05, 2011, 04:43 pm
Quote
Whenever i use a different symbol than | the software says it is missing a ; before the contant

Do you mean the bitwise OR operation?

What other symbols did you try?  (and, other than '+', why?)

#### rlwilliams

#7
##### Apr 05, 2011, 05:15 pm
I think bit wise

Eg.

// Setting some bits
daisydata[2] |= 0x43;
daisydata[2] A= 0x43;
daisydata[2] £= 0x43;
daisydata[2] @= 0x43;

The green line works fine

The red lines and others alike the arduino software asks for this below

daisydata[2] ;A= 0x43;

The red semi colon then produces the error that 'A' is undefined.

any ideas?

#### Korman

#8
##### Apr 05, 2011, 05:29 pm
Ouch.

The line:
Code: [Select]
`daisydata[2] |= 0x43;`

is equivalent to writing:

Code: [Select]
`daisydata[2] = daisydata[2] | 0x43;`

which means that daisydata[2] will have all bits of the hexadecimal 0x43 (01000011 in binary) set. You can't put any random characters in there, the operators with = exist only for some operations (+ - * / % ^ ~  \$ and |)

Korman

#### Grumpy_Mike

#9
##### Apr 05, 2011, 07:04 pm
This wouldn't happen to be a home work assignment would it? We got an almost identical request some months back.

#### AWOL

#10
##### Apr 05, 2011, 11:13 pm
Quote
daisydata[2] £= 0x43;

Ah! the old pound-sign (note to US readers: yes, that really is a pound sign) operator.
Why didn't you say so before?

#### GrooveFlotilla

#11
##### Apr 06, 2011, 03:47 pm
@Korman
what does the \$ operator do?
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

#12

#### Korman

#13
##### Apr 06, 2011, 05:57 pmLast Edit: Apr 06, 2011, 06:00 pm by Korman Reason: 1

@Korman
what does the \$ operator do?

The main application of the \$-operator is to exercise the syntax checker of the compiler to prevent the accumulation of rust between the bits. It's used mostly by typing-challenged posters who were targeting the &-key but missed it wildly by 3 keys. This inaccuracy training is also a prerequisite for the job of missile guidance officer in war zones to properly execute surgical strikes with carpet bombing.

Korman

#14
##### Apr 07, 2011, 10:26 pm

It's used mostly by typing-challenged posters who were targeting the &-key but missed it wildly by 3 keys.

When I read that, the thought, "What keyboard is he using where the \$ is anywhere near the &" popped into my head before even getting to the latter part describing how badly you missed it.

Go Up