Embedded C Ppts
Embedded C Ppts
1/17/2012
www.thinnkware.com
C VS EMBEDDED C
1. 2. C is for desktop computers, but embedded C usually is for microcontroller based applications. C use the resources of desktop computers (memory, OS, etc), but Embedded C use only limited resources available in chip (limited RAM, ROM, ports, etc). Embedded C could be a subset of C. Usually in C what we develop would have headers like stdio.h, using which the system yields a output file feasible to that particular OS(windows, Linux etc), but when it comes to embedded C, everything is same except, you will get an output file which could be directly loaded in to the microcontroller. In C the output is an executable file, but in embedded C the output is a hexadecimal file. Libraries and headers used in C are different from that of Embedded C because the libraries used in embedded C differ in case of different microcontrollers.
www.thinnkware.com 2
3.
4. 5.
1/17/2012
1/17/2012
www.thinnkware.com
EXAMPLE OF C PROGRAM
/* This is my first C program */ #include <reg52.h> int main() { return 0; }
1/17/2012
www.thinnkware.com
UNDERSTANDING C SYNTAX
All the syntaxes have been defined in C when it was developed. Syntaxes are to be followed very strictly while writing C program as they as the major source of error while compiling any C code. Each language has a syntax and semantics to work on and write programs in it. These syntax are checked by the compiler in the highest possible way so that at run time there should not b any issues in the program. A .c file is called a module. Many programs are composed of several .c files and libraries that are linked together during the compile process to create a single executable file.
1/17/2012
www.thinnkware.com
RESERVED WORDS IN C
1. C programs are constructed from a set of reserved words which provide control and from libraries which perform special functions. The basic instructions are built up using a reserved set of words, such as main, for, if, while, default, double, extern, for, and int, to name just a few. 2. You cannot use default, for example, as the name of a variable. An attempt to do so will result in a compilation error. 3. If you use a word which has already been adopted in a library, there will be a conflict between your choice and the library.
1/17/2012
www.thinnkware.com
STRUCTURE OF C PROGRAM
The form of an ideal embedded C program should be: 1. Preprocessor commands. 2. Functions. 3. Declarations 4. Variables 5. Statements
1/17/2012
www.thinnkware.com
VARIABLES IN C
1. While making codes we need to store some values which keeps on varying from the beginning to the end of the execution of code. Depending on these values various decisions can be taken to change the execution flow. These varying quantities are known as variables. 2. The name of these variables are actually what we call as declaration. The names given to these variables must not be a reserved word of C libraries. 3. It will cause an error if you keep the same name for a variable and reserved word.
1/17/2012
www.thinnkware.com
1/17/2012
www.thinnkware.com
1/17/2012
www.thinnkware.com
10
1/17/2012
www.thinnkware.com
11
SCOPE : LOCAL
Local These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. They are declared and defined in the function only. For e.g.: int main() { int x, x = 10; x++; } Here x is a local variable which is local to main function. Any function outside main cannot access this x variable.
1/17/2012 www.thinnkware.com 12
SCOPE : GLOBAL
Global These variables can be accessed (i.e. known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled. They are declared and defined out of every function. They are also like preprocessor variables. They are defined outside of main and other functions. int y = 10; int main() { int x = 11; x++; y++; } Here y is a global variable and x is a local variable. Y has been declared and defined outside any function and hence can be used any where in the program as shown here in main.
1/17/2012 www.thinnkware.com 13
DATA TYPES IN C
The most basic concept that is used in C programming is Data Types. The variables are the most used and important part of a C code, as they hold data based upon which decisions can be taken. But a limitation comes in size of the variable as some dont have memory size large enough to hold a big value or vice versa. This size of the variable for any variable is actually told by the Data type of the variable. Data type is written with the declaration. There are different data types in C. Different data types, their size and value range that can be stored in them is mentioned here.
Name char int float double
1/17/2012
Range unsigned: 0 -255 unsigned:0-65535 +/- 3.4e +/- 38 (~7 digits) +/- 1.7e +/- 308 (~15 digits)
16
int a, b, c, d ; a = b = c = 10 ; or int a = b = c = d = 10 ;
1/17/2012
www.thinnkware.com
1/17/2012
www.thinnkware.com
19
1/17/2012
www.thinnkware.com
20
* / % + - >> << < > <= left-to-right >= == != & ^ | && || ?: = += -= *= /= %= >>= <<= &= ^= |= , right-to-left right-to-left left-to-right
1/17/2012
www.thinnkware.com
21
1/17/2012
www.thinnkware.com
22
23
WHILE EXAMPLE int main( ) { int i = 1 , j = 1 ; while ( i++ <= 100 { while ( j++ <= 200 ) { printf ( "%d %d\n", i, j ); }} return 0; }
FOR EXAPMLE int main( ) { int i, j ; for ( i = 1 ; i <= 10 ; i++ ) { printf ( "\n%d %d\n", i, j ) ; } return 0; }
The difference between the while and the do-while loops is the location of the test. With a while loop the test is made before the statement that forms the body of the loop is executed; it is possible that the statement is never executed. With the dowhile loop, the statement will always be executed at least once; the value of the expression then determines if the statement is executed again.
1/17/2012 www.thinnkware.com 25
SWITCH STATEMENTS: Switch statements can be used in place of conditional if statement. BREAK AND CONTINUE STATEMENTS: break statement helps the program to control branch out of the switch statement. continue statements can only be used in loop statements to skip executing statements after the continue statements for the iteration for which such conditions are implied in a c-program. GOTO STATEMENTS:
goto makes the c-program unstructured. goto can help the program control to branch from one program section to anywhere within the program.
1/17/2012
www.thinnkware.com
26
A venture of KC Robotics & Embedded Pvt. Ltd. GOTO STATEMENT EXAMPLE int main( ) { int goals ; printf ( "Enter the number of goals scored); scanf ( "%d", &goals ) ; if ( goals <= 5 ) goto sos ; else { printf ( "About time soccer players learnt C\n" ) ; printf ( "and said goodbye! adieu! to soccer" ) ; exit( ) ; /* terminates program execution */ } sos : printf ( "To err is human!" ) ; return 0; }
www.thinnkware.com 27
SWITCH CASE EXAMPLE int main( ) { int i = ; scanf ( "%d", &i ); switch (i) { case 1 : printf ( "I am in case 1 \n" ) ; break; case 2 : printf ( "I am in case 2 \n" ) ; case 3 : printf ( "I am in case 3 \n" ) ; default : printf ( "I am in default \n" ) ; } return 0; }
1/17/2012
BREAK EXAMPLE int main( ) { int i = 1 , j = 1 ; while ( i ++<= 100 { while ( j++<= 200 ) { if ( j == 150 ) break ; else printf ( "%d %d\n", i, j ); } return 0; }
1/17/2012
CONTINUE EXAMPLE int main( ) { int i, j ; for ( i = 1 ; i <= 2 ; i++ ) { for ( j = 1 ; j <= 2 ; j++ ) { if ( i == j ) continue ; printf ( "\n%d %d\n", i, j ) ; } } return 0; }
www.thinnkware.com
28
1/17/2012
www.thinnkware.com
30
FUNCTIONS
The ANSI C style of declaring and defining a function is: Return_type Function_name (parameters list) { statements.. /*Function definition*/ } A function definition is where the function name, parameters, code and return type are specified. A function declaration is where the name and return type of a function are given. The definition of a function includes a declaration of that same function implicitly. A function can be declared many times (as long as the declarations declare the function to be of the same type) but can only be defined once. Declarations of functions are sometimes necessary to appease the compiler, which always assumes, if the information is not available, that all functions return int.
1/17/2012 31
www.thinnkware.com
FUNCTIONS
The ANSI C standard introduces function prototypes. An example is given below. It also allows function definitions to be written in the same form as the new prototypes. For e.g: double minimum(double, double); /* prototype of minimum() */ int main() { printf("%f\n", minimum(1.23, 4.56)); /*Function called*/ return 0; } double minimum(double x, double y) /* definition of minimum() */ { if (x < y) return x; else return y; }
1/17/2012 www.thinnkware.com 32
MACROS
Macros can be a useful way to customize our interface to C and make our code easier to read and less redundant. Macros are used to define constants. Macros can be used to improve code readability. They work at the time of preprocessing in compilation stages. Prior to compilation of the code, it replaces the name of the macros with the values defined for the respective names. For e.g: #define PI 3.14159 They are globally declared. So, when compiler works it searches the word PI in the whole code and replaces that word with 3.14159. Macros are faster in processing. Complex equations can also be defined as macros.
1/17/2012
www.thinnkware.com
33
MACROS
Examples of macros: #define SQ(x) ( (x) * (x) ) #define MIN(x,y) ( ((x) < (y)) ? (x) : (y) ) In effect you can write often used simple functions as a macro rather than a function, the advantage is speed (there is no function call overhead) and no type definition is necessary (it can be reused for ints, doubles, floats etc.). The disadvantages are that if you use the macro several times, then the code will appear in your program several times, and there can be side effects caused by not creating new copies of the parameters (which is what happens when a function is called). When defining a macro there cannot be any spaces between the macro name and the parameter list; when calling a macro no such restriction holds. A general mistake which is made while defining functions in macros is not using brackets. For e.g: #define square(a+b) a+b * a+b This will be interpreted as a+(b*a)+b So make sure to put brackets at correct places as #define square(a+b) (a+b) * (a+b)
1/17/2012 www.thinnkware.com 34
POINTERS IN C
Pointers are special kind of variables used in c to store addresses of another variable. Pointers are made to access the variables by their addresses rather than their name. Pointers are actually data-types which points to another variable of same data type. The * and & Operators : & : gives the address of something in memory, that is it generates a pointer to the object. * : gives what is pointed at by a pointer (called dereferencing). For e.g: int i = 0 , *j; j = &i; Here pointer j is pointing to the variable i. That means j holds the address of i. j = memory address of variable i. *j = integer value of variable i.
1/17/2012 www.thinnkware.com 35
POINTERS ARITHMETIC
Pointer arithmetic is same as that of a variable. For e.g: *j = *i + *j; *a = *a/ *b; (a = *a+ b This gives an error). char * p, s[100]; int * a, b[100]; double * f, g[100]; p = s; /* p points to s[0] */ a = b; /* a points to b[0] */ f = g; /* f points to g[0] */ p++; /* p points to s[1] */ a++; /* a points to b[1] */ f++; /* f points to g[1] */ The size of a pointer is always 4 bytes irrespective of the data type it is pointing to. This is because pointer holds the address of the variable not the value of it.
1/17/2012 www.thinnkware.com 36
Typecasting
This is a method which is used to change the data type of a variable to another data type. For e.g: int i = 0; char c = c; i = (int) c; Here the data type of the character is changed into integer. The character c is stored into integer i. Thus its actually the ASCII value of c that is stored into integer i. An integer can also be stored in a character. But size of an integer is 2 bytes and size of a character is 1 byte. Hence while storing an integer into a character, data gets lost because character cant store an integer of 2 bytes. Similarly pointer datatype can also be changed like this. For e.g: int* i, char* j; j = (int*) i;
1/17/2012 www.thinnkware.com 37
1/17/2012
www.thinnkware.com
39
1/17/2012
www.thinnkware.com
40
1/17/2012
www.thinnkware.com
42
1/17/2012
www.thinnkware.com
43
MALLOC
Malloc: The memory allocated using this method may be contiguous may not be contiguous. The memory allocated has garbage values. For e.g: id we have to allocate a memory of 6 integers, int* memory; memory = (int *) malloc (6 * sizeof (int) ); Here to store 20 integers, typecasting is used before malloc because malloc returns void*. So we have to typecast it to the pointer data type that we have to store. In case of malloc the memory assigned to malloc in non contiguous form the memory pointer points to the first address of the memory space. This memory space then points to the next memory address allocated to the malloc. In this way it keeps a track of the individual elements of the array. A pictorial representation gives you a clear idea.
1/17/2012
www.thinnkware.com
44
MALLOC
1/17/2012
www.thinnkware.com
45
CALLOC
Calloc: The memory allocated using this method is always contiguous. The memory allocated is initialized with value 0. Thats why calloc() is faster as compared to malloc(). For e.g: If we have to store 6 integers, int * memory; memory = (int*) calloc (6, sizeof ( int) );
1/17/2012
www.thinnkware.com
46
FREE
Free: It is the method to free the memory allocated using malloc() or calloc(), so that the memory can be used further for allocation. For e.g: Free (memory); It will free the memory allocated during previous malloc() or calloc() operations.
1/17/2012
www.thinnkware.com
47
1/17/2012
www.thinnkware.com
48