Sitronix ST7032 controlled AQM1602A character LCD module to I2C adapter http://mbed.org/users/okini3939/notebook/i2c-lcd-library/

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 int I2CLCD::_putc (int value) {
00024 
00025     if (value == '\n') {
00026         x = 0;
00027         y ++;
00028         if (y >= rows()) {
00029             y = 0;
00030             lcd_out(address(x, y), 0);
00031         }
00032 
00033     } else {
00034 
00035         lcd_out(address(x, y), 0);
00036         lcd_out(value, 1);
00037         x ++;
00038         if (x >= cols()) {
00039             x = 0;
00040             y ++;
00041             if (y >= rows()) {
00042                 y = 0;
00043                 lcd_out(address(x, y), 0);
00044             }
00045         }
00046     }
00047 
00048     return value;
00049 }
00050 
00051 /**
00052  * @brief get character from LCD
00053  * @retval ASCII character code
00054  */
00055 int I2CLCD::_getc() {
00056     return lcd_in(0);
00057 }
00058 
00059 
00060 /**
00061  * @brief put character to LCD
00062  * @param p_sda port of I2C SDA
00063  * @param p_scl port of I2C SCL
00064  * @param p_i2caddr I2C address
00065  */
00066 I2CLCD::I2CLCD (PinName p_sda, PinName p_scl, int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_sda, p_scl) {
00067     init(p_i2caddr, p_type, p_config);
00068 }
00069 
00070 /**
00071  * @brief put character to LCD
00072  * @param p_i2c instance of I2C class
00073  * @param p_i2caddr I2C address
00074  */
00075 I2CLCD::I2CLCD (I2C& p_i2c, int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_i2c) {
00076     init(p_i2caddr, p_type, p_config);
00077 }
00078 
00079 void I2CLCD::init (int p_i2caddr, I2CLCDType p_type, I2CLCDConfig p_config) {
00080 
00081     i2caddr = p_i2caddr;
00082     type = p_type;
00083 
00084     lcd_cfg(p_config);
00085 
00086     wait_ms(500);
00087     lcd_out(0x38, 0); // 8bit bus mode, 2-line display mode 
00088     wait_ms(5);
00089     lcd_out(0x39, 0); // extention instruction selected.
00090     wait_ms(2);
00091     lcd_out(0x14, 0); // int. OSC freq. set
00092     wait_ms(2);
00093     lcd_out(0x73, 0); // Contrast set 
00094     wait_ms(2);
00095     lcd_out(0x56, 0); // Power/ICON/Contrast control
00096     wait_ms(2);
00097     lcd_out(0x6c, 0); // Follower control
00098     wait_ms(300);
00099 
00100     lcd_out(0x38, 0); // 8bit bus mode, 2-line display mode 
00101     wait_ms(5);
00102     lcd_out(0x01, 0); // clear display
00103     wait_ms(1);
00104     lcd_out(0x0c, 0); // Display on/off control
00105 
00106 }
00107 
00108 void I2CLCD::cls() {
00109     lcd_out(0x01, 0); // clear
00110     wait_ms(2);
00111     lcd_out(0x02, 0); // home
00112     wait_ms(2);
00113     locate(0, 0);
00114 }
00115 
00116 void I2CLCD::locate(int col, int row) {
00117     x = col;
00118     y = row;
00119     lcd_out(address(x, y), 0);
00120 }
00121 
00122 int I2CLCD::address(int col, int row) {
00123     switch (type) {
00124         case LCD16x1:
00125             return (col < 8 ? 0x80 : 0xc0) + (col & 0x03);
00126         case LCD16x4:
00127         case LCD20x4:
00128             switch (row) {
00129                 case 0:
00130                     return 0x80 + col;
00131                 case 1:
00132                     return 0xc0 + col;
00133                 case 2:
00134                     return 0x94 + col;
00135                 case 3:
00136                     return 0xd4 + col;
00137             }
00138         case LCD16x2B:
00139             return 0x80 + (row * 40) + col;
00140         case LCD8x2:
00141         case LCD16x2:
00142         case LCD20x2:
00143         default:
00144             return 0x80 + (row * 0x40) + col;
00145     }
00146 }
00147 
00148 int I2CLCD::cols() {
00149     switch (type) {
00150         case LCD8x2:
00151             return 8;
00152         case LCD20x4:
00153         case LCD20x2:
00154             return 20;
00155         case LCD16x1:
00156         case LCD16x2:
00157         case LCD16x2B:
00158         case LCD16x4:
00159         default:
00160             return 16;
00161     }
00162 }
00163 
00164 int I2CLCD::rows() {
00165     switch (type) {
00166         case LCD16x1:
00167             return 1;
00168         case LCD16x4:
00169         case LCD20x4:
00170             return 4;
00171         case LCD8x2:
00172         case LCD16x2:
00173         case LCD16x2B:
00174         case LCD20x2:
00175         default:
00176             return 2;
00177     }
00178 }
00179 
00180 void I2CLCD::lcd_cfg (I2CLCDConfig cfg) {
00181     i2c.start();
00182     i2c.write(i2caddr);
00183     i2c.write(LCDCFG_ENABLE | (cfg & 0x1f));
00184     i2c.stop();
00185 }
00186 
00187 void I2CLCD::lcd_out (char dat, char rs) {
00188     i2c.start();
00189     i2c.write(i2caddr);
00190     i2c.write(rs ? 0x40 : 0);
00191     i2c.write(dat);
00192     i2c.stop();
00193 }
00194 
00195 char I2CLCD::lcd_in (char rs) {
00196     char i;
00197 
00198     i2c.start();
00199     i2c.write(i2caddr);
00200     i2c.write(rs ? 0x40 : 0);
00201 
00202     i2c.start();
00203     i2c.write(i2caddr | 0x01);
00204     i = i2c.read(0);
00205     i2c.stop();
00206 
00207     return i;
00208 }