You are on page 1of 8
THE 8051 MICROCONTROLLER AND EMBEDDED SYSTEMS Muhammad Ali Mazidi Janice Gillispie Mazidi Prentice Hall Upper Saddle River, New Jersey Columbus, Ohio This chapter explores some real-world applications of the 8051. We explain how to interface the 8051 to devices such as an LCD, ADC, and sensors. In Section 12.1, we show LCD interfacing with the 8051. In Section 12.2, we describe analog-to-digital converter (ADC) connection with sensors and the 80S |. SECTION 12.1: INTERFACING AN LCD TO THE 8051 This section describes the operation modes of LCDs, then describes how to program and interface an LCD to an $051 LCD operation In recent years the LCD is finding widespread use replacing LEDs (seven- segment LEDs or other multisegment LEDs). This is due to the following reasons: 1. The declining prices of LCDs. 2. The ability to display numbers, characters, and graphics. This is in contrast to LEDs, which are limited to numbers and a few characters, 3. Incorporation of a refreshing controller into the LCD, thereby relieving the CPU of the task of refreshing the LCD. In contrast, the LED must be refreshed by the CPU (or in some other way) to keep displaying the data : 4. Ease of programming, for characters and graphics. LCD pin descriptions ‘The LCD discussed in this section has 14 pins. The function of each pin is given in Table 12-1. Figure 12-1 shows the pin positions for various LCDs, Vee, Vss, and Veg Table 12-1, Pin Descriptions for LCD While Vee and Vss provide +5V and pin Symbol VO Description ground, respectively, Veg is used for control- Ves ‘Ground ling LCD contrast 2 Veo SV power supply RS, register select 3 Vir 7 ‘Power supply There are two very important registers - to contol contrast inside the LCD. The RS pin is used for their 4 RS ot RS=0 10 select selection as follows. If RS = 0, the instruction command register, command code register is selected, allowing the RS=I_ to select user fo send a command such as clear display, data register casor at home, ete. If RS = I the data register 5 UWI R/W=0 for write, is selected, allowing the user to send data to be R/W=1 for read displayed on the LCD. © __E WO Enable ; 7___DRO VO The 8-bit data bus RW, read/write 3__DBI_VO__ The 8-bit data bus RJW input allows the user to write infor- 9 DB2_V/O The 8-bit data bus mation fo the LCD or read information from it. TQ DB3_VO The 8-bit data bus RUW = 1 when reading; R/W = 0 when writing, TI DB4 VO The 8-bit data bus 12 DBS_VO The 8-bit data bus _ E enable 13 DBG6_WO The & bit data b a. ik 1 &-bit dat The enable pin is used by the LCD © 7-—pBy TO Tke bit data bus larch information presented to its data pins. 236 When data is supplied to data pins, « tigh-to-low pulse must be applied to this pin in order for the LCD to latch in the data present at the data pins, This pulse must be a minimum of 450 ns wide. D0- D7 The 8-bit data pins, DO - D7, are used to send information to the LCD or read the contents of the LCD’s internal registers. ‘To display letters and numbers, we send ASCII codes for the letters A - Z, az, and numbers 0 - 9 40 these pins while making RS There are also instruc- tion command codes that can Table 12-2: LCD Command Codes be sent to the LCD to clear the Tode~ Command to struction display or force the cursor to (ex) Register the home position or blink the fe ae y Screen cursor, Table 12-2 lists the Return home instwwetion command codes. Decrement cursor Ghill cusorto Tey We also use RS = 0 to increment cursor (shi cursor to right) check the busy flag bit to sei 5 Shift display right the LCD is ready to receive 7 Shift display lett information. The busy lag is § Display off cursor off D7 and can be read when RW ‘4 _Display off, cursor on = | and RS = 0, as follows: if Display on, cursor off RIW=1,RS=0.When D7= Display on, cursor blinking \ (busy flag = 1), the LCD is F Display on, eursor blinking busy taking care of internal JO Shift cursor position to left operations and will not accept 14 Shift cursor position to right any new information. When 18 Shift she entire display to the lef D7 = 0, the LCD is ready 10 1C Shift the entire display to the right receive new information, Note: 80 Force cursor to beginning of Ist Tine I is recommended to check the CO Force cursor to beginning of 2nd line busy flag before writing any 382 lines and 5x7 matrix data to the LCD. Note- This table is extracted Irom Table 12-4 x io 1 u © UODNOONOOOOOO LC ° © fpnnnnnnnnnnnn DMEI6104, 1 DMcI6106B 21 DMC2026) DMC I606C DMC16207 DMe24937 DMCI6IT DMC16230 DMC24138 DMCI6128 DMC20215 32 { DMCI6}29. DMC32216 39 i DMC1616083 DMCAOI31 DMC20434 DMCAO218 Figure 13-1. Pin Positions for Various LCDs from Optrex CHAPTER 12: REAL-WORLD INTERFACING 237 Sending commands and data to LCDs with a time delay To send any of the commands from Table 12-2 to the LCD, make pin RS. =0. For data, make RS = 1. Then send a high-to-low pulse to the E pin to enable the internal latch of the LCD. This is shown in the code below. See Figure 12-2. lls a time delay before sending next data/command jeal ; P1,0-P1,7 are connected to LCD data pins 90-D7 7 P2.0 is connected to RS pin of LeD 2.1 is connected to R/# pin of LeD 7 P2.2 is connected to pin of LCD ORG MoV A,438H init. LCD 2 lines, 5x7 matrix ACALL — COMNWRT call commard subroutine ACALL DELAY give LCD some time KOV a, #028 sdisplay on, cursor on ACALL © COMNIRT ycald command subroutine ACALL DELAY give LCD some time wov A, #OL clear LCD ACALL — COMNWRT reall conmand subroutine ACALL DELAY give LCD some time HOV A, 06H shift cursor right ACALL — CONNWRT pcall command subroutine ACALL DELAY igive LCD some time HOV A, 34H cursor at line l,pos. 4 | ACALL — COMNWRT seall command subroutine j ACALL DELAY igive LCD some time : MOY A, EIN! display letter W i ACALL — DATAWRT ACALL DELAY call display subroutine give LCD some time Koy a, #0" isplay letter 0 ACALL DATAWRT all display subroutine AGAIN: SJMP AGAIN tay here COMNW3T isend command to LCD mov P1,A copy reg A to portl CLR P2.0 7RS for command CLR R/#=0 for write SETB P2.2 pE=1 for high pulse CLR P2.2 7E=0 for H-to-L pulse DATAWRT: wurite data to LCD copy reg A to portl for data for write for high pulse 0 for H-to-L pulse RET DELAY: — MOV R3, #50 higher for fast CPUs HERE2: = MOV R4,8255 7 R4~255 HERE: DIN2 RA, HERE stay until R4 becomes 0 DINZ —-R3, HERE ; RET { BED } — E Sending code or data to the. LCD with checking busy flag The above code showed how to send commands to the LCD without checking the busy flag. Notice that we must put a long delay in between issuing data or commands to the LCD. However, much better way is 10 monitor the busy flag before issuing a command or data to the LCD. This is shown below. ;Pisdata pin 722.0 connected to 88 pin 722.1 connected to R/W pin 722.2 connected to E pin ORG MOV A, #384 ACALL — COMMAND MOV A, #0BK ACALL — COMMAND MOV A, HOLE ACALL — COMMAND ov A, 4068 ACALL — COMMAND Mov A, #B6H ACALL MAND Mov ArAIN' ACALL DATA_DISPLAY MOV A, ATOr ACALL —DATA_DISPLAY HER SMP HERE COMMAND: ACALL — READY Mov PLA CLR 2.0 CLR P2.1 SETS P22 CLR 2.2 Rl DATA_DISPLAY ACALL — READY Mov PLA SETB P2.0 CLR 2.1 SETB P2.2 CLR P2.2 RET uf CHAPTER 12: REAL-WORLD INTERFACING 1 BOS as LeD| Prop] bo Ved] Vie] = 3 10K pi. | 7 Por Rs RW E Yq nob —l Figure 12-2. LCD Connect yinit. LCD 2 lines, 5x7 mate heck busy flag before sending data, command to LCD : command #LCD on, cursor on sissue command iclear LCD command zissue command cursor rignt command line 1, pos. 6 subroutine idisplay letter ssue display letter 0 ue command code 0 for command <0 to write to LCD for H-to-L pul latch in jis LCD ready? pissve data ;RS=1 for data ;R/W=0 to write to LCD }B-i for H-to-L puls ;8-0, latch in 239 READY: make PL.7 input port RS=0 access command reg =1 read command reg and check busy flag BACK:CLR # for H-to-L pulse SETB ; H-to-L pulse JB ay until busy flag=0 RET END Notice in the above program that the busy flag is D7 of the command reg- ister. To read the command register we make R/W = 1, RS = 0, and a H-to-L pulse for the E pin will provide us the command register. After reading the command register, if bit D7 (the busy flag) is high, the LCD is busy and no information (command or data) should be issued to it, Only when D7 = 0 can we send data or commands to the LCD. Notice in this method that there are no time delays used since we are checking the busy flag before issuing commands or data to the LCD. LCD data sheet In the LCD, one can put data at any location. The following shows address locations and how they are accessed. RS RAV DBT DB6 DBS DBY DB} DB2 DBI DBO o 0 ' A A A A A AOA where AAAAAAA = 0000000 to 0100111 for line 1 and AAAAAAA = 1000000 to 1100111 for Tine 2. See Table 12-3. ‘Table 12-3: LCD Addressing DB?_DB6_DBS_DB4_DB3_DB2_DBI_DBO Tine T (min) oo oo Line 1 (max) 10 a ee es 1 1 Line 2 (min), 1 1 oo 0 0 0 0 Line 2 (max) 1 1 ro OT i i The upper address range can go as high as 0100111 for the 40-charac- ter-wide LCD while for the 20-character-wide LCD it goes up 10 010011 (19 dec- imal = 10011 binary). Notice that the upper range 0100111 (binary) = 39 decimal which corresponds to locations 0 to 39 for the LCDs of 40x2 size. Erom the above discussion we can get the addresses of cursor positions for various sizes of LCDs. See Figure 12-3, Note that all the addresses are in hex. Figure 12-4 gives a diagram of LCD timing. Table (2-4 provides a detailed list of LCD commands and instructions. Table 12-2 is extracted front this table, 16 x 2 LCD 80 a 8 Bf 85 86 through OF co c2_¢3 C4 C56 through CF 20 x 1 TCD 80 263 through 93 20 x 2 LCD 80 82. 83 through 93 co C2__C3___ through _p3 20 x 4 LCD 80 82.83 through 93 co c2 C3 through D3 94 96 97 through a7 D4 Dé__O7 through £7 Jao x 2 LCD 80 @2 83 through AT co 23 through £7 Note: All data is in hex. Figure 12-3 Cursor Addresses for Some LCDs | E ——— bh = pwn RW bas ae * AH pwn ~ Enable polse width ~ 450 ns (minimum) {pg ~ Data set vp time = 19S ns (miniemum) yg Data hold time = 10 ns (minionun) tas" Setup time prior to E (going high) for both RS and R/W tit ~ Hold time after E has come down for bot RS and RAW 10s (inion) 10 ns (evn Review Questions 1, The RS pin is an (input, output) pin for the LCD. 2. The E pin isan input, output) pin for the LCD. 3. The E pin requires an (H-to-L, L-to-H) pulse to latch in information at the data pins of the LCD. 4. For the LCD to recognize information at the data pins as data, RS must be set i to (high, low). 5, Give the command codes for line 1, first character, and line 2, first character CHAPTER 12; REAL-WORLD INTERFACING I 241 ‘Table 12-4: List of LCD Instructions Execution I i ZESSESRR8 Time struction ; “ 22ARSR 5685 —Vescription (Max) Clear Display 0 0 0 00 0 0 0 0 1 Clears enlire uisplay and sets DDD 1.64 ms RAM address 0 in address counter Return Home 0 0 9 0 6 G0 0 1 ~ Sets DD RAM address 0 as address 1.64 ms. counter. Also returns display being, shified to original position. DD RAM contents remain unchanged, Enity Mode 90 5 0 0 0 911/05 Sels cursor move direction and specifies 40 ws Set shift of display. These operations are performed during data write and read. Display On) 0 0D 0 00 1D CB Sets On/OM of entire display (D), Tis Of Control cursor On/OFF (C), and blink of cursor position character (B), ‘Cursor oF TO 0 =~ Moves cursor and shifts display with- 40 us) Display Shit, out changing DD RAM contents, Function Set 0 0 0 0 1 5 NF — — Sets interface data length (DL), num- ‘ser of display lines (L.) and character font (F). SetCGRAM_ 00 0 REC Sets CG RAM address. CG RAM data 405s Address, is sent and received afler this setting, Set DD RAM 0 07 Sets DD RAM address, DD RAM data 40 ys Address is sent and received after this setting, Read Busy 028 AC Reads Busy flag (BF) indicating inter- 40 ys | Flag & Address nal operation is being performed and | reads address counter contents. | WriteDala 10 Writes data into DD or CG RAM, 40s | (Gor DD RAM | Read Data 1a Reads data from DD or CGRAM, 40 us CG or DD RAM tes are maximum times when fep oF fose is 250 kHz. 1e changes when frequency changes, Ex: When fep or fose is 270 kHz: 40 js « 250/270 =37 ys. Display data RAM. CGRAM Character generator RAM ACC CG RAM address ADD DD RAM aukiress, corresponds o cursor adress ac Addiess counter used for both DD and CG RAM addresses, UD=1 Increment WD=0 Decrement SI ‘Accompanies display shift Display shit YC=0 Cursor move Shift to the ight; RAL=O0 Shift w the le Sbits, DL = 0:4 bits line, N= 01 Hine 5x 10 dots, F= 05 x 7 dots internal operation BF = 0 Can accept instruction

You might also like