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

Chapter 4: C Types, Operators, and Expressions

unsigned

If the modifier unsigned is used in the definition of a char variable: ‘unsigned char’, the value is from 0 to 255. Many C compilers will have ‘byte’ or ‘Byte’ defined as equaling ‘unsigned char’. The ‘byte’ keyword is not part of C, but it is very convenient, since in microcontrollers we usually use a lot of numbers, but not a lot of ‘char’acters.

int

On AVR microcontrollers int declares a 16 bit data variable as having values from –32768 to +32767. A variable declared with ‘unsigned int’ will have a value from 0 to 65535.

The long and short of it

Everybody else makes that dumb joke at this point, so why be different?

You can declare variables as ‘short int’ and ‘long int’. For C the size is machine dependent, but on many systems a short int is the same as an int, 16 bits, while a long int is 32 bits.

Variable Names

The changeable data you are processing is stored in bytes of RAM, Random Access Memory, at specific addresses. Variables are names that provide an alias for the address being used. We’ll look at the gory details in the ‘Variables External, Static, and Register’ section of.

Constants

Constants are data that cannot be changed by the program and are usually stored in ROM, Read Only Memory. We could just type in the constant value wherever needed, but that will get old quick, so we alias the value with a name. We usually do this in a header file or at the start of the software module, which adds the advantage that if we ever want to change the constant we can do it once in the definition instead of at each occurrence in the code. By convention, constant names are all caps. For example we might want to use pi in calculation (pi containts a decimal so we use the float data type) so we define as follows:

#define PI 3.1415926

49

Chapter 4: C Types, Operators, and Expressions

We can then use PI anywhere in our software and the compiler will automatically substitute the numerical value for it:

float pieCircumference = 0.0; float piePanRadius = 0.0;

pieCircumference = PI * (piePanRadius^2);

Declarations

A declaration is a text statement that declares to the complier how your words are to be used. When you declare ‘unsigned char counter = 0’ you are telling the compiler that when it encounters the word ‘counter’ to consider it as data stored at some specific location with the alias name ‘counter’ that can have values from 0 to 255, but in this case initially has a value of 0.

Arithmetic Operators

Operators seem like ordinary arithmetic or algebra symbols, and they mostly are. But they are different from arithmetic or algebra often enough that you need to pay attention when operations don’t act like you think they should. The compiler might just be doing what you told it to do, rather than what you wanted it to do. An example of the kind of confusion you can run into when you use the ‘=’ assignment operator and the ‘==’ ‘is equal to’ operator:

x = y;

if(x==y) _delay_loop_2(30000);

The first statement assigns x to the value of y. The second calls the _delay_loop_2(30000) function if x is equal to y. What about:

if(x=y) _delay_loop_2(30000); //BAD STATEMENT

This will set x equal to y, and then call the _delay_loop_2(30000) function. The ‘if’ becomes meaningless because the condition, x=y, is always true, so the delay will always run. The WinAVR compiler will think something is strange and issue this warning:

Warning: suggest parentheses around assignment used as truth value

50

Chapter 4: C Types, Operators, and Expressions

Which will scroll by so fast you won’t see it, so you’ll assume the compile was good. Notice how clear (NOT) this warning was? Most complier warnings are even more cryptic. Not all compilers will flag this error with a warning. It is a very easy mistake to make, and you will feel really dumb after an hour of debugging, looking for something obscure, only to find a lousy missing ‘=’ character. I do this all the time.

Note: Some of these operators may seem strange at this point, but they are explained fully in later sections. Then they’ll seem really strange.

Table 1: Arithmetic Operators

Operator

Name

Example

Defined

*

Multiplication

x*y

Multiply x times y

/

Division

x/y

Divide x by y

%

Modulo

x%y

Provide the remainder of x divided by y

+

Addition

x+y

Add x and y

-

Subtraction

x-y

Subtract y from x

++

Increment

x++

Increment x after using it

--

Decrement

--x

Decrement x before using it

-

Negation

-x

Multiply x by –1

+

Unary Plus

+x

Show x is positive (not really needed)

Table 2: Data Access and Size Operators

Operator

Name

Example

Defined

[]

Array element

x[6]

Seventh element of array x

.

Member selection

PORTD.2

Bit 2 of Port D

->

Member selection

pStruct->x

Member x of the structure pointed to

 

 

 

by pStruct

*

Indirection

*p

Contents of memory located at

 

 

 

address p

&

Address of

&x

Address of the variable x

51

Chapter 4: C Types, Operators, and Expressions

Table 3: Miscellaneous Operators

Operator

Name

Example

Defined

()

Function

wait(10)

call wait with an argument of 10

(type)

Type cast

(double)x

x converted to a double

?:

Conditional

x?y:z

If x is not 0 evaluate y, otherwise evaluate

 

 

 

z

,

Sequential

x++,y++

Increment x first, then increment y

 

evaluation

 

 

Relational and Logical Operators

Table 4: Logical and Relational Operators

Operator

Name

Example

Defined

>

Greater than

x>y

1 if x is greater than y, otherwise 0

>=

Greater than

x>=y

1 if x is greater than or equal to y,

 

or equal to

 

otherwise 0

<

Less than

x<y

1 if x is less than y, otherwise 0

<=

Less than or

x<=y

1 if x is less than or equal to y, otherwise

 

equal to

 

0

 

 

 

 

==

Equal to

x==y

1 if x equals y, otherwise 0

!=

Not equal to

x!=y

1 if x is not equal to y, otherwise 0

!

Logical NOT

!x

1 if x is 0, otherwise 0

&&

Logical AND

x&&y

0 if either x or y is 0, otherwise 1

||

Logical OR

x||y

0 if both x and y are 0, otherwise 1

52