[go: up one dir, main page]

0% found this document useful (0 votes)
8 views5 pages

Embedded C_Lecture 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Embedded C_Lecture 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Embedded C Programming Concepts

Lecture 3: Programming in Embedded C

3.1 Pre-processors and Macros


Pre-processor in ‟C‟ is a compiler/cross-compiler directives used by compiler/cross-compiler to
filter the source code before compilation/cross-compilation. The pre-processor directives are
mere directions to the compiler/cross-compilers on how the source file should be compiled/cross-
compiled. No executable code is generated for pre-processor directives on compilation.

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.

Pre-processors are grouped into 3 categories

1. File Inclusion Pre-processor directives


2. Compile control pre-processor directives
3. Macro substitution pre-processor directives

3.1.1 File Inclusion Pre-processor directives- These include external files


containing macro definitions, function declarations, constant definitions etc. to the current
source file.

„# include‟ is the pre processor directive used for file inclusion.

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.

Example-#ifdef, #ifndef, #else, #endif, #undef

#ifdef uses a name as argument and returns true if the argument is already defined

3.1.3 Macro substitution pre-processor directives- Macros are a means of


creating portable inline code. „Inline‟ means wherever the macro is called in a source
file it is replaced directly with the code defined by macro. It improves the execution
speed. The #define pre-processor directive is used for coding macros.

# define PI 3.1415 is an example of a simple macro function


3.2Constant declarations in Embedded C- In embedded applications the qualifier
(keyword) „const‟ represents a „Read only‟ variable. The different types of constant variables
used in embedded „C‟ application are explained below.

3.2.1 Constant data-Data held by a variable cannot be modified by application. The general
form of declaration

Const data type variable name (const float PI=3.1417)

Or

Data type const variable (float const PI =3.1417)

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

const int* x or int const* x

3.2.3 Constant pointer to data- Constant pointer has a significant importance in


Embedded C application. An embedded system under consideration may have various external
chips like, data memory, Real Time Clock (RTC), etc interfaced to the target
processor/controller, using memory mapped technique. The range of address assigned to each
chips and their registers are dependent on the hardware design. At the time of designing
hardware itself, address ranges are allocated to different chips and the target hardware is
developed according to these address allocation.

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;

char *const x= (char*) 0x3007;

3.2.4 Constant pointer to constant data: It is also extensively used in embedded


application. Typical uses are reading configuration data held at ROM chips which are memory
mapped at a specified address range, Read only status registers of different chips. For example

const char *const x;

char const* const x = (char*) 0x3007;


3.3The ‘Volatile’ Type Qualifier in Embedded C:The keyword „volatile‟ prefixed
with any variable as qualifier informs the cross-compiler that the value of the variable is subject
to change at any point of time( asynchronous) other than the current statement of the code where
control is at present.

Examples of variables subject to asynchronous modification

1. Memory mapped hardware register


2. Variables common to interrupt

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.

volatile data type variable name; (volatile unsigned char x;)

or

data type volatile variable name; ( unsigned char volatile x;)

3.4Delay Generation and Infinite Loops in Embedded C-Almost every embedded


application involves delay programming. It employs delay for

i) waiting for a fixed time interval till a device is ready

ii) inserting delay between displays updating to give user sufficient time to view the contents
displayed.

iii) bit transmission and reception in asynchronous serial transmission(I2C)

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.

i)Bitwise AND (Operator is „&‟)

ii) Bitwise OR (Operator is „│‟)

iii) Bitwise XOR (operator is „^‟)

iv) Bitwise NOT (operator is „~‟)

Setting and Clearing the bit field of flag register.

Let us assume flag is of 8 bits character variable and 1 is represented by “00000001”.Then

i) flag = flag │ 1; --will set the 0th bit always 1

ii) flag = flag│ (1<<6);

flag │= (1<<6);

flag │= 0x40 ;

All three instructions set the 6th bit of the flag

iii) flag & = ~ (1<<6);


flag &=0xBF;
The above instruction will clear the 6th bit of the character variable flag.

iv) flag ^ = (1<<6) ;


The above instruction toggles bit 6 of the flag.

----------------------------------------END OF LECTURE 3----------------------------------------------

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

You might also like