microcontroller 8051 training

54
Microcontroller 8051 Training -Pradip Bhandari 3/12/22 Robotics Club, IOE, WRC 1

Upload: pradip-bhandari

Post on 16-Aug-2015

231 views

Category:

Devices & Hardware


16 download

TRANSCRIPT

  1. 1. Microcontroller 8051 Training -Pradip Bhandari Tuesday, July 14, 2015 Robotics Club, IOE, WRC 1
  2. 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. 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. 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. 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. 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
  7. 7. Pin Description of the 8051 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (T0)P3.4 (T1)P3.5 XTAL2 XTAL1 GND (INT0)P3.2 (INT1)P3.3 (RD)P3.7 (WR)P3.6 Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.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, July 14, 2015 Robotics Club, IOE, WRC 7
  8. 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. 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. 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. 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
  12. 12. Power-On RESET Circuit 30 pF 30 pF 8.2 K 10 uF + Vcc 11.0592 MHz EA/VPP X1 X2 RST 31 19 18 9 Tuesday, July 14, 2015 Robotics Club, IOE, WRC 12
  13. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 23. Proteus Isis should be highlighted (Clickable) Click it to open to initiate design Tuesday, July 14, 2015 Robotics Club, IOE, WRC 23
  24. 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. 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. 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. 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. 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. 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. 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
  31. 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
  32. 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
  33. 48. OVERALL PROGRAM FOR LCD Tuesday, July 14, 2015 Robotics Club, IOE, WRC 48
  34. 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
  35. 50. void msdelay(unsigned char time) { unsigned int i,j; for(i=0;i
  36. 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, July 14, 2015 Robotics Club, IOE, WRC 51
  37. 52. void lcdfirst() { lcdcmd(0x38); //2 lines and 5x7 matrix lcdcmd(0x0e); msdelay(50); //cursor blink lcdcmd(0x01); //clear display screen lcdcmd(0x06); //shift cursor to right lcdcmd(0x80); } void lcdsecond() { lcdcmd(0x38); //2 lines and 5x7 matrix lcdcmd(0x0e); msdelay(50); //cursor blink lcdcmd(0x06); //shift cursor to right lcdcmd(0xc0); } Tuesday, July 14, 2015 Robotics Club, IOE, WRC 52
  38. 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, July 14, 2015 Robotics Club, IOE, WRC 53
  39. 54. ANY QUESTION? Tuesday, July 14, 2015 Robotics Club, IOE, WRC 54