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.
Dependents: ForEhab Serial_HelloWorld_Mbed ForOfek
Fork of LCDSerial by
SerialLCD.cpp
00001 #include "SerialLCD.h" 00002 00003 SerialLCD::SerialLCD(PinName tx, uint8_t type) : 00004 Serial(tx, NC) 00005 { 00006 // Init 00007 baud(LCD_BAUD); 00008 setType(type); 00009 00010 _rowoffset = 0; 00011 00012 // Reset 00013 clear(); 00014 setBrightness(30); 00015 } 00016 00017 void SerialLCD::clear() 00018 { 00019 command(LCD_CLEARDISPLAY); 00020 } 00021 00022 void SerialLCD::clearLine(uint8_t line) 00023 { 00024 if(line > 0 && line <= _numlines){ 00025 setCursor(line, 1); 00026 printf(" "); 00027 setCursor(line, 1); 00028 } 00029 } 00030 00031 void SerialLCD::selectLine(uint8_t line) 00032 { 00033 if(line > 0 && line <= _numlines){ 00034 setCursor(line, 1); 00035 } 00036 } 00037 00038 void SerialLCD::setBrightness(uint8_t num) 00039 { 00040 if(num >= 1 && num <= 30){ 00041 specialCommand(LCD_BACKLIGHT | (num - 1)); 00042 } 00043 } 00044 00045 void SerialLCD::home() 00046 { 00047 command(LCD_RETURNHOME); 00048 } 00049 00050 void SerialLCD::setSplash() 00051 { 00052 specialCommand(LCD_SETSPLASHSCREEN); 00053 } 00054 00055 void SerialLCD::setType(uint8_t type) 00056 { 00057 /* 00058 3: type 2x16 00059 4: type 2x20 00060 5: type 4x16 00061 6: type 4x20 00062 */ 00063 specialCommand(type); 00064 switch (type) { 00065 case 3: 00066 _numlines = LCD_2LINE; 00067 _numchars = LCD_16CHAR; 00068 00069 break; 00070 case 4: 00071 _numlines = LCD_2LINE; 00072 _numchars = LCD_20CHAR; 00073 00074 break; 00075 case 5: 00076 _numlines = LCD_4LINE; 00077 _numchars = LCD_16CHAR; 00078 00079 break; 00080 case 6: 00081 _numlines = LCD_4LINE; 00082 _numchars = LCD_20CHAR; 00083 00084 break; 00085 00086 default: 00087 _numlines = LCD_2LINE; 00088 _numchars = LCD_16CHAR; 00089 } 00090 } 00091 00092 void SerialLCD::toggleSplash() 00093 { 00094 specialCommand(LCD_SPLASHTOGGLE); 00095 } 00096 00097 void SerialLCD::leftToRight() 00098 { 00099 _displaymode |= LCD_ENTRYLEFT; 00100 command(LCD_ENTRYMODESET | _displaymode); 00101 } 00102 00103 void SerialLCD::rightToLeft() 00104 { 00105 _displaymode &= ~LCD_ENTRYLEFT; 00106 command(LCD_ENTRYMODESET | _displaymode); 00107 } 00108 00109 void SerialLCD::blink() 00110 { 00111 _displaycontrol |= LCD_BLINKON; 00112 command(LCD_DISPLAYCONTROL | _displaycontrol); 00113 } 00114 00115 void SerialLCD::noBlink() 00116 { 00117 _displaycontrol &= ~LCD_BLINKON; 00118 command(LCD_DISPLAYCONTROL | _displaycontrol); 00119 } 00120 00121 void SerialLCD::cursor() 00122 { 00123 _displaycontrol |= LCD_CURSORON; 00124 command(LCD_DISPLAYCONTROL | _displaycontrol); 00125 } 00126 00127 void SerialLCD::noCursor() 00128 { 00129 _displaycontrol &= ~LCD_CURSORON; 00130 command(LCD_DISPLAYCONTROL | _displaycontrol); 00131 } 00132 00133 void SerialLCD::display() 00134 { 00135 _displaycontrol |= LCD_DISPLAYON; 00136 command(LCD_DISPLAYCONTROL | _displaycontrol); 00137 } 00138 00139 void SerialLCD::noDisplay() 00140 { 00141 _displaycontrol &= ~LCD_DISPLAYON; 00142 command(LCD_DISPLAYCONTROL | _displaycontrol); 00143 } 00144 00145 void SerialLCD::setCursor(uint8_t row, uint8_t col) 00146 { 00147 int row_offsets[2][4] = { 00148 { 0x00, 0x40, 0x10, 0x50 }, 00149 { 0x00, 0x40, 0x14, 0x54 } 00150 }; 00151 if((row > 0 && row < 3) && (col > 0 && col < 17)){ 00152 command(LCD_SETDDRAMADDR | ((col - 1) + row_offsets[_rowoffset][(row - 1)])); 00153 } 00154 } 00155 00156 void SerialLCD::createChar(uint8_t location, uint8_t charmap[]) 00157 { 00158 location -= 1; 00159 location &= 0x07; 00160 for (int i=0; i<8; i++){ 00161 command(LCD_SETCGRAMADDR | (location << 3) | i); 00162 putc(charmap[i]); 00163 } 00164 } 00165 00166 void SerialLCD::printCustomChar(uint8_t num) 00167 { 00168 putc((num - 1)); 00169 } 00170 00171 void SerialLCD::scrollLeft() 00172 { 00173 command(0x18); 00174 } 00175 00176 void SerialLCD::scrollRight() 00177 { 00178 command(0x1C); 00179 } 00180 00181 // Private Functions 00182 00183 void SerialLCD::command(uint8_t value) 00184 { 00185 putc(0xFE); 00186 putc(value); 00187 wait_ms(5); 00188 } 00189 00190 void SerialLCD::specialCommand(uint8_t value) 00191 { 00192 putc(0x7C); 00193 putc(value); 00194 wait_ms(5); 00195 }
Generated on Tue Jul 12 2022 19:12:00 by
1.7.2
