Microcontroller 8051 training
-
Upload
pradip-bhandari -
Category
Devices & Hardware
-
view
864 -
download
3
Transcript of Microcontroller 8051 training
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 1
Microcontroller 8051 Training
-Pradip Bhandari
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 4
IntroductionMicrocontroller• 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
TimerSerial COM Port
Microcontroller
CPUA single chip
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 6
Feature 8051 8052 8031ROM (program space in bytes) 4K 8K 0KRAM (bytes) 128 256 128Timers 2 3 2I/O pins 32 32 32 Serial port 1 1 1 Interrupt sources 6 8 6
Features and Comparison 8051
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 7
Pin Description of the 8051
1234567891011121314151617181920
4039383736353433323130292827262524232221
P1.0P1.1P1.2P1.3P1.4P1.5P1.6P1.7RST
(RXD)P3.0(TXD)P3.1
(T0)P3.4(T1)P3.5
XTAL2XTAL1
GND
(INT0)P3.2(INT1)P3.3
(RD)P3.7(WR)P3.6
VccP0.0(AD0)P0.1(AD1)P0.2(AD2)P0.3(AD3)P0.4(AD4)P0.5(AD5)P0.6(AD6)P0.7(AD7)EA/VPPALE/PROGPSENP2.7(A15)P2.6(A14)P2.5(A13)P2.4(A12)P2.3(A11)P2.2(A10)P2.1(A9)P2.0(A8)
8051(8031) PORT 0
PORT 2
PORT 1
PORT 3
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 8
Pins of 8051( 1/4)• 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 • Example 4-1 shows the relationship between XTAL and the
machine cycle.
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 10
XTAL Connection to an External Clock Source
NC
EXTERNALOSCILLATORSIGNAL
XTAL2
XTAL1
GND
• Using a TTL oscillator• XTAL2 is unconnected.
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 11
Pins of 8051( 2/4)• RST ( pin 9 ): reset
• It is an input pin and is active high ( normally 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 1 : Power-on reset circuit • Way 2 : Power-on reset with debounce
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 12
Power-On RESET Circuit
30 pF
30 pF
8.2 K
10 uF
+
Vcc
11.0592 MHz
EA/VPPX1
X2
RST
31
19
18
9
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 13
Power-On RESET with Debounce
EA/VPPX1
X2RST
Vcc
10 uF
8.2 K
30 pF
9
31
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 14
Pins of 8051( 3/4)• /EA ( pin 31 ): external 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.
• /PSEN ( pin 29 ): program store enable• This is an output pin and is connected to the OE pin of the ROM.• See Chapter 14.
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 15
Pins of 8051( 4/4)• ALE ( pin 30 ): address 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, May 2, 2023 Robotics Club, IOE, WRC 16
Pins of I/O Port
• The 8051 has four I/O ports• Port 0 ( pins 32-39 ): P0 ( P0.0 ~ P0.7 ) Bit address (0x80~0x87)• Port 1 ( pins 1-8 ) : P1 ( P1.0 ~ P1.7 ) Bit address (0x90~0x97)• Port 2 ( pins 21-28 ): P2 ( P2.0 ~ P2.7 ) Bit address (0xA0~0xA7)• Port 3 ( pins 10-17 ): P3 ( P3.0 ~ P3.7 ) Bit address (0xB0~0xB7)• Each port has 8 pins.
• Named P0.X ( X=0,1,...,7 ) , P1.X, P2.X, P3.X• Ex : P0.0 is the bit 0 ( LSB ) of P0 • Ex : P0.7 is the bit 7 ( MSB ) of P0• These 8 bits form a byte.
• Each port can be used as input or output (bi-direction).
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 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-D7When ALE=1, it has address A0-A7
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 21
Environment SetupProteus 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 Electronics\Proteus 8 Professional) 10. Copy "MODELS" To ProgramData (C:\ProgramData\Labcenter Electronics\Proteus 8 Professional) 11. Run Proteus and enjoy!
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 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, May 2, 2023 Robotics Club, IOE, WRC 23
Proteus
Isis should be highlighted (Clickable) Click it to open to initiate design
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 24
12
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, May 2, 2023 Robotics Club, IOE, WRC 25
Create a basic Led Blink Circuit
12
1. Click Component mode 3. Type at89c522. Click pick device 4. Select the first one
3
4
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 26
Pick the LED1
2
3
4
1. Type Keyword led2. Select Optoelectronics in Category3. Select LEDs in sub-category4. Select LED-RED or LED-GREEn or LED-
BLUE in Results
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 27
Led blink circuit1. 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, May 2, 2023 Robotics Club, IOE, WRC 28
LED blink Program in Keil using C1. Open keil2. Goto Project>New uvision Project> give it a name as Blink (file
extension not needed)and save it3. From CPU Database select Atmel > AT89C52 press ok and click No
to not to copy start up files4. Goto file menu and select new(or press ctrl + N)5. Save it as Blink.c (don’t 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, May 2, 2023 Robotics Club, IOE, WRC 29
Creating HEX file6. Under Flash menu goto Configure Flash Tools…7. 1
2 3
4
51. Select Target2. Enter Xtal value as 12.03. Check Use On-chip ROM
4. Select Output5. Check Create HEX FileClick OK
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 30
Simple LED BLINk.C#include<reg51.h>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;i<=time;i++)for(j=0;j<=1275;j++);
}
1.To Address Port we use sfr or #define keyword sfr ldata = P1; // P must be Capital sfr ldata = 0x90; #define ldata P1 // no equals to, no semicolon
2. To Address A single bit of a Port we use sbit keyword sbit out = P2^0; // it indicates P2.0 sbit enable = P2^1; // P must be Capital sbit buttonChech = P2^2;
3. At initial stage or at reset condition the output of each port or port bit(pin) will be 1(high)
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 31
Compile and Build1
2 3
1. Make a Habit to save your work all the time, press (Ctrl+S) to save
2. Compile or Translate, Click it to compile your code to check errors and warning(Ctrl+f7)
3. Press Build or Rebuild to generate HEX file
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 32
Uploading HEX File
12
3
1. Double Click at Microcontroller AT89C52 to open Edit Component Dialog Box
2. Browse for HEX file that you created earlier
3. Press Ok4. Run Your Simulation
4
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 33
DEMO
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 34
To make a port or single pin as an input we need to declare it as HIGH or 1
#include<reg51.h>sbit out = P2^0;sbit in = P2^1;void msdelay(unsigned int);void main(){in = 1;P2=0;while(1){if( in == 1){ out=1;
}else {
out=0; }}}
void msdelay(unsigned int time){unsigned int i,j;
for(i=0;i<=time;i++)for(j=0;j<=1275;j++);
}
Search Button in Component search box
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 35
Liquid Crystal Display (LCD):-LCD is finding widespread use replacing LEDs because The declining prices of LCD The ability to display numbers, characters and graphicsEase of programming for characters and graphics
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 36
Pin Descriptions for LCDPin Symbol I/O Descriptions
1 VSS ----- Ground2 VCC ----- +5 v power supply3 VEE ----- Power supply to contrast 4 RS I Register Select( 0 for command
register and 1 for data register)5 __
R /WI 1 for Read
0 for write6 E I/O Enable7-14 DB0-
DB7I/O The 8-bit data bus
15 LED + ----- Usually Connected t0 +5v of power supply
16 LED- ----- Connected to Ground
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 37
Instruction VS Data• Instruction Register (IR) and Data Register (DR) • Instruction is a Command, Data is a displaying Value• There are two 8-bit registers in Our LCD controller IC, Instruction and
Data register. Instruction register corresponds to the register where you send commands to LCD e.g LCD shift command, LCD clear, LCD address etc. and Data register is used for storing data which is to be displayed on LCD.
• Only the instruction register (IR) and the data register (DR) of the LCD can be controlled by our 8051 microcontroller
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 38
LCD PROTEUS SIMULATION1.Lcd is named as LM016L in Component Mode Pick Device i.e Library
2. Connect P2.0~P2.7 of microcontroller to D0~D7 of LCD respectively
3. Connect P3.0 to RS, P3.1 to RW and P3.2 to E
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 39
Before using the LCD for display purpose, LCD has to be initialized either by the internal reset circuit or sending set of commands to initialize the LCD. It is the user who has to decide whether an LCD has to be initialized by instructions or by internal reset circuit. we will dicuss both ways of initialization one by one. We need to initialize LCD to define type of LCD, Cursor Position, Home Location etcType includes like 16x2 or 14x1 or 20x2 etc
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 40
Some Points to note while Programming
A high to low pulse should be provided in an enable pin for Write Operation
A low to high pulse should be provided in an enable pin for Read Operation
Bit D7 of LCD is busy flag which can be used to check if LCD is busy in its own internal operation
Busy flag can be read when R/W =1 and RS=0 and a L to H pulse in enable pin
NOTE:- It is recommended to check busy flag before writing any data to LCD.
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 41
LCD BUSY CHECK (BF-FLAG)• Busy Flag is an status indicator flag for LCD. When we send a
command or data to the LCD for processing, this flag is set (i.e BF =1) and as soon as the instruction is executed successfully this flag is cleared (BF = 0). This is helpful in producing and exact amount of delay. for the LCD processing.
• To read Busy Flag, the condition RS = 0 and R/W = 1 must be met and The MSB of the LCD data bus (D7) act as busy flag. When BF = 1 means LCD is busy and will not accept next command or data and BF = 0 means LCD is ready for the next command or data to process.
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 42
BUSY CHECK
void lcdbusy(){busy=1; // declaring D7 of LCD as input to check busy flag rw=1;rs=0;while(busy==1)
{en=0;msdelay(1);en=1;}return;
}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 43
Sending Command To LCD
void lcdcmd(unsigned char value){lcdbusy(); //Wait for LCD to process the commandldata=value; //Function set: 2 Line, 8-bit, 5x7 dots rs=0; //Selected command register rw=0; //We are writing in instruction registeren=1; //Enable H->L msdelay(1);en=0; //Enable H->L return;}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 44
LCD Command Codes
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 45
Sending Data To LCD
void lcddata(unsigned char value){lcdbusy(); //Wait for LCD to process the commandldata=value; //Function set: 2 Line, 8-bit, 5x7 dots rs=1; //Selected data register rw=0; //We are writing in data registeren=1; //Enable H->L msdelay(1);en=0; //Enable H->L return;}// we will pass the character to display as argument to function
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 46
Sending String Data To LCDvoid 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, May 2, 2023 Robotics Club, IOE, WRC 47
SELECTING FIRST AND SECOND ROW
void lcdfirst(){lcdcmd(0x38);lcdcmd(0x0e);msdelay(50);lcdcmd(0x01);// clearlcdcmd(0x06);lcdcmd(0x80);}
void lcdsecond(){lcdcmd(0x38);lcdcmd(0x0e);msdelay(50);lcdcmd(0x06);lcdcmd(0xc0);}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 48
OVERALL PROGRAM FOR LCD
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 49
#include<reg51.h>#define ldata P2 //lcd datasbit rs=P3^0; //lcd signalssbit rw=P3^1;sbit en=P3^2;sbit busy=P2^7;void lcdbusy(); //to check if lcd is busy or notvoid lcdcmd(unsigned char) ; // to give lcd commandsvoid lcddata(unsigned char); //to give data to the lcdvoid lcdfirst(); // first line of lcd initializationvoid lcdsecond(); // second line of lcd initializationvoid lcdstr(unsigned char[]); // to display stringvoid msdelay (unsigned char);
void main(){
P2=0;P3=0;
while(1){
lcdfirst();lcdstr("Hello");msdelay(1000);
lcdsecond();lcdstr("Pokhara1");msdelay(1000); }
}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 50
void msdelay(unsigned char time){
unsigned int i,j;for(i=0;i<time;i++)
for(j=0;j<=1275;j++);}
void lcdbusy(){busy=1;rw=1;rs=0;while(busy==1)
{en=0;msdelay(1);en=1;}return;
}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 51
void lcdcmd(unsigned char value){lcdbusy();ldata=value;rs=0;rw=0;en=1;msdelay(1);en=0;return;}
void lcddata(unsigned char value){lcdbusy();ldata=value;rs=1;rw=0;en=1;msdelay(1);en=0;return;}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 52
void lcdfirst(){lcdcmd(0x38); //2 lines and 5x7 matrixlcdcmd(0x0e);msdelay(50); //cursor blinklcdcmd(0x01); //clear display screenlcdcmd(0x06); //shift cursor to rightlcdcmd(0x80);}
void lcdsecond(){lcdcmd(0x38); //2 lines and 5x7 matrixlcdcmd(0x0e);msdelay(50); //cursor blinklcdcmd(0x06); //shift cursor to rightlcdcmd(0xc0);}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 53
void lcdstr(unsigned char msg[]) //to print string in lcd{unsigned char i=0;while(msg[i]!='\0'){if(i>16){msdelay(10);lcdcmd(0x07);}if(msg[i]==' ')msdelay(50);if(i==15)lcdcmd(0x07);lcddata(msg[i]);i++;}}
Tuesday, May 2, 2023 Robotics Club, IOE, WRC 54
THANK YOU
ANY QUESTION?