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:
Tue Sep 22 08:14:07 2015 +0000
Revision:
19:241842336d78
Parent:
18:68125c2172fd
Doc Update

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 14:393116a59d2f 27 * Simple example:
rlanghbv 4:264d9b06bf60 28 * @code
rlanghbv 4:264d9b06bf60 29 * #include "mbed.h"
rlanghbv 4:264d9b06bf60 30 * #include "TextLCD.h"
rlanghbv 6:c80d81c3be4d 31 *
rlanghbv 4:264d9b06bf60 32 * TextLCD lcd(D11,D10,D9,D5,D4,D3,D2);
rlanghbv 4:264d9b06bf60 33 * int main()
rlanghbv 4:264d9b06bf60 34 * {
rlanghbv 4:264d9b06bf60 35 * lcd.gotoxy(1,1);
rlanghbv 4:264d9b06bf60 36 * lcd.printf("Hello");
rlanghbv 4:264d9b06bf60 37 *
rlanghbv 4:264d9b06bf60 38 * lcd.gotoxy(1,2);
rlanghbv 12:296a9b15ecd1 39 * lcd.printf(" World");
rlanghbv 4:264d9b06bf60 40 *
rlanghbv 4:264d9b06bf60 41 * while(1) {
rlanghbv 4:264d9b06bf60 42 * wait_ms(300);
rlanghbv 4:264d9b06bf60 43 * }
rlanghbv 4:264d9b06bf60 44 * }
rlanghbv 4:264d9b06bf60 45 * @endcode
rlanghbv 4:264d9b06bf60 46 */
rlanghbv 6:c80d81c3be4d 47 class TextLCD : public Stream
rlanghbv 1:8902f6be12a5 48 {
rlanghbv 1:8902f6be12a5 49 public:
rlanghbv 1:8902f6be12a5 50
rlanghbv 4:264d9b06bf60 51
rlanghbv 6:c80d81c3be4d 52 /** Create a TextLCD interface and initiated 16x2 char mode
rlanghbv 6:c80d81c3be4d 53 *
rlanghbv 6:c80d81c3be4d 54 * @param rs Instruction/data control line
rlanghbv 6:c80d81c3be4d 55 * @param rw Read/Write (is forced to '1')
rlanghbv 6:c80d81c3be4d 56 * @param e Enable line (clock)
rlanghbv 6:c80d81c3be4d 57 * @param d4-d7 Data lines for using as a 4-bit interface
rlanghbv 9:9529e943259c 58 * @param name I/O stream name (Optional)
rlanghbv 11:0af18f5aa473 59 * Stream example:
rlanghbv 9:9529e943259c 60 * @code
rlanghbv 9:9529e943259c 61 * #include "mbed.h"
rlanghbv 9:9529e943259c 62 * #include "TextLCD.h"
rlanghbv 9:9529e943259c 63 *
rlanghbv 14:393116a59d2f 64 * TextLCD lcd(D11,D10,D9,D5,D4,D3,D2,"lcdOut");
rlanghbv 9:9529e943259c 65 * int main()
rlanghbv 9:9529e943259c 66 * {
rlanghbv 9:9529e943259c 67 * freopen("/lcdOut", "w", stdout);
rlanghbv 9:9529e943259c 68 * printf("Hello World");
rlanghbv 9:9529e943259c 69 * while(1) {
rlanghbv 9:9529e943259c 70 * wait_ms(300);
rlanghbv 9:9529e943259c 71 * }
rlanghbv 9:9529e943259c 72 * }
rlanghbv 11:0af18f5aa473 73 * @endcode
rlanghbv 6:c80d81c3be4d 74 */
rlanghbv 6:c80d81c3be4d 75 TextLCD(PinName rs,PinName rw, PinName e, PinName d4, PinName d5,
rlanghbv 15:dc4f024fd1aa 76 PinName d6, PinName d7,const char* name=NULL) ;
rlanghbv 6:c80d81c3be4d 77
rlanghbv 6:c80d81c3be4d 78 /** Writes a Command to the LCD-module
rlanghbv 4:264d9b06bf60 79 *
rlanghbv 6:c80d81c3be4d 80 * @param cmd command to be sendt to the LCD-Controller
rlanghbv 4:264d9b06bf60 81 */
rlanghbv 6:c80d81c3be4d 82 void lcdComand(unsigned char cmd);
rlanghbv 6:c80d81c3be4d 83
rlanghbv 6:c80d81c3be4d 84
rlanghbv 6:c80d81c3be4d 85 /** Writes charecters to the LCD display
rlanghbv 6:c80d81c3be4d 86 *
rlanghbv 6:c80d81c3be4d 87 * @param data char to be sendt to the LCD-Controller
rlanghbv 6:c80d81c3be4d 88 */
rlanghbv 6:c80d81c3be4d 89 void lcdData(unsigned char data);
rlanghbv 6:c80d81c3be4d 90
rlanghbv 4:264d9b06bf60 91 /** moves text cursor to a screen column and row
rlanghbv 4:264d9b06bf60 92 *
rlanghbv 4:264d9b06bf60 93 * @param column The horizontal position from the left, indexed from 0
rlanghbv 4:264d9b06bf60 94 * @param row The vertical position from the top, indexed from 0
rlanghbv 4:264d9b06bf60 95 */
rlanghbv 4:264d9b06bf60 96 void gotoxy(int , int );
rlanghbv 6:c80d81c3be4d 97 #if DOXYGEN_ONLY
rlanghbv 6:c80d81c3be4d 98 /** Write a character to the LCD
rlanghbv 6:c80d81c3be4d 99 *
rlanghbv 6:c80d81c3be4d 100 * @param c The character to write to the display
rlanghbv 6:c80d81c3be4d 101 */
rlanghbv 6:c80d81c3be4d 102 int putc(int c);
rlanghbv 6:c80d81c3be4d 103
rlanghbv 6:c80d81c3be4d 104 /** Write a formatted string to the LCD
rlanghbv 6:c80d81c3be4d 105 *
rlanghbv 6:c80d81c3be4d 106 * @param format A printf-style format string, followed by the
rlanghbv 6:c80d81c3be4d 107 * variables to use in formatting the string.
rlanghbv 6:c80d81c3be4d 108 */
rlanghbv 6:c80d81c3be4d 109 int printf(const char* format, ...);
rlanghbv 6:c80d81c3be4d 110 #endif
rlanghbv 4:264d9b06bf60 111 protected:
rlanghbv 18:68125c2172fd 112 /** Writes the low nible of data to the LCD-module
rlanghbv 18:68125c2172fd 113 *
rlanghbv 18:68125c2172fd 114 * @param data Writes the low-nible to the LCD data pins D4 to D7
rlanghbv 18:68125c2172fd 115 */
rlanghbv 18:68125c2172fd 116 void writeLcdBitD4toD7(char data);
rlanghbv 18:68125c2172fd 117
rlanghbv 19:241842336d78 118 /** Causes the LCD-module to read the data on the data input pins
rlanghbv 19:241842336d78 119 * EN = 1 for L-to-H /
rlanghbv 19:241842336d78 120 * EN = 0 for H-to-L
rlanghbv 19:241842336d78 121 */
rlanghbv 4:264d9b06bf60 122 void pulseEn();
rlanghbv 19:241842336d78 123
rlanghbv 19:241842336d78 124 /** Enable 4 bit mode From KS0066U Documentation
rlanghbv 19:241842336d78 125 */
rlanghbv 1:8902f6be12a5 126 void init_4BitMode2LinesDisplayOn();
rlanghbv 1:8902f6be12a5 127 // Stream implementation functions
rlanghbv 1:8902f6be12a5 128 virtual int _putc(int value);
rlanghbv 1:8902f6be12a5 129 virtual int _getc();
rlanghbv 6:c80d81c3be4d 130
rlanghbv 3:d2f70de20dbe 131 DigitalOut LCD_RS,LCD_RW,LCD_EN;
rlanghbv 8:b12188ddd403 132 BusOut LCD_D4to7;
rlanghbv 1:8902f6be12a5 133 };
rlanghbv 1:8902f6be12a5 134
rlanghbv 1:8902f6be12a5 135 #endif /* LCD_H_ */