Sitronix ST7032 controlled AQM1602A character LCD module to I2C adapter http://mbed.org/users/okini3939/notebook/i2c-lcd-library/
I2CLCD.cpp
- Committer:
- okini3939
- Date:
- 2010-10-14
- Revision:
- 0:b069b7027af2
- Child:
- 1:bf21aa3f7cdc
File content as of revision 0:b069b7027af2:
/* * mbed library to use a I2C LCD * Copyright (c) 2010 Hiroshi Suga * Released under the MIT License: http://mbed.org/license/mit */ #include "mbed.h" #include "I2CLCD.h" #define I2CLCD_ADDR 0x7c int I2CLCD::_putc (int value) { if (value == '\n') { x = 0; y ++; if (y >= rows()) { y = 0; lcd_out(address(x, y), 0); } } else { lcd_out(address(x, y), 0); lcd_out(value, 1); x ++; if (x >= cols()) { x = 0; y ++; if (y >= rows()) { y = 0; lcd_out(address(x, y), 0); } } } return value; } int I2CLCD::_getc() { return lcd_in(0); } I2CLCD::I2CLCD (PinName p_sda, PinName p_scl, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_sda, p_scl) { init(p_type, p_config); } I2CLCD::I2CLCD (I2C& p_i2c, I2CLCDType p_type, I2CLCDConfig p_config) : i2c(p_i2c) { init(p_type, p_config); } void I2CLCD::init (I2CLCDType p_type, I2CLCDConfig p_config) { type = p_type; lcd_cfg(p_config); wait(0.5); lcd_out(0x30, 0); wait(0.005); lcd_out(0x30, 0); wait(0.002); lcd_out(0x30, 0); lcd_out(0x38, 0); // func lcd_out(0x10, 0); // shift lcd_out(0x0c, 0); // display lcd_out(0x06, 0); // entry mode cls(); } void I2CLCD::cls() { lcd_out(0x01, 0); // clear wait(0.002); lcd_out(0x02, 0); // home wait(0.002); locate(0, 0); } void I2CLCD::locate(int col, int row) { x = col; y = row; lcd_out(address(x, y), 0); } int I2CLCD::address(int col, int row) { switch (type) { case LCD16x1: return (col < 8 ? 0x80 : 0xc0) + (col & 0x03); case LCD16x4: case LCD20x4: switch (row) { case 0: return 0x80 + col; case 1: return 0xc0 + col; case 2: return 0x94 + col; case 3: return 0xd4 + col; } case LCD16x2B: return 0x80 + (row * 40) + col; case LCD8x2: case LCD16x2: case LCD20x2: default: return 0x80 + (row * 0x40) + col; } } int I2CLCD::cols() { switch (type) { case LCD8x2: return 8; case LCD20x4: case LCD20x2: return 20; case LCD16x1: case LCD16x2: case LCD16x2B: case LCD16x4: default: return 16; } } int I2CLCD::rows() { switch (type) { case LCD16x1: return 1; case LCD16x4: case LCD20x4: return 4; case LCD8x2: case LCD16x2: case LCD16x2B: case LCD20x2: default: return 2; } } void I2CLCD::lcd_cfg (I2CLCDConfig cfg) { i2c.start(); i2c.write(I2CLCD_ADDR); i2c.write(LCDCFG_ENABLE | (cfg & 0x1f)); i2c.stop(); } void I2CLCD::lcd_out (int dat, int rs) { i2c.start(); i2c.write(I2CLCD_ADDR); i2c.write(rs ? 0x40 : 0); i2c.write(dat); i2c.stop(); } int I2CLCD::lcd_in (int rs) { int i; i2c.start(); i2c.write(I2CLCD_ADDR); i2c.write(rs ? 0x40 : 0); i2c.start(); i2c.write(I2CLCD_ADDR | 0x01); i = i2c.read(0); i2c.stop(); return i; }