EET 2261 Unit 12 Controlling LCD and Keypad Read Almy, Appendix E. Homework #12 and Lab #12 due next week. Quiz next week. Liquid-Crystal Display •The Dragon12 board has a liquid-crystal display (LCD) module that lets the programmer display text messages to the user. •Datasheet for SHZJ-A162A LCD module •The LCD module contains an LCD panel and an LCD microcontroller that has its own memory distinct from the HCS12’s memory. This microcontroller handles most of the work of displaying characters on the LCD panel. •Datasheet for S6A0069 LCD controller Dimensions of Our LCD Panel •The S6A0069 can control a variety of LCD panels. Variations include: •Number of lines (1 or 2) •Number of characters per line (16, 20, 40… ) •Size of matrix of dots that make up a character (5x8, 5x11). •Our panel has two lines of 16 characters per line, with a 5x8 matrix of dots for each character. 5x8 Dot Matrix Example •The figure to the right shows which of the dots in the 5x8 matrix are lit to display an A. •Normally we won’t have to worry about this level of detail. We’ll just send the ASCII code for A to the LCD module, and it will take care of lighting up the correct dots. •But we do have the ability to create custom characters if we wish. List of LCD Commands • See Table 7 on page 16 of the S6A0069 datasheet for a list of all commands that the LCD recognizes. • Most of these commands are used for initialization, such as specifying: • Whether our LCD panel has 1 line or 2. • Whether it displays characters with a 5x8 dot matrix or a 5x11 matrix. • Whether we want the cursor to be visible. • Whether we want the cursor to blink. Typical Initialization Code I/O Pins on the LCD Module •Figure and table from the SHZJA162A datasheet. LCD Connections on Dragon12 •LCD is connected to the HCS12’s Port K. •The LCD’s R/W pin is permanently grounded (through jumper J5), placing the LCD permanently in write mode. Figure from page 4 of the Dragon12 schematic diagrams. •Only 4 of the 8 bits on the LCD’s data bus are connected to the HCS12. This is a common practice, to save pins. Command or Data? •We can send two kinds of things to the LCD module: 1. Commands, such as: •Clear the display. •Move cursor to the 4th position on line 1. •Make the cursor blink. 2. Data, which is ASCII code for text to be displayed, such as “Rock on!” • We must set the RS (Register Select) bit LOW when we’re sending a command, and set it HIGH when we’re sending data. Executing the Command or Data •We must hold the EN line (which connects to the LCD module’s E pin) LOW most of the time. •After we’ve set the RS line to its correct level, and put a command or data on the bus, we must send a brief LOW-to-HIGH-to-LOW pulse on the EN line. This pulse is what actually tells the LCD module to perform the action we’ve requested. Steps for Sending a Command to the LCD • To send a command (such as Clear Screen) to the LCD, a program must: 1. Store the upper nibble of the command byte to Port K bits 2-5, and set Port K bit 0 LOW. (Bit 0 tells whether we’re sending a command or data.) 2. Pulse Port K bit 1 HIGH-then-LOW. 3. Store the lower nibble of the command byte to Port K bits 2-5 , and set Port K bit 0 LOW. 4. Pulse Port K bit 1 HIGH-then-LOW. Sample Code For Sending a Command (Assumes command byte is in Accumulator A) Steps for Sending Data to the LCD • To send data (such as the character H) to the LCD, a program must: 1. Store the upper nibble of the data byte to Port K bits 2-5, and set Port K bit 0 HIGH. 2. Pulse Port K bit 1 HIGH-then-LOW. 3. Store the lower nibble of the data byte to Port K bits 2-5 , and set Port K bit 0 HIGH. 4. Pulse Port K bit 1 HIGH-then-LOW. Code For Sending Data • You can easily modify the SendCommand subroutine (on the slide before the previous one) to create a SendData subroutine. Sample Code for Displaying a Character • Assuming we’ve set everything up correctly with our initialization code, we can send text to be displayed. The code below positions the cursor and then sends an H. ASCII Code • As defined in the 1960’s, ASCII code is a 7-bit code, whose values range from $0 to $7F (or %0111 1111). Thus it contains the codes for 128 characters, as shown on page 375 of our textbook. • The first 32 codes (from $0 to $1F) and the last code ($7F) were originally defined as non-printable control codes to control Teletype equipment. Most of these control codes are obsolete in today’s world. Extending the ASCII Code • Since the 1960’s, manufacturers have extended the ASCII code in two ways: 1. By redefining some of the now-obsolete control codes. 2. By extending the code from 7 bits to 8 bits, thus allowing for an additional 128 characters. • • One such widely used extension is the one that IBM used on its PCs: see http://en.wikipedia.org/wiki/Code_page_437. Our LCD module assigns different characters to these codes. Our LCD Module’s Character Set • On our LCD module: • Codes $00 through $07 are reserved for user-defined characters. • Codes $08 through $1F are unused. • Most of the codes in the range $20 through $7F agree with the standard ASCII set. • Codes $80 through $9F are unused. • Codes $A0 through $FF produce special characters. Dragon12-Plus2 Keypad Our Dragon12-Plus2 board has a keypad with 16 switches (or keys). The switches are arranged in a 4-by-4 matrix. Each row of this matrix is attached to one of the Port A pins PA4 to PA7. Each column of the matrix is attached to one of the Port A pins PA0 to PA3. See diagram on next slide. Dragon12 Keypad Connections Figure from p. 26 of Dragon12-Plus2 manual. Different from the Textbook’s Keypad Caution: the keypad connections on our Dragon12-Plus2 board are different from the connections assumed in the textbook’s discussion (as shown in the figures on page 340 of the textbook). Therefore, while the general principles of the book’s discussion do apply to our keypad, the details are different. Configuring the Keypad To use the keypad, we must first configure it: Configure Port A pins 0 to 3 as outputs. (These are the pins connected to the keypad columns.) Configure Port A pins 4 to 7 as inputs. (These are the pins connected to the keypad rows.) Identifying Which Key is Pressed General idea: Make one column output HIGH at a time (while other columns are LOW), and then check each row input to see if any of them are HIGH. If it is, then we know which key is being pressed. See next slide for modified version of keyboard scan routine from p. 27 of Dragon12 manual. Keyboard Scan Routine Modified from p. 27 of Dragon12 manual. Set PA0 high and PA1, PA2, PA3 low, and then test PA4-PA7. If no key is pressed, PA4-PA7 remain low. If PA7 = high, then key 12 is pressed. If PA6 = high, then key 8 is pressed. If PA5 = high, then key 4 is pressed. If PA4 = high, then key 0 is pressed. Set PA1 high and PA0, PA2, PA3 low, and then test PA4-PA7. If no key is pressed, PA4-PA7 remain low. If PA7 = high, then key 13 is pressed. If PA6 = high, then key 9 is pressed. If PA5 = high, then key 5 is pressed. If PA4 = high, then key 1 is pressed. Set PA2 high and PA0, PA1, PA3 low, and then test PA4-PA7. If no key is pressed, PA4-PA7 remain low. If PA7 = high, then key 14 is pressed. If PA6 = high, then key 10 is pressed. If PA5 = high, then key 6 is pressed. If PA4 = high, then key 2 is pressed. Set PA3 high and PA0, PA1, PA2 low, and then test PA4-PA7. If no key is pressed, PA4-PA7 remain low. If PA7 = high, then key 15 is pressed. If PA6 = high, then key 11 is pressed. If PA5 = high, then key 7 is pressed. If PA4 = high, then key 3 is pressed. Code That Implements the Keyboard Scan Routine Download the code from http://people.sinclair.edu/nickreeder/eet2261/ labs/lab12KeypadNumberToLEDs.txt . Key Numbers Versus Key Labels On previous slides we’ve been referring to keys by their numbers, starting with key #0 in upper left corner of keypad and ending with key #15 in lower right corner. Don’t confuse these key numbers with the labels on the keys, which are shown at right.