Você está na página 1de 35

Microcontroller Systems

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

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

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

Basic Concepts

Simplest C program:
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:


#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:


#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:


#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:


#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:


#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).

10

Basic Concepts

Adding some functionality:


#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.

11

Basic Concepts

Adding some functionality:


#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.

12

Basic Concepts

Adding some functionality:


#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
13

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:


default defined do double eeprom else enum extern flash float for funcused goto if inline int interrupt long register return short short signed sizeof sfrb sfrw static struct switch typedef union unsigned void volatile while

auto break bit case char const continue

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

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


Range 0 or 1 -128 to 127 0 to 255 -128 to 127 -32768 to 32767 -32768 to 32767 0 to 65535 Type signed int long int unsigned long signed long int float double Size 16 32 32 32 32 32 Range -32768 to 32767 -2,147,483,648 to 2147483647 0 to 4294967295 -2,147,483,648 to 2147483647 1.175e-38 to 3.402e38 1.175e-38 to 3.402e38
17

Type bit char unsigned char signed char int short int unsigned int

Size 1 8 8 8 16 16 16

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 Clear function usually performed by the startup function Startup is generated by the compiler Invisible to the programmer

Typically cleared (set to 0) when main( ) is started

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) ; }

globey is a global variable and so it may be used in either function

//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) ; }

tween is local to function_z ( ) and so it may be used in function_z ( )

//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) ; }

main_loc is local to main ( ) and so it may be used in main ( )

//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

May not be modified during execution Part of the compiled program


In ROM rather than RAM Stored in code space Examples:

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

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 Unsigned integers U suffix Long integers L suffix Unsigned long integers UL suffix Floating point F suffix
27

Modifiers

Constants

Common non-printable and special characters


Bell Backspace Tab LF CR \

\a \b \t \n \r \\ \

(newline character)

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 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

static local variable


register local variable


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

What is the value of z at the end of this program segment?

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

So how is this fixed


32

Type casting

Force y to be seen as an integer (cast)


int z; int x = 150; char y = 63; \\z is a signed 16-bit integer \\declare and initialize x \\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
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

Você também pode gostar