Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WattBob_TextLCD.cpp
00001 /* draft mbed TextLCD 00002 * (c) 2007/8, sford 00003 * Modified jherd 00004 */ 00005 00006 #include "WattBob_TextLCD.h" 00007 #include "MCP23017.h" 00008 00009 #include "mbed.h" 00010 00011 /* 00012 * Initialisation 00013 * ============== 00014 * 00015 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state 00016 * 00017 * - wait approximately 15 ms so the display is ready to execute commands 00018 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now). 00019 * - 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. 00020 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now. 00021 * - Execute the "clear display" command 00022 * 00023 * Timing 00024 * ====== 00025 * 00026 * Nearly all commands transmitted to the display need 40us for execution. 00027 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position" 00028 * These commands need 1.64ms for execution. These timings are valid for all displays working with an 00029 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you 00030 * can use the busy flag to test if the display is ready to accept the next command. 00031 * 00032 * _e is kept high apart from calling clock 00033 * _rw is kept 0 (write) apart from actions that uses it differently 00034 * _rs is set by the data/command writes 00035 * 00036 * RS = 7 00037 * RW = 6 00038 * E = 5 00039 * Back light = 4 00040 * D4 = 0 00041 * D5 = 1 00042 * D6 = 2 00043 * D7 = 3 00044 */ 00045 00046 WattBob_TextLCD::WattBob_TextLCD(MCP23017 *port) { 00047 // 00048 // Initialise pointer to MCP23017 object 00049 // 00050 par_port = port; 00051 par_port->config(0x0F00, 0x0F00, 0x0F00); 00052 00053 _rows = 2; 00054 _columns = 16; 00055 00056 // 00057 // Time to allow unit to initialise 00058 // 00059 wait(DISPLAY_INIT_DELAY_SECS); 00060 00061 _rw(0); 00062 _e(0); 00063 _rs(0); // command mode 00064 00065 // 00066 // interface defaults to an 8-bit interface. However, we need to ensure that we 00067 // are in 8-bit mode 00068 // 00069 for(int i=0; i<3; i++) { 00070 writeNibble(CMD4_SET_8_BIT_INTERFACE); 00071 wait(0.00164); // this command takes 1.64ms, so wait for it 00072 } 00073 00074 writeNibble(CMD4_SET_4_BIT_INTERFACE); // now force into 4-bit mode 00075 00076 // writeCommand(CMD_NULL); 00077 // writeCommand(CMD_NULL); 00078 // writeCommand(CMD_NULL); 00079 // writeCommand(CMD_NULL); 00080 // writeCommand(CMD_NULL); 00081 00082 writeCommand(CMD_FUNCTION_SET | INTERFACE_4_BIT | TWO_LINE_DISPLAY | FONT_5x8 | ENGL_JAPAN_FONT_SET); // 0x28 00083 writeCommand(CMD_DISPLAY_CONTROL | DISPLAY_ON | CURSOR_OFF | CURSOR_CHAR_BLINK_OFF); // 0xC0 00084 cls(); 00085 writeCommand(CMD_RETURN_HOME); 00086 writeCommand(CMD_ENTRY_MODE | CURSOR_STEP_RIGHT | DISPLAY_SHIFT_OFF ); // 0x06 00087 } 00088 00089 int WattBob_TextLCD::_putc(int value) { 00090 if(value == '\n') { 00091 newline(); 00092 } else { 00093 writeData(value); 00094 } 00095 return value; 00096 } 00097 00098 int WattBob_TextLCD::_getc() { 00099 return 0; 00100 } 00101 00102 void WattBob_TextLCD::newline() { 00103 _column = 0; 00104 _row++; 00105 if(_row >= _rows) { 00106 _row = 0; 00107 } 00108 locate(_column, _row); 00109 } 00110 00111 void WattBob_TextLCD::locate(int row, int column) { 00112 if(column < 0 || column >= _columns || row < 0 || row >= _rows) { 00113 // error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows); 00114 return; 00115 } 00116 00117 _row = row; 00118 _column = column; 00119 int address = 0x80 + (_row * 0x40) + _column; // memory starts at 0x80, and internally it is 40 chars per row (only first 16 used) 00120 writeCommand(address); 00121 } 00122 00123 void WattBob_TextLCD::cls() { 00124 writeCommand(CMD_CLEAR_DISPLAY); // 0x01 00125 wait(DISPLAY_CLEAR_DELAY); // 00126 locate(0, 0); 00127 } 00128 00129 void WattBob_TextLCD::reset() { 00130 cls(); 00131 } 00132 00133 void WattBob_TextLCD::clock() { 00134 wait(0.000040f); 00135 _e(1); 00136 wait(0.000040f); // most instructions take 40us 00137 _e(0); 00138 } 00139 00140 void WattBob_TextLCD::writeNibble(int value) { 00141 _d(value); 00142 clock(); 00143 } 00144 00145 void WattBob_TextLCD::writeByte(int value) { 00146 writeNibble((value >> 4) & 0x000F); 00147 writeNibble((value >> 0) & 0x000F); 00148 } 00149 00150 void WattBob_TextLCD::writeCommand(int command) { 00151 _rs(0); 00152 writeByte(command); 00153 } 00154 00155 void WattBob_TextLCD::writeData(int data) { 00156 _rs(1); 00157 writeByte(data); 00158 _column++; 00159 if(_column >= _columns) { 00160 newline(); 00161 } 00162 } 00163 00164 void WattBob_TextLCD::_rs(int data) { 00165 par_port->write_bit(data, RS_BIT); 00166 } 00167 00168 void WattBob_TextLCD::_rw(int data) { 00169 par_port->write_bit(data, RW_BIT); 00170 } 00171 00172 void WattBob_TextLCD::_e(int data) { 00173 par_port->write_bit(data, E_BIT); 00174 } 00175 00176 void WattBob_TextLCD::_d(int data) { 00177 par_port->write_mask((unsigned short)data, (unsigned short)0x000F); 00178 } 00179
Generated on Wed Jul 13 2022 00:50:23 by
