LCD-Library

Dependents:   Projekt_Wecker_LCD Projekt_Wecker_LCD Projekt_Wecker_LCD

Files at this revision

API Documentation at this revision

Comitter:
wolpra98
Date:
Thu Apr 30 10:35:52 2015 +0000
Commit message:
A LCD-Library;

Changed in this revision

LCD.cpp Show annotated file Show diff for this revision Revisions of this file
LCD.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r b25367985a15 LCD.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD.cpp	Thu Apr 30 10:35:52 2015 +0000
@@ -0,0 +1,124 @@
+
+
+#include "mbed.h"
+#include "LCD.h"
+
+LCD::LCD(PinName RS, PinName EN, PinName DB4, PinName DB5, PinName DB6, PinName DB7){
+    _rs = RS;
+    _en = EN;
+    _db4 = DB4;
+    _db5 = DB5;
+    _db6 = DB6;
+    _db7 = DB7;
+    DigitalOut rs(RS);    
+    DigitalOut en(EN);
+    BusOut db(DB4, DB5, DB6, DB7);
+    
+     // warten auf die Bereitschaft des LCD
+    wait_ms( LCD_BOOTUP_MS );
+ 
+    // Soft-Reset muss 3mal hintereinander gesendet werden zur Initialisierung
+    out( LCD_SOFT_RESET );
+    wait_ms( LCD_SOFT_RESET_MS1 );
+    enable();
+    wait_ms( LCD_SOFT_RESET_MS2 );
+    enable();
+    wait_ms( LCD_SOFT_RESET_MS3 );
+ 
+    // 4-bit Modus aktivieren 
+    out( LCD_SET_FUNCTION |
+             LCD_FUNCTION_4BIT );
+    wait_ms( LCD_SET_4BITMODE_MS );
+ 
+    // 4-bit Modus / 2 Zeilen / 5x7
+    command( LCD_SET_FUNCTION |
+                 LCD_FUNCTION_4BIT |
+                 LCD_FUNCTION_2LINE |
+                 LCD_FUNCTION_5X7 );
+ 
+    // Display ein / Cursor aus / Blinken aus
+    command( LCD_SET_DISPLAY |
+                 LCD_DISPLAY_ON |
+                 LCD_CURSOR_ON |
+                 LCD_BLINKING_OFF); 
+ 
+    // Cursor inkrement / kein Scrollen
+    command( LCD_SET_ENTRY |
+                 LCD_ENTRY_INCREASE |
+                 LCD_ENTRY_NOSHIFT );
+ 
+    clear();   
+};
+
+void LCD::clear(void){
+    command(LCD_CLEAR_DISPLAY );
+    wait_ms( LCD_CLEAR_DISPLAY_MS );   
+};
+
+void LCD::home(void){
+    command( LCD_CURSOR_HOME );
+    wait_ms( LCD_CURSOR_HOME_MS );   
+}
+
+void LCD::data(unsigned char data){
+    DigitalOut rs(_rs);
+    rs = 1;    // RS auf 1 setzen
+    out(data>>4);            // zuerst die oberen, 
+    out(data);         // dann die unteren 4 Bit senden
+    wait_us( LCD_WRITEDATA_US );    
+}
+
+// Schreibt einen String auf das LCD
+void LCD::string(const char *data){
+    while( *data != '\0' )
+        LCD::data( *data++ );   
+}
+
+// Setzt den Cursor in Spalte x (0..15) Zeile y (1..4) 
+void LCD::setcursor(unsigned char x, unsigned char y )
+{
+    unsigned char data;
+    switch (y)
+    {
+        case 1:    // 1. Zeile
+            data = LCD_SET_DDADR + LCD_DDADR_LINE1 + x;
+            break;
+        case 2:    // 2. Zeile
+            data = LCD_SET_DDADR + LCD_DDADR_LINE2 + x;
+            break;
+        case 3:    // 3. Zeile
+            data = LCD_SET_DDADR + LCD_DDADR_LINE3 + x;
+            break;
+        case 4:    // 4. Zeile
+            data = LCD_SET_DDADR + LCD_DDADR_LINE4 + x;
+            break;
+        default:
+            return;                                   // für den Fall einer falschen Zeile
+    }
+    command(data);
+}
+
+void LCD::out(unsigned char data){
+    BusOut db(_db4, _db5, _db6, _db7);
+    db = data;     // Bits setzen
+    enable();    
+};
+
+void LCD::enable(){
+    DigitalOut en(_en);
+    en = 1;
+    wait_us( LCD_ENABLE_US );  // kurze Pause
+    en = 0;
+};
+
+void LCD::command(unsigned char data){
+    DigitalOut rs(_rs);
+    // RS auf 0 setzen
+    rs = 0;
+    out(data>>4);             // zuerst die oberen, 
+    out(data);           // dann die unteren 4 Bit senden
+    wait_us( LCD_COMMAND_US );   
+};
+
+
+
diff -r 000000000000 -r b25367985a15 LCD.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD.h	Thu Apr 30 10:35:52 2015 +0000
@@ -0,0 +1,125 @@
+
+#ifndef _LCD_H
+    #define _LCD_H
+
+class LCD
+{
+    public:
+        LCD(PinName RS, PinName EN, PinName DB4, PinName DB5, PinName DB6, PinName DB7);
+        // Sendet den Befehl zur Löschung des Displays
+        void clear(void);
+        // Sendet den Befehl: Cursor Home
+        void home(void);
+        // Sendet ein Datenbyte an das LCD
+        void data(unsigned char data);
+        // Schreibt einen String auf das LCD
+        void string(const char *data);
+        // Setzt den Cursor in Spalte x (0..15) Zeile y (1..4) 
+        void setcursor(unsigned char x, unsigned char y);
+    private:
+        // Sendet eine 4-bit Ausgabeoperation an das LCD
+        void out(unsigned char data);
+        // Erzeugt einen Enable-Puls
+        void enable();
+        // Sendet einen Befehl an das LCD
+        void command(unsigned char data);
+        
+    
+        PinName _rs;
+        PinName _en;
+        PinName _db4;
+        PinName _db5;
+        PinName _db6;
+        PinName _db7;
+};
+
+// LCD Befehle und Argumente.
+// Zur Verwendung in lcd_command
+// Kopiert von http://www.mikrocontroller.net/articles/AVR-GCC-Tutorial/LCD-Ansteuerung
+
+////////////////////////////////////////////////////////////////////////////////
+// LCD Ausführungszeiten (MS=Millisekunden, US=Mikrosekunden)
+ 
+#define LCD_BOOTUP_MS           30
+#define LCD_ENABLE_US           20
+#define LCD_WRITEDATA_US        46
+#define LCD_COMMAND_US          42
+ 
+#define LCD_SOFT_RESET_MS1      5
+#define LCD_SOFT_RESET_MS2      1
+#define LCD_SOFT_RESET_MS3      1
+#define LCD_SET_4BITMODE_MS     5
+ 
+#define LCD_CLEAR_DISPLAY_MS    2
+#define LCD_CURSOR_HOME_MS      2
+ 
+////////////////////////////////////////////////////////////////////////////////
+// Zeilendefinitionen des verwendeten LCD
+// Die Einträge hier sollten für ein LCD mit einer Zeilenlänge von 16 Zeichen passen
+// Bei anderen Zeilenlängen müssen diese Einträge angepasst werden
+ 
+#define LCD_DDADR_LINE1         0x00
+#define LCD_DDADR_LINE2         0x40
+#define LCD_DDADR_LINE3         0x10
+#define LCD_DDADR_LINE4         0x50
+ 
+// Clear Display -------------- 0b00000001
+#define LCD_CLEAR_DISPLAY       0x01
+ 
+// Cursor Home ---------------- 0b0000001x
+#define LCD_CURSOR_HOME         0x02
+ 
+// Set Entry Mode ------------- 0b000001xx
+#define LCD_SET_ENTRY           0x04
+ 
+#define LCD_ENTRY_DECREASE      0x00
+#define LCD_ENTRY_INCREASE      0x02
+#define LCD_ENTRY_NOSHIFT       0x00
+#define LCD_ENTRY_SHIFT         0x01
+ 
+// Set Display ---------------- 0b00001xxx
+#define LCD_SET_DISPLAY         0x08
+ 
+#define LCD_DISPLAY_OFF         0x00
+#define LCD_DISPLAY_ON          0x04
+#define LCD_CURSOR_OFF          0x00
+#define LCD_CURSOR_ON           0x02
+#define LCD_BLINKING_OFF        0x00
+#define LCD_BLINKING_ON         0x01
+ 
+// Set Shift ------------------ 0b0001xxxx
+#define LCD_SET_SHIFT           0x10
+ 
+#define LCD_CURSOR_MOVE         0x00
+#define LCD_DISPLAY_SHIFT       0x08
+#define LCD_SHIFT_LEFT          0x00
+#define LCD_SHIFT_RIGHT         0x04
+ 
+// Set Function --------------- 0b001xxxxx
+#define LCD_SET_FUNCTION        0x20
+ 
+#define LCD_FUNCTION_4BIT       0x00
+#define LCD_FUNCTION_8BIT       0x10
+#define LCD_FUNCTION_1LINE      0x00
+#define LCD_FUNCTION_2LINE      0x08
+#define LCD_FUNCTION_5X7        0x00
+#define LCD_FUNCTION_5X10       0x04
+ 
+#define LCD_SOFT_RESET          0x30
+ 
+// Set CG RAM Address --------- 0b01xxxxxx  (Character Generator RAM)
+#define LCD_SET_CGADR           0x40
+ 
+#define LCD_GC_CHAR0            0
+#define LCD_GC_CHAR1            1
+#define LCD_GC_CHAR2            2
+#define LCD_GC_CHAR3            3
+#define LCD_GC_CHAR4            4
+#define LCD_GC_CHAR5            5
+#define LCD_GC_CHAR6            6
+#define LCD_GC_CHAR7            7
+ 
+// Set DD RAM Address --------- 0b1xxxxxxx  (Display Data RAM)
+#define LCD_SET_DDADR           0x80
+
+#endif