A TextLCD interface for driving 4-bit 16x2 KS0066U LCD

Dependents:   KS0066U4_16x2 LAB05_Oppgave4 LAB05_Oppgave2 LAB05_Oppgave3 ... more

Import program

00001  #include "mbed.h"
00002  #include "TextLCD.h"
00003  
00004  TextLCD lcd(D11,D10,D9,D5,D4,D3,D2);
00005  int main()
00006  {    
00007     lcd.gotoxy(1,1);
00008     lcd.printf("Hello");
00009  
00010     lcd.gotoxy(4,2);
00011     lcd.printf("World");
00012     
00013     while(1) {
00014         wait_ms(300);
00015     }
00016  }

Import library

Public Member Functions

TextLCD (PinName rs, PinName rw, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, const char *name=NULL)
Create a TextLCD interface and initiated 16x2 char mode.
void lcdComand (unsigned char cmd)
Writes a Command to the LCD-module.
void lcdData (unsigned char data)
Writes charecters to the LCD display.
void gotoxy (int, int)
moves text cursor to a screen column and row
int putc (int c)
Write a character to the LCD.
int printf (const char *format,...)
Write a formatted string to the LCD.

Protected Member Functions

void writeLcdBitD4toD7 (char data)
Writes the low nible of data to the LCD-module.
void pulseEn ()
Causes the LCD-module to read the data on the data input pins EN = 1 for L-to-H / EN = 0 for H-to-L.
void init_4BitMode2LinesDisplayOn ()
Enable 4 bit mode From KS0066U Documentation.

/media/uploads/rlanghbv/lcdmoduletop.jpg /media/uploads/rlanghbv/lcdmodulebottom.jpg

Committer:
rlanghbv
Date:
Sun Sep 20 15:55:57 2015 +0000
Revision:
8:b12188ddd403
Parent:
7:5e18f9a04e62
Child:
9:9529e943259c
Code cleanup DigitalOut replaced by BusOut

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rlanghbv 4:264d9b06bf60 1 /* mbed TextLCD Library,4-bit 16x2 LCD for KS0066U
rlanghbv 6:c80d81c3be4d 2 * Copyright (c) 2015 Rune Langøy
rlanghbv 4:264d9b06bf60 3 *
rlanghbv 4:264d9b06bf60 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
rlanghbv 4:264d9b06bf60 5 * of this software and associated documentation files (the "Software"), to deal
rlanghbv 4:264d9b06bf60 6 * in the Software without restriction, including without limitation the rights
rlanghbv 4:264d9b06bf60 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rlanghbv 4:264d9b06bf60 8 * copies of the Software, and to permit persons to whom the Software is
rlanghbv 4:264d9b06bf60 9 * furnished to do so, subject to the following conditions:
rlanghbv 1:8902f6be12a5 10 *
rlanghbv 4:264d9b06bf60 11 * The above copyright notice and this permission notice shall be included in
rlanghbv 4:264d9b06bf60 12 * all copies or substantial portions of the Software.
rlanghbv 4:264d9b06bf60 13 *
rlanghbv 4:264d9b06bf60 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rlanghbv 4:264d9b06bf60 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rlanghbv 4:264d9b06bf60 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
rlanghbv 4:264d9b06bf60 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rlanghbv 4:264d9b06bf60 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rlanghbv 4:264d9b06bf60 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
rlanghbv 4:264d9b06bf60 20 * THE SOFTWARE.
rlanghbv 4:264d9b06bf60 21 */
rlanghbv 1:8902f6be12a5 22 #ifndef LCD_H_
rlanghbv 1:8902f6be12a5 23 #define LCD_H_
rlanghbv 4:264d9b06bf60 24
rlanghbv 6:c80d81c3be4d 25 /** A TextLCD interface for driving 4-bit 16x2 KS0066U LCD
rlanghbv 4:264d9b06bf60 26 *
rlanghbv 4:264d9b06bf60 27 * @code
rlanghbv 4:264d9b06bf60 28 * #include "mbed.h"
rlanghbv 4:264d9b06bf60 29 * #include "TextLCD.h"
rlanghbv 6:c80d81c3be4d 30 *
rlanghbv 4:264d9b06bf60 31 * TextLCD lcd(D11,D10,D9,D5,D4,D3,D2);
rlanghbv 4:264d9b06bf60 32 * int main()
rlanghbv 4:264d9b06bf60 33 * {
rlanghbv 4:264d9b06bf60 34 * lcd.gotoxy(1,1);
rlanghbv 4:264d9b06bf60 35 * lcd.printf("Hello");
rlanghbv 4:264d9b06bf60 36 *
rlanghbv 4:264d9b06bf60 37 * lcd.gotoxy(1,2);
rlanghbv 4:264d9b06bf60 38 * lcd.printf(" World");
rlanghbv 4:264d9b06bf60 39 *
rlanghbv 4:264d9b06bf60 40 * while(1) {
rlanghbv 4:264d9b06bf60 41 * wait_ms(300);
rlanghbv 4:264d9b06bf60 42 * }
rlanghbv 4:264d9b06bf60 43 * }
rlanghbv 4:264d9b06bf60 44 * @endcode
rlanghbv 4:264d9b06bf60 45 */
rlanghbv 6:c80d81c3be4d 46 class TextLCD : public Stream
rlanghbv 1:8902f6be12a5 47 {
rlanghbv 1:8902f6be12a5 48 public:
rlanghbv 1:8902f6be12a5 49
rlanghbv 4:264d9b06bf60 50
rlanghbv 6:c80d81c3be4d 51 /** Create a TextLCD interface and initiated 16x2 char mode
rlanghbv 6:c80d81c3be4d 52 *
rlanghbv 6:c80d81c3be4d 53 * @param rs Instruction/data control line
rlanghbv 6:c80d81c3be4d 54 * @param rw Read/Write (is forced to '1')
rlanghbv 6:c80d81c3be4d 55 * @param e Enable line (clock)
rlanghbv 6:c80d81c3be4d 56 * @param d4-d7 Data lines for using as a 4-bit interface
rlanghbv 6:c80d81c3be4d 57 */
rlanghbv 6:c80d81c3be4d 58 TextLCD(PinName rs,PinName rw, PinName e, PinName d4, PinName d5,
rlanghbv 6:c80d81c3be4d 59 PinName d6, PinName d7) ;
rlanghbv 6:c80d81c3be4d 60
rlanghbv 6:c80d81c3be4d 61 //
rlanghbv 6:c80d81c3be4d 62
rlanghbv 6:c80d81c3be4d 63 /** Writes the low nible of data to the LCD-module
rlanghbv 6:c80d81c3be4d 64 *
rlanghbv 6:c80d81c3be4d 65 * @param data Writes the low-nible to the LCD data pins D4 to D7
rlanghbv 6:c80d81c3be4d 66 */
rlanghbv 6:c80d81c3be4d 67 void writeLcdBitD4toD7(char data);
rlanghbv 6:c80d81c3be4d 68
rlanghbv 6:c80d81c3be4d 69 /** Writes a Command to the LCD-module
rlanghbv 4:264d9b06bf60 70 *
rlanghbv 6:c80d81c3be4d 71 * @param cmd command to be sendt to the LCD-Controller
rlanghbv 4:264d9b06bf60 72 */
rlanghbv 6:c80d81c3be4d 73 void lcdComand(unsigned char cmd);
rlanghbv 6:c80d81c3be4d 74
rlanghbv 6:c80d81c3be4d 75
rlanghbv 6:c80d81c3be4d 76 /** Writes charecters to the LCD display
rlanghbv 6:c80d81c3be4d 77 *
rlanghbv 6:c80d81c3be4d 78 * @param data char to be sendt to the LCD-Controller
rlanghbv 6:c80d81c3be4d 79 */
rlanghbv 6:c80d81c3be4d 80 void lcdData(unsigned char data);
rlanghbv 6:c80d81c3be4d 81
rlanghbv 4:264d9b06bf60 82 /** moves text cursor to a screen column and row
rlanghbv 4:264d9b06bf60 83 *
rlanghbv 4:264d9b06bf60 84 * @param column The horizontal position from the left, indexed from 0
rlanghbv 4:264d9b06bf60 85 * @param row The vertical position from the top, indexed from 0
rlanghbv 4:264d9b06bf60 86 */
rlanghbv 4:264d9b06bf60 87 void gotoxy(int , int );
rlanghbv 6:c80d81c3be4d 88 #if DOXYGEN_ONLY
rlanghbv 6:c80d81c3be4d 89 /** Write a character to the LCD
rlanghbv 6:c80d81c3be4d 90 *
rlanghbv 6:c80d81c3be4d 91 * @param c The character to write to the display
rlanghbv 6:c80d81c3be4d 92 */
rlanghbv 6:c80d81c3be4d 93 int putc(int c);
rlanghbv 6:c80d81c3be4d 94
rlanghbv 6:c80d81c3be4d 95 /** Write a formatted string to the LCD
rlanghbv 6:c80d81c3be4d 96 *
rlanghbv 6:c80d81c3be4d 97 * @param format A printf-style format string, followed by the
rlanghbv 6:c80d81c3be4d 98 * variables to use in formatting the string.
rlanghbv 6:c80d81c3be4d 99 */
rlanghbv 6:c80d81c3be4d 100 int printf(const char* format, ...);
rlanghbv 6:c80d81c3be4d 101 #endif
rlanghbv 4:264d9b06bf60 102 protected:
rlanghbv 4:264d9b06bf60 103 // EN = 1 for L-to-H /
rlanghbv 4:264d9b06bf60 104 // EN = 0 for H-to-L
rlanghbv 4:264d9b06bf60 105 // Causes the LCD-module to read the data on the data input pins
rlanghbv 4:264d9b06bf60 106 void pulseEn();
rlanghbv 1:8902f6be12a5 107 //Enable 4 bit mode From KS0066U Documentation
rlanghbv 1:8902f6be12a5 108 void init_4BitMode2LinesDisplayOn();
rlanghbv 6:c80d81c3be4d 109
rlanghbv 1:8902f6be12a5 110 // Stream implementation functions
rlanghbv 1:8902f6be12a5 111 virtual int _putc(int value);
rlanghbv 1:8902f6be12a5 112 virtual int _getc();
rlanghbv 6:c80d81c3be4d 113
rlanghbv 3:d2f70de20dbe 114 DigitalOut LCD_RS,LCD_RW,LCD_EN;
rlanghbv 8:b12188ddd403 115 BusOut LCD_D4to7;
rlanghbv 1:8902f6be12a5 116 };
rlanghbv 1:8902f6be12a5 117
rlanghbv 1:8902f6be12a5 118 #endif /* LCD_H_ */