takahiro maeda / Mbed 2 deprecated i2c_lcd_ACM1602NI

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2cLCD.cpp Source File

I2cLCD.cpp

00001 //8x2 lcd
00002 #include "I2cLCD.h"
00003 #include "mbed.h"
00004 
00005 #define    I2CLCD_ADDR    0xa0//0x7C
00006 
00007 #define RS_CMD        0x00
00008 #define RS_DATA       0x80
00009 const short WAIT1 = 10;
00010 
00011 I2cLCD::I2cLCD(PinName sda, PinName scl) :  _i2c( sda , scl ){
00012     //_i2c.frequency(50000);
00013     wait_ms(15);
00014     writeCommand(0x01);
00015     wait_ms(5);
00016     writeCommand(0x38);
00017     wait_ms(5);
00018     writeCommand(0x0f);
00019     wait_ms(5);
00020     writeCommand(0x06);
00021     wait_ms(5);
00022     writeCommand(0x0c);
00023     wait_ms(5);       
00024 }
00025 
00026 
00027 
00028 void I2cLCD::character(int column, int row, int c) {
00029     int a = address(column, row);
00030     writeCommand(a);
00031     writeData(c);
00032 }
00033 
00034 void I2cLCD::cls() {
00035     writeCommand(0x01); // cls, and set cursor to 0
00036     wait_ms(2);
00037     locate(0, 0);
00038 }
00039 
00040 void I2cLCD::locate(int column, int row) {
00041     _column = column;
00042     _row = row;
00043 }
00044 
00045 int I2cLCD::_putc(int value) {
00046     if (value == '\n') {
00047         _column = 0;
00048         _row++;
00049         if (_row >= rows()) {
00050             _row = 0;
00051         }
00052     } else {
00053         character(_column, _row, value);
00054         _column++;
00055         if (_column >= columns()) {
00056             _column = 0;
00057             _row++;
00058             if (_row >= rows()) {
00059                 _row = 0;
00060             }
00061         }
00062     }
00063     return value;
00064 }
00065 
00066 int I2cLCD::_getc() {
00067     return -1;
00068 }
00069 
00070 void I2cLCD::writeCommand( int cmd )
00071 {
00072     char cmds[2];
00073     
00074     cmds[0] = RS_CMD;
00075     cmds[1] = cmd;
00076     
00077     //_i2c.write(I2CLCD_ADDR, cmds, 2);
00078     _i2c.start();
00079     wait_us(9);
00080     _i2c.write(I2CLCD_ADDR);
00081     wait_us(WAIT1);
00082     _i2c.write(cmds[0]);
00083     wait_us(WAIT1); 
00084     _i2c.write(cmds[1]);
00085     wait_us(WAIT1); 
00086     _i2c.stop();
00087 }
00088 
00089 void I2cLCD::writeData( int data )
00090 {
00091     char cmd[2];
00092     
00093     cmd[0] = RS_DATA;
00094     cmd[1] = data;
00095     
00096     //_i2c.write(I2CLCD_ADDR, cmd, 2);
00097     _i2c.start();
00098     wait_us(9);
00099     _i2c.write(I2CLCD_ADDR);
00100     wait_us(WAIT1); 
00101     _i2c.write(cmd[0]);
00102     wait_us(WAIT1); 
00103     _i2c.write(cmd[1]);
00104     wait_us(WAIT1); 
00105     _i2c.stop();
00106 
00107 }
00108 
00109 int I2cLCD::address(int column, int row) {
00110 
00111     return 0x80 + (row * 0x40) + column;
00112 }
00113 
00114 int I2cLCD::columns() {
00115     return 16;
00116 }
00117 
00118 int I2cLCD::rows() {
00119     return 2;
00120 }