Beruflich Dokumente
Kultur Dokumente
BASIC
Training material
EMBEDDED C
Contents
Contents.........................................................................................................................................................................2
1.
Introduction ...........................................................................................................................................................4
Variables ................................................................................................................................................................9
4.
Decision making and branching in C program: if, else, goto, switch statements ....................................................... 15
EMBEDDED C
Simple if-else statements ........................................................................................................................................... 15
else-if ladder statements ............................................................................................................................................ 15
goto statement ........................................................................................................................................................... 16
switch statement ........................................................................................................................................................ 16
7.
EMBEDDED C
1. Introduction
C is a powerful and effective programming language. We can create System software,
Embedded hardware and application software by programming with C. C is
popularly termed as the successor of basic combined programming language known as
B. The B language was implemented in 1960 at Cambridge University. Embedded C
programming is an essential tool for all electronic engineers and developers.
Development of C language
The development of C language is directly related to the Unix operating
system development. C was invented by Dennis Richey and Barian.W in 1972 at Bell
laboratory. C language is compatible with Windows, Dos and Unix operating systems.
The Unix operating system is mainly used for C language development. The coding
done in Unix and the coding method of C is almost similar in structure. Unix operating
system was also developed from Cambridge university.
Importance of C language
C programming language has accurate frame structure, so we can call it as a
structural programming language. There are wide varieties
of functions and operators available in C, it is possible to develop complex
programs, system software, embedded hardware and application software by
manipulating these operators and functions. Group of programs that helps the
working of a computer is known as system software, for example- Compiler,
Interpreter, Operating system, etc. Application software is nothing but a group of
programs that control a single application only; for example MS Word, MS Power
point, etc. C programming is a free language, that is it can run be in any computer
as well. Also the portability feature makes C language more popular. The programs
written in C language are very efficient and effective. For example to add 1 to 10
numbers using C language, it takes lower time as compared to that of written in
Basic.
EMBEDDED C
Method of Coding in C
In normal case we are using small letter English alphabets for program coding,
but capital letters can also be used. Capital letters are often used for defining
some constants or variables. Another symbol for C language coding is {}
brackets. It is used to include the group of codes together while a program is
executing. This helps the programmer to easily identify the faults and errors in the
program. We can write the coding statements in any row, because C is an
independent programming language. Consider an example, a= b+c;
d=e-f;
i=a*b;
Above statements can also be written in single line as,
a=b+c; d=e-f; i=a*b;
It is important to provide semicolon ; at the end of any statement.
Stages of a C program
Programming language is a medium for communication between a computer and a
user. Before studying C programming we must be aware of the different symbols,
words, syntax etc. used in C language. The following are the different stages in a C
program.
EMBEDDED C
Compiling a program
After the development of a program, we must convert it in to machine level
language from high level language because computer knows machine level
language only. The media for this conversion is called as compiler. Compiler is
simply a translating program, it translate High level language to machine level
language. The high level language programs written by the programmer are
called source codes. These source codes are then converted to machine level language
using compiler, and then it is known as objective program. A compiler is capable of
identifying the syntax errors in the program but it cant identify the logical
mistakes.
EMBEDDED C
1
2
3
4
5
6
7
8
9
10
11
12
Documentation section
Linkage section
Global variable declaration section
Main()
{
Local variable declaration part;
Execution part;
}
User defined functions()
{
..........;
}
Documentation section
Name of program, name of programmer, date etc. are included in the documentation part. These are non
executable. To start and stop documentation we are using /* and */ command. For example,
1 /*This program is done by i-St@r*/
2 /*Written date xx-06-2012*/
Linkage section
In this section we provide instructions for linking different functions from the system library to the compiler.
This section is also known as Pre Processing stage, because this will run before the main() at the time of
execution. The file name starts with # symbol. For example,#include. Here we included the library
EMBEDDED C
function stdio.h in our program. stdio.h contain standard input output functions. Without the stdio.h header
file the commands printf(), scanf() etc will not work.
Int a,b,c
Main()
{
a=10;
b=2;
}
Function1()
{
c=a+b;
}
Here the variables a, b, c are global variables. The main() and function1() can also access these variables.
Main() function
We can group some codes together, these grouped program codes are called functions. Main()is the entry
function of any program. It indicates the starting of program. There are two sections in main function(),
1.Declaration part
In this part we declare all the variable used in the main() program. The variables cannot be accessible from
outside of main() function. For example
1
2
3
4
5
Main()
{
int a,b,c;
. ;
}
2. Executable part
Different operations are included in this part. For example
1 Main()
2 {
3 int a,b,c;/*Declaration part*/
4 a=b+c; /*Execution part*/
EMBEDDED C
5 }
We can write main() function once in a program, so main() works as a built in function.
The user combines all other functions in main() section.
3. Variables
Variable is used to store a value. This value will change according to different situations
while the program is executing.
Rules for a variable
o
Variable name should not be a keyword eg: int, float, if, else, etc.
EMBEDDED C
Arithmetic operators
Logical operators
Relational operators
Assignment operators
Conditional operators
10
EMBEDDED C
Special operators
Arithmetic operators
Arithmetic operators are commonly used operator in programming languages. C
programming language consists of Binary arithmetic and Unary arithmetic. The
data, which is operated by the operator, is called operand. In Unary operation there
will be only one operand (eg: -1Negative number). To perform a binary
arithmetic, we need at least 2 operands. Different arithmetic operators are given
below:
Logical operators
Logical operators are also called Boolean operators. These operators are used
to check two or more conditions. Boolean operators are followed:
Relational operators
Operators used to compare two values according to their relation are called Relational
operators. These are also called Comparison operators. For performing relational
operation we must need two operands, then only we can illustrate and check the
11
EMBEDDED C
Assignment operators
Assignment operators are used to assign a value to a variable. = is used as assignment
operator. For example i=10;
Conditional operators
These operators work according to some condition. The syntax is given by
i=expression1? Expression2:expression3
Eg: i=a>b?a:b;
The meaning of above statement is if a>b, then i=a, else i=b
12
EMBEDDED C
i=i+1;
i; meaning i=i-1;
Special operators
Comma operator (,) and Size of operator are the special operators in C.
Size of operator
For calculating the length of a variable we are using size of operator. It will calculate
thenumber of bytes in that variable.
Syntax
i=size of(variable);
13
EMBEDDED C
Scanf()
We can read any value using this function.
Syntax:
scanf(%d,&a);
Meaning:
Here the compiler accept a value (integer type) from the keyboard and it
is saved in theaddress of a. Here %d indicates that the value is integer data type. To
read other data types refer the following:
%d reads Integer data type
%f reads Floating point data type
%c reads Single character
%s reads String value
%o reads Octal integer
%x reads Hexadecimal integer
printf()
Used to display an output data in standard output device.
Syntax:
printf(i-St@r Electronics);
Above statement displays i-St@r Electronics on the monitor
Another syntax:
printf(%d,a); It displays the value of variable a in the monitor.
14
EMBEDDED C
Decision making and branching in C program: if, else, goto, switch statements
We can arrange the execution sequence of a program in different ways using decision
making and branching statements. These instructions are also called as control
statements.
if(expression 1)
{statement 1;}
else if(expression 2)
{statement 2;}
else if(expression 3)
{statement 3;}
else
{statement 4;}
15
EMBEDDED C
goto statement
It is used in unconditional branching. The syntax of goto statement is given below:
1
2
3
4
5
6
7
8
9
Goto Label;
Statement 1;
Statement 2;
;
;
Statement n;
Label:
Statement x;
Statement y;
switch statement
This statement is used for multiple branching. Here the branching takes place
Switch(expression)
{
Case 1: statement 1; break;
Case 2: statement 2; break;
;
Default: Statement; break;
}
7. Loops in C programming
The statements which appear in the source code only once but execute many
times, such kind of statements are called loops. Repeated executions of certain
statements are popularly known as loops. Almost all the programming languages
support looping instructions. In C program while, do while, for are the looping instructions.
16
EMBEDDED C
For example, consider you want to calculate the sum of 1 to 10 numbers; the program
will be like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
main()
{
int n=1,sum=0;
loop:
sum=sum+n;
if(n==10)
{
Goto out:
}
Else
{
N=n+1;
Goto loop;
}
Out:
Printf(\nSum = %d,sum);
}
In the above program the statement sum=sum+n will execute 10 times. A loop will
execute continuously till it satisfy the loop exit condition.
The following processes are included in a loop.
o
while loop
It is an entry controlled loop, the loop body will execute till the test condition is false.
Thesyntax of while() loop is given below,
1
2
3
4
17
While(test condition)
{
Body of the loop;
Increment ore decrement;
SWASTIKA RESEARCH & TRAINING INSTITUTE
EMBEDDED C
5 }
#include<stdio.h>
#include<conio.h>
Main()
{
Int n=1,sum=0;
While(n<=10)
{
Sum=sum+n;
N++;
}
Printf(\nsum= %d,sum);
}
If the test condition is always true, indefinite loop will arise. That is the body will
execute without break. This method is used in any embedded C
programming. while(1) is commonly used to create indefinite loops. So while(1) is an
essential part of any embedded C programming.
dowhile loop
This is an exit controlled loop, because the loop body will execute at least
once even if the test condition is false.
Syntax:
1
2
3
4
5
Do
{
Body of the loop;
}
While(test condition);
When the program control reaches the do, the body will execute once, it will verify the
test condition only after the first execution of loop body.
for loop
It is somewhat efficient entry controlled loop in C program.
Syntax:
18
EMBEDDED C
1
2
3
4
We are initializing a counter variable (loop control variable) i=0 or 1, something like that.
After the execution, the value of loop control variable is either incremented or decremented, again
validates the test condition.
If the condition is true, to body will execute, and this process will repeat.
#include<stdio.h>
#include<conio.h>
Main()
{
Int sum=0;
For(n=1;n<=10;n++)
Sum=sum+n;
}
#include<stdio.h>
#include<conio.h>
main()
{
float a,b,s; /*Declaring 3 floating point variables*/
clrscr(); /*Clear screen*/
printf("Enter the 1st number= ");
19
EMBEDDED C
20
EMBEDDED C
Step 2:: Extend the editor window by clicking in the arrow mark shown
21
EMBEDDED C
22
EMBEDDED C
23
EMBEDDED C
24
EMBEDDED C
Now you can see ISTAR.EXE application file. Double click on the file, it is your stand alone EXEfile.
25
EMBEDDED C
GPIO Introduction
The GPIO line of any microcontroller can be used mainly to perform two things
26
EMBEDDED C
The digital signal can be of TTL / CMOS logic depending up on the microcontroller .In
general, the microcontrollers are provided with GPIO lines except for some
microcontrollers which are very application specific and may not have GPIO lines.
The process of controlling GPIO lines for most of the controllers are similar in nature.
There are mainly two configurations for any GPIO
which you intend to use in performing certain functions. Same way when you
wish to generate a digital signal then the GPIO will be configured as an output and the
GPIO output can generate a digital signal, which can be fed to an external device.
Now let us assume that there is a circuitry which gives a digital signal to microcontroller
when ever the battery is charged. So, microcontroller reads the digital signal from the
battery charger and then generates a digital signal to blink an LED indicator.
Configuring GPIO
The configuration of GPIO for various microcontrollers have almost similar procedure,
but the procedure is not completely same for all microcontrollers. There may be few
more steps to be considered, depending on the microcontroller. In this document the
common configuration procedure is explained.
27
EMBEDDED C
28
EMBEDDED C
for (i=0;i<50000;i++)
{
/* Set the GPIO bit data/port register as HIGH */
PORTB = 0x01;
}
}
return 0;
}
/* End of Program */
Result
LED blinks continuously as long as the microcontroller is powered ON.
29