LCD driver
Dependents: ecu_reader ecu_reader ecu_simulator regulator_napona
TextLCD.cpp
00001 /* mbed TextLCD Library 00002 * Copyright (c) 2007-2009 sford 00003 * Released under the MIT License: http://mbed.org/license/mit 00004 */ 00005 00006 #include "TextLCD.h" 00007 00008 #include "mbed.h" 00009 #include "error.h" 00010 00011 using namespace mbed; 00012 00013 /* 00014 * useful info found at http://www.a-netz.de/lcd.en.php 00015 * 00016 * 00017 * Initialisation 00018 * ============== 00019 * 00020 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state 00021 * 00022 * - wait approximately 15 ms so the display is ready to execute commands 00023 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). 00024 * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command. 00025 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. 00026 * - Execute the "clear display" command 00027 * 00028 * Timing 00029 * ====== 00030 * 00031 * Nearly all commands transmitted to the display need 40us for execution. 00032 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" 00033 * These commands need 1.64ms for execution. These timings are valid for all displays working with an 00034 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you 00035 * can use the busy flag to test if the display is ready to accept the next command. 00036 * 00037 * _e is kept high apart from calling clock 00038 * _rw is kept 0 (write) apart from actions that uyse it differently 00039 * _rs is set by the data/command writes 00040 */ 00041 00042 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1, 00043 PinName d2, PinName d3, int columns, int rows) : _rw(rw), _rs(rs), 00044 _e(e), _d(d0, d1, d2, d3), _columns(columns), _rows(rows) { 00045 00046 // _rows = 2; 00047 // _columns = 16; 00048 // Mon, 27 Apr 2009 23:32:34 +0200 00049 // Kevin Konradt: 00050 // When using a LCD with 1 row x 16 characters 00051 // instead of 2x16, try changing _columns to 8. 00052 // (display seems to split the 16 characters into 00053 // 2 virtual rows with 8 characters each.) 00054 00055 _rw = 0; 00056 _e = 1; 00057 _rs = 0; // command mode 00058 00059 // Should theoretically wait 15ms, but most things will be powered up pre-reset 00060 // so i'll disable that for the minute. If implemented, could wait 15ms post reset 00061 // instead 00062 // wait(0.015); 00063 00064 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) 00065 for(int i=0; i<3; i++) { 00066 writeNibble(0x3); 00067 wait(0.00164); // this command takes 1.64ms, so wait for it 00068 } 00069 writeNibble(0x2); // 4-bit mode 00070 00071 writeCommand(0x28); // Function set 001 BW N F - - 00072 writeCommand(0x0C); 00073 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes 00074 00075 cls(); 00076 } 00077 00078 int TextLCD::_putc(int value) { 00079 if(value == '\n') { 00080 newline(); 00081 } else { 00082 writeData(value); 00083 } 00084 return value; 00085 } 00086 00087 int TextLCD::_getc() { 00088 return 0; 00089 } 00090 00091 void TextLCD::newline() { 00092 _column = 0; 00093 _row++; 00094 if(_row >= _rows) { 00095 _row = 0; 00096 } 00097 locate(_column, _row); 00098 } 00099 00100 void TextLCD::locate(int column, int row) { 00101 if(column < 0 || column >= _columns || row < 0 || row >= _rows) { 00102 error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows); 00103 return; 00104 } 00105 00106 _row = row; 00107 _column = column; 00108 int address = 0x80 + (_row * 40) + _column; // memory starts at 0x80, and is 40 chars long per row 00109 writeCommand(address); 00110 } 00111 00112 void TextLCD::cls() { 00113 writeCommand(0x01); // Clear Display 00114 wait(0.00164f); // This command takes 1.64 ms 00115 locate(0, 0); 00116 } 00117 00118 void TextLCD::reset() { 00119 cls(); 00120 } 00121 00122 void TextLCD::clock() { 00123 wait(0.000040f); 00124 _e = 0; 00125 wait(0.000040f); // most instructions take 40us 00126 _e = 1; 00127 } 00128 00129 void TextLCD::writeNibble(int value) { 00130 _d = value; 00131 clock(); 00132 } 00133 00134 void TextLCD::writeByte(int value) { 00135 writeNibble(value >> 4); 00136 writeNibble(value >> 0); 00137 } 00138 00139 void TextLCD::writeCommand(int command) { 00140 _rs = 0; 00141 writeByte(command); 00142 } 00143 00144 void TextLCD::writeData(int data) { 00145 _rs = 1; 00146 writeByte(data); 00147 _column++; 00148 if(_column >= _columns) { 00149 newline(); 00150 } 00151 }
Generated on Tue Jul 12 2022 13:01:42 by 1.7.2