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.
Dependencies: AdaFruit_RGBLCDShield MCP23017 mbed RTclock
Fork of MCP_test by
lcd2004.cpp
00001 #include "mbed.h" 00002 #include "lcd2004.h" 00003 00004 // commands 00005 #define LCD_CLEARDISPLAY 0x01 00006 #define LCD_RETURNHOME 0x02 00007 #define LCD_ENTRYMODESET 0x04 00008 #define LCD_DISPLAYCONTROL 0x08 00009 #define LCD_CURSORSHIFT 0x10 00010 #define LCD_FUNCTIONSET 0x20 00011 #define LCD_SETCGRAMADDR 0x40 00012 #define LCD_SETDDRAMADDR 0x80 00013 00014 // flags for display entry mode 00015 #define LCD_ENTRYRIGHT 0x00 00016 #define LCD_ENTRYLEFT 0x02 00017 #define LCD_ENTRYSHIFTINCREMENT 0x01 00018 #define LCD_ENTRYSHIFTDECREMENT 0x00 00019 00020 // flags for display on/off control 00021 #define LCD_DISPLAY 0x04 00022 #define LCD_CURSOR 0x02 00023 #define LCD_BLINK 0x01 00024 00025 // flags for display/cursor shift 00026 #define LCD_DISPLAYMOVE 0x08 00027 #define LCD_CURSORMOVE 0x00 00028 #define LCD_MOVERIGHT 0x04 00029 #define LCD_MOVELEFT 0x00 00030 00031 #define ADDR 0x4E 00032 #define PIN_E 1<<2 00033 #define PIN_RW 1<<1 00034 #define PIN_RS 1<<0 00035 #define PIN_D4 1<<4 00036 #define PIN_D5 1<<5 00037 #define PIN_D6 1<<6 00038 #define PIN_D7 1<<7 00039 #define PIN_BL 1<<3 00040 00041 Serial cPC(SERIAL_TX, SERIAL_RX); 00042 00043 const uint8_t k_aMapper[] = 00044 { 00045 PIN_D4, 00046 PIN_D5, 00047 PIN_D6, 00048 PIN_D7, 00049 }; 00050 00051 LCD2004::LCD2004(I2C & in_cI2C) 00052 : LCD(in_cI2C) 00053 , m_nDisplayControl(LCD_DISPLAY) 00054 { 00055 ::wait_ms(100); 00056 write_reg(PIN_D5 | PIN_D4); 00057 ::wait_ms(5); 00058 write_reg(PIN_D5 | PIN_D4); 00059 ::wait_us(100); 00060 write_reg(PIN_D5 | PIN_D4); 00061 00062 // IV 00063 write_reg(PIN_D5); 00064 write_reg(PIN_D5); 00065 write_reg(PIN_D7); 00066 write_reg(0); 00067 write_reg(PIN_D7); 00068 write_reg(0); 00069 write_reg(PIN_D4); 00070 write_reg(0); 00071 write_reg(PIN_D7 | PIN_D6 /* | PIN_D5 | PIN_D4 */); // D5 = cursor on D4 = BLINK 00072 } 00073 00074 int LCD2004::_putc(int in_nValue) 00075 { 00076 write_data(PIN_RS, in_nValue); 00077 return 0; 00078 } 00079 00080 void LCD2004::clear() 00081 { 00082 write_data(0, LCD_CLEARDISPLAY); 00083 } 00084 00085 uint8_t LCD2004::columns() 00086 { 00087 return 20; 00088 } 00089 00090 void LCD2004::createChar(uint8_t location, uint8_t charmap[]) 00091 { 00092 location &= 0x7; // we only have 8 locations 0-7 00093 write_data(0, LCD_SETCGRAMADDR | (location << 3)); 00094 00095 for (int i=0; i<8; i++) 00096 { 00097 _putc(charmap[i]); 00098 } 00099 00100 write_data(0, LCD_SETDDRAMADDR); // unfortunately resets the location to 0,0 00101 } 00102 00103 void LCD2004::home() 00104 { 00105 write_data(0, LCD_RETURNHOME); 00106 } 00107 00108 uint8_t LCD2004::read_reg(void) 00109 { 00110 char nData = PIN_RW | PIN_BL; 00111 m_cI2C.write(ADDR,&nData,1); 00112 00113 nData = PIN_RW | PIN_BL | PIN_E; 00114 m_cI2C.write(ADDR,&nData,1); 00115 00116 ::wait_ms(0.05); 00117 00118 char nValue = 0; 00119 m_cI2C.read(ADDR,&nValue,1); 00120 00121 nData = PIN_RW | PIN_BL; 00122 m_cI2C.write(ADDR,&nData,1); 00123 00124 ::wait_ms(0.05); 00125 00126 return nValue & (PIN_D7 | PIN_D6 | PIN_D5 | PIN_D4); 00127 } 00128 00129 uint8_t LCD2004::remap(uint8_t in_nValue) 00130 { 00131 uint8_t nValue = 0; 00132 for (size_t i = 0; i < 4; i++) 00133 { 00134 if (in_nValue & (1 << i)) nValue |= k_aMapper[i]; 00135 } 00136 00137 return nValue; 00138 } 00139 00140 uint8_t LCD2004::rows() 00141 { 00142 return 4; 00143 } 00144 00145 void LCD2004::setCursor(uint8_t in_nX, uint8_t in_nY) 00146 { 00147 int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; 00148 00149 // D7 = Set DDRAM address 00150 uint8_t nValue = LCD_SETDDRAMADDR | (in_nX + row_offsets[in_nY]); 00151 write_data(0, nValue); 00152 } 00153 00154 void LCD2004::setDisplayControl(uint8_t in_nReg, bool in_bEnable) 00155 { 00156 uint8_t nDisplayControl = m_nDisplayControl; 00157 00158 if (in_bEnable) 00159 { 00160 m_nDisplayControl |= in_nReg; 00161 } 00162 else 00163 { 00164 m_nDisplayControl &= ~in_nReg; 00165 } 00166 00167 if (nDisplayControl != m_nDisplayControl) 00168 { 00169 write_data(0,LCD_DISPLAYCONTROL | m_nDisplayControl); 00170 } 00171 } 00172 00173 void LCD2004::showBlink(bool in_bShow) 00174 { 00175 setDisplayControl(LCD_BLINK,in_bShow); 00176 } 00177 00178 void LCD2004::showCursor(bool in_bShow) 00179 { 00180 setDisplayControl(LCD_CURSOR, in_bShow); 00181 } 00182 00183 void LCD2004::showDisplay(bool in_bShow) 00184 { 00185 setDisplayControl(LCD_DISPLAY,in_bShow); 00186 } 00187 00188 void LCD2004::write_data(uint8_t in_nReg, uint8_t in_nValue) 00189 { 00190 write_reg(in_nReg | remap(in_nValue >> 4)); 00191 write_reg(in_nReg | remap(in_nValue & 0x0F)); 00192 } 00193 00194 void LCD2004::write_reg(uint8_t in_nValue) 00195 { 00196 char nData = PIN_E | PIN_BL | in_nValue; 00197 m_cI2C.write(ADDR,&nData,1); 00198 00199 ::wait_us(1450); 00200 00201 nData = PIN_BL | in_nValue; 00202 m_cI2C.write(ADDR,&nData,1); 00203 }
Generated on Thu Jul 14 2022 00:55:23 by
1.7.2
