Miguel Urco / I2CLCD

Dependents:   JRO_CR2 frdm_test JRO_DDSv2 JRO_DDSv2_rev2019

Fork of I2CLCD by Suga koubou

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CLCD.cpp Source File

I2CLCD.cpp

Go to the documentation of this file.
00001 /*
00002  * mbed library for I2C LCD
00003  * Copyright (c) 2011 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  *
00006  * This product includes:
00007  * mbed TextLCD Library, for a 4-bit LCD based on HD44780
00008  * Copyright (c) 2007-2010, sford
00009  */
00010 
00011 /** @file I2CLCD.cpp
00012  * @brief I2C LCD library (mbed Phone Platform)
00013  */
00014 
00015 #include "mbed.h"
00016 #include "I2CLCD.h"
00017 
00018 /**
00019  * @brief put character to LCD
00020  * @param value ASCII character code
00021  * @retval value
00022  */
00023  
00024 void I2CLCD::printf(const char *text, int x, int y)
00025 {
00026     int n, len;
00027 
00028     len = strlen(text);
00029     
00030     i2c.start();
00031     i2c.write(i2caddr);
00032     i2c.write(0x00);        //Comsend
00033     i2c.write(address(x,y));
00034     i2c.stop();
00035   
00036     wait_us(200);
00037     
00038     i2c.start();
00039     i2c.write(i2caddr);
00040     i2c.write(0x40);        //Datasend
00041     for(n=0; n<len; n++){
00042            i2c.write(*text);
00043            ++text;
00044     }
00045     i2c.stop();
00046     wait_us(200);  
00047 } 
00048 
00049 int I2CLCD::_putc (int value) {
00050 
00051     if (value == '\n') {
00052         x = 0;
00053         y ++;
00054         if (y >= rows()) {
00055             y = 0;
00056             lcd_out(address(x, y), 0);
00057         }
00058 
00059     } else {
00060 
00061         lcd_out(address(x, y), 0);
00062         lcd_out(value, 1);
00063         x ++;
00064         if (x >= cols()) {
00065             x = 0;
00066             y ++;
00067             if (y >= rows()) {
00068                 y = 0;
00069                 lcd_out(address(x, y), 0);
00070             }
00071         }
00072     }
00073 
00074     return value;
00075 }
00076 
00077 /**
00078  * @brief get character from LCD
00079  * @retval ASCII character code
00080  */
00081 int I2CLCD::_getc() {
00082     return lcd_in(0);
00083 }
00084 
00085 
00086 /**
00087  * @brief put character to LCD
00088  * @param p_sda port of I2C SDA
00089  * @param p_scl port of I2C SCL
00090  * @param p_i2caddr I2C address
00091  */
00092 I2CLCD::I2CLCD (PinName p_sda, PinName p_scl, int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_sda, p_scl) {
00093     init(p_i2caddr, p_type, p_config);
00094 }
00095 
00096 /**
00097  * @brief put character to LCD
00098  * @param p_i2c instance of I2C class
00099  * @param p_i2caddr I2C address
00100  */
00101 I2CLCD::I2CLCD (I2C& p_i2c, int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_i2c) {
00102     init(p_i2caddr, p_type, p_config);
00103 }
00104 
00105 void I2CLCD::init (int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) {
00106 
00107     i2caddr = p_i2caddr;
00108     type = p_type;
00109     /*
00110 #ifndef LCD_NHD_C022BiZ
00111     //lcd_cfg(p_config);
00112 #endif
00113     wait_ms(500);
00114     lcd_out(0x30, 0);
00115     wait_us(50);
00116     lcd_out(0x30, 0);
00117     wait_us(50);
00118     lcd_out(0x39, 0);
00119     wait_us(50);
00120     lcd_out(0x14, 0);       //Bias set
00121     wait_us(50);
00122     lcd_out(0x78, 0);       //Constrast set
00123     wait_us(50); 
00124     lcd_out(0x5E, 0);       //Power/ Icon/ Constrast control
00125     wait_us(50);
00126     lcd_out(0x6A, 0);       //Follower control
00127     wait_ms(300); 
00128     
00129     lcd_out(INST_FUNC, 0); // func 
00130     wait_us(50);
00131     lcd_out(INST_SHIFT, 0); // shift
00132     wait_us(50);
00133     lcd_out(INST_DISPLAY, 0); // display
00134     wait_us(50);
00135     lcd_out(INST_CLEAR, 0); // clear
00136     wait_ms(5);
00137     lcd_out(INST_ENTRY_MODE, 0); // entry mode
00138     wait_us(50);
00139     cls();
00140     wait_us(50);
00141     */
00142     
00143     i2c.frequency(100000);
00144     wait_ms(100);
00145     i2c.start();
00146     i2c.write(i2caddr);
00147     i2c.write(0x00);        //Comsend
00148     i2c.write(0x38);
00149     wait_ms(10);
00150     i2c.write(0x39);
00151     wait_ms(10);
00152     i2c.write(0x14);
00153     i2c.write(0x78);
00154     i2c.write(0x5E);
00155     i2c.write(0x6D);
00156     i2c.write(0x0C);
00157     i2c.write(0x01);
00158     i2c.write(0x06);
00159     wait_ms(10);
00160     i2c.stop();
00161 }
00162 
00163 void I2CLCD::cls() {
00164     lcd_out(0x01, 0); // clear
00165     wait_ms(2);
00166     lcd_out(0x02, 0); // home
00167     wait_ms(2);
00168     locate(0, 0);
00169 }
00170 
00171 void I2CLCD::locate(int col, int row) {
00172     x = col;
00173     y = row;
00174     lcd_out(address(x, y), 0);
00175 }
00176 
00177 int I2CLCD::address(int col, int row) {
00178     switch (type) {
00179         case LCD16x1:
00180             return (col < 8 ? 0x80 : 0xc0) + (col & 0x03);
00181         case LCD16x4:
00182         case LCD20x4:
00183             switch (row) {
00184                 case 0:
00185                     return 0x80 + col;
00186                 case 1:
00187                     return 0xc0 + col;
00188                 case 2:
00189                     return 0x94 + col;
00190                 case 3:
00191                     return 0xd4 + col;
00192             }
00193         case LCD16x2B:
00194             return 0x80 + (row * 40) + col;
00195         case LCD8x2:
00196         case LCD16x2:
00197         case LCD20x2:
00198         default:
00199             return 0x80 + (row * 0x40) + col;
00200     }
00201 }
00202 
00203 int I2CLCD::cols() {
00204     switch (type) {
00205         case LCD8x2:
00206             return 8;
00207         case LCD20x4:
00208         case LCD20x2:
00209             return 20;
00210         case LCD16x1:
00211         case LCD16x2:
00212         case LCD16x2B:
00213         case LCD16x4:
00214         default:
00215             return 16;
00216     }
00217 }
00218 
00219 int I2CLCD::rows() {
00220     switch (type) {
00221         case LCD16x1:
00222             return 1;
00223         case LCD16x4:
00224         case LCD20x4:
00225             return 4;
00226         case LCD8x2:
00227         case LCD16x2:
00228         case LCD16x2B:
00229         case LCD20x2:
00230         default:
00231             return 2;
00232     }
00233 }
00234 
00235 void I2CLCD::lcd_cfg (I2CLCDConfig cfg) {
00236     i2c.start();
00237     i2c.write(i2caddr);
00238     i2c.write(LCDCFG_ENABLE | (cfg & 0x1f));
00239     i2c.stop();
00240 }
00241 
00242 void I2CLCD::lcd_out (char dat, char is_value) {
00243     
00244     i2c.start();
00245     i2c.write(i2caddr);
00246     i2c.write(is_value ? 0x40 : 0x00);
00247     i2c.write(dat);
00248     i2c.stop();
00249 }
00250 
00251 char I2CLCD::lcd_in (char is_value) {
00252     char i;
00253 
00254     i2c.start();
00255     i2c.write(i2caddr);
00256     i2c.write(is_value ? 0x40 : 0x00);
00257     i2c.start();
00258     i2c.write(i2caddr | 0x01);
00259     i = i2c.read(0);
00260     i2c.stop();
00261 
00262     return i;
00263 }