Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Maik Schmidt - Arduino A Quick-Start Guide, 2nd Edition (The Pragmatic Programmers) - 2015.pdf
Скачиваний:
146
Добавлен:
22.03.2016
Размер:
30.47 Mб
Скачать

Bit Operations 251

Bit Operations

In embedded computing, you often have to manipulate bits. You sometimes have to read single bits to get some sensor data. In other cases, you have to set bits to turn a device into a certain status or to make it perform some action.

For bit manipulation, you need only a few operations. The simplest is the not operation that inverts a bit. It turns a 0 into a 1 and vice versa. Most programming languages implement the binary not operation with a ~ operator:

int x = 42; // In binary this is 101010 int y = ~x; // y == 010101

In addition, you’ll find three binary operations named AND, OR, and XOR

(eXclusive OR). Most programming languages call the corresponding operators

&, |, and ^, and their definitions are as follows:

a

b

a AND b

a OR b

a XOR b

 

 

a & b

a | b

a ^ b

0

0

0

0

0

1

0

0

1

1

0

1

0

1

1

1

1

1

1

0

With these operators, it’s possible to mask bits in a number, so you can extract certain bits. If you’re interested only in the lower two bits of a number, you can do it as follows:

int x = 42; // In binary this is 101010 int y = x & 0x03; // y == 2 == B10

You can also set one or more bits in a number using the OR operation. The following code sets the fifth bit in x regardless of whether this bit is 0 or 1.

int x = 42; // In binary this is 101010 int y = x | 0x10; // y == 58 == B111010

The bit shift operators « and » let you move bits to a certain position before you work with them. The first one moves bits to the left, and the second moves them to the right:

int x = 42; // In binary this is 101010 int y = x << 1; // y == 84 == B1010100

int z = x >> 2; // z == 10 == B1010

report erratum • discuss

Appendix 2. Advanced Arduino Programming 252

Shifting operations might seem intuitive, but you have to be careful when shifting signed values.2 Although they look similar, binary operators aren’t the same as Boolean operators. Boolean operators such as && and || don’t operate on the bit level. They implement the rules of Boolean algebra.3

Beginners are often afraid of bit operations, but there’s no reason to fear them. Microcontrollers operate on a bit level, so you have to be able to make the bits obey your will. It takes some training, but it’s not rocket science.

2.http://en.wikipedia.org/wiki/Arithmetic_shift

3.http://en.wikipedia.org/wiki/Boolean_algebra_%28logic%29

report erratum • discuss

APPENDIX 3

Advanced Serial Programming

In nearly all of the book’s projects, we’ve used the Arduino’s serial port. Sometimes we only emitted debug messages to monitor the current state of our sketches, but often we needed it to actually output information or to send commands. And the fact is, we’ve used the Serial class without explaining how serial communication actually works. We’ll catch up on that in this appendix.

To communicate with an Arduino, we mainly used JavaScript, and in some cases we used Processing. But many developers prefer other languages, and in this appendix, you’ll also learn how to use C/C++, Java, Ruby, Python, and Perl to talk to an Arduino.

Learning More About Serial Communication

In Chapter 2, Creating Bigger Projects with the Arduino, on page 23, you saw that you need only three wires for serial communication: a common ground, a line for transmitting data (TX), and one for receiving data (RX). (See the diagram on page 28.)

Data is transmitted as electrical pulses, so both communication partners need a reference for the voltage level, and that’s what the common ground is for. The transmission line is used to send data to the recipient and has to be connected to the recipient’s receiving line. This enables full-duplex communication where both partners can send and receive data simultaneously. (Wouldn’t it be great if people could also communicate full-duplex?)

We now know how to connect two devices, but we still have to transmit some data. Therefore, both communication partners have to agree on a protocol, and on page 254, you can see what a typical serial communication looks like. The different states of a bit are represented by different voltage levels. Usually,

report erratum • discuss

Appendix 3. Advanced Serial Programming 254

a 0 bit is represented by 0 volts, while 5 volts stands for a 1 bit. (Some protocols use -12V and 12V, respectively.)

The following parameters control a serial communication:

 

 

 

 

 

 

Data

 

 

 

Parity

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0

0

1

0

0

1

1

0

 

0

1

 

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Start Bit

 

 

 

 

 

 

 

 

 

 

 

 

Stop Bit

A start bit indicates the beginning of a data word and is used to synchronize the transmitter and receiver. It is always 0.

A stop bit tells us when the last data bit has been sent and separates two consecutive data words. Depending on the particular protocol agreement, there can be more than one stop bit, but that happens rarely.

Information is transferred as binary data bits; that is, if you’d like to transmit the letter M, you have to turn it into a number first. Several character set encodings are available, but when working with the Arduino, the ASCII encoding fits best. In ASCII, an uppercase M is encoded as the decimal number 77, which is 01001101 in binary. This is the bit sequence that eventually gets transmitted.

The parity bit indicates whether the number of 1s in the data has been odd or even. This is a simple error-checking algorithm that is rarely used and that stems from a time when network connections were less reliable than they are today. Parity control can be “none” (no parity bit is sent),

“odd” (the parity bit is set if the amount of 1s in the data bits is odd; otherwise, it is 0), or “even” (the parity bit is set if the amount of 1s in the data bits is even; otherwise, it is 0). We chose odd parity for our data, and because there are 4 bits set to 1 in 01001101, the parity bit is 0.

The baud rate defines the transmission speed and is measured in transmission steps per second. When working with the Arduino, typical baud rates are 9600, 14400, 19200, or even 115200. Note that the baud rate doesn’t define how much data is actually transferred per second, because you have to take the control bits into account. If your connection settings are 1 start bit, 1 stop bit, no parity, and 8 bits per byte, then you have to transfer 1 + 1 + 8 = 10 bits to transfer a single byte. With a baud rate set to 9600, you can then theoretically send 9600 / 10 = 960 bytes per sec- ond—at least if every bit gets transferred in exactly one transmission step.

report erratum • discuss

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]