SlideShare a Scribd company logo
1 of 42
Jay Pandit 
12MT06PED016 
1
Contents 
 Keil 
 Proteus 
 Some basic techniques for C programming 
 Application 1 
 Application 2 
 Application 3 
2
3
How to start 1st Keil project.. 
Install keil software from the installation file. 
Start keil program from start menu or from desktop 
shortcut. 
4
Select New 
μvision Project 
5
Do not 
change the 
file type!!! 
6
Atmel 
7
AT89C51 
8
Create 
New File 
9
Save file 
as .c or 
.asm 
Right Click and select 
Add files to group 
Source 1 
10
Click Add 
11
12
Select Options for 
Target 
13
14
15 
Any one want to try this ?????
Before we start programming 
16 
Data Types in Embedded C 
Data Type Size in Bits Data range 
Unsigned char 8-bit 0 to 255 
(Signed) char 8-bit -128 to 127 
Unsigned int 16-bit 0 to 65535 
(Signed) int 16-bit -32768 to 32767 
sbit 1-bit SFR bit Addressable 
only 
Bit 1-bit RAM bit-addressable 
only 
sfr 8-bit RAM addresses
Before we start programming 
17 
Interrupt numbers in C 
Interrupt Name Number used by 8051 
External interrupt 0 INT0 0 
Timer interrupt 0 TF0 1 
External interrupt 1 INT1 2 
Timer interrupt 1 TF1 3 
Serial communication RI+TI 4
18 
For loop 
char i; 
for(i=0;i<10;i++) 
{ 
} 
Write your 
code here
19 
Infinite loop 
while(1) 
{ 
} 
Write your 
code here
Assembly instructions in C code 
20 
#pragma ASM 
Write your 
code here 
#pragma END ASM
21 
Functions 
int addition(int,int); 
void main() 
{ 
int i,j,k; 
i = 10; 
j = 10; 
k = addition(i,j); 
} 
int addition(int a, int b) 
{ 
int c 
c = a + b; 
return(c); 
}
Requirements from microcontroller 
Provide gate signals. 
What are your expectations 
from a microcontroller???? 
Variation in duty ratio of gate signals. 
Variation in frequency of gate signals. 
Sensing of voltage. 
Sensing of current. 
Sensing of speed. 
22
DC –DC Converter open loop 
 Gate signals are in the form of square wave 
 There may be change in duty ratio of gate 
signals. 
 There may be change in frequency of gate 
signals. 
23
Program 1 
24 
Problem statement : 
Write a program to generate a square wave on pin 1 of port 2 
Frequency of square wave = 1kHz 
Duty ratio = 50% 
Delay calculation : 
Assume that XTAL = 11.0592 MHz . 
 n = 500 μ /1.085μ = 460 
 Count = 65536 – n = 65076 
 Count in hex = FE34H 
 Load TH = FEH and TL =34H
Program 
25 
#include <reg51.h> 
sbit SW = P2^1; 
void delay(); 
void main() 
{ 
TMOD = 0x01; 
while(1) 
{ 
SW = 1; 
delay(); 
SW = 0; 
delay(); 
} 
} 
void delay() 
{ 
TH0 = 0xFE; 
TL0 = 0x34; 
TR0 = 1; 
while(!TF0) 
; 
TR0 = 0; 
TF0 = 0; 
} 
Lets go to Keil for trying this program
Program 2 
26 
Problem statement : 
Write a program to generate a square wave on pin 1 of port 2 
Frequency of square wave = 10kHz 
Duty ratio = variable. 
Time period of wave = 100 μsec 
We would be requiring two delays 
 One for on time. 
 Another for off time.
27 
Program 
#include <reg51.h> 
sbit SW P2^1 
void delay1(); 
void delay2(); 
extern char count; 
void main() 
{ 
count = 0; 
EA = 1; 
EX0 = 1; 
EX1 = 1; 
IT0 = 1; 
IT1 = 1; 
TMOD = 0x01; 
while(1) 
{ 
SW = 1; 
delay1(); 
SW = 0; 
delay2; 
} 
} 
void delay1() 
{ 
TH0 = 0xFF; 
TL0 = 210+count; 
TR0 = 1; 
while(!TF0) 
; 
TR0=0; 
TF0=0; 
}
28 
Program cont. 
void delay2() 
{ 
TH0 = 0xFF; 
TL0 = 210-count; 
TR0 = 1; 
while(!TF0) 
; 
TR0=0; 
TF0=0; 
} 
void external0() interrupt 0 
{ 
count++; 
} 
void external0() interrupt 1 
{ 
count--; 
} 
Lets try to simulate this program
Program 3 
29 
Problem statement : 
Write a program to generate a square wave on pin 1 of port 2 
Frequency of square wave = Variable. 
Duty ratio = 50%. 
Time period of wave = variable 
Hence, 
Count to be loaded in timer register = variable 
Requirements to be satisfied by the program 
User should be able to vary the frequency. 
Frequency raise and lower commands are given by user. 
There should be upper and lower limit on frequency.
30 
Solutions 
• Delay can be created using - Timers 
• User interface can be created by using - Interrupts 
How to satisfy these 
requirements using 
program???? 
• Limits can be put by using conditions like if-else
Program 
31 
#include <reg51.h> 
sbit SW = P2^1 
void delay(); 
extern unsigned char count; 
void main() 
{ 
count = 0x01; 
EA = 1; 
EX0 = 1; 
IT0 = 1; 
IT1 = 1; 
TMOD = 0x01; 
while(1) 
{ 
SW = 1; 
delay(); 
SW = 0; 
delay(); 
} 
} 
void delay() 
{ 
TH0 = 0xFF; 
TL0 = count; 
while(!TF0); 
TR0 = 0; 
TF0 = 0; 
}
32 
Program cont. 
void external0() interrupt 0 
{ 
if(count < 0xFA) 
count++; 
else 
count = 0xFA; 
} 
void external0() interrupt 1 
{ 
if(count > 0x01) 
count--; 
else 
count = 0x01; 
}
Requirements from microcontroller 
Provide gate signals. 
Variation in duty ratio of gate signals. 
Variation in frequency of gate signals. 
Sensing of voltage. 
Sensing of current. 
Sensing of speed. 
33
34 
How to sense analog signals like voltage, current??? 
Solution : Analog to digital converters. 
Limitations of ADC: 
It can read only positive voltage. 
It can read voltage between 0-5V. 
ADC can not read current. 
Solution : 
Current sensor produces proportional voltage output for 
input current. 
Voltage divider is used for sensing high voltage. 
Tachometer generates voltage signal proportional to speed 
Signal conditioning is required to sense negative voltages.
35 
ADC0804 chip 
ADC0804 is an 8-bit parallel ADC. 
It works with +5V voltage supply. 
It has a resolution of 8 bits. 
Conversion time depends on clocking signals applied to 
CLK IN pin, but it cannot be faster than 110μs.
36 
Pin details of ADC0804 
CS Chip select is an active low input used to activate chip. 
RD Used to read the converted data. Signal should be active low 
WR Start of conversion. Signal should be active low. 
CLK IN Input for clock 
INTR End of conversion 
Vcc Supply voltage +5V 
Vref/2 Kept open for reading voltage in range 0-5V. 
D0-D7 Digital data output pins.
37 
Relation of Vref/2 and Vin range 
Vref/2(V) Vin(V) Step size(mV) 
Not connected 0 to 5 19.53 
2.0 0 to 4 15.62 
1.5 0 to 3 11.71 
1.28 0 to 2.56 10 
Step size = Vin(max)/256
38 
How to calculate output 
Dout = Digital data output in decimal. 
Vin = analog input voltage. 
Dout = Vin/step size
39 
Program 4 
Problem statement: Using the ADC and current sensor 
read the value of current flowing in the branch of circuit, 
and display it on port3 
Solution: 
Current sensor ACS712ELCTR-20A-T is used. 
Current sensor can read current up to +/- 20A. 
Gain of current sensor (refer data sheet). 
ADC0804 is used. 
Internal clock is used.
40 
Program 
#include<reg51.h> 
sbit READ = P2^5; 
sbitWRITE = P2^6; 
sbit INTR = P2^7; 
void main() 
{ 
unsigned char value; 
P1 = 0xFF; 
INTR = 1; 
READ = 1; 
WRITE = 1; 
while(1) 
{ 
WRITE = 0; 
WRITE = 1; 
while(INTR); 
READ = 0; 
value = P1; 
READ = 1; 
P3 = value; 
} 
} 
Lets simulate this code.
Our achievements 
Provide gate signals. 
Variation in duty ratio of gate signals. 
Variation in frequency of gate signals. 
Sensing of voltage. 
Sensing of current. 
Sensing of speed. 
41
42

More Related Content

What's hot

32 bit ALU Chip Design using IBM 130nm process technology
32 bit ALU Chip Design using IBM 130nm process technology32 bit ALU Chip Design using IBM 130nm process technology
32 bit ALU Chip Design using IBM 130nm process technologyBharat Biyani
 
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Sivaranjan Goswami
 
Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 CardOmar Sanchez
 
Practical file
Practical filePractical file
Practical filerajeevkr35
 
AVR programming - BASICS
AVR programming - BASICSAVR programming - BASICS
AVR programming - BASICSRobotix 2011
 
Uart VHDL RTL design tutorial
Uart VHDL RTL design tutorialUart VHDL RTL design tutorial
Uart VHDL RTL design tutorialNabil Chouba
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED CAman Sharma
 
Digital Alarm Clock 446 project report
Digital Alarm Clock 446 project reportDigital Alarm Clock 446 project report
Digital Alarm Clock 446 project reportAkash Mhankale
 
Verilog VHDL code Decoder and Encoder
Verilog VHDL code Decoder and EncoderVerilog VHDL code Decoder and Encoder
Verilog VHDL code Decoder and EncoderBharti Airtel Ltd.
 
CODING IN ARDUINO
CODING IN ARDUINOCODING IN ARDUINO
CODING IN ARDUINOS Ayub
 
Microcontrollers ii
Microcontrollers iiMicrocontrollers ii
Microcontrollers iiKumar Kumar
 

What's hot (20)

32 bit ALU Chip Design using IBM 130nm process technology
32 bit ALU Chip Design using IBM 130nm process technology32 bit ALU Chip Design using IBM 130nm process technology
32 bit ALU Chip Design using IBM 130nm process technology
 
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
 
Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 Card
 
Uart
UartUart
Uart
 
Practical file
Practical filePractical file
Practical file
 
REPORT
REPORTREPORT
REPORT
 
Class9
Class9Class9
Class9
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
AVR programming - BASICS
AVR programming - BASICSAVR programming - BASICS
AVR programming - BASICS
 
W10: Interrupts
W10: InterruptsW10: Interrupts
W10: Interrupts
 
Uart VHDL RTL design tutorial
Uart VHDL RTL design tutorialUart VHDL RTL design tutorial
Uart VHDL RTL design tutorial
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
 
Digital Alarm Clock 446 project report
Digital Alarm Clock 446 project reportDigital Alarm Clock 446 project report
Digital Alarm Clock 446 project report
 
8155 GPPI
8155 GPPI8155 GPPI
8155 GPPI
 
Verilog VHDL code Decoder and Encoder
Verilog VHDL code Decoder and EncoderVerilog VHDL code Decoder and Encoder
Verilog VHDL code Decoder and Encoder
 
Lab8 s1
Lab8 s1Lab8 s1
Lab8 s1
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
Lpc 1768 timers
Lpc 1768 timersLpc 1768 timers
Lpc 1768 timers
 
CODING IN ARDUINO
CODING IN ARDUINOCODING IN ARDUINO
CODING IN ARDUINO
 
Microcontrollers ii
Microcontrollers iiMicrocontrollers ii
Microcontrollers ii
 

Viewers also liked

Applications of microcontroller(8051)
Applications of microcontroller(8051) Applications of microcontroller(8051)
Applications of microcontroller(8051) vijaydeepakg
 
Reeja b ed ict individual (1)
Reeja b ed ict individual (1)Reeja b ed ict individual (1)
Reeja b ed ict individual (1)REEJASHA
 
Keypad and dc motor
Keypad and dc motor Keypad and dc motor
Keypad and dc motor vijaydeepakg
 
Timer programming
Timer programming Timer programming
Timer programming vijaydeepakg
 
Reeja b ed ict individual (1)
Reeja b ed ict individual (1)Reeja b ed ict individual (1)
Reeja b ed ict individual (1)REEJASHA
 
Interrupt programming
Interrupt programming Interrupt programming
Interrupt programming vijaydeepakg
 
Dizee rascal analysis
Dizee rascal analysisDizee rascal analysis
Dizee rascal analysisLD7
 
Construction
ConstructionConstruction
ConstructionLD7
 
Task 5 + 6
Task 5 + 6Task 5 + 6
Task 5 + 6LD7
 
Https _doc-0o-c4-apps-viewer.googleusercontent
Https  _doc-0o-c4-apps-viewer.googleusercontent Https  _doc-0o-c4-apps-viewer.googleusercontent
Https _doc-0o-c4-apps-viewer.googleusercontent vijaydeepakg
 
Sudhir tms 320 f 2812
Sudhir tms 320 f 2812 Sudhir tms 320 f 2812
Sudhir tms 320 f 2812 vijaydeepakg
 
Acls bolsillo 2010
Acls bolsillo 2010Acls bolsillo 2010
Acls bolsillo 2010nvklnd
 
Deploy django apps using docker
Deploy django apps using dockerDeploy django apps using docker
Deploy django apps using dockerThomas Kremmel
 
Cinéma - Les bonnes pratiques pour promouvoir un film sur Facebook
Cinéma - Les bonnes pratiques pour promouvoir un film sur FacebookCinéma - Les bonnes pratiques pour promouvoir un film sur Facebook
Cinéma - Les bonnes pratiques pour promouvoir un film sur FacebookBenjamin Martin
 
Diapo chap 09-travail-emploi-chômage (14-15)
Diapo chap 09-travail-emploi-chômage (14-15)Diapo chap 09-travail-emploi-chômage (14-15)
Diapo chap 09-travail-emploi-chômage (14-15)Philippe Watrelot
 
Brand content et evenementiel : pari gagnant
Brand content et evenementiel : pari gagnantBrand content et evenementiel : pari gagnant
Brand content et evenementiel : pari gagnantLabCom
 

Viewers also liked (20)

Applications of microcontroller(8051)
Applications of microcontroller(8051) Applications of microcontroller(8051)
Applications of microcontroller(8051)
 
Reeja b ed ict individual (1)
Reeja b ed ict individual (1)Reeja b ed ict individual (1)
Reeja b ed ict individual (1)
 
Keypad and dc motor
Keypad and dc motor Keypad and dc motor
Keypad and dc motor
 
Timer programming
Timer programming Timer programming
Timer programming
 
Reeja b ed ict individual (1)
Reeja b ed ict individual (1)Reeja b ed ict individual (1)
Reeja b ed ict individual (1)
 
Interrupt programming
Interrupt programming Interrupt programming
Interrupt programming
 
Dizee rascal analysis
Dizee rascal analysisDizee rascal analysis
Dizee rascal analysis
 
Construction
ConstructionConstruction
Construction
 
Task 5 + 6
Task 5 + 6Task 5 + 6
Task 5 + 6
 
12 mt06ped001
12 mt06ped001 12 mt06ped001
12 mt06ped001
 
Https _doc-0o-c4-apps-viewer.googleusercontent
Https  _doc-0o-c4-apps-viewer.googleusercontent Https  _doc-0o-c4-apps-viewer.googleusercontent
Https _doc-0o-c4-apps-viewer.googleusercontent
 
Sudhir tms 320 f 2812
Sudhir tms 320 f 2812 Sudhir tms 320 f 2812
Sudhir tms 320 f 2812
 
12 mt06ped007
12 mt06ped007 12 mt06ped007
12 mt06ped007
 
Lp 30
Lp 30Lp 30
Lp 30
 
12 mt06ped008
12 mt06ped008 12 mt06ped008
12 mt06ped008
 
Acls bolsillo 2010
Acls bolsillo 2010Acls bolsillo 2010
Acls bolsillo 2010
 
Deploy django apps using docker
Deploy django apps using dockerDeploy django apps using docker
Deploy django apps using docker
 
Cinéma - Les bonnes pratiques pour promouvoir un film sur Facebook
Cinéma - Les bonnes pratiques pour promouvoir un film sur FacebookCinéma - Les bonnes pratiques pour promouvoir un film sur Facebook
Cinéma - Les bonnes pratiques pour promouvoir un film sur Facebook
 
Diapo chap 09-travail-emploi-chômage (14-15)
Diapo chap 09-travail-emploi-chômage (14-15)Diapo chap 09-travail-emploi-chômage (14-15)
Diapo chap 09-travail-emploi-chômage (14-15)
 
Brand content et evenementiel : pari gagnant
Brand content et evenementiel : pari gagnantBrand content et evenementiel : pari gagnant
Brand content et evenementiel : pari gagnant
 

Similar to C Programming Techniques and Applications for Microcontrollers

m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for recordG Lemuel George
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACnanocdac
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Omkar Rane
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiUnmesh Baile
 
SIMPLE Frequency METER using AT89c51
SIMPLE Frequency METER using AT89c51 SIMPLE Frequency METER using AT89c51
SIMPLE Frequency METER using AT89c51 aroosa khan
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfSPonmalar1
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
MicrocontrollersII.ppt
MicrocontrollersII.pptMicrocontrollersII.ppt
MicrocontrollersII.pptSatheeshMECE
 
STM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosSTM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosps6005tec
 
8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf
8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf
8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdfShashiKiran664181
 

Similar to C Programming Techniques and Applications for Microcontrollers (20)

m.tech esd lab manual for record
m.tech esd lab manual for recordm.tech esd lab manual for record
m.tech esd lab manual for record
 
Keypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDACKeypad interfacing 8051 -NANOCDAC
Keypad interfacing 8051 -NANOCDAC
 
Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148Analog To Digital Conversion (ADC) Programming in LPC2148
Analog To Digital Conversion (ADC) Programming in LPC2148
 
Switch Control and Time Delay - Keypad
Switch Control and Time Delay - KeypadSwitch Control and Time Delay - Keypad
Switch Control and Time Delay - Keypad
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
Anup2
Anup2Anup2
Anup2
 
Lecture7
Lecture7Lecture7
Lecture7
 
SIMPLE Frequency METER using AT89c51
SIMPLE Frequency METER using AT89c51 SIMPLE Frequency METER using AT89c51
SIMPLE Frequency METER using AT89c51
 
EC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdfEC8691 - UNIT 5.pdf
EC8691 - UNIT 5.pdf
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
MicrocontrollersII.ppt
MicrocontrollersII.pptMicrocontrollersII.ppt
MicrocontrollersII.ppt
 
Chapter5 dek3133
Chapter5 dek3133Chapter5 dek3133
Chapter5 dek3133
 
STM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicosSTM_ADC para microcontroladores STM32 - Conceptos basicos
STM_ADC para microcontroladores STM32 - Conceptos basicos
 
8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf
8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf
8051 TIMER COUNTER SERIAL COMM. INTERUPT PROGRAMMING.pdf
 
Bidirect visitor counter
Bidirect visitor counterBidirect visitor counter
Bidirect visitor counter
 
Em s7 plc
Em s7 plcEm s7 plc
Em s7 plc
 
chapter 4
chapter 4chapter 4
chapter 4
 
8051 FINIAL
8051 FINIAL8051 FINIAL
8051 FINIAL
 

C Programming Techniques and Applications for Microcontrollers

  • 2. Contents  Keil  Proteus  Some basic techniques for C programming  Application 1  Application 2  Application 3 2
  • 3. 3
  • 4. How to start 1st Keil project.. Install keil software from the installation file. Start keil program from start menu or from desktop shortcut. 4
  • 6. Do not change the file type!!! 6
  • 10. Save file as .c or .asm Right Click and select Add files to group Source 1 10
  • 12. 12
  • 13. Select Options for Target 13
  • 14. 14
  • 15. 15 Any one want to try this ?????
  • 16. Before we start programming 16 Data Types in Embedded C Data Type Size in Bits Data range Unsigned char 8-bit 0 to 255 (Signed) char 8-bit -128 to 127 Unsigned int 16-bit 0 to 65535 (Signed) int 16-bit -32768 to 32767 sbit 1-bit SFR bit Addressable only Bit 1-bit RAM bit-addressable only sfr 8-bit RAM addresses
  • 17. Before we start programming 17 Interrupt numbers in C Interrupt Name Number used by 8051 External interrupt 0 INT0 0 Timer interrupt 0 TF0 1 External interrupt 1 INT1 2 Timer interrupt 1 TF1 3 Serial communication RI+TI 4
  • 18. 18 For loop char i; for(i=0;i<10;i++) { } Write your code here
  • 19. 19 Infinite loop while(1) { } Write your code here
  • 20. Assembly instructions in C code 20 #pragma ASM Write your code here #pragma END ASM
  • 21. 21 Functions int addition(int,int); void main() { int i,j,k; i = 10; j = 10; k = addition(i,j); } int addition(int a, int b) { int c c = a + b; return(c); }
  • 22. Requirements from microcontroller Provide gate signals. What are your expectations from a microcontroller???? Variation in duty ratio of gate signals. Variation in frequency of gate signals. Sensing of voltage. Sensing of current. Sensing of speed. 22
  • 23. DC –DC Converter open loop  Gate signals are in the form of square wave  There may be change in duty ratio of gate signals.  There may be change in frequency of gate signals. 23
  • 24. Program 1 24 Problem statement : Write a program to generate a square wave on pin 1 of port 2 Frequency of square wave = 1kHz Duty ratio = 50% Delay calculation : Assume that XTAL = 11.0592 MHz .  n = 500 μ /1.085μ = 460  Count = 65536 – n = 65076  Count in hex = FE34H  Load TH = FEH and TL =34H
  • 25. Program 25 #include <reg51.h> sbit SW = P2^1; void delay(); void main() { TMOD = 0x01; while(1) { SW = 1; delay(); SW = 0; delay(); } } void delay() { TH0 = 0xFE; TL0 = 0x34; TR0 = 1; while(!TF0) ; TR0 = 0; TF0 = 0; } Lets go to Keil for trying this program
  • 26. Program 2 26 Problem statement : Write a program to generate a square wave on pin 1 of port 2 Frequency of square wave = 10kHz Duty ratio = variable. Time period of wave = 100 μsec We would be requiring two delays  One for on time.  Another for off time.
  • 27. 27 Program #include <reg51.h> sbit SW P2^1 void delay1(); void delay2(); extern char count; void main() { count = 0; EA = 1; EX0 = 1; EX1 = 1; IT0 = 1; IT1 = 1; TMOD = 0x01; while(1) { SW = 1; delay1(); SW = 0; delay2; } } void delay1() { TH0 = 0xFF; TL0 = 210+count; TR0 = 1; while(!TF0) ; TR0=0; TF0=0; }
  • 28. 28 Program cont. void delay2() { TH0 = 0xFF; TL0 = 210-count; TR0 = 1; while(!TF0) ; TR0=0; TF0=0; } void external0() interrupt 0 { count++; } void external0() interrupt 1 { count--; } Lets try to simulate this program
  • 29. Program 3 29 Problem statement : Write a program to generate a square wave on pin 1 of port 2 Frequency of square wave = Variable. Duty ratio = 50%. Time period of wave = variable Hence, Count to be loaded in timer register = variable Requirements to be satisfied by the program User should be able to vary the frequency. Frequency raise and lower commands are given by user. There should be upper and lower limit on frequency.
  • 30. 30 Solutions • Delay can be created using - Timers • User interface can be created by using - Interrupts How to satisfy these requirements using program???? • Limits can be put by using conditions like if-else
  • 31. Program 31 #include <reg51.h> sbit SW = P2^1 void delay(); extern unsigned char count; void main() { count = 0x01; EA = 1; EX0 = 1; IT0 = 1; IT1 = 1; TMOD = 0x01; while(1) { SW = 1; delay(); SW = 0; delay(); } } void delay() { TH0 = 0xFF; TL0 = count; while(!TF0); TR0 = 0; TF0 = 0; }
  • 32. 32 Program cont. void external0() interrupt 0 { if(count < 0xFA) count++; else count = 0xFA; } void external0() interrupt 1 { if(count > 0x01) count--; else count = 0x01; }
  • 33. Requirements from microcontroller Provide gate signals. Variation in duty ratio of gate signals. Variation in frequency of gate signals. Sensing of voltage. Sensing of current. Sensing of speed. 33
  • 34. 34 How to sense analog signals like voltage, current??? Solution : Analog to digital converters. Limitations of ADC: It can read only positive voltage. It can read voltage between 0-5V. ADC can not read current. Solution : Current sensor produces proportional voltage output for input current. Voltage divider is used for sensing high voltage. Tachometer generates voltage signal proportional to speed Signal conditioning is required to sense negative voltages.
  • 35. 35 ADC0804 chip ADC0804 is an 8-bit parallel ADC. It works with +5V voltage supply. It has a resolution of 8 bits. Conversion time depends on clocking signals applied to CLK IN pin, but it cannot be faster than 110μs.
  • 36. 36 Pin details of ADC0804 CS Chip select is an active low input used to activate chip. RD Used to read the converted data. Signal should be active low WR Start of conversion. Signal should be active low. CLK IN Input for clock INTR End of conversion Vcc Supply voltage +5V Vref/2 Kept open for reading voltage in range 0-5V. D0-D7 Digital data output pins.
  • 37. 37 Relation of Vref/2 and Vin range Vref/2(V) Vin(V) Step size(mV) Not connected 0 to 5 19.53 2.0 0 to 4 15.62 1.5 0 to 3 11.71 1.28 0 to 2.56 10 Step size = Vin(max)/256
  • 38. 38 How to calculate output Dout = Digital data output in decimal. Vin = analog input voltage. Dout = Vin/step size
  • 39. 39 Program 4 Problem statement: Using the ADC and current sensor read the value of current flowing in the branch of circuit, and display it on port3 Solution: Current sensor ACS712ELCTR-20A-T is used. Current sensor can read current up to +/- 20A. Gain of current sensor (refer data sheet). ADC0804 is used. Internal clock is used.
  • 40. 40 Program #include<reg51.h> sbit READ = P2^5; sbitWRITE = P2^6; sbit INTR = P2^7; void main() { unsigned char value; P1 = 0xFF; INTR = 1; READ = 1; WRITE = 1; while(1) { WRITE = 0; WRITE = 1; while(INTR); READ = 0; value = P1; READ = 1; P3 = value; } } Lets simulate this code.
  • 41. Our achievements Provide gate signals. Variation in duty ratio of gate signals. Variation in frequency of gate signals. Sensing of voltage. Sensing of current. Sensing of speed. 41
  • 42. 42