Yaroslav Barabanov / Mbed 2 deprecated LiquidCrystal_I2C_for_Nucleo

Dependencies:   mbed

Dependents:   mbed-os-example-mbed6-wifi_MY_SOCKET_rigtech_copy_

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LiquidCrystal_I2C.cpp Source File

LiquidCrystal_I2C.cpp

00001 #include "LiquidCrystal_I2C.h"
00002 #include "mbed.h"
00003 
00004 // When the display powers up, it is configured as follows:
00005 //
00006 // 1. Display clear
00007 // 2. Function set:
00008 //    DL = 1; 8-bit interface data
00009 //    N = 0; 1-line display
00010 //    F = 0; 5x8 dot character font
00011 // 3. Display on/off control:
00012 //    D = 0; Display off
00013 //    C = 0; Cursor off
00014 //    B = 0; Blinking off
00015 // 4. Entry mode set:
00016 //    I/D = 1; Increment by 1
00017 //    S = 0; No shift
00018 //
00019 // Note, however, that resetting the Arduino doesn't reset the LCD, so we
00020 // can't assume that its in that state when a sketch starts (and the
00021 // LiquidCrystal constructor is called).
00022 
00023 
00024 I2C _i2c(PB_11, PB_10); // SDA, SCL
00025 
00026 LiquidCrystal_I2C::LiquidCrystal_I2C(unsigned char lcd_addr, unsigned char lcd_cols, unsigned char lcd_rows, unsigned char charsize)
00027 {
00028     _addr = lcd_addr;
00029     _cols = lcd_cols;
00030     _rows = lcd_rows;
00031     _charsize = charsize;
00032     _backlightval = LCD_BACKLIGHT;
00033 }
00034 
00035 void LiquidCrystal_I2C::begin() {
00036     //Wire.begin();
00037     _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
00038 
00039     if (_rows > 1) {
00040         _displayfunction |= LCD_2LINE;
00041     }
00042 
00043     // for some 1 line displays you can select a 10 pixel high font
00044     if ((_charsize != 0) && (_rows == 1)) {
00045         _displayfunction |= LCD_5x10DOTS;
00046     }
00047 
00048     // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
00049     // according to datasheet, we need at least 40ms after power rises above 2.7V
00050     // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
00051     wait_ms(50);
00052 
00053     // Now we pull both RS and R/W low to begin commands
00054     expanderWrite(_backlightval);   // reset expanderand turn backlight off (Bit 8 =1)
00055     wait_ms(1000);
00056 
00057     //put the LCD into 4 bit mode
00058     // this is according to the hitachi HD44780 datasheet
00059     // figure 24, pg 46
00060 
00061     // we start in 8bit mode, try to set 4 bit mode
00062     write4bits(0x03 << 4);
00063     wait_us(4500); // wait min 4.1ms
00064 
00065     // second try
00066     write4bits(0x03 << 4);
00067     wait_us(4500); // wait min 4.1ms
00068 
00069     // third go!
00070     write4bits(0x03 << 4);
00071     wait_us(150);
00072 
00073     // finally, set to 4-bit interface
00074     write4bits(0x02 << 4);
00075 
00076     // set # lines, font size, etc.
00077     command(LCD_FUNCTIONSET | _displayfunction);
00078 
00079     // turn the display on with no cursor or blinking default
00080     _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
00081     display();
00082 
00083     // clear it off
00084     clear();
00085 
00086     // Initialize to default text direction (for roman languages)
00087     _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
00088 
00089     // set the entry mode
00090     command(LCD_ENTRYMODESET | _displaymode);
00091 
00092     home();
00093 }
00094 
00095 /********** high level commands, for the user! */
00096 void LiquidCrystal_I2C::clear(){
00097     command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
00098     wait_us(2000);  // this command takes a long time!
00099 }
00100 
00101 void LiquidCrystal_I2C::home(){
00102     command(LCD_RETURNHOME);  // set cursor position to zero
00103     wait_us(2000);  // this command takes a long time!
00104 }
00105 
00106 void LiquidCrystal_I2C::setCursor(unsigned char col, unsigned char row){
00107     int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
00108     if (row > _rows) {
00109         row = _rows-1;    // we count rows starting w/0
00110     }
00111     command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
00112 }
00113 
00114 // Turn the display on/off (quickly)
00115 void LiquidCrystal_I2C::noDisplay() {
00116     _displaycontrol &= ~LCD_DISPLAYON;
00117     command(LCD_DISPLAYCONTROL | _displaycontrol);
00118 }
00119 void LiquidCrystal_I2C::display() {
00120     _displaycontrol |= LCD_DISPLAYON;
00121     command(LCD_DISPLAYCONTROL | _displaycontrol);
00122 }
00123 
00124 // Turns the underline cursor on/off
00125 void LiquidCrystal_I2C::noCursor() {
00126     _displaycontrol &= ~LCD_CURSORON;
00127     command(LCD_DISPLAYCONTROL | _displaycontrol);
00128 }
00129 void LiquidCrystal_I2C::cursor() {
00130     _displaycontrol |= LCD_CURSORON;
00131     command(LCD_DISPLAYCONTROL | _displaycontrol);
00132 }
00133 
00134 // Turn on and off the blinking cursor
00135 void LiquidCrystal_I2C::noBlink() {
00136     _displaycontrol &= ~LCD_BLINKON;
00137     command(LCD_DISPLAYCONTROL | _displaycontrol);
00138 }
00139 void LiquidCrystal_I2C::blink() {
00140     _displaycontrol |= LCD_BLINKON;
00141     command(LCD_DISPLAYCONTROL | _displaycontrol);
00142 }
00143 
00144 // These commands scroll the display without changing the RAM
00145 void LiquidCrystal_I2C::scrollDisplayLeft(void) {
00146     command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
00147 }
00148 void LiquidCrystal_I2C::scrollDisplayRight(void) {
00149     command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
00150 }
00151 
00152 // This is for text that flows Left to Right
00153 void LiquidCrystal_I2C::leftToRight(void) {
00154     _displaymode |= LCD_ENTRYLEFT;
00155     command(LCD_ENTRYMODESET | _displaymode);
00156 }
00157 
00158 // This is for text that flows Right to Left
00159 void LiquidCrystal_I2C::rightToLeft(void) {
00160     _displaymode &= ~LCD_ENTRYLEFT;
00161     command(LCD_ENTRYMODESET | _displaymode);
00162 }
00163 
00164 // This will 'right justify' text from the cursor
00165 void LiquidCrystal_I2C::autoscroll(void) {
00166     _displaymode |= LCD_ENTRYSHIFTINCREMENT;
00167     command(LCD_ENTRYMODESET | _displaymode);
00168 }
00169 
00170 // This will 'left justify' text from the cursor
00171 void LiquidCrystal_I2C::noAutoscroll(void) {
00172     _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
00173     command(LCD_ENTRYMODESET | _displaymode);
00174 }
00175 
00176 // Allows us to fill the first 8 CGRAM locations
00177 // with custom characters
00178 void LiquidCrystal_I2C::createChar(unsigned char location, unsigned char charmap[]) {
00179     location &= 0x7; // we only have 8 locations 0-7
00180     command(LCD_SETCGRAMADDR | (location << 3));
00181     for (int i=0; i<8; i++) {
00182         write(charmap[i]);
00183     }
00184 }
00185 
00186 // Turn the (optional) backlight off/on
00187 void LiquidCrystal_I2C::noBacklight(void) {
00188     _backlightval=LCD_NOBACKLIGHT;
00189     expanderWrite(0);
00190 }
00191 
00192 void LiquidCrystal_I2C::backlight(void) {
00193     _backlightval=LCD_BACKLIGHT;
00194     expanderWrite(0);
00195 }
00196 bool LiquidCrystal_I2C::getBacklight() {
00197   return _backlightval == LCD_BACKLIGHT;
00198 }
00199 
00200 
00201 /*********** mid level commands, for sending data/cmds */
00202 
00203 inline void LiquidCrystal_I2C::command(unsigned char value) {
00204     send(value, 0);
00205 }
00206 
00207 inline int LiquidCrystal_I2C::write(unsigned char value) {
00208     send(value, Rs);
00209     return 1;
00210 }
00211 
00212 
00213 /************ low level data pushing commands **********/
00214 
00215 // write either command or data
00216 void LiquidCrystal_I2C::send(unsigned char value, unsigned char mode) {
00217     unsigned char highnib=value&0xf0;
00218     unsigned char lownib=(value<<4)&0xf0;
00219     write4bits((highnib)|mode);
00220     write4bits((lownib)|mode);
00221 }
00222 
00223 void LiquidCrystal_I2C::write4bits(unsigned char value) {
00224     expanderWrite(value);
00225     pulseEnable(value);
00226 }
00227 
00228 void LiquidCrystal_I2C::expanderWrite(unsigned char _data){
00229     char data_write[2];
00230     data_write[0] = _data | _backlightval;
00231     //Wire.beginTransmission(_addr);
00232     //Wire.write((int)(_data) | _backlightval);
00233     //Wire.endTransmission();
00234     _i2c.write(_addr, data_write, 1, 0);
00235     _i2c.stop();
00236 }
00237 
00238 void LiquidCrystal_I2C::pulseEnable(unsigned char _data){
00239     expanderWrite(_data | En);  // En high
00240     wait_us(1);       // enable pulse must be >450ns
00241 
00242     expanderWrite(_data & ~En); // En low
00243     wait_us(50);      // commands need > 37us to settle
00244 }
00245 
00246 void LiquidCrystal_I2C::load_custom_character(unsigned char char_num, unsigned char *rows){
00247     createChar(char_num, rows);
00248 }
00249 
00250 void LiquidCrystal_I2C::setBacklight(unsigned char new_val){
00251     if (new_val) {
00252         backlight();        // turn backlight on
00253     } else {
00254         noBacklight();      // turn backlight off
00255     }
00256 }
00257 
00258 void LiquidCrystal_I2C::printstr(const char c[]){
00259     //This function is not identical to the function used for "real" I2C displays
00260     //it's here so the user sketch doesn't have to be changed
00261     //print(c);
00262 }
00263 
00264 int LiquidCrystal_I2C::print(const char* text) {
00265   
00266   while (*text !=0) {
00267     //_putc(*text);
00268     send(*text, Rs);
00269     text++;
00270   }
00271   return 0;
00272 }
00273 
00274 /*
00275 void lcd_dat(unsigned char p)
00276 {
00277 PORTC |= (1 << RS)|(1 << EN); // RS = 1, EN = 1 (начало записи команды в LCD)
00278 PORTD = p; // Вывод команды на шину DB0-7 LCD
00279 _delay_us(100); // Длительность сигнала EN
00280 PORTC &= ~(1 << EN); // EN = 0 (конец записи команды в LCD)
00281 _delay_us(100); // Пауза для выполнения команды
00282 }
00283 */