0
Offline
Newbie
Karma: 0
Posts: 32
Arduino rocks
|
 |
« on: October 08, 2010, 08:20:36 am » |
Hi,
I'm fairly new to Arduino and I am currently working with a Serial GPS receiver.
I receive some returned bytes such as firmware versions as a byte, however the higher 2 bits form the major revision, then there should be a decimal place, then the lower 2 bits form the minor revision number.
Please can anyone help me or point me in the right direction to split bytes in 2? The bit() command only seems to allow you to look at individal bits, when I need a 'nibble', 2 bit size.
Many Thanks, Morrolan
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #1 on: October 08, 2010, 08:27:15 am » |
I always thought of nibbles (or nybbles) as being half a byte, or four bits.
hint: if you can read a single bit, you're halfway there.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 32
Arduino rocks
|
 |
« Reply #2 on: October 08, 2010, 08:31:53 am » |
Groove,
I'm sorry you are correct, a nibble is 4 bits, allowing values between 0 - 15.
Maybe it is my lack of understanding of C++, but how would one add the four individual bits together form 1 single nibble-sized number?
Many Thanks, Morrolan
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #3 on: October 08, 2010, 08:47:08 am » |
typedef struct { unsigned low:4; unsigned high:4; } nibble;
nibble test = { 3, 6 };
void setup() { Serial.begin(9600); Serial.println(test.low); Serial.println(test.high); } void loop(){}
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 222
Arduino rocks
|
 |
« Reply #4 on: October 08, 2010, 03:25:36 pm » |
Assuming you are trying to decompose a received byte into two nibbles: byte data;
byte lownibble = data & B00001111;
byte highnibble = data >> 4;
|
|
|
|
« Last Edit: October 08, 2010, 04:50:51 pm by lefstin »
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 5
Posts: 469
what?
|
 |
« Reply #5 on: October 08, 2010, 06:58:28 pm » |
Im with Professor Chaos on this one, you could use some inline asm also such as swapf, data which swaps the high nib and low nib in a byte, it is useful when talking to 4bit lcd's
|
|
|
|
|
Logged
|
|
|
|
|
Van Alstyne, TX
Offline
Full Member
Karma: 1
Posts: 154
Haven't smoked an Arduino... yet.
|
 |
« Reply #6 on: October 08, 2010, 10:07:33 pm » |
And I don't believe that there's anything in the C or C++ language references that would guarantee that the 'high' bitfield would be aligned with the four most significant bits nor the 'low' bitfield being aligned with the four least significant bits. Maybe on the Arduino it might work....
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 119
Posts: 10165
|
 |
« Reply #7 on: October 09, 2010, 12:09:31 am » |
Maybe on the Arduino it might work.... It does. It works well. For those concerned with performance... certain combinations of bit fields will generate several machine instructions. AlphaBeta's example is fairly lean. I think it's the same as using shift-and.
|
|
|
|
« Last Edit: October 09, 2010, 12:14:01 am by bcook »
|
Logged
|
|
|
|
|
|
|
England
Offline
Full Member
Karma: 0
Posts: 211
Arduino rocks
|
 |
« Reply #9 on: October 09, 2010, 06:28:13 am » |
|
|
|
|
« Last Edit: October 09, 2010, 06:29:09 am by UltraMagnus »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: October 09, 2010, 06:57:40 am » |
bitfields and unions are incredibly useful in uC programming And also incredibly irritating if you inadvertently use the same union at opposite ends of a link with different endianness.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
England
Offline
Full Member
Karma: 0
Posts: 211
Arduino rocks
|
 |
« Reply #11 on: October 09, 2010, 07:23:48 am » |
And also incredibly irritating if you inadvertently use the same union at opposite ends of a link with different endianness. perhaps, but that is nothing that cannot be solved by a quick lookup table.
|
|
|
|
|
Logged
|
|
|
|
|
SF Bay Area (USA)
Online
Faraday Member
Karma: 78
Posts: 5454
Strongly opinionated, but not official!
|
 |
« Reply #12 on: October 11, 2010, 06:13:17 pm » |
nothing that cannot be solved by a quick lookup table. IIRC, the IP "Fragment offset" field can not be represented as a C bitfield on a little-endian CPU/compiler. It spans the byte boundry on a big-endian system, but ends up discontiguous on little-endian systems. So there are things that can't be solved with a lookup table... :-)
|
|
|
|
|
Logged
|
|
|
|
|
|