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

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
takeuchi
Date:
Mon May 09 08:38:27 2016 +0000
Commit message:
VI?????????????I2C?INA????I2C?LCD?????????

Changed in this revision

I2cLCD.cpp Show annotated file Show diff for this revision Revisions of this file
I2cLCD.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2cLCD.cpp	Mon May 09 08:38:27 2016 +0000
@@ -0,0 +1,176 @@
+#include "I2cLCD.h"
+#include "mbed.h"
+
+#define    I2CLCD_ADDR    0x7C
+
+#define RS_CMD        0x00
+#define RS_DATA       0x40
+
+#define FUNC_SET1    0x38
+#define FUNC_SET2    0x39
+#define INT_OSC      0x14
+
+#define CNTR_DEF    0x20
+
+unsigned char icon_data[]=
+{
+    0x00, 0x10,
+    0x02, 0x10,
+    0x04, 0x10,
+    0x06, 0x10,
+    
+    0x07, 0x10,
+    0x07, 0x08,
+    0x09, 0x10,
+    0x0B, 0x10,
+    
+    0x0D, 0x08,
+    0x0D, 0x04,
+    0x0D, 0x02,
+    0x0D, 0x10,
+
+    0x0F, 0x10,
+};
+
+I2cLCD::I2cLCD(PinName sda, PinName scl, PinName rp) :  _rs( rp ) , _i2c( sda , scl ){
+    
+    contrast = CNTR_DEF;
+    icon = 0;
+    
+    wait(0.015);
+    // reset LOW->HIGH
+    _rs = 0;
+    wait(0.01);
+    _rs = 1;
+    wait(0.05);
+    
+    writeCommand(FUNC_SET1);
+    writeCommand(FUNC_SET2);
+    writeCommand(INT_OSC);
+    
+    writeCommand(0x70 | (contrast & 0xF));
+    writeCommand(0x5C | ((contrast >> 4) & 0x3));
+    
+    writeCommand(0x6C);
+    wait(0.3);
+    
+    writeCommand(0x38); // function set
+    writeCommand(0x0C); // Display On
+    
+    cls(); // Clear Display
+    
+}
+
+
+
+void I2cLCD::character(int column, int row, int c) {
+    int a = address(column, row);
+    writeCommand(a);
+    writeData(c);
+}
+
+void I2cLCD::cls() {
+    writeCommand(0x01); // cls, and set cursor to 0
+    wait(0.00164f);     // This command takes 1.64 ms
+    locate(0, 0);
+}
+
+void I2cLCD::locate(int column, int row) {
+    _column = column;
+    _row = row;
+}
+
+int I2cLCD::_putc(int value) {
+    if (value == '\n') {
+        _column = 0;
+        _row++;
+        if (_row >= rows()) {
+            _row = 0;
+        }
+    } else {
+        character(_column, _row, value);
+        _column++;
+        if (_column >= columns()) {
+            _column = 0;
+            _row++;
+            if (_row >= rows()) {
+                _row = 0;
+            }
+        }
+    }
+    return value;
+}
+
+int I2cLCD::_getc() {
+    return -1;
+}
+
+void I2cLCD::writeCommand( int cmd )
+{
+    char cmds[2];
+    
+    cmds[0] = RS_CMD;
+    cmds[1] = cmd;
+    
+    _i2c.write(I2CLCD_ADDR, cmds, 2);
+}
+
+void I2cLCD::writeData( int data )
+{
+    char cmd[2];
+    
+    cmd[0] = RS_DATA;
+    cmd[1] = data;
+    
+    _i2c.write(I2CLCD_ADDR, cmd, 2);
+}
+
+int I2cLCD::address(int column, int row) {
+
+    return 0x80 + (row * 0x40) + column;
+}
+
+int I2cLCD::columns() {
+    return 16;
+}
+
+int I2cLCD::rows() {
+    return 2;
+}
+
+void I2cLCD::seticon(IconType type)
+{
+    icon |= type;
+    puticon( icon );
+}
+
+void I2cLCD::clearicon(IconType type)
+{
+    icon &= ~type;
+    puticon( icon );
+}
+
+
+void I2cLCD::puticon(int flg)
+{
+    static unsigned char icon_buff[16];
+    unsigned char i;
+    
+    for(i=0;i<sizeof(icon_data)/2;i++)
+    {
+        if(flg & (0x1000>>i))
+        {
+            icon_buff[icon_data[i*2]] |= icon_data[i*2+1];
+        }
+        else
+        {
+            icon_buff[icon_data[i*2]] &= ~icon_data[i*2+1];
+        }
+    }
+
+    for(i=0;i<16;i++){
+        writeCommand(0x39);
+        writeCommand(0x40+i);
+        writeData(icon_buff[i]);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2cLCD.h	Mon May 09 08:38:27 2016 +0000
@@ -0,0 +1,58 @@
+
+#ifndef MBED_I2CLCD_H
+#define MBED_I2CLCD_H
+
+#include "mbed.h"
+
+class I2cLCD : public Stream {
+public:
+
+    enum IconType {
+        Mark       =  0x0001,
+        Battery_1  =  0x0004,
+        Battery_2  =  0x0002,
+        Battery_3  =  0x0010,
+        Battery_4  =  0x0008,
+        NoSound    =  0x0020,
+        Lock       =  0x0040,
+        ArrowDown  =  0x0080,
+        ArrowUp    =  0x0100,
+        Input      =  0x0200,
+        Alarm      =  0x0400,
+        Tell       =  0x0800,
+        Antenna    =  0x1000,
+    };
+
+    I2cLCD(PinName sda, PinName scl, PinName rp);
+#if DOXYGEN_ONLY
+    int putc(int c);
+    int printf(const char* format, ...);
+#endif
+    void locate(int column, int row);
+    void cls();
+    int rows();
+    int columns();
+    void seticon(IconType type);
+    void clearicon(IconType type);
+    void puticon(int flg);
+    
+protected:
+    virtual int _putc(int value);
+    virtual int _getc();
+
+    int address(int column, int row);
+    void character(int column, int row, int c);
+    void writeCommand( int cmd );
+    void writeData( int data );
+    
+    DigitalOut _rs;
+    I2C    _i2c;
+    int _column;
+    int _row;
+    
+    char contrast;
+    int icon;
+    
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 09 08:38:27 2016 +0000
@@ -0,0 +1,158 @@
+ // VI Meter Kiban
+
+
+#include "mbed.h"
+#include "I2cLCD.h"
+
+I2cLCD lcd(dp5,dp27,dp26); //sda scl reset
+I2C Ina226(dp5,dp27);//SDA SCL
+DigitalOut led_yellow(dp16);
+DigitalOut led_green(dp15);
+
+unsigned int waddr=0x80,raddr=0x81;
+char cmd[3],val[3];
+double vc,ic,ihosei;
+
+#define ON 1
+#define OFF 0
+#define XON 0
+#define XOFF 1
+
+#define batt1_ON lcd.seticon( I2cLCD::Battery_1 )
+#define batt2_ON lcd.seticon( I2cLCD::Battery_2 )
+#define batt3_ON lcd.seticon( I2cLCD::Battery_3 ) 
+#define batt4_ON lcd.seticon( I2cLCD::Battery_4 )
+
+#define batt1_OFF lcd.clearicon( I2cLCD::Battery_1 )
+#define batt2_OFF lcd.clearicon( I2cLCD::Battery_2 )
+#define batt3_OFF lcd.clearicon( I2cLCD::Battery_3 ) 
+#define batt4_OFF lcd.clearicon( I2cLCD::Battery_4 )
+
+#define arrow_down_on lcd.seticon( I2cLCD::ArrowDown );
+#define arrow_up_on   lcd.seticon( I2cLCD::ArrowUp );
+#define arrow_down_off lcd.clearicon( I2cLCD::ArrowDown ); 
+#define arrow_up_off   lcd.clearicon( I2cLCD::ArrowUp );
+
+/*
+void batt_mark(){
+    if(vc < 4.5 ){
+      batt1_ON;
+      batt2_OFF;
+      batt3_OFF;
+      batt4_OFF;
+    }
+    else if(vc < 4.8){
+      batt1_ON;
+      batt2_ON;
+      batt3_OFF;
+      batt4_OFF;
+    }
+    else if(vc < 5.0){
+      batt1_ON;
+      batt2_ON;
+      batt3_ON;
+      batt4_OFF;
+    }
+    else{
+      batt1_ON;
+      batt2_ON;
+      batt3_ON;
+      batt4_ON;
+    }
+}     
+*/
+      
+void led_flash(){
+    int i;
+    for(i=0;i<3;i++){
+      led_green=ON;
+      led_yellow=ON;
+      wait(0.1);
+      led_green=OFF;
+      led_yellow=OFF;
+      wait(0.1);
+    }
+}
+
+int main() {      
+
+  led_flash();
+  lcd.cls();
+  lcd.locate(0,0);
+  lcd.printf("* VI Meter 1.1 *");
+  
+  cmd[0]=0x05;// calibration registar
+  cmd[1]=0x0a;// shunt reg=2m ohm
+  cmd[2]=0x00;
+  Ina226.write(waddr,cmd,3);
+  wait(0.1);
+  
+  cmd[0]=0x00;// config reg.
+  cmd[1]=0x45;// average set & number=16
+  cmd[2]=0x27;
+  Ina226.write(waddr,cmd,3);
+  wait(0.1);
+  
+  cmd[0]=0x04;// current read
+  Ina226.write(waddr,cmd,1);
+  Ina226.read(raddr,val,2);
+  ic=(short)(val[0]<<8 | val[1]);
+  if(-3.0 <ic && ic < 3.0 ){
+    ihosei=0.0-ic;
+  }    
+  wait(0.1);
+    
+  while(1){
+  
+    cmd[0]=0x04;// current read
+    Ina226.write(waddr,cmd,1);
+    Ina226.read(raddr,val,2);
+    ic=(short)(val[0]<<8 | val[1])+ihosei;
+    if(ic < 0){ //discharge
+      arrow_down_on;
+      arrow_up_off;
+      led_green=OFF;
+      led_yellow=ON;
+    }
+    else{ //charge
+      arrow_up_on;
+      arrow_down_off;
+      led_green=ON;
+      led_yellow=OFF; 
+    }
+      
+    wait(0.1);
+ 
+    cmd[0]=0x02;// voltage read
+    Ina226.write(waddr,cmd,1);
+    Ina226.read(raddr,val,2);
+    vc=(val[0]<<8 | val[1])*1.25/1000;
+ /*   
+    cmd[0]=0x03;// power
+    Ina226.write(waddr,cmd,1);
+    Ina226.read(raddr,val,2);
+    pw=(val[0]<<8 | val[1])/25;
+ 
+    
+    pw=out_v*ic/1000;// power 
+    if(out_v >= 12.0){
+      out_v=12.0;
+    }
+    if(pduty <= 0.03){
+      out_v=0;
+    }
+*/ 
+ 
+    lcd.locate(0,1);
+    lcd.printf("%4.1fV,%4.0fmA",vc,ic);        
+
+    led_green=ON;
+    wait(1.0);
+    led_green=OFF;
+    wait(1.0);
+    
+   // batt_mark();
+ 
+  }//while     
+}//main
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon May 09 08:38:27 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file