2. What is Embedded Systems
An embedded µc is a chip which has a computer processer
with all it‟s support function memory and i/p o/p built in
the device.
It includes Hardware and Mechanical parts.
Necessity
Use in daily life.
3.
4. INTRODUCTION OF ‘C’
• C is a general purpose programming language.
• ‘C’ language is developed at AT & T’s
• Bell Laboratories of USA in 1972.
• Designed & written by Dennis Ritchie.
• C is relable,simple & easy to use.
• Applcation:-mobile phones
Computer games
Steps in learning C
5. Constant & Variable
•A variable can be considered as a name given to any memory location.
Example: int i;
• Here „i‟ is a name assigned to a particular memory location.
•A constant is a quantity that does not change.
Example: const int i = 500;
Keywords
•The keyword can‟t be used as variable names.
•The keywords are also called “Reserved Words”.
•There are 32 keywords available in C.
•Some example of keywords:
auto, break, case, char, const, while.
Instructions
There are basically four types of instructions in C.
•Declaration Instruction
•Input / Output Instruction
•Arithmetic Instruction
•Control Instruction
6. Data type declaration
There are only a few basic data types in C.
1. Integer
2. Float
Data Type Range Bytes Format
3. Double
4. Character signed char -128 to + 127 1 %c
unsigned char 0 to 255 1 %c
short signed int -32768 to +32767 2 %d
short unsigned int 0 to 65535 2 %u
long signed int -2147483648 to +2147483647 4 %ld
long unsigned int 0 to 4294967295 4 %lu
Float -3.4e38 to +3.4e38 4 %f
Double -1.7e308 to +1.7e308 8 %lf
long double -1.7e4932 to +1.7e4932 10 %Lf
7. Operators
•Arithmetic Operators •Assignment Operators •Increment / Decrement Operators
multiply Operator Example Meaning Operator Meaning When?
*
= X=5 X=5 count++ count = count + 1; After use
/ divide
+= X += 5 X=X+5 ++count count = count + 1; Before
% reminder –= X–=5 X=X–5 use
/= X /= 5 X=X/5 count-- count = count – 1; After use
+ Add
*= X *= 5 X=X*5
Subtract
--count count = count – 1; Before
- %= X %= 5 X=X%5
use
Logical Operators Bitwise Operators Exponentiation Operators
& bitwise AND Exponentiation is not written as x**2 or x^2
! (not) | bitwise OR C does not have an exponentiation operator.
a != b is true if a and b are not equal ^ bitwise X-OR You can use the math function pow(a, b)
&& (and) << Left Shift You must include <math.h> in source code
5<6 && 7>4 is true, but
>> Right Shift
5>6 && 7>4 is not true (i.e. false)
|| (or)
~ one‟s complement
5<6 || 7>4 is true, and
5>6 || 7>4 is also true
8. CONDITIONAL STATEMENT
•The if-else statement
•The if statement
(a) if ( condition ) (a) if ( condition ) (b) if ( condition ) (c) if ( condition )
do this ; do this ; do this ; {
(b) if ( condition ) else else if ( condition )
{ do this ; { do this ;
do this ; if ( condition ) else
and this ; do this ; {
} else do this ;
{ and this ;
do this ; }
•The conditional operators and this ;
expression 1 ? expression 2 : expression 3 }
} else
char a ; } do this ;
int y ;
scanf ( "%c", &a ) ;
y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;
Here 1 would be assigned to y if a >=65 && a <=90
evaluates to true, otherwise 0 would be assigned.
9. LOOPS 1.Using a while statement
2.Using a for statement
3.Using a do-while statement
• As a rule the while must test a condition
1.The while Loop that will eventually become false, otherwise the
initialise loop counter ; loop would be executed forever, indefinitely.
while ( test loop counter using a condition ) main( )
{ {
do this ; int i = 1 ;
and this ; while (i <= 10 )
increment loop counter ;
printf ( " %dn", i ) ;
}
}
• The statements within the loop may be a single line or a block of statements. In the first
case the parentheses are optional. For example,
while ( i <= 10 )
i = i + 1 is same as
while ( i <= 10)
{
i=i+1;
}
10. The for Loop
The general form of for statement is as under: PROGRAM FOR CALCULATE
for( initialise counter ; test counter ; increment counter ) RATE OF INTEREST
{ main ( )
do this ; {
and this ; int p, n, count ;
and this ; float r, si ;
} for ( count = 1 ; count <= 3 ; count = count + 1 )
{
Thus the following for loops are not ok. printf ( "Enter values of p, n, and r " ) ;
for ( i = 10 ; i ; i -- ) scanf ( "%d %d %f", &p, &n, &r ) ;
printf ( "%d", i ) ; si = p * n * r / 100 ;
for ( i < 4 ; j = 5 ; j = 0 ) printf ( "Simple Interest = Rs.%fn", si ) ;
}
printf ( "%d", i ) ;
}
for ( i = 1; i <=10 ;
printf ( "%d",i++ ) ;
for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
printf ( "%d", i ) ;
11. The do-while Loop
The do-while loop looks like this:
do
{
this ;
and this ;
and this ;
and this ;
} while ( this condition is true ) ;
Difference between while and do-while loop
{
do
main( )
{
{
while ( 4 < 1 ) printf ( "Hello there n") ;
printf ( "Hello there n") ; } while ( 4 < 1 ) ;
} }
12. FUNCTIONS
A function is a self contained block of statements that perform a task of some kind.
Every C program is a collection of these functions
main() “calls” the function message()
Example Control passes to the message() func.
#include <stdio.h> After execution of message statements, control returns to main and
void main() begin executing the code at the exact point where it left.
{ main becomes the “calling funtion”
message(); Fnction ucan be called from any other function
printf(“nUdaipur”); A function can call itself. It is called Recursion.
} A function can be called from any other function but
message() a function can’t be defined in another function.
{
printf(“SSCE”);
• General form of a function
} function name (argument declaration)
Output: {
SSCE local variables declaration
Udaipur executable statements
………………
………………
return(expression)
}
13. Function Types
1) Library Functions
2) User Defined Functions
Call by VExample : Call by Value
• Methods used for declaration of arguments int square (int x)
• Method 1 {
function ( a, b, c ) return x*x;
int a , b , c ; }
Here declaration is done in separate statement.
This method is known as K & R method. int main ( )
• Method 2 {
function ( int a, int b, int c ) int num = 10;
int answer;
answer = square(num);
Passing the values to the functions printf(“Answer is : %d “, answer); // answer is 100
during function call printf(“Value of a is : %d “, num); // num will be 10
}
• Call by Value : • alue :
on calling a function we are passing on calling a function we are passing
values of variables to it. values of variables to it.
• Call by Reference : • Call by Reference :
Instead of passing the value, Instead of passing the value,
we pass the address of the variables we pass the address of the variables
14. ARRAY
Definition : A set of similar data types is called array.
An array is a collective name given to a group of similar quantities.
Each member in the group is referred to by its position in its group.
These similar elements can be all ints or all floats or all characters etc.
Array Declaration :
int marks[5]; Entering the Data into an array
Type of variables : Integer Enter marks of 5 students.
Dimension : 5
marks : Name of the variable for(i = 0;i<5;i++)
[ ] : Indication of an array {
• Each member in the array is referred to by its position. printf(“Enter the marks :”);
• Position is from 0 to 4. scanf(“%d”,&marks[i]);
Array Initialization }
Array Initialization :
Int marks[5] = {32,45,56,67,78} Reading Data from an array
Int marks[ ] = {32,45,56,67,78}
Array elements are always stored in
Find average of 5 student marks
contiguous locations.
for(i = 0;i<5;i++)
marks[0] marks[1] marks[2] marks[3] marks[4] sum = sum + marks[i];
32 45 56 67 78 average = sum/5;
2004 2006 2008 2010 2012
Example : arr0.c
15. Passing array element to a function
1. We can pass the array elements by value or by reference.
2. In call by value, we pass the values of array elements to the function.
3. In call by reference, we pass the address of array elements to the function.
• Call by Value • Call by Reference
main() main()
{ {
int i, marks[5] = {32,45,56,67,78} int i, marks[5] = {32,45,56,67,78}
for(i = 0;i<5;i++) for(i = 0;i<5;i++)
findsum(marks[i]); findsum(&marks[i]);
} }
findsum(int m) findsum(int *m)
{ {
static int sum; static int sum;
sum = sum + m; sum = sum + *m;
printf(“Sum = %d”,sum); printf(“Sum = %d”,sum);
} }
16. SWITCH CASE
• When decision from a number of choices is required, at that time switch – case – default is
used.
• Syntax for the switch - case structure is as follows :
Switch ( integer expression)
{
case value1 : action1 ;
case value2 : action2 ;
default : action3 ;
}
• Execution of the Switch
1. First the expression is evaluated.
2. After getting the value it is matched one by one with constant values in case statements.
3. When a match is found, it executes the statements following that case and all subsequent case
and default statements as well
17. Notes while using the SWITCH CASE
• Integer expression is any constant or expression that evaluates to an integer.
• The keyword case is followed by an integer or a character constant.
• Float is not allowed as case value.
• Each constant in each case must be different from all others.
• Order is not important while putting the case values.
• No enclosure is needed in case statements
• break statement must be used to prevent the execution of subsequent cases.
• Use of continue will not take the control to beginning of switch.
BREAK Statement
• Terminates the execution of the nearest enclosing do, for, switch, or while
statement in which it appears.
• Control passes to the statement that follows the terminated statement.
18.
19. A typical 8051 contains:
CPU with Boolean processor;
5 interrupts: 2 are external, 2 priority levels;
2 16-bit timer/counters;
programmable full-duplex serial port (baud rate
provided by one of the timers);
32 I/O lines (four 8-bit ports);
RAM and ROM/EPROM in some models.
21. Block Diagram
External interrupts
4K ROM
Interrupt program 128 bytes Timer0 Counter
Control code
RAM Timer1 Inputs
CPU
Bus Serial
4 I/O Ports Port
OSC Control
TXD RXD
P0 P2 P1 P3
Address/Data
23. Pins of 8051
Vcc(pin 40) :
Vcc provides supply voltage to the chip.
The voltage source is +5V.
GND(pin 20)- ground
XTAL1 and XTAL2(pins 19,18)
These 2 pins provide external clock.
Way 1:using a quartz crystal oscillator.
Way 2:using a TTL oscillator.
24. Pins of 8051
RST(pin 9):reset
It is an input pin and is active high (normally low).
The high pulse must be high at least for 2 machine cycles.
It is a power-on reset.
High logical state on this input halts the MCU and
clears all the registers.
Bringing this pin back to logical state zero starts the program a new
as if the power had just been turned on.
In another words, positive voltage impulse on this pin resets the
MCU.
Way 1:Power-on reset circuit
Way 2:Power-on reset with debounce
25. XTAL Connection to 8051
Using a quartz crystal oscillator
We can observe the frequency on the XTAL2 pin.
C2
XTAL2
30pF
C1
XTAL1
30pF
GND
26.
27. Program Memory
64K FFFFH
4K External
0FFFH ROM
EA=0
Internal
ROM
EA=1
0000H 0000H
PSEN
30. ALE Pin
The ALE pin is used for de-multiplexing the address and data by
connecting to the G pin of the 74LS373 latch.
When ALE=0, P0 provides data D0-D7.
When ALE=1, P0 provides address A0-A7.
The reason is to allow P0 to multiplex address and data.
32. Internal RAM and SFR (special function register) region
FFH FFH
Upper Accessible
Accessible SFR
128 by Indirect by Direct
addressing addressing
only Ports,
only
Timers,
Control
Registers,
80H 80H
Accumulator,
7FH
Stack
Pointer,
Accessible
Etc,.
by Direct &
Lower Indirect
128 addressing
00H
33. Program status Word
CY AC F0 RS1 RS0 OV -- P
CY PSW.7 Carry Flag
AC PSW.6 Auxiliary Carry flag
F0 PSW.5 Available to user for general purpose
RS1 PSW.4 Register bank selector bit 1
RS0 PSW.3 Register bank selector bit 0
OV PSW.2 Overflow flag
-- PSW.1 Not defined
P PSW.0 Parity flag. Set/cleared by hardware to
indicate an odd/even number of „1‟ in the
accumulator.
34.
35. Assembly Language
Data transfer instructions
Push and pop opcode
Data Exchange
Rotate operations
Logical operations
Arithmetic operations
Jump instructions
Call instruction
36. Embedded ‘C’ Language
Library files - #include<reg51.h>
Declaration data types - int, char, Sbit.
Body - statements
39. • LED connected to any port of microcontroller
LED • In figure 8 LED are connected to PORT-2 of uc
• Example:-
Program of running LED
#include<reg51.h>
run[]={0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81};
void delay()
{
long int i,k;
for(i=0;i<=1114;i++)
{
for(k=0;k<=0;k++);
}
}
void main()
{
int j;
for(j=0;j<=7;j++)
{
P2=run[j];
delay();
}
}
40. 7-SEGMENT
• 7-segment display use to indicate number up to 0-9
• 7-segment display has 10-pin in this 7-pin for 7-led (a,b,c,d,e,f,g) and 1 for VCC , 1 for ground.
• Example
#include<reg51.h>
sbit EN=P3^7;
sbit EN2=P3^6;
look_up[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay()
{
int j,k;
for(j=0;j<=1000;j++)
for(k=0;k<=100;k++);
}
void main()
{
int i;
EN=0;
EN2=1;
for(i=0;i<=9;i++)
{
P2=look_up[i];
delay();
}
}
41. LCD
•LCD is finding widespread use replacing LEDS
•The ability to display numbers, characters, and graphics
•Ease of programming for characters and graphics
• LCD pin description
PIN SYMBOL I/O DESCRIPTION
1 VSS - Ground
2 VCC - +5V power supply
3 VEE - Power supply to control contrast
4 RS I RS=0 to select command register,
RS=1 to select data register
5 R/W I R/W=0 for write, R/W=1 for read
6 E I/O Enable
7 DB0 I/O The 8-bit data bus
8 DB1 I/O The 8-bit data bus
9 DB2 I/O The 8-bit data bus
10 DB3 I/O The 8-bit data bus
11 DB4 I/O The 8-bit data bus
12 DB5 I/O The 8-bit data bus
13 DB6 I/O The 8-bit data bus
14 DB7 I/O The 8-bit data bus
42. • LCD Command Codes
HEX CODE Command to LCD Instruction Register
0x01 Clear display screen
0x02 Return home
0x04 Decrement cursor (shift cursor to left)
0x06 Increment cursor (shift cursor to right)
0x05 Shift display right
0x07 Shift display left
0x08 Display off, cursor off
0x0A Display off, cursor on
0x0C Display on, cursor off
0x0E Display on, cursor blinking
0x0F Display on, cursor blinking
0x10 Shift cursor position to left
0x14 Shift cursor position to right
0x18 Shift the entire display to the left
0x1C Shift the entire display to the right
0x80 Force cursor to beginning to 1st line
0xC0 Force cursor to beginning to 2nd line
0x38 2 lines and 5x7 matrix
44. MATRIX KEYBOARD
• A 4x4 matrix connected to PORT-1
• The rows are connected to an output
port and the columns are connected
to an input port
• Keyboards are organized in a
matrix of rows and columns
• When a key is pressed, a row
and a column make a contact
• Otherwise, there is no
connection between rows and
columns
45.
46. • The 8051 has two timers/counters, they can be used either as
•Timers to generate a time delay
•Event counters to count events happening outside the microcontroller
•Both Timer 0 and Timer 1 are 16 bits wide
•Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two
separate registers of low byte and high byte
• Timer 0 Register
TH0 TL0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
• Timer 1 Register
TH1 TL1
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
47. •TMOD is a 8-bit register
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
• The lower 4 bits are for Timer 0
• The upper 4 bits are for Timer 1
In each case,
The lower 2 bits are used to set the timer mode
The upper 2 bits to specify the operation
•To generate a time delay
48. • Example to calculate 10ms delay
1. Divide crystal frequency by 12
crystal frequency is 11.0592Mhz/12=0.9216
2. T=1/f so 1/0.9216=1.085us
3. Divide desired time delay by 1.085us
10ms/1.085us=9259.25
4. Then perform 65536-9259=56277
5. Convert result into Hex i.e dbd5
6. Then set TH=db & TL=d5
• Example:-
#include<reg51.h>
void delay() void main()
{ {
int i; while(1)
for (i=0;i<=200;i++) {
{ P2=0xff;
TMOD=0x01; // 8-bit control word delay();
TH0=0xee; // timer 0 higher byte P2=0x00;
TL0=0x00; // timer 0 lower byte delay();
TR0=1; }
while (TF0==0); // if overflow flag is 0 so come out from loop }
TR0=0; // reset timer register
TF0=0; // reset overflow flag
}
}
49.
50. What is an interrupt ?
• Interrupt - Unexpected Function Call
• Hardware interrupts generated by external signal
• Interrupt is a signal indicating that the CPU should suspend it‟s
current task to service a designated activity
• It is some external event that overrides any current microcontroller
action and causes certain special program to get executed.
• It is used by devices to transfer data to the microcontroller without
wasting time.
51. Interrupt Service Routines
• CPUs have fixed number of interrupts
Every interrupt has to be associated with a piece of code called
“Interrupt Service Routine”, or ISR.
If interrupt-x is received by CPU, the ISR-x is executed
• CPU architecture defines a specific “code address” for each ISR, which is
stored in the,
• ISRs are basically “subroutines”, but they end with the RETI, instruction
instead of RET
• When an interrupt occurs, the CPU fetches its ISR code address from the
and executes it.
52. Interrupt Vectors
Symbol Address Interrupt Source
RESET 00H Power Up or Reset
EXTI0 03H External Interrupt 0
TIMER0 0BH Timer 0 Interrupt
EXTI1 13H External Interrupt 1
TIMER1 1BH Timer 1 Interrupt
SINT 23H Serial Port Interrupt
53. 8051 Interrupts
• There are 5 interrupts in the 8051.
Two external interrupts (INT0 and INT1), two timer interrupts (TF0 and
TF1) and one serial port interrupt (SI).
• Interrupts can be individually enabled or disabled. This is done in the IE
(Interrupt Enable) register (A8H).
IE is bit addressable.
• All interrupts correspond to bits in registers.
Therefore, it is possible to cause an interrupt by setting the
appropriate bit in the appropriate register.
The end result is exactly as if the hardware interrupt occurred.
54. Interrupt Enable Register
7 0
EA ES ET1 EX1 ET0 EX0
EA= 1; //enables interrupts
EA=1; EX0=1; //enable interrupts from EX0 only
IE= 0x87; //enable interrupts from EX0, ET0, and EX1
Interrupts MUST be turned on to work. Default is off
55. • Example
void main()
#include<reg51.h>
{
void delay()
IE=0x86;
{
IP=0x04;
int i,j;
//IT0=0x00;
for(i=0;i<=1000;i++)
IT1=0x01;
for(j=0;j<=100;j++);
TMOD=0x09; // 8-bit control word
}
TH0=0x3c; // timer 0 higher byte
void a() interrupt 1 //TIMER 0
TL0=0xb0; // timer 0 lower byte
{
TR0=1;
P2=0x77;
while(TF0==0); // if overflow flag is 1
delay();
so come out from loop
P2=0x88;
TR0=0; // reset timer register
delay();
TF0=0; // reset overflow flag
return;
}
while(1)
void add() interrupt 2 // EXTERNAL 1
{
{
P2=0xff;
P2=0x11;
delay();
delay();
P2=0x00;
// TCON = 0x10;
delay();
P2=0x22;
}
delay();
}
return;
}