1. Microcontroller 8051 Training -Pradip Bhandari Tuesday, July
14, 2015 Robotics Club, IOE, WRC 1
2. I assume every body is familiar with Basic C programming
Everybody is well known about basic Digital gates Everybody knows
Number Systems Everybody has concept of basic electronics including
RAM and ROM memory And all are expert in using computer Tuesday,
July 14, 2015 Robotics Club, IOE, WRC 2
3. Contents Introduction Block Diagram and Pin Description of
the 8051 Environment setup (Proteus and Keil) Introduction to
Proteus Simple LED Blink DEMO LCD Programming DEMO Tuesday, July
14, 2015 Robotics Club, IOE, WRC 3
4. Introduction Microcontroller CPU, RAM, ROM, I/O and timer
are all on a single chip fix amount of on-chip ROM, RAM, I/O ports
for applications in which cost, power and space are critical
single-purpose RAM ROM I/O Port Timer Serial COM Port
Microcontroller CPU A single chip Tuesday, July 14, 2015 Robotics
Club, IOE, WRC 4
5. Block Diagram CPU On-chip RAM On-chip ROM for program code 4
I/O Ports Timer 0 Serial PortOSC Interrupt Control External
interrupts Timer 1 Timer/Counter Bus Control TxD RxDP0 P1 P2 P3
Address/Data Counter Inputs Tuesday, July 14, 2015 Robotics Club,
IOE, WRC 5
6. Feature 8051 8052 8031 ROM (program space in bytes) 4K 8K 0K
RAM (bytes) 128 256 128 Timers 2 3 2 I/O pins 32 32 32 Serial port
1 1 1 Interrupt sources 6 8 6 Features and Comparison 8051 Tuesday,
July 14, 2015 Robotics Club, IOE, WRC 6
8. Pins of 80511/4 Vccpin 40 Vcc provides supply voltage to the
chip. The voltage source is +5V. GNDpin 20ground XTAL1 and
XTAL2pins 19,18 These 2 pins provide external clock. Way 1using a
quartz crystal oscillator Way 2using a TTL oscillator Example 4-1
shows the relationship between XTAL and the machine cycle. Tuesday,
July 14, 2015 Robotics Club, IOE, WRC 8
9. XTAL Connection to 8051 C2 30pF C1 30pF XTAL2 XTAL1 GND
Using a quartz crystal oscillator We can observe the frequency on
the XTAL2 pin. Tuesday, July 14, 2015 Robotics Club, IOE, WRC
9
10. XTAL Connection to an External Clock Source N C EXTERNAL
OSCILLATOR SIGNAL XTAL2 XTAL1 GND Using a TTL oscillator XTAL2 is
unconnected. Tuesday, July 14, 2015 Robotics Club, IOE, WRC 10
11. Pins of 80512/4 RSTpin 9reset It is an input pin and is
active highnormally low. The high pulse must be high at least 2
machine cycles. It is a power-on reset. Upon applying a high pulse
to RST, the microcontroller will reset and all values in registers
will be lost. Reset values of some 8051 registers Way 1Power-on
reset circuit Way 2Power-on reset with debounce Tuesday, July 14,
2015 Robotics Club, IOE, WRC 11
13. Power-On RESET with Debounce EA/VPP X1 X2 RST Vcc 10 uF 8.2
K 30 pF 9 31 Tuesday, July 14, 2015 Robotics Club, IOE, WRC 13
14. Pins of 80513/4 /EApin 31external access There is no
on-chip ROM in 8031 and 8032 . The /EA pin is connected to GND to
indicate the code is stored externally. /PSEN ALE are used for
external ROM. For 8051, /EA pin is connected to Vcc. / means active
low. /PSENpin 29program store enable This is an output pin and is
connected to the OE pin of the ROM. See Chapter 14. Tuesday, July
14, 2015 Robotics Club, IOE, WRC 14
15. Pins of 80514/4 ALEpin 30address latch enable It is an
output pin and is active high. 8051 port 0 provides both address
and data. The ALE pin is used for de-multiplexing the address and
data by connecting to the G pin of the 74LS373 latch. I/O port pins
The four ports P0, P1, P2, and P3. Each port uses 8 pins. All I/O
pins are bi-directional. Tuesday, July 14, 2015 Robotics Club, IOE,
WRC 15
16. Pins of I/O Port The 8051 has four I/O ports Port 0 pins
32-39P0P0.0P0.7Bit address (0x80~0x87) Port 1pins 1-8 P1P1.0P1.7
Bit address (0x90~0x97) Port 2pins 21-28P2P2.0P2.7 Bit address
(0xA0~0xA7) Port 3pins 10-17P3P3.0P3.7 Bit address (0xB0~0xB7) Each
port has 8 pins. Named P0.X X=0,1,...,7, P1.X, P2.X, P3.X ExP0.0 is
the bit 0LSBof P0 ExP0.7 is the bit 7MSBof P0 These 8 bits form a
byte. Each port can be used as input or output (bi-direction).
Tuesday, July 14, 2015 Robotics Club, IOE, WRC 16
17. PORT P0 (PIN 39 TO 32) Port P0 has got three
functionalities.(I/O port, lower order address(A0 to A7) and data
(D0 to D7). It can be used for input or output , each pin must be
connected externally to a 10K ohm pull-up resistor. ALE indicates
if P0 has address or data When ALE=0, it provides data D0-D7 When
ALE=1, it has address A0-A7 Tuesday, July 14, 2015 Robotics Club,
IOE, WRC 17
18. Pull Up Resistor Pull-up resistor pulls the voltage of the
signal it is connected to towards its voltage source level Pull-up
resistor ensures that the wire is at a defined logic level even if
no active devices are connected to it At reset condition the port
zero will be at floating condition ,i.e no 0 no 1 something other
state which we cannot predict so to ensure proper logic level we
use Pull-up resistor Tuesday, July 14, 2015 Robotics Club, IOE, WRC
18
19. PORT P1 (PIN 1 TO 8) Port P1 is simple I/O port . Internal
pull up resistor is provided. PORT P2(PIN 21 TO 28) Port P2 has
dual functionality.( I/o port and higher order address(A8 to A15)
Port 2 must be used along with P0 to provide the 16-bit address for
the external memory. Internal pull up resistor is provided.
Tuesday, July 14, 2015 Robotics Club, IOE, WRC 19
20. PORT P3 (PIN 10 TO 17) Port 3 can be used as input or
output. Port 3 does not need any pull-up resistors. Port 3 has the
additional function of providing some extremely important signals.
Tuesday, July 14, 2015 Robotics Club, IOE, WRC 20
21. Environment Setup Proteus Installation (Proteus 8.1
SP1)(Recommend to use latest version) 1. Exact the provided package
2. Open LICENSE.EXE 3. Change the attribute of Key.lxk to no hidden
and no readonly 4. Browse for key -> Select Key.lxk 5. Install
key 6. Close LICENSE.EXE 7. Install Proteus 8.1 SP1.exe 8. Do not
run after installation 9. Copy "BIN" To Installation Folder
(C:Program Files (x86)Labcenter ElectronicsProteus 8 Professional)
10. Copy "MODELS" To ProgramData (C:ProgramDataLabcenter
ElectronicsProteus 8 Professional) 11. Run Proteus and enjoy!
Tuesday, July 14, 2015 Robotics Club, IOE, WRC 21
22. Keil Installation I recommend to use latest version of
keil(version 4 for 51) Install the Provided Keil software(trial
version or cracked ) Provide the basic information Done Tuesday,
July 14, 2015 Robotics Club, IOE, WRC 22
23. Proteus Isis should be highlighted (Clickable) Click it to
open to initiate design Tuesday, July 14, 2015 Robotics Club, IOE,
WRC 23
24. 1 2 3 1. Component mode, Click here to enable Component
mode so that we can browse any basic component from 2(pick device)
2. Pick Device, Click here to pick any require component into our
workspace 3. Terminal Nodes, terminal nodes like Ground, Power
source etc is available over here Tuesday, July 14, 2015 Robotics
Club, IOE, WRC 24
25. Create a basic Led Blink Circuit 1 2 1. Click Component
mode 3. Type at89c52 2. Click pick device 4. Select the first one 3
4 Tuesday, July 14, 2015 Robotics Club, IOE, WRC 25
26. Pick the LED 1 2 3 4 1. Type Keyword led 2. Select
Optoelectronics in Category 3. Select LEDs in sub-category 4.
Select LED-RED or LED-GREEn or LED- BLUE in Results Tuesday, July
14, 2015 Robotics Club, IOE, WRC 26
27. Led blink circuit 1. Pick At89c52 microcontroller from
device selector 2. Also pick led as shown in figure and wire them
3. Pick a ground from terminal nodes 4. Zoom the workspace as per
your easiness 5. Now save your work and give it a name Blink 6. Now
its time to write program in keil Tuesday, July 14, 2015 Robotics
Club, IOE, WRC 27
28. LED blink Program in Keil using C 1. Open keil 2. Goto
Project>New uvision Project> give it a name as Blink (file
extension not needed)and save it 3. From CPU Database select Atmel
> AT89C52 press ok and click No to not to copy start up files 4.
Goto file menu and select new(or press ctrl + N) 5. Save it as
Blink.c (dont forget to include file extension as .c) 6. Under
project > Target 1 > Source group 1 add Blink.c (double click
Source group 1 to add blink.c) Tuesday, July 14, 2015 Robotics
Club, IOE, WRC 28
29. Creating HEX file 6. Under Flash menu goto Configure Flash
Tools 7. 1 2 3 4 5 1. Select Target 2. Enter Xtal value as 12.0 3.
Check Use On-chip ROM 4. Select Output 5. Check Create HEX File
Click OK Tuesday, July 14, 2015 Robotics Club, IOE, WRC 29
30. Simple LED BLINk.C #include sbit out = P2^0; void
msdelay(unsigned int); void main() { while(1) { out=1;
msdelay(500); out=0; msdelay(500); } } void msdelay(unsigned int
time) { unsigned int i,j; for(i=0;iL return; } // we will pass the
character to display as argument to function Tuesday, July 14, 2015
Robotics Club, IOE, WRC 45
46. Sending String Data To LCD void lcdstr(unsigned char
msg[15]) //to print string in lcd { unsigned char i=0;
while(msg[i]!='$') { if(i>16) { msdelay(10); lcdcmd(0x07);
//shift display to left } if(msg[i]==' ') msdelay(50); if(i==15)
lcdcmd(0x07); lcddata(msg[i]); i++; } }Tuesday, July 14, 2015
Robotics Club, IOE, WRC 46
47. SELECTING FIRST AND SECOND ROW void lcdfirst() {
lcdcmd(0x38); lcdcmd(0x0e); msdelay(50); lcdcmd(0x01);// clear
lcdcmd(0x06); lcdcmd(0x80); } void lcdsecond() { lcdcmd(0x38);
lcdcmd(0x0e); msdelay(50); lcdcmd(0x06); lcdcmd(0xc0); } Tuesday,
July 14, 2015 Robotics Club, IOE, WRC 47
48. OVERALL PROGRAM FOR LCD Tuesday, July 14, 2015 Robotics
Club, IOE, WRC 48
49. #include #define ldata P2 //lcd data sbit rs=P3^0; //lcd
signals sbit rw=P3^1; sbit en=P3^2; sbit busy=P2^7; void lcdbusy();
//to check if lcd is busy or not void lcdcmd(unsigned char) ; // to
give lcd commands void lcddata(unsigned char); //to give data to
the lcd void lcdfirst(); // first line of lcd initialization void
lcdsecond(); // second line of lcd initialization void
lcdstr(unsigned char[]); // to display string void msdelay
(unsigned char); void main() { P2=0; P3=0; while(1) { lcdfirst();
lcdstr("Hello"); msdelay(1000); lcdsecond(); lcdstr("Pokhara1");
msdelay(1000); } } Tuesday, July 14, 2015 Robotics Club, IOE, WRC
49
50. void msdelay(unsigned char time) { unsigned int i,j;
for(i=0;i