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

Revision:
1:8902f6be12a5
Child:
2:f0a520c95838
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.cpp	Sun Sep 20 10:36:08 2015 +0000
@@ -0,0 +1,146 @@
+/*
+ * LCD.c
+ *
+ * Created: 20.01.2012 13:05:38
+ *  Author: rul
+ */
+
+#include "mbed.h"
+#include "LCD_IO_MAP.h"
+#include "TextLCD.h"
+
+#define SET_EN() (LCD_EN=1)
+#define SET_RS() (LCD_RS=1)
+#define SET_RW() (LCD_RW=1)
+
+#define CLEAR_EN() (LCD_EN=0)
+#define CLEAR_RS() (LCD_RS=0)
+#define CLEAR_RW() (LCD_RW=0)
+
+
+//Enables the LCD Module for use 2-Lines...
+TextLCD::TextLCD()
+{
+
+    CLEAR_EN(); //EN =0
+    wait_ms(500);
+
+    init_4BitMode2LinesDisplayOn();  //enabler 4 bit mode
+    wait_us(100);
+
+    lcdComand(0x28);//(0b0010 1000);	// 4- bit mode   LCD 2 line 5x7 Matrix
+    wait_us(100);
+    lcdComand(0xE);//(0b0000 1110); //Hva gj�r vi her ?
+    wait_us(100);
+    lcdComand(0x01); //(0b00000001); //Hva gj�r vi her ?
+    wait_us(100);
+    lcdComand(0x06);//(0b00000110); //Hva gj�r vi her ?
+    wait_us(100);
+}
+
+
+// Causes the LCD-module to read the data on the data input pins
+void TextLCD::pulseEn()
+{
+    SET_EN();		// EN = 1 for L-to-H /
+    wait_us(200);
+    CLEAR_EN();		// EN = 0 for H-to-L
+}
+
+//Writes the low lible of data to the LCD-module data pins D4 to D7
+void TextLCD::writeLcdBitD4toD7(char data)
+{
+    if ( ( data >> 3) & 0x01) LCD_D7=1;
+    else  LCD_D7=0;
+    if ( ( data >> 2) & 0x01) LCD_D6=1;
+    else  LCD_D6=0;
+    if ( ( data >> 1) & 0x01) LCD_D5=1;
+    else  LCD_D5=0;
+    if (  data  & 0x01)  LCD_D4=1;
+    else  LCD_D4=0;
+}
+
+//Enable 4 bit mode  From KS0066U Documentation
+void TextLCD::init_4BitMode2LinesDisplayOn()
+{
+    //Start by selecting configuration mode
+    CLEAR_RS();
+    CLEAR_RW();
+
+    //Enable 4 bit mode  From KS0066U Documentation
+    writeLcdBitD4toD7(0x2);//(0b0010);   // Select 4- bit start
+    pulseEn();	//LCD exec function
+    wait_us(100);
+
+    writeLcdBitD4toD7(0x2);////(0b0010);   // Select 4- bit start  (Already on the out port no need to write once more )
+    pulseEn();	//LCD exec function
+    wait_us(100);
+
+    writeLcdBitD4toD7(0xC);//(0b1100);   // 2 Lines + Disp On
+    pulseEn();	//LCD exec function
+    wait_us(100);
+}
+
+
+//Writes the byte comand to the LCD-module using 4 bits mode
+void TextLCD::lcdComand(unsigned char cmd)
+{
+    writeLcdBitD4toD7(cmd>>4); //Write the first high cmd nibble
+
+    CLEAR_RS();		// RS = 0 for command
+    CLEAR_RW();		// RW = 0 for write
+
+    pulseEn();  //EN Hi-Lo
+
+    writeLcdBitD4toD7(cmd); //Write the second low cmd nibble
+
+    pulseEn();  //EN H to Lo
+
+    wait_us(100); //wait
+}
+
+void TextLCD::lcdData(unsigned char data)
+{
+    writeLcdBitD4toD7(data>>4); //Write the first high data nibble
+
+    SET_RS();       // RS = 1 for data
+    CLEAR_RW();     // RW = 0 for write
+    pulseEn();      // EN H to Lo
+    wait_us(100); //wait
+
+    writeLcdBitD4toD7(data);
+
+    pulseEn();  //EN H to Lo
+    wait_us(100); //wait
+}
+
+
+
+//Moves cursor to the X,Y position
+void TextLCD::gotoxy(unsigned char x, unsigned char y)
+{
+    unsigned char firstCharAdr[]= { 0x80,0xc0,0x94,0xD4};
+    lcdComand(firstCharAdr[y-1]+ x -1);
+
+    wait_us(100);
+}
+
+int TextLCD::_putc(int value)
+{
+    lcdData(value);
+    return value;
+}
+
+int TextLCD::_getc() {
+    return -1;
+}
+
+//Writes a string to the LCD Display
+void TextLCD::lcd_print(char *str)
+{
+    unsigned char i =0;
+    while(str[i] !=0) {
+        lcdData(str[i]);
+        i++;
+    }
+}
\ No newline at end of file