Embedded C_Lecture 3
Embedded C_Lecture 3
Advantage-They are very useful for selecting target processor/controller dependent pieces of
code for different target systems and allow a single source code to be compiled and run on
several different target boards.
3.1.2 Compile control pre-processor directives- These are used for controlling the
compilation process such as skipping compilation of a portion of code, adding debug
features, etc.
#ifdef uses a name as argument and returns true if the argument is already defined
3.2.1 Constant data-Data held by a variable cannot be modified by application. The general
form of declaration
Or
3.2.2 Pointer to constant data –It is a pointer which points to a data which is read only.
The pointer pointing to the data can be changed but the data is non-modifiable. Example
For example, let us assume that we have an RTC chip which is memory mapped at address range
0x3000 to 0x3010 and memory mapped address of register holding the time information is at
0x3007.This memory address is fixed and to get the time information we have to look at the
register residing at location 0x3007.But the content of the register located at address 0x3007 is
subject to change according to change in time. We can access this data by using a constant
pointer. The declaration will be
char *const x;
The „volatile‟ keyword informs the cross-compiler that the variable with „volatile‟ qualifier is
subject to asynchronous change and there by cross compiler turns off any optimization for these
variables.
or
ii) inserting delay between displays updating to give user sufficient time to view the contents
displayed.
If the delay employed is not accurate, the bits may be lost. The library function
delay_ms(),sleep(),and many more similar type of functions for different
microcontroller/processor provides delays in milliseconds with reasonable accuracy. By coding
also delay can be generated. Normally „for loops‟ are used for coding delays.
Infinite loops are created using various loop control instructions like while (), do while (), for and
goto labels.
1. while(1)
{
}
2. do
{
}
while (1)
3. for (; ; ;)
{
}
4. label: //Task to be repeated
//instruction
//instruction
goto label:
break; statement is used for coming out of an infinite loop.
while() loop is the best choice
„goto‟ is favorite choice of programming for migrating from Assembly to Embedded C.
3.5 Bit Manipulation Operations-Boolean variables required in embedded application are
quite often stored as char variables. A character variable is used to store 8 Boolean variables. If
the Boolean variables are packed for saving memory, depending upon the program requirement
each variable may have to be extracted and some manipulation (setting, clearing, inverting etc.)
needs to be performed on the bits. The following Bit manipulation operations are employed for
the same.
flag │= (1<<6);
flag │= 0x40 ;
Also the interested students are asked to have idea on recursive function, Iterative function,
recursion vs. iteration, Re-entrant function, Re-entrant vs. Recursive functions, Dynamic
memory allocation (malloc(), calloc(), realloc() etc.) from any standard „C‟ book.
Reference:
1. Introduction to Embedded Systems – Shibu K.V Mc Graw Hill
--------------------------STAY HOME,STAY UPDATED,STAY PREPARED------------------------