Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
272
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 4: C Types, Operators, and Expressions

Chapter 4: C Types, Operators, and

Expressions

Data Types and Sizes

Seen on a shirt at a Robothon event:

There are exactly 10 types of people in the world.

Those who understand binary numbers and those who don’t.

If this doesn’t make sense to you now, it will in a minute.

Bits

The first computers were people with quill pens who spent their lives calculating tables of things like cannonball trajectories to help soldiers more accurately slaughter their enemies. Later mechanical computers, with brass gears and cams, were developed to make the slaughter cheaper, quicker, and easier. Then one day a genius figured that you could do all this computing even easier if you used switches. Switches can be off or on, and the fundamental datum is the ‘bit’ with exactly two ‘binary’, states. We variously refer to these states as ‘0 and 1’ or ‘on and off’ or ‘true and false’. It’s the latter that allows us to use bits to automate Boolean logic and thus the modern binary logic computer entered the world and now slaughter is so cheap, quick and easy to compute that anybody can do it. Maybe this is skimming the topic a bit (har!) but a full explanation would begin with the first sentence of Genesis and only hit its stride about the time Albert Turing offed himself as his unjust reward for saving the free world, and while fascinating, it won’t get us blinking LEDs any quicker, so Let’s move on.

Each of our LEDs is connected to a microcontroller pin that can have two voltage states: ground or +3v, which can be manipulated as a data bit.

Bytes

The AVR and many other microcontrollers physically handle data in 8-bit units called bytes, a data type that can have 256 states, 0 thru 255. This is shown in the following sequence of states, (leaving out 9 thru 247, see Appendix 5 to see them all, and be sure to take a magnifying glass):

45

Chapter 4: C Types, Operators, and Expressions

00000000 = 0

 

1111000 = 248

00000001 = 1

 

1111001 = 249

00000010 = 2

 

1111010 = 250

00000011 = 3

(9 thru 247)

1111011 = 251

00000100 = 4

 

1111100 = 252

00000101 = 5

 

1111101 = 253

00000110 = 6

 

1111110 = 254

00000111 = 7

 

1111111 = 255

00001000 = 8

 

 

Look at our Cylon eye and notice that we have 8 LEDs with one lit at a time scrolling back and forth. What you are seeing is 8 of the 256 possible states being presented in a sequence that fools us into thinking we are seeing a back and forth scrolling motion. If the presentation sequence were random, we’d just see the light blinking on and off chaotically. Using binary numbers where the lit LED is represented by 1 shown next to the hexadecimal and decimal equivalent, what we are seeing is:

00000001 = 0x01 = 1

00000010 = 0x02 = 2

00000100 = 0x04 = 4

00001000 = 0x08 = 8

00010000 = 0x10 = 16

00100000 = 0x20 = 32

01000000 = 0x40 = 64

10000000 = 0x80 = 128

01000000 = 0x40 = 64

00100000 = 0x20 = 32

00010000 = 0x10 = 16

00001000 = 0x08 = 8

00000100 = 0x04 = 4

00000010 = 0x02 = 2

00000001 = 0x01 = 1

In microcontroller applications, we will often be dealing with the states of bytesized ports, like Port D. A port is a place where ships come and go, or in the case

46

Chapter 4: C Types, Operators, and Expressions

of a microcontroller it is a place where outside voltages (0v or 3v) can be read or set.

We use binary and hexadecimal numbers for ports because it is cumbersome and non-intuitive to think of port data as decimal numbers, Quick, what will 66 look like on our LEDs? Quick, what will 01000010 look like on our LEDs? Since 01000010 = 66, you see my point? And I bet you get the joke at the beginning of this section.

The hexadecimal system is another commonly seen number system used in microcontrollers. It has a base of 16, that is 16 states per digit:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

Since we use numbers to the base 10 because we have ten digits, fingers if you count the thumb as a finger, to count with. It might help to imagine an alien with 16 fingers, or better yet: 4 hands with three fingers and on thumb on each. In C, a hexadecimal number is preceded by 0x. The hex byte representation of the decimal number 129 is 0x81. The decimal and binary equivalents of the hex numbers are:

0 = 0000 = 0x0

1 = 0001 = 0x1

2 = 0010 = 0x2

3 = 0011 = 0x3

4 = 0100 = 0x4

5 = 0101 = 0x5

6 = 0110 = 0x6

7 = 0111 = 0x7

8 = 1000 = 0x8

9 = 1001 = 0x9

10 = 1010 = 0xA

11 = 1011= 0xB

12 = 1100 = 0xC

13 = 1101 = 0xD

14 = 1110 = 0xE

15 = 1111 = 0xF

47

Chapter 4: C Types, Operators, and Expressions

It is very common for new users of hex numbers to make the mistake of saying, ‘Well there are 16 hex integers, so 0xF, the last one, is 16.’ We make this mistake because we think of counting beginning with 1, but for most computer use you’ll see counting beginning with 0. 0 is the first integer and 15 is the 16th integer. When you count like a computer your first digit (left thumb?) is 0 not 1. If a computer had those alien hands to count on, the first thumb would be 0 and the last would be 15 (0xF if it was speaking hex instead of dec). Try to keep this in mind because it will bite you later.

Experienced microcontroller programmers memorize the binary equivalent of hex digits and find hex numbers very useful. For instance, given 0xA9, what would the LEDs (or the voltage states of an 8-bit register) look like? If you memorize the table, you come up with 0xA = 1010 and 0x9 = 1001, so the LEDs (voltage states) will look like: 10101001. As pointed out earlier, ask the same question in decimal, what will 169 look like on the LEDs and good luck, on doing that in your head. Look at Appendix 5 to see all the byte states in decimal, hexadecimal, and binary. Finally, all jokes equating byte to bite are prohibited.

char

The name of this data type is short for character, and is typically used to represent a character in the ASCII character set (Appendix 4 – ASCII Table). Originally, there were 127 ASCII characters used by Teletype machines to transmit and receive data. You will note that in Figure 1, you see Dennis Ritchie, who wrote C, standing next to Ken Thompson, who wrote UNIX, working on a Teletype machine. Clunky as they were (the Teletype, not Ritchie and Thompson), Teletypes were light years ahead of entering data by individual switches representing each bit of data. Teletypes send and receive characters so a lot of C, especially the standard library, is character oriented. The number of bits in a char is machine dependent, but in all machines I’ve encountered including the AVR, a char is an 8-bit byte which can have 256 bit states. The computer uses this byte of data as representing a signed value from –128 to + 127.

The ASCII code was extended to include characters for 128 to 255 primarily to do weird European characters, math symbols, and character graphics on early PCs.

48