Hardware Reference
In-Depth Information
Interpreting a Binary Protocol
Since most of the protocols you've dealt with in this
topic have been text-based, you haven't had to do a lot of
interpreting of the bits of a byte. Binary protocols often
demand that you know which bit represents what, so it's
useful to know a little about the architecture of a byte and
how to manipulate it. Let's now talk for a bit about bits.
Bit Reading and Writing
Arduino offers you some commands for reading and
writing the bits of a byte:
// to read the value of a bit:
myBit = bitRead(someByte, bitNumber);
// to write the value of a bit:
bitWrite(someByte, bitNumber, bitValue);
Binary protocols often show up in communications
between chips in a complex device, particularly in syn-
chronous serial protocols. Many SPI and I2C devices have
small command sets. Their single-byte operational codes ,
or opcodes , are often combined with the parameters for
the commands in the same byte. You saw this in action in
Chapter 9 when you sent the opcode to read the firmware
of the SM130 RFID reader.
Giving a bit the value 1 is called setting the bit in general
programming terms, and giving it the value 0 is known as
clearing the bit . So you also have commands for these:
// to make a bit equal to 1:
bitSet(someByte, bitNumber);
These protocols are usually written out in hexadecimal
notation, binary notation, or both. You've seen hexa-
decimal or base-16 notation already in this topic. Just as
hexadecimal numbers begin with 0x , binary numbers in
Arduino—and by extension in C—begin with 0b, like so
0b10101010.
// to make a bit equal to 0:
bitClear(someByte, bitNumber);
Bit Shifting
Sometimes it's easier to manipulate several bits at once.
The shift left and shift right operators in Arduino, C, and
Java allow you to just that. The shift left operator ( << )
moves bits to the left in a byte, and the shift right operator
( >> ) moves them to the right:
Which digit matters most in the number below?
$2,508
0b00001111 << 2; // gives 0b00111100
0b10000000 >> 7; // gives 0b0000001
The 2, because it represents the largest amount, two
thousands, or two groups of 10 3 . It is the most significant
digit . That number was in decimal, or base-10 notation.
The same principle applies when you're writing in binary,
or base-2. Which is the most significant bit :
Bit shifting is useful when you need a particular value to be
in a specific part of a byte, as you'll see shortly.
Bit Masking
The logical operators AND, OR, and XOR allow you to
combine bits in some interesting ways.
0b10010110
The leftmost 1 is most significant because it represents 1
group of 128, or 2 7 . Usually when you see bits written out,
though, you care less about their decimal numeric values
than their position in the byte. For example, in the project
that follows, you're going to make music using MIDI , the
Musical Instrument Digital Interface protocol. MIDI is a
binary protocol in which all bytes with a 1 in the most sig-
nificant bit are commands (verbs), and all bytes with a 0 in
the most significant bit are data bytes (nouns, adjectives,
or adverbs). A MIDI interpreter could look at the most sig-
nificant bit (which happens to be the first to arrive serially)
and know whether it's getting a command or not.
AND (&) : if the two bits are equal, the result is 1.
Otherwise, it's 0:
1 & 1 = 1
0 & 0 = 1
1 & 0 = 0
0 & 1 = 0
OR (|) : if either bit is 1, the result is 1. Otherwise, it's 0:
1 | 1 = 1
0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
 
Search WWH ::




Custom Search