LCD-Library
Dependents: Projekt_Wecker_LCD Projekt_Wecker_LCD Projekt_Wecker_LCD
LCD.cpp@0:b25367985a15, 2015-04-30 (annotated)
- Committer:
- wolpra98
- Date:
- Thu Apr 30 10:35:52 2015 +0000
- Revision:
- 0:b25367985a15
A LCD-Library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolpra98 | 0:b25367985a15 | 1 | |
wolpra98 | 0:b25367985a15 | 2 | |
wolpra98 | 0:b25367985a15 | 3 | #include "mbed.h" |
wolpra98 | 0:b25367985a15 | 4 | #include "LCD.h" |
wolpra98 | 0:b25367985a15 | 5 | |
wolpra98 | 0:b25367985a15 | 6 | LCD::LCD(PinName RS, PinName EN, PinName DB4, PinName DB5, PinName DB6, PinName DB7){ |
wolpra98 | 0:b25367985a15 | 7 | _rs = RS; |
wolpra98 | 0:b25367985a15 | 8 | _en = EN; |
wolpra98 | 0:b25367985a15 | 9 | _db4 = DB4; |
wolpra98 | 0:b25367985a15 | 10 | _db5 = DB5; |
wolpra98 | 0:b25367985a15 | 11 | _db6 = DB6; |
wolpra98 | 0:b25367985a15 | 12 | _db7 = DB7; |
wolpra98 | 0:b25367985a15 | 13 | DigitalOut rs(RS); |
wolpra98 | 0:b25367985a15 | 14 | DigitalOut en(EN); |
wolpra98 | 0:b25367985a15 | 15 | BusOut db(DB4, DB5, DB6, DB7); |
wolpra98 | 0:b25367985a15 | 16 | |
wolpra98 | 0:b25367985a15 | 17 | // warten auf die Bereitschaft des LCD |
wolpra98 | 0:b25367985a15 | 18 | wait_ms( LCD_BOOTUP_MS ); |
wolpra98 | 0:b25367985a15 | 19 | |
wolpra98 | 0:b25367985a15 | 20 | // Soft-Reset muss 3mal hintereinander gesendet werden zur Initialisierung |
wolpra98 | 0:b25367985a15 | 21 | out( LCD_SOFT_RESET ); |
wolpra98 | 0:b25367985a15 | 22 | wait_ms( LCD_SOFT_RESET_MS1 ); |
wolpra98 | 0:b25367985a15 | 23 | enable(); |
wolpra98 | 0:b25367985a15 | 24 | wait_ms( LCD_SOFT_RESET_MS2 ); |
wolpra98 | 0:b25367985a15 | 25 | enable(); |
wolpra98 | 0:b25367985a15 | 26 | wait_ms( LCD_SOFT_RESET_MS3 ); |
wolpra98 | 0:b25367985a15 | 27 | |
wolpra98 | 0:b25367985a15 | 28 | // 4-bit Modus aktivieren |
wolpra98 | 0:b25367985a15 | 29 | out( LCD_SET_FUNCTION | |
wolpra98 | 0:b25367985a15 | 30 | LCD_FUNCTION_4BIT ); |
wolpra98 | 0:b25367985a15 | 31 | wait_ms( LCD_SET_4BITMODE_MS ); |
wolpra98 | 0:b25367985a15 | 32 | |
wolpra98 | 0:b25367985a15 | 33 | // 4-bit Modus / 2 Zeilen / 5x7 |
wolpra98 | 0:b25367985a15 | 34 | command( LCD_SET_FUNCTION | |
wolpra98 | 0:b25367985a15 | 35 | LCD_FUNCTION_4BIT | |
wolpra98 | 0:b25367985a15 | 36 | LCD_FUNCTION_2LINE | |
wolpra98 | 0:b25367985a15 | 37 | LCD_FUNCTION_5X7 ); |
wolpra98 | 0:b25367985a15 | 38 | |
wolpra98 | 0:b25367985a15 | 39 | // Display ein / Cursor aus / Blinken aus |
wolpra98 | 0:b25367985a15 | 40 | command( LCD_SET_DISPLAY | |
wolpra98 | 0:b25367985a15 | 41 | LCD_DISPLAY_ON | |
wolpra98 | 0:b25367985a15 | 42 | LCD_CURSOR_ON | |
wolpra98 | 0:b25367985a15 | 43 | LCD_BLINKING_OFF); |
wolpra98 | 0:b25367985a15 | 44 | |
wolpra98 | 0:b25367985a15 | 45 | // Cursor inkrement / kein Scrollen |
wolpra98 | 0:b25367985a15 | 46 | command( LCD_SET_ENTRY | |
wolpra98 | 0:b25367985a15 | 47 | LCD_ENTRY_INCREASE | |
wolpra98 | 0:b25367985a15 | 48 | LCD_ENTRY_NOSHIFT ); |
wolpra98 | 0:b25367985a15 | 49 | |
wolpra98 | 0:b25367985a15 | 50 | clear(); |
wolpra98 | 0:b25367985a15 | 51 | }; |
wolpra98 | 0:b25367985a15 | 52 | |
wolpra98 | 0:b25367985a15 | 53 | void LCD::clear(void){ |
wolpra98 | 0:b25367985a15 | 54 | command(LCD_CLEAR_DISPLAY ); |
wolpra98 | 0:b25367985a15 | 55 | wait_ms( LCD_CLEAR_DISPLAY_MS ); |
wolpra98 | 0:b25367985a15 | 56 | }; |
wolpra98 | 0:b25367985a15 | 57 | |
wolpra98 | 0:b25367985a15 | 58 | void LCD::home(void){ |
wolpra98 | 0:b25367985a15 | 59 | command( LCD_CURSOR_HOME ); |
wolpra98 | 0:b25367985a15 | 60 | wait_ms( LCD_CURSOR_HOME_MS ); |
wolpra98 | 0:b25367985a15 | 61 | } |
wolpra98 | 0:b25367985a15 | 62 | |
wolpra98 | 0:b25367985a15 | 63 | void LCD::data(unsigned char data){ |
wolpra98 | 0:b25367985a15 | 64 | DigitalOut rs(_rs); |
wolpra98 | 0:b25367985a15 | 65 | rs = 1; // RS auf 1 setzen |
wolpra98 | 0:b25367985a15 | 66 | out(data>>4); // zuerst die oberen, |
wolpra98 | 0:b25367985a15 | 67 | out(data); // dann die unteren 4 Bit senden |
wolpra98 | 0:b25367985a15 | 68 | wait_us( LCD_WRITEDATA_US ); |
wolpra98 | 0:b25367985a15 | 69 | } |
wolpra98 | 0:b25367985a15 | 70 | |
wolpra98 | 0:b25367985a15 | 71 | // Schreibt einen String auf das LCD |
wolpra98 | 0:b25367985a15 | 72 | void LCD::string(const char *data){ |
wolpra98 | 0:b25367985a15 | 73 | while( *data != '\0' ) |
wolpra98 | 0:b25367985a15 | 74 | LCD::data( *data++ ); |
wolpra98 | 0:b25367985a15 | 75 | } |
wolpra98 | 0:b25367985a15 | 76 | |
wolpra98 | 0:b25367985a15 | 77 | // Setzt den Cursor in Spalte x (0..15) Zeile y (1..4) |
wolpra98 | 0:b25367985a15 | 78 | void LCD::setcursor(unsigned char x, unsigned char y ) |
wolpra98 | 0:b25367985a15 | 79 | { |
wolpra98 | 0:b25367985a15 | 80 | unsigned char data; |
wolpra98 | 0:b25367985a15 | 81 | switch (y) |
wolpra98 | 0:b25367985a15 | 82 | { |
wolpra98 | 0:b25367985a15 | 83 | case 1: // 1. Zeile |
wolpra98 | 0:b25367985a15 | 84 | data = LCD_SET_DDADR + LCD_DDADR_LINE1 + x; |
wolpra98 | 0:b25367985a15 | 85 | break; |
wolpra98 | 0:b25367985a15 | 86 | case 2: // 2. Zeile |
wolpra98 | 0:b25367985a15 | 87 | data = LCD_SET_DDADR + LCD_DDADR_LINE2 + x; |
wolpra98 | 0:b25367985a15 | 88 | break; |
wolpra98 | 0:b25367985a15 | 89 | case 3: // 3. Zeile |
wolpra98 | 0:b25367985a15 | 90 | data = LCD_SET_DDADR + LCD_DDADR_LINE3 + x; |
wolpra98 | 0:b25367985a15 | 91 | break; |
wolpra98 | 0:b25367985a15 | 92 | case 4: // 4. Zeile |
wolpra98 | 0:b25367985a15 | 93 | data = LCD_SET_DDADR + LCD_DDADR_LINE4 + x; |
wolpra98 | 0:b25367985a15 | 94 | break; |
wolpra98 | 0:b25367985a15 | 95 | default: |
wolpra98 | 0:b25367985a15 | 96 | return; // für den Fall einer falschen Zeile |
wolpra98 | 0:b25367985a15 | 97 | } |
wolpra98 | 0:b25367985a15 | 98 | command(data); |
wolpra98 | 0:b25367985a15 | 99 | } |
wolpra98 | 0:b25367985a15 | 100 | |
wolpra98 | 0:b25367985a15 | 101 | void LCD::out(unsigned char data){ |
wolpra98 | 0:b25367985a15 | 102 | BusOut db(_db4, _db5, _db6, _db7); |
wolpra98 | 0:b25367985a15 | 103 | db = data; // Bits setzen |
wolpra98 | 0:b25367985a15 | 104 | enable(); |
wolpra98 | 0:b25367985a15 | 105 | }; |
wolpra98 | 0:b25367985a15 | 106 | |
wolpra98 | 0:b25367985a15 | 107 | void LCD::enable(){ |
wolpra98 | 0:b25367985a15 | 108 | DigitalOut en(_en); |
wolpra98 | 0:b25367985a15 | 109 | en = 1; |
wolpra98 | 0:b25367985a15 | 110 | wait_us( LCD_ENABLE_US ); // kurze Pause |
wolpra98 | 0:b25367985a15 | 111 | en = 0; |
wolpra98 | 0:b25367985a15 | 112 | }; |
wolpra98 | 0:b25367985a15 | 113 | |
wolpra98 | 0:b25367985a15 | 114 | void LCD::command(unsigned char data){ |
wolpra98 | 0:b25367985a15 | 115 | DigitalOut rs(_rs); |
wolpra98 | 0:b25367985a15 | 116 | // RS auf 0 setzen |
wolpra98 | 0:b25367985a15 | 117 | rs = 0; |
wolpra98 | 0:b25367985a15 | 118 | out(data>>4); // zuerst die oberen, |
wolpra98 | 0:b25367985a15 | 119 | out(data); // dann die unteren 4 Bit senden |
wolpra98 | 0:b25367985a15 | 120 | wait_us( LCD_COMMAND_US ); |
wolpra98 | 0:b25367985a15 | 121 | }; |
wolpra98 | 0:b25367985a15 | 122 | |
wolpra98 | 0:b25367985a15 | 123 | |
wolpra98 | 0:b25367985a15 | 124 |