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 10:36:08 2015 +0000
Revision:
1:8902f6be12a5
Child:
2:f0a520c95838
Changed to .cpp class lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rlanghbv 1:8902f6be12a5 1 /*
rlanghbv 1:8902f6be12a5 2 * LCD.c
rlanghbv 1:8902f6be12a5 3 *
rlanghbv 1:8902f6be12a5 4 * Created: 20.01.2012 13:05:38
rlanghbv 1:8902f6be12a5 5 * Author: rul
rlanghbv 1:8902f6be12a5 6 */
rlanghbv 1:8902f6be12a5 7
rlanghbv 1:8902f6be12a5 8 #include "mbed.h"
rlanghbv 1:8902f6be12a5 9 #include "LCD_IO_MAP.h"
rlanghbv 1:8902f6be12a5 10 #include "TextLCD.h"
rlanghbv 1:8902f6be12a5 11
rlanghbv 1:8902f6be12a5 12 #define SET_EN() (LCD_EN=1)
rlanghbv 1:8902f6be12a5 13 #define SET_RS() (LCD_RS=1)
rlanghbv 1:8902f6be12a5 14 #define SET_RW() (LCD_RW=1)
rlanghbv 1:8902f6be12a5 15
rlanghbv 1:8902f6be12a5 16 #define CLEAR_EN() (LCD_EN=0)
rlanghbv 1:8902f6be12a5 17 #define CLEAR_RS() (LCD_RS=0)
rlanghbv 1:8902f6be12a5 18 #define CLEAR_RW() (LCD_RW=0)
rlanghbv 1:8902f6be12a5 19
rlanghbv 1:8902f6be12a5 20
rlanghbv 1:8902f6be12a5 21 //Enables the LCD Module for use 2-Lines...
rlanghbv 1:8902f6be12a5 22 TextLCD::TextLCD()
rlanghbv 1:8902f6be12a5 23 {
rlanghbv 1:8902f6be12a5 24
rlanghbv 1:8902f6be12a5 25 CLEAR_EN(); //EN =0
rlanghbv 1:8902f6be12a5 26 wait_ms(500);
rlanghbv 1:8902f6be12a5 27
rlanghbv 1:8902f6be12a5 28 init_4BitMode2LinesDisplayOn(); //enabler 4 bit mode
rlanghbv 1:8902f6be12a5 29 wait_us(100);
rlanghbv 1:8902f6be12a5 30
rlanghbv 1:8902f6be12a5 31 lcdComand(0x28);//(0b0010 1000); // 4- bit mode LCD 2 line 5x7 Matrix
rlanghbv 1:8902f6be12a5 32 wait_us(100);
rlanghbv 1:8902f6be12a5 33 lcdComand(0xE);//(0b0000 1110); //Hva gj�r vi her ?
rlanghbv 1:8902f6be12a5 34 wait_us(100);
rlanghbv 1:8902f6be12a5 35 lcdComand(0x01); //(0b00000001); //Hva gj�r vi her ?
rlanghbv 1:8902f6be12a5 36 wait_us(100);
rlanghbv 1:8902f6be12a5 37 lcdComand(0x06);//(0b00000110); //Hva gj�r vi her ?
rlanghbv 1:8902f6be12a5 38 wait_us(100);
rlanghbv 1:8902f6be12a5 39 }
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 1:8902f6be12a5 53 if ( ( data >> 3) & 0x01) LCD_D7=1;
rlanghbv 1:8902f6be12a5 54 else LCD_D7=0;
rlanghbv 1:8902f6be12a5 55 if ( ( data >> 2) & 0x01) LCD_D6=1;
rlanghbv 1:8902f6be12a5 56 else LCD_D6=0;
rlanghbv 1:8902f6be12a5 57 if ( ( data >> 1) & 0x01) LCD_D5=1;
rlanghbv 1:8902f6be12a5 58 else LCD_D5=0;
rlanghbv 1:8902f6be12a5 59 if ( data & 0x01) LCD_D4=1;
rlanghbv 1:8902f6be12a5 60 else LCD_D4=0;
rlanghbv 1:8902f6be12a5 61 }
rlanghbv 1:8902f6be12a5 62
rlanghbv 1:8902f6be12a5 63 //Enable 4 bit mode From KS0066U Documentation
rlanghbv 1:8902f6be12a5 64 void TextLCD::init_4BitMode2LinesDisplayOn()
rlanghbv 1:8902f6be12a5 65 {
rlanghbv 1:8902f6be12a5 66 //Start by selecting configuration mode
rlanghbv 1:8902f6be12a5 67 CLEAR_RS();
rlanghbv 1:8902f6be12a5 68 CLEAR_RW();
rlanghbv 1:8902f6be12a5 69
rlanghbv 1:8902f6be12a5 70 //Enable 4 bit mode From KS0066U Documentation
rlanghbv 1:8902f6be12a5 71 writeLcdBitD4toD7(0x2);//(0b0010); // Select 4- bit start
rlanghbv 1:8902f6be12a5 72 pulseEn(); //LCD exec function
rlanghbv 1:8902f6be12a5 73 wait_us(100);
rlanghbv 1:8902f6be12a5 74
rlanghbv 1:8902f6be12a5 75 writeLcdBitD4toD7(0x2);////(0b0010); // Select 4- bit start (Already on the out port no need to write once more )
rlanghbv 1:8902f6be12a5 76 pulseEn(); //LCD exec function
rlanghbv 1:8902f6be12a5 77 wait_us(100);
rlanghbv 1:8902f6be12a5 78
rlanghbv 1:8902f6be12a5 79 writeLcdBitD4toD7(0xC);//(0b1100); // 2 Lines + Disp On
rlanghbv 1:8902f6be12a5 80 pulseEn(); //LCD exec function
rlanghbv 1:8902f6be12a5 81 wait_us(100);
rlanghbv 1:8902f6be12a5 82 }
rlanghbv 1:8902f6be12a5 83
rlanghbv 1:8902f6be12a5 84
rlanghbv 1:8902f6be12a5 85 //Writes the byte comand to the LCD-module using 4 bits mode
rlanghbv 1:8902f6be12a5 86 void TextLCD::lcdComand(unsigned char cmd)
rlanghbv 1:8902f6be12a5 87 {
rlanghbv 1:8902f6be12a5 88 writeLcdBitD4toD7(cmd>>4); //Write the first high cmd nibble
rlanghbv 1:8902f6be12a5 89
rlanghbv 1:8902f6be12a5 90 CLEAR_RS(); // RS = 0 for command
rlanghbv 1:8902f6be12a5 91 CLEAR_RW(); // RW = 0 for write
rlanghbv 1:8902f6be12a5 92
rlanghbv 1:8902f6be12a5 93 pulseEn(); //EN Hi-Lo
rlanghbv 1:8902f6be12a5 94
rlanghbv 1:8902f6be12a5 95 writeLcdBitD4toD7(cmd); //Write the second low cmd nibble
rlanghbv 1:8902f6be12a5 96
rlanghbv 1:8902f6be12a5 97 pulseEn(); //EN H to Lo
rlanghbv 1:8902f6be12a5 98
rlanghbv 1:8902f6be12a5 99 wait_us(100); //wait
rlanghbv 1:8902f6be12a5 100 }
rlanghbv 1:8902f6be12a5 101
rlanghbv 1:8902f6be12a5 102 void TextLCD::lcdData(unsigned char data)
rlanghbv 1:8902f6be12a5 103 {
rlanghbv 1:8902f6be12a5 104 writeLcdBitD4toD7(data>>4); //Write the first high data nibble
rlanghbv 1:8902f6be12a5 105
rlanghbv 1:8902f6be12a5 106 SET_RS(); // RS = 1 for data
rlanghbv 1:8902f6be12a5 107 CLEAR_RW(); // RW = 0 for write
rlanghbv 1:8902f6be12a5 108 pulseEn(); // EN H to Lo
rlanghbv 1:8902f6be12a5 109 wait_us(100); //wait
rlanghbv 1:8902f6be12a5 110
rlanghbv 1:8902f6be12a5 111 writeLcdBitD4toD7(data);
rlanghbv 1:8902f6be12a5 112
rlanghbv 1:8902f6be12a5 113 pulseEn(); //EN H to Lo
rlanghbv 1:8902f6be12a5 114 wait_us(100); //wait
rlanghbv 1:8902f6be12a5 115 }
rlanghbv 1:8902f6be12a5 116
rlanghbv 1:8902f6be12a5 117
rlanghbv 1:8902f6be12a5 118
rlanghbv 1:8902f6be12a5 119 //Moves cursor to the X,Y position
rlanghbv 1:8902f6be12a5 120 void TextLCD::gotoxy(unsigned char x, unsigned char y)
rlanghbv 1:8902f6be12a5 121 {
rlanghbv 1:8902f6be12a5 122 unsigned char firstCharAdr[]= { 0x80,0xc0,0x94,0xD4};
rlanghbv 1:8902f6be12a5 123 lcdComand(firstCharAdr[y-1]+ x -1);
rlanghbv 1:8902f6be12a5 124
rlanghbv 1:8902f6be12a5 125 wait_us(100);
rlanghbv 1:8902f6be12a5 126 }
rlanghbv 1:8902f6be12a5 127
rlanghbv 1:8902f6be12a5 128 int TextLCD::_putc(int value)
rlanghbv 1:8902f6be12a5 129 {
rlanghbv 1:8902f6be12a5 130 lcdData(value);
rlanghbv 1:8902f6be12a5 131 return value;
rlanghbv 1:8902f6be12a5 132 }
rlanghbv 1:8902f6be12a5 133
rlanghbv 1:8902f6be12a5 134 int TextLCD::_getc() {
rlanghbv 1:8902f6be12a5 135 return -1;
rlanghbv 1:8902f6be12a5 136 }
rlanghbv 1:8902f6be12a5 137
rlanghbv 1:8902f6be12a5 138 //Writes a string to the LCD Display
rlanghbv 1:8902f6be12a5 139 void TextLCD::lcd_print(char *str)
rlanghbv 1:8902f6be12a5 140 {
rlanghbv 1:8902f6be12a5 141 unsigned char i =0;
rlanghbv 1:8902f6be12a5 142 while(str[i] !=0) {
rlanghbv 1:8902f6be12a5 143 lcdData(str[i]);
rlanghbv 1:8902f6be12a5 144 i++;
rlanghbv 1:8902f6be12a5 145 }
rlanghbv 1:8902f6be12a5 146 }