O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

370 A. PROGRAMMING
//program constants
#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0
A.3.5 VARIABLES
There are two types of variables used within a program: global variables and local variables. A global
variable is available and accessible to all portions of the program. Whereas, a local variable is only
known and accessible within the function where it is declared.
When declaring a variable in C, the number of bits used to store the operator is also specified.
In Figure A.4, we provide a list of common C variable sizes. The size of other variables such as
pointers, shorts, longs, etc., are contained in the compiler documentation [ImageCraft].
Type
Size
Range
unsigned char
signed char
unsigned int
signed int
float
double
1
1
2
2
4
4 - 8
0..255
-128..127
0..65535
-32768..32767
+/-1.175e-38.. +/-3.40e+38
compiler dependent
Figure A.4: Typical C variable sizes.
When programming microcontrollers, it is important to know the number of bits used to
store the variable and also where the variable will be assigned. For example, assigning the contents
of an unsigned char variable, which is stored in 8-bits, to an 8-bit output port will have a predictable
result. However, assigning an unsigned int variable, which is stored in 16-bits, to an 8-bit output
port does not provide predictable results. It is wise to insure your assignment statements are balanced

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required