Escolar Documentos
Profissional Documentos
Cultura Documentos
Agenda
Define and identify variable and constant types and scopes Construct variable and constant declarations Assign values to variables and constants Discuss variable type casting
Basic Concepts
In embedded C:
Instructions are put together to perform a function Functions are then treated as higher-level operations Functions are combined to form the complete program
Basic Concepts
Foundation of the C program Starting point for a program Lowest level task
Typically:
Basic Concepts
Simplest C program:
void main ( ) { while (1); }
Embedded systems programs are infinite loops: they will continue to execute until the microcontroller is turned off
Basic Concepts
#include: a common preprocessor compiler directive tells the compiler to include the file stdio.h as part of this program
Basic Concepts
Declaration of the function main ( ) All statements and function calls must fall between the braces {}
Basic Concepts
The keyword void tells the compiler that the main( ) function will not return any variables
Basic Concepts
The parentheses will contain any input parameters (or constants) to the function in this case there are no input parameters
Basic Concepts
printf( ) is a function. It is defined in the file stdio.h (that is why we must include stdio.h).
10
Basic Concepts
anything but Hello World. is the string input parameter to the printf function. This parameter is passed to printf in the function call.
11
Basic Concepts
All program statements and function calls must end with a semi-colon.
12
Basic Concepts
while is a C command (or reserved word). Normally it would have statements between braces (following the (1)) and before the ; The while statement continues as long as the parameter within the parentheses is true in C the constant 1 is always true
13
Basic Rules
Identifiers:
Must begin with a letter or underscore (_) May be followed by any alpha-numeric character or _ Case sensitive May be any length But some compilers recognize a limited number Cannot be reserved words
14
Reserved Words
C is a freeform language: white space is ignored (i.e.; space, tab, and new line characters (carriage return plus linefeed) )
15
Variables
Variables and constants make up the stored data in the embedded system
Variables are declared by the reserved word that indicates the size and type of variable, followed by the variables identifier Examples:
unsigned char Port_Name; int students, credit_hours; long int total_sch;
16
Variables
Memory space is limited The compiler needs to know how much memory to set aside Programmers must declare variables
Type bit char unsigned char signed char int short int unsigned int
Size 1 8 8 8 16 16 16
Variable Scope
Local variables:
Placed on the program stack or heap space Not accessible from other functions
Scope limited to the function in which they are declared To other functions the variable does not exist
The same identifier may be used in other functions since it doesnt exist outside the local function
18
Variable Scope
Global variables:
Can be accessed by all functions in the program Can be modified by any function in the program
Will retain its value from function to function Clear function usually performed by the startup function Startup is generated by the compiler Invisible to the programmer
If a local variable has the same name as a global, the local variable will be used
19
Variable Scope
Example:
//a global variable
unsigned char globey; void function_z (void) { unsigned int tween; tween = 12; globey = 47; main_loc = 12; } void main ( ) { unsigned char main_loc; globey = 34; tween = 12; while (1) ; }
//local variable
20
Variable Scope
Example:
//a global variable
unsigned char globey; void function_z (void) { unsigned int tween; tween = 12; globey = 47; main_loc = 12; } void main ( ) { unsigned char main_loc; globey = 34; tween = 12; while (1) ; }
//local variable
21
Variable Scope
Example:
//a global variable
unsigned char globey; void function_z (void) { unsigned int tween; tween = 12; globey = 47; main_loc = 12; } void main ( ) { unsigned char main_loc; globey = 34; tween = 12; while (1) ; }
//local variable
22
Variable Scope
Example:
//a global variable
unsigned char globey; void function_z (void) { unsigned int tween; tween = 12; globey = 47; main_loc = 12; } void main ( ) { unsigned char main_loc; globey = 34; tween = 12; while (1) ; }
//local variable
tween is local to function_z ( ) and so it may be used in function_z ( ) but not in main ( )
This will cause an error
23
Variable Scope
Example:
//a global variable
unsigned char globey; void function_z (void) { unsigned int tween; tween = 12; globey = 47; main_loc = 12; } void main ( ) { unsigned char main_loc; globey = 34; tween = 12; while (1) ; }
//local variable
24
Variable Scope
Example:
//a global variable
unsigned char globey; void function_z (void) { unsigned int tween; tween = 12; globey = 47; main_loc = 12; } void main ( ) { unsigned char main_loc; globey = 34; tween = 12; while (1) ; }
//local variable
main_loc is local to main ( ) and so it may be used in main ( ) but not in function_z ( )
This will cause an error
25
Constants
Fixed values
x = 3 + y; // 3 is the constant printf (this message); // this message is the constant x = B; // B is the constant const char c = 57; // c is the constant and has a value of 57 which is also a constant
26
Constants
Decimal just the number Binary 0b prefix (zero b) Octal 0 prefix Hexadecimal 0x prefix Unsigned integers U suffix Long integers L suffix Unsigned long integers UL suffix Floating point F suffix
27
Modifiers
Constants
\a \b \t \n \r \\ \
(newline character)
28
Storage Classes
Uninitialized when declared Programmer must make sure it has a value before it is used Default storage class Local scope but allocated in global memory Initialized to 0 when function is entered the first time Retains its value when exiting the function Uninitialized and temporary Compiler tries to put it into a register, saving RAM data space
29
Type casting
Times when programmer wants to force the type and size of a variable Example:
int z; int x = 150; char y = 63; z = (y * 10) + x; \\z is a signed 16-bit integer \\declare and initialize x \\y is a signed 8 bit integer
30
Type casting
Times when programmer wants to force the type and size of a variable Example:
int z; int x = 150; char y = 63; z = (y * 10) + x; \\z is a signed 16-bit integer \\declare and initialize x \\y is a signed 8 bit integer
What is the value of z at the end of this program segment? Result: z = 268 (but it should be 780)
31
Type casting
Example:
int z; int x = 150; char y = 63; z = (y * 10) + x; \\z is a signed 16-bit integer \\declare and initialize x \\y is a signed 8 bit integer
Explanation:
1) 2) 3) 4) 5) y is a signed 8 bit integer it can have a range of -128 to 127 When y is multiplied by 10, the result should be 630 (0b1001110110) Because it is an 8 bit number, the upper bits are truncated (0b01110110) 0b01110110 = 118 118 + 150 = 268
Type casting
z = ((int) y * 10) + x;
Now, y will be treated as an integer. It will not get truncated when it is multiplied by 10 and so the result within the parentheses will be 630 (as it should be). General Rule: When performing an operation with more than one type, it is safer to cast variables to the appropriate type
33
Summary
Defined and identified variable and constant types and scopes Constructed variable and constant declarations Assigned values to variables and constants Discussed variable type casting
34
35