VIメータのプログラムですI2C LCDとINA226を装備しています。

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2cLCD.cpp Source File

I2cLCD.cpp

00001 #include "I2cLCD.h"
00002 #include "mbed.h"
00003 
00004 #define    I2CLCD_ADDR    0x7C
00005 
00006 #define RS_CMD        0x00
00007 #define RS_DATA       0x40
00008 
00009 #define FUNC_SET1    0x38
00010 #define FUNC_SET2    0x39
00011 #define INT_OSC      0x14
00012 
00013 #define CNTR_DEF    0x20
00014 
00015 unsigned char icon_data[]=
00016 {
00017     0x00, 0x10,
00018     0x02, 0x10,
00019     0x04, 0x10,
00020     0x06, 0x10,
00021     
00022     0x07, 0x10,
00023     0x07, 0x08,
00024     0x09, 0x10,
00025     0x0B, 0x10,
00026     
00027     0x0D, 0x08,
00028     0x0D, 0x04,
00029     0x0D, 0x02,
00030     0x0D, 0x10,
00031 
00032     0x0F, 0x10,
00033 };
00034 
00035 I2cLCD::I2cLCD(PinName sda, PinName scl, PinName rp) :  _rs( rp ) , _i2c( sda , scl ){
00036     
00037     contrast = CNTR_DEF;
00038     icon = 0;
00039     
00040     wait(0.015);
00041     // reset LOW->HIGH
00042     _rs = 0;
00043     wait(0.01);
00044     _rs = 1;
00045     wait(0.05);
00046     
00047     writeCommand(FUNC_SET1);
00048     writeCommand(FUNC_SET2);
00049     writeCommand(INT_OSC);
00050     
00051     writeCommand(0x70 | (contrast & 0xF));
00052     writeCommand(0x5C | ((contrast >> 4) & 0x3));
00053     
00054     writeCommand(0x6C);
00055     wait(0.3);
00056     
00057     writeCommand(0x38); // function set
00058     writeCommand(0x0C); // Display On
00059     
00060     cls(); // Clear Display
00061     
00062 }
00063 
00064 
00065 
00066 void I2cLCD::character(int column, int row, int c) {
00067     int a = address(column, row);
00068     writeCommand(a);
00069     writeData(c);
00070 }
00071 
00072 void I2cLCD::cls() {
00073     writeCommand(0x01); // cls, and set cursor to 0
00074     wait(0.00164f);     // This command takes 1.64 ms
00075     locate(0, 0);
00076 }
00077 
00078 void I2cLCD::locate(int column, int row) {
00079     _column = column;
00080     _row = row;
00081 }
00082 
00083 int I2cLCD::_putc(int value) {
00084     if (value == '\n') {
00085         _column = 0;
00086         _row++;
00087         if (_row >= rows()) {
00088             _row = 0;
00089         }
00090     } else {
00091         character(_column, _row, value);
00092         _column++;
00093         if (_column >= columns()) {
00094             _column = 0;
00095             _row++;
00096             if (_row >= rows()) {
00097                 _row = 0;
00098             }
00099         }
00100     }
00101     return value;
00102 }
00103 
00104 int I2cLCD::_getc() {
00105     return -1;
00106 }
00107 
00108 void I2cLCD::writeCommand( int cmd )
00109 {
00110     char cmds[2];
00111     
00112     cmds[0] = RS_CMD;
00113     cmds[1] = cmd;
00114     
00115     _i2c.write(I2CLCD_ADDR, cmds, 2);
00116 }
00117 
00118 void I2cLCD::writeData( int data )
00119 {
00120     char cmd[2];
00121     
00122     cmd[0] = RS_DATA;
00123     cmd[1] = data;
00124     
00125     _i2c.write(I2CLCD_ADDR, cmd, 2);
00126 }
00127 
00128 int I2cLCD::address(int column, int row) {
00129 
00130     return 0x80 + (row * 0x40) + column;
00131 }
00132 
00133 int I2cLCD::columns() {
00134     return 16;
00135 }
00136 
00137 int I2cLCD::rows() {
00138     return 2;
00139 }
00140 
00141 void I2cLCD::seticon(IconType type)
00142 {
00143     icon |= type;
00144     puticon( icon );
00145 }
00146 
00147 void I2cLCD::clearicon(IconType type)
00148 {
00149     icon &= ~type;
00150     puticon( icon );
00151 }
00152 
00153 
00154 void I2cLCD::puticon(int flg)
00155 {
00156     static unsigned char icon_buff[16];
00157     unsigned char i;
00158     
00159     for(i=0;i<sizeof(icon_data)/2;i++)
00160     {
00161         if(flg & (0x1000>>i))
00162         {
00163             icon_buff[icon_data[i*2]] |= icon_data[i*2+1];
00164         }
00165         else
00166         {
00167             icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1];
00168         }
00169     }
00170 
00171     for(i=0;i<16;i++){
00172         writeCommand(0x39);
00173         writeCommand(0x40+i);
00174         writeData(icon_buff[i]);
00175     }
00176 }