A TextLCD interface for driving 4-bit 16x2 KS0066U LCD
Dependents: KS0066U4_16x2 LAB05_Oppgave4 LAB05_Oppgave2 LAB05_Oppgave3 ... more
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.
|
TextLCD.cpp@10:8d5383e2546e, 2015-09-20 (annotated)
- Committer:
- rlanghbv
- Date:
- Sun Sep 20 17:05:30 2015 +0000
- Revision:
- 10:8d5383e2546e
- Parent:
- 9:9529e943259c
- Child:
- 15:dc4f024fd1aa
Warning fix/ Code Cleanup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlanghbv | 1:8902f6be12a5 | 1 | |
rlanghbv | 1:8902f6be12a5 | 2 | #include "mbed.h" |
rlanghbv | 1:8902f6be12a5 | 3 | #include "TextLCD.h" |
rlanghbv | 1:8902f6be12a5 | 4 | |
rlanghbv | 1:8902f6be12a5 | 5 | #define SET_EN() (LCD_EN=1) |
rlanghbv | 1:8902f6be12a5 | 6 | #define SET_RS() (LCD_RS=1) |
rlanghbv | 1:8902f6be12a5 | 7 | #define SET_RW() (LCD_RW=1) |
rlanghbv | 1:8902f6be12a5 | 8 | |
rlanghbv | 1:8902f6be12a5 | 9 | #define CLEAR_EN() (LCD_EN=0) |
rlanghbv | 1:8902f6be12a5 | 10 | #define CLEAR_RS() (LCD_RS=0) |
rlanghbv | 1:8902f6be12a5 | 11 | #define CLEAR_RW() (LCD_RW=0) |
rlanghbv | 1:8902f6be12a5 | 12 | |
rlanghbv | 1:8902f6be12a5 | 13 | |
rlanghbv | 4:264d9b06bf60 | 14 | TextLCD::TextLCD(PinName rs,PinName rw, PinName e, PinName d4, PinName d5, |
rlanghbv | 10:8d5383e2546e | 15 | PinName d6, PinName d7, const char* name=NULL ) : Stream(name), LCD_RS(rs),LCD_RW(rw), LCD_EN(e), |
rlanghbv | 10:8d5383e2546e | 16 | LCD_D4to7(d4,d5,d6,d7) |
rlanghbv | 10:8d5383e2546e | 17 | { |
rlanghbv | 4:264d9b06bf60 | 18 | CLEAR_RW(); |
rlanghbv | 4:264d9b06bf60 | 19 | wait_ms(50); // Wait for disp to turn on |
rlanghbv | 4:264d9b06bf60 | 20 | |
rlanghbv | 3:d2f70de20dbe | 21 | CLEAR_EN(); //EN =0 |
rlanghbv | 4:264d9b06bf60 | 22 | CLEAR_RS(); |
rlanghbv | 3:d2f70de20dbe | 23 | |
rlanghbv | 4:264d9b06bf60 | 24 | init_4BitMode2LinesDisplayOn(); //enabler 4 bit mode |
rlanghbv | 4:264d9b06bf60 | 25 | wait_ms(1); |
rlanghbv | 4:264d9b06bf60 | 26 | |
rlanghbv | 4:264d9b06bf60 | 27 | lcdComand(0x00); |
rlanghbv | 3:d2f70de20dbe | 28 | wait_us(100); |
rlanghbv | 3:d2f70de20dbe | 29 | |
rlanghbv | 4:264d9b06bf60 | 30 | lcdComand(0x28);//(0b0010 1000); // 4- bit mode LCD 2 line 16x2 Matrix |
rlanghbv | 3:d2f70de20dbe | 31 | wait_us(100); |
rlanghbv | 4:264d9b06bf60 | 32 | |
rlanghbv | 8:b12188ddd403 | 33 | lcdComand(0xE);//(0b0000 1110); |
rlanghbv | 3:d2f70de20dbe | 34 | wait_us(100); |
rlanghbv | 8:b12188ddd403 | 35 | lcdComand(0x01); //(0b00000001); |
rlanghbv | 3:d2f70de20dbe | 36 | wait_us(100); |
rlanghbv | 8:b12188ddd403 | 37 | lcdComand(0x06);//(0b00000110); |
rlanghbv | 3:d2f70de20dbe | 38 | wait_us(100); |
rlanghbv | 9:9529e943259c | 39 | gotoxy(1,1); |
rlanghbv | 1:8902f6be12a5 | 40 | } |
rlanghbv | 1:8902f6be12a5 | 41 | |
rlanghbv | 1:8902f6be12a5 | 42 | // Causes the LCD-module to read the data on the data input pins |
rlanghbv | 1:8902f6be12a5 | 43 | void TextLCD::pulseEn() |
rlanghbv | 1:8902f6be12a5 | 44 | { |
rlanghbv | 1:8902f6be12a5 | 45 | SET_EN(); // EN = 1 for L-to-H / |
rlanghbv | 1:8902f6be12a5 | 46 | wait_us(200); |
rlanghbv | 1:8902f6be12a5 | 47 | CLEAR_EN(); // EN = 0 for H-to-L |
rlanghbv | 1:8902f6be12a5 | 48 | } |
rlanghbv | 1:8902f6be12a5 | 49 | |
rlanghbv | 1:8902f6be12a5 | 50 | //Writes the low lible of data to the LCD-module data pins D4 to D7 |
rlanghbv | 1:8902f6be12a5 | 51 | void TextLCD::writeLcdBitD4toD7(char data) |
rlanghbv | 1:8902f6be12a5 | 52 | { |
rlanghbv | 8:b12188ddd403 | 53 | LCD_D4to7=data; |
rlanghbv | 1:8902f6be12a5 | 54 | } |
rlanghbv | 1:8902f6be12a5 | 55 | |
rlanghbv | 1:8902f6be12a5 | 56 | //Enable 4 bit mode From KS0066U Documentation |
rlanghbv | 1:8902f6be12a5 | 57 | void TextLCD::init_4BitMode2LinesDisplayOn() |
rlanghbv | 1:8902f6be12a5 | 58 | { |
rlanghbv | 1:8902f6be12a5 | 59 | //Start by selecting configuration mode |
rlanghbv | 1:8902f6be12a5 | 60 | CLEAR_RS(); |
rlanghbv | 1:8902f6be12a5 | 61 | CLEAR_RW(); |
rlanghbv | 1:8902f6be12a5 | 62 | |
rlanghbv | 4:264d9b06bf60 | 63 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
rlanghbv | 4:264d9b06bf60 | 64 | writeLcdBitD4toD7(0x3); |
rlanghbv | 4:264d9b06bf60 | 65 | pulseEn(); |
rlanghbv | 4:264d9b06bf60 | 66 | wait_ms(2); |
rlanghbv | 4:264d9b06bf60 | 67 | writeLcdBitD4toD7(0x3); |
rlanghbv | 4:264d9b06bf60 | 68 | pulseEn(); |
rlanghbv | 4:264d9b06bf60 | 69 | wait_ms(2); |
rlanghbv | 4:264d9b06bf60 | 70 | writeLcdBitD4toD7(0x3); |
rlanghbv | 4:264d9b06bf60 | 71 | pulseEn(); |
rlanghbv | 4:264d9b06bf60 | 72 | wait_ms(2); |
rlanghbv | 4:264d9b06bf60 | 73 | |
rlanghbv | 1:8902f6be12a5 | 74 | //Enable 4 bit mode From KS0066U Documentation |
rlanghbv | 1:8902f6be12a5 | 75 | writeLcdBitD4toD7(0x2);//(0b0010); // Select 4- bit start |
rlanghbv | 1:8902f6be12a5 | 76 | pulseEn(); //LCD exec function |
rlanghbv | 1:8902f6be12a5 | 77 | wait_us(100); |
rlanghbv | 1:8902f6be12a5 | 78 | |
rlanghbv | 1:8902f6be12a5 | 79 | writeLcdBitD4toD7(0x2);////(0b0010); // Select 4- bit start (Already on the out port no need to write once more ) |
rlanghbv | 1:8902f6be12a5 | 80 | pulseEn(); //LCD exec function |
rlanghbv | 1:8902f6be12a5 | 81 | wait_us(100); |
rlanghbv | 1:8902f6be12a5 | 82 | |
rlanghbv | 1:8902f6be12a5 | 83 | writeLcdBitD4toD7(0xC);//(0b1100); // 2 Lines + Disp On |
rlanghbv | 1:8902f6be12a5 | 84 | pulseEn(); //LCD exec function |
rlanghbv | 1:8902f6be12a5 | 85 | wait_us(100); |
rlanghbv | 1:8902f6be12a5 | 86 | } |
rlanghbv | 1:8902f6be12a5 | 87 | |
rlanghbv | 1:8902f6be12a5 | 88 | |
rlanghbv | 1:8902f6be12a5 | 89 | //Writes the byte comand to the LCD-module using 4 bits mode |
rlanghbv | 1:8902f6be12a5 | 90 | void TextLCD::lcdComand(unsigned char cmd) |
rlanghbv | 1:8902f6be12a5 | 91 | { |
rlanghbv | 1:8902f6be12a5 | 92 | writeLcdBitD4toD7(cmd>>4); //Write the first high cmd nibble |
rlanghbv | 1:8902f6be12a5 | 93 | |
rlanghbv | 1:8902f6be12a5 | 94 | CLEAR_RS(); // RS = 0 for command |
rlanghbv | 1:8902f6be12a5 | 95 | CLEAR_RW(); // RW = 0 for write |
rlanghbv | 1:8902f6be12a5 | 96 | |
rlanghbv | 1:8902f6be12a5 | 97 | pulseEn(); //EN Hi-Lo |
rlanghbv | 1:8902f6be12a5 | 98 | |
rlanghbv | 1:8902f6be12a5 | 99 | writeLcdBitD4toD7(cmd); //Write the second low cmd nibble |
rlanghbv | 1:8902f6be12a5 | 100 | |
rlanghbv | 1:8902f6be12a5 | 101 | pulseEn(); //EN H to Lo |
rlanghbv | 1:8902f6be12a5 | 102 | |
rlanghbv | 1:8902f6be12a5 | 103 | wait_us(100); //wait |
rlanghbv | 1:8902f6be12a5 | 104 | } |
rlanghbv | 1:8902f6be12a5 | 105 | |
rlanghbv | 1:8902f6be12a5 | 106 | void TextLCD::lcdData(unsigned char data) |
rlanghbv | 1:8902f6be12a5 | 107 | { |
rlanghbv | 1:8902f6be12a5 | 108 | writeLcdBitD4toD7(data>>4); //Write the first high data nibble |
rlanghbv | 1:8902f6be12a5 | 109 | |
rlanghbv | 1:8902f6be12a5 | 110 | SET_RS(); // RS = 1 for data |
rlanghbv | 1:8902f6be12a5 | 111 | CLEAR_RW(); // RW = 0 for write |
rlanghbv | 1:8902f6be12a5 | 112 | pulseEn(); // EN H to Lo |
rlanghbv | 1:8902f6be12a5 | 113 | wait_us(100); //wait |
rlanghbv | 1:8902f6be12a5 | 114 | |
rlanghbv | 1:8902f6be12a5 | 115 | writeLcdBitD4toD7(data); |
rlanghbv | 1:8902f6be12a5 | 116 | |
rlanghbv | 1:8902f6be12a5 | 117 | pulseEn(); //EN H to Lo |
rlanghbv | 1:8902f6be12a5 | 118 | wait_us(100); //wait |
rlanghbv | 1:8902f6be12a5 | 119 | } |
rlanghbv | 1:8902f6be12a5 | 120 | |
rlanghbv | 1:8902f6be12a5 | 121 | //Moves cursor to the X,Y position |
rlanghbv | 2:f0a520c95838 | 122 | void TextLCD::gotoxy(int x, int y) |
rlanghbv | 1:8902f6be12a5 | 123 | { |
rlanghbv | 1:8902f6be12a5 | 124 | unsigned char firstCharAdr[]= { 0x80,0xc0,0x94,0xD4}; |
rlanghbv | 1:8902f6be12a5 | 125 | lcdComand(firstCharAdr[y-1]+ x -1); |
rlanghbv | 1:8902f6be12a5 | 126 | |
rlanghbv | 1:8902f6be12a5 | 127 | wait_us(100); |
rlanghbv | 1:8902f6be12a5 | 128 | } |
rlanghbv | 1:8902f6be12a5 | 129 | |
rlanghbv | 1:8902f6be12a5 | 130 | int TextLCD::_putc(int value) |
rlanghbv | 1:8902f6be12a5 | 131 | { |
rlanghbv | 1:8902f6be12a5 | 132 | lcdData(value); |
rlanghbv | 1:8902f6be12a5 | 133 | return value; |
rlanghbv | 1:8902f6be12a5 | 134 | } |
rlanghbv | 1:8902f6be12a5 | 135 | |
rlanghbv | 4:264d9b06bf60 | 136 | int TextLCD::_getc() |
rlanghbv | 1:8902f6be12a5 | 137 | { |
rlanghbv | 4:264d9b06bf60 | 138 | return -1; |
rlanghbv | 1:8902f6be12a5 | 139 | } |