Chapter 3 LCD and Keyboard Interfacing STM 32
Chapter 3: LCD and Keyboard Interfacing STM 32 F 0 Arm Cortex M 0 Programming for Embedded Systems Using C Language with STM 32 F 0 Nucleo Board by Mazidi, et al. www. microdigitaled. com
The reasons why the LCD is replacing LEDs: 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. (The new OLED panels are relatively much more expensive except the very small ones. But their prices are dropping. The interface and programming to OLED are similar to graphic LCD. ) 3. Incorporation of the refreshing controller into the LCD itself, thereby relieving the CPU of the task of refreshing the LCD. 4. Ease of programming for both characters and graphics. 5. The extremely low power consumption of LCD (when the backlight is not used).
Pin 1 2 3 4 Symbol VSS VCC VEE RS I/O ---I 5 R/W I 6 7 8 9 10 11 12 13 14 E DB 0 DB 1 DB 2 DB 3 DB 4 DB 5 DB 6 DB 7 I I/O I/O Description Ground +5 V power supply Power supply to control contrast RS = 0 to select command register, RS = 1 to select data register R/W = 0 for write, R/W = 1 for read Enable The 8 -bit data bus The 4/8 -bit data bus Table 3 -1: Pin Descriptions for LCD
Figure 3 -1: Pin Positions for Various LCDs from Optrex
Code (Hex) 1 2 6 F 80 C 0 38 28 Command to LCD Instruction Register Clear display screen Return cursor home Increment cursor (shift cursor to right) Display on, cursor blinking Force cursor to the beginning of 1 st line Force cursor to the beginning of 2 nd line 2 lines and 5 x 7 character (8 -bit data, D 0 to D 7) 2 lines and 5 x 7 character (4 -bit data, D 4 to D 7) Table 3 -2: Some commonly used LCD Command Codes
Figure 3 -2: LCD Connection to Microcontroller
Notice the following for the connection in Figure 3 -2: 1. The LCD's data pins are connected to PORTX of the microcontroller. 2. The LCD's RS pin is connected to Pin 2 of PORTY of the microcontroller. 3. The LCD's R/W pin is connected to Pin 4 of PORTY of the microcontroller. 4. The LCD's E pin is connected to Pin 5 of PORTY of the microcontroller. 5. Both Ports X and Y are configured as output ports.
Figure 3 -3: LCD Connection for 4 -bit Data
where A 6 A 5 A 4 A 3 A 2 A 1 A 0= 0000000 to 0100111 for line 1 and A 6 A 5 A 4 A 3 A 2 A 1 A 0 = 1000000 to 1100111 Line 1 (min) Line 1 (max) Line 2 (min) Line 2 (max) DB 7 1 1 DB 6 0 0 1 1 DB 5 0 1 DB 4 0 0 DB 3 0 0 Table 3 -3: LCD Addressing Commands DB 2 0 1 DB 1 0 1 DB 0 0 1
Figure 3 -4: Cursor Addresses for Some LCDs
Figure 3 -4: Cursor Addresses for Some LCDs
Figure 3 -5: LCD Write Timing
Figure 3 -6: LCD Read Timing
Instruction RS R/W DB 7 DB 6 DB 5 DB 4 DB 3 DB 2 DB 1 DB 0 Description Clear display 0 0 0 0 0 1 Return Home 0 0 0 0 1 - Entry Mode Set 0 0 0 0 1 I/D S Display On/Off Control 0 0 0 1 D C B Cursor or Display shift 0 0 0 1 S/C R/L - - Function Set 0 0 1 DL N F - - Set CG RAM Address 0 0 0 1 Clears entire display and sets DD RAM address 0 in address counter Sets DD RAM address to 0 as address counter. Also, returns display being shifted to original positions. DD RAM contents remain unchanged. Sets cursor move direction and specifies shift of display. These operations are performed during data write and read. Sets On/Off of entire display (D), cursor On/Off (C), and blink of cursor position character (B). Moves cursor and shifts display without changing DD RAM contents. Sets interface data length (DL), number of display lines (L), and character font (F) Sets CG RAM address. CG RAM data is sent and received after this setting. AGC Set DD RAM Address 0 0 1 ADD Read Busy Flag & 0 1 BF AC Address Write Data CG or DD 1 0 Write Data RAM Read Data CG or DD 1 1 Read Data RAM Abbreviations: DD RAM: Display data RAM CG RAM: Character generator RAM AGC: CG RAM address ADD: DD RAM address, corresponds to cursor address AC: address counter used for both DD and CG RAM addresses I/D: 1 = Increment, 0: Decrement S =1: Accompanies display shift S/C: 1 = Display shift, 0: Cursor move R/L: 1: Shift to the right, 0: Shift to the left DL: 1 = 8 bits, 0 = 4 bits N: 1 = 2 -line, 0 = 1 -line F: 1 = 5 x 10 dots, 0 = 5 x 7 dots BF: 1 = Internal operation, 0 = Can accept instruction Execution Time (Max) 1. 64 ms 40µs 40µs Sets DD RAM address. DD RAM data is sent and received after this setting. Reads Busy flag (BF) indicating the internal operation is being performed and reads address counter contents. 40µs Writes data into DD or CG RAM. 40µs Reads data from DD or CG RAM. 40µs Table 3 -4: List of LCD Instructions 40µs
Figure 3 -7: Matrix Keyboard Connection to Ports
Example 3 -1 From Figure 3 -7, identify the row and column of the pressed key for each of the following. (a) D 3–D 0 = 1110 for the row, D 7–D 4= 1011 for the column (b) D 3–D 0 = 1101 for the row, D 7–D 4= 0111 for the column Solution: From Figure 3 -7, the row and column can be used to identify the key. (a) The row belongs to D 0 and the column belongs to D 6; therefore, the key number 2 was pressed. (b) The row belongs to D 1 and the column belongs to D 7; therefore, the key number 4 was pressed.
Figure 3 -8: The Flowchart for Key Press Detection and Identification
Figure 3 -9: Switch contact bounces
- Slides: 18