Topic 5-Embedded C Basics
Topic 5-Embedded C Basics
1
ELET 3232
Topic 5: Embedded C Basics
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
2
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
3
Basic Concepts
All C programs must have at least one
function: main( )
Foundation of the C program
Starting point for a program
Lowest level task
Typically:
Contains a few initialization instructions
Calls to other functions
4
Basic Concepts
Simplest C program:
5
void main ( )
{
while (1); //do forever an endless loop
}
Embedded systems programs are infinite loops: they
will continue to execute until the microcontroller is
turned off
Basic Concepts
Adding some functionality:
6
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
#include: a common preprocessor compiler directive
tells the compiler to include the file stdio.h as part of this program
Basic Concepts
Adding some functionality:
7
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
Declaration of the function main ( )
All statements and function calls must fall between the braces {}
Basic Concepts
Adding some functionality:
8
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
The keyword void tells the compiler that the main( ) function will not return any
variables
Basic Concepts
Adding some functionality:
9
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
The parentheses will contain any input parameters (or constants) to the function
in this case there are no input parameters
Basic Concepts
Adding some functionality:
10
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
printf( ) is a function. It is defined in the file stdio.h (that is why we must include
stdio.h).
Basic Concepts
Adding some functionality:
11
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
anything but Hello World. is the string input parameter to the printf function.
This parameter is passed to printf in the function call.
Basic Concepts
Adding some functionality:
12
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
All program statements and function calls must end with a semi-colon.
Basic Concepts
Adding some functionality:
13
#include <stdio.h>
void main ( )
{
printf (anything but Hello World.);
while (1); //do forever an endless loop
}
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
Basic Rules
Identifiers:
Variables or Functions names
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
The following are reserved words:
15
auto default extern inline short switch
break defined flash int signed typedef
bit do float interrupt sizeof union
case double for long sfrb unsigned
char eeprom funcused register sfrw void
const else goto return static volatile
continue enum if short struct while
C is a freeform language: white space is ignored (i.e.; space, tab, and
new line characters (carriage return plus linefeed) )
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:
16
unsigned char Port_Name;
int students, credit_hours;
long int total_sch;
Variables
Variables and constants are stored in memory
on the microcontroller
Memory space is limited
The compiler needs to know how much memory to set aside
Programmers must declare variables
Specify both size and type
17
Type Size Range Type Size Range
bit 1 0 or 1 signed int 16 -32768 to 32767
char 8 -128 to 127 long int 32 -2,147,483,648 to 2147483647
unsigned char 8 0 to 255 unsigned long 32 0 to 4294967295
signed char 8 -128 to 127 signed long int 32 -2,147,483,648 to 2147483647
int 16 -32768 to 32767 float 32 1.175e-38 to 3.402e38
short int 16 -32768 to 32767 double 32 1.175e-38 to 3.402e38
unsigned int 16 0 to 65535
Variable Scope
Local variables:
Memory spaces allocated by the function when the
function is entered
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:
Memory spaces allocated by the compiler
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
Typically cleared (set to 0) when main( ) is started
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:
20
unsigned char globey; //a global variable
void function_z (void) {
unsigned int tween; //local variable
tween =12;
globey =47;
main_loc =12;
}
void main ( ) {
unsigned char main_loc;
globey =34;
tween =12;
while (1)
;
}
Variable Scope
Example:
21
unsigned char globey; //a global variable
void function_z (void) {
unsigned int tween; //local variable
tween =12;
globey =47;
main_loc =12;
}
void main ( ) {
unsigned char main_loc;
globey =34;
tween =12;
while (1)
;
}
globey is a global variable
and so it may be used in
either function
Variable Scope
Example:
22
unsigned char globey; //a global variable
void function_z (void) {
unsigned int tween; //local variable
tween =12;
globey =47;
main_loc =12;
}
void main ( ) {
unsigned char main_loc;
globey =34;
tween =12;
while (1)
;
}
tween is local to function_z ( )
and so it may be used in
function_z ( )
Variable Scope
Example:
23
unsigned char globey; //a global variable
void function_z (void) {
unsigned int tween; //local variable
tween =12;
globey =47;
main_loc =12;
}
void main ( ) {
unsigned char main_loc;
globey =34;
tween =12;
while (1)
;
}
tween is local to function_z ( )
and so it may be used in
function_z ( ) but not in
main ( )
This will cause an error
Variable Scope
Example:
24
unsigned char globey; //a global variable
void function_z (void) {
unsigned int tween; //local variable
tween =12;
globey =47;
main_loc =12;
}
void main ( ) {
unsigned char main_loc;
globey =34;
tween =12;
while (1)
;
}
main_loc is local to main ( )
and so it may be used in
main ( )
Variable Scope
Example:
25
unsigned char globey; //a global variable
void function_z (void) {
unsigned int tween; //local variable
tween=12;
globey=47;
main_loc =12;
}
void main ( ) {
unsigned char main_loc;
globey=34;
tween=12;
while (1)
;
}
main_loc is local to main ( )
and so it may be used in
main ( ) but not in
function_z ( )
This will cause an error
Constants
Fixed values
May not be modified during execution
Part of the compiled program
In ROM rather than RAM
Stored in code space
Examples:
26
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
Constants
Numeric constants may be declared in many
ways
Different number systems
Decimal just the number
Binary 0b prefix (zero b)
Octal 0 prefix
Hexadecimal 0x prefix
Modifiers
Unsigned integers U suffix
Long integers L suffix
Unsigned long integers UL suffix
Floating point F suffix
27
Constants
Common non-printable and special characters
Bell \a
Backspace \b
Tab \t
LF \n (newline character)
CR \r
\ \\
\
28
Storage Classes
Three possible storage classes
automatic (auto) local variable
Uninitialized when declared
Programmer must make sure it has a value before it is used
Default storage class
static local variable
Local scope but allocated in global memory
Initialized to 0 when function is entered the first time
Retains its value when exiting the function
register local variable
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:
30
int z; \\z is a signed 16-bit integer
int x =150; \\declare and initialize x
char y =63; \\y is a signed 8 bit integer
z =(y * 10) +x;
What is the value of z at the end of this program segment?
Type casting
Times when programmer wants to force the
type and size of a variable
Example:
31
int z; \\z is a signed 16-bit integer
int x =150; \\declare and initialize x
char y =63; \\y is a signed 8 bit integer
z =(y * 10) +x;
What is the value of z at the end of this program segment?
Result: z =268
(but it should be 780)
Type casting
Example:
32
int z; \\z is a signed 16-bit integer
int x =150; \\declare and initialize x
char y =63; \\y is a signed 8 bit integer
z =(y * 10) +x;
Explanation:
1) y is a signed 8 bit integer it can have a range of -128 to 127
2) When y is multiplied by 10, the result should be 630 (0b1001110110)
3) Because it is an 8 bit number, the upper bits are truncated (0b01110110)
4) 0b01110110 =118
5) 118 +150 =268
So how is this fixed
Type casting
Force y to be seen as an integer (cast)
33
int z; \\z is a signed 16-bit integer
int x =150; \\declare and initialize x
char y =63; \\y is a signed 8 bit integer
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
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