Measure resistance with INA226

Dependencies:   INA226 TextLCD mbed

Fork of INA226TEST by Toshihisa T

Files at this revision

API Documentation at this revision

Comitter:
Naoto_111
Date:
Sun May 15 01:51:55 2016 +0000
Parent:
3:1bd37786be90
Commit message:
serialmode???

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 1bd37786be90 -r 04014356c355 main.cpp
--- a/main.cpp	Sat May 14 10:10:05 2016 +0000
+++ b/main.cpp	Sun May 15 01:51:55 2016 +0000
@@ -1,7 +1,7 @@
 /*前バージョンからの変更点
-・INA226のAveraging modeと同じような機能を実装
-・シリアル通信モードからテキストディスプレイモードに戻れるようにした
-(qキーを押すことで使用可)
+・シリアル通信モードでもオフセットの初期値を取得するように変更
+・シリアル通信モードで最初に送られるデータが(0.0, 0.0, 0,0)だったのを、
+センサから取得した値を送るように変更
 */
 #include "mbed.h"
 #include "TextLCD.h"
@@ -45,16 +45,16 @@
 {
     pc.baud(115200);
     VCmonitor.rawWrite(0x00,0x4EDF);
-    if(!VCmonitor.isExist()){
+    if(!VCmonitor.isExist()) {
         pc.printf("VCmonitor NOT FOUND\r\n");
-        while(1){}
+        while(1) {}
     }
     pc.printf("VCmonitor FOUND\r\n");
 
     val = 0;
-    if(VCmonitor.rawRead(0x00,&val) != 0){
+    if(VCmonitor.rawRead(0x00,&val) != 0) {
         pc.printf("VCmonitor READ ERROR\r\n");
-        while(1){}
+        while(1) {}
     }
     pc.printf("VCmonitor Reg 0x00 : 0x%04x\r\n",val);
 
@@ -70,7 +70,10 @@
         wait(0.5);
     }
 
-    led[0] = led[1] = */resistance = offset = 0;
+    led[0] = led[1] = */
+    resistance = offset = 0;
+    if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0))
+        offset = (float) 10*V/C;
     //tim.stop();
     //FILE *fp = NULL;
     pc.attach(isrRx,Serial::RxIrq);//割り込みハンドラ登録
@@ -81,20 +84,23 @@
         //if(fp!=NULL)fclose(fp);
         dsp_t.cls();
         dsp_t.printf("Serial mode");
-        pc.printf("%f,%f,%f\n", 0.0, 0.0, 0.0);
-        while(1){
-            if(serialmode == false)goto BUNKI;
-        }
+
+        float voltage = 0;
+        for(int i = 0; i < SAMPLES; i++)voltage += sensor.read();
+        voltage = voltage * 3.3 / SAMPLES;
+        resistance = R1 / (VCC / voltage - 1);
+        pc.printf("%f,%f,%f\n", voltage, resistance, offset);
+        //pc.printf("%f,%f,%f\n", 0.0, 0.0, 0.0);
+
+        while(1)if(serialmode == false)goto BUNKI;
     } else {//テキストディスプレイモード
-        
+
         float t = 1.0/261.626;//「ド」の音(C4)の周期
         float a = (t-(t/2/2))/GAUGE_MAX;//C4の周期と2オクターブ上の音の周期の差を(GAUGE_MAX)分割
 
         led[0] = 1;
         led[1] = 0;
-        if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)){
-                offset = (float) 10*V/C;
-        }
+
         //offset = R1 / (VCC / (sensor.read() * 3.3)- 1);//オフセット初期値の取得
         speaker = 0.0;
 
@@ -104,15 +110,15 @@
         setoffset.rise(&gaugeOffset);//割り込みハンドラ登録
         mode.rise(&changeMode);//割り込みハンドラ登録
 
-/*
-        if((fp = fopen("/local/data.txt", "r")) != NULL) { //data.txtの検出
-            if(fgetc(fp)!=EOF) {
-                dsp_t.printf("Err:data exists\nPlease move it.");
-                return EXIT_FAILURE;
-            }
-            fclose(fp);
-        }
-*/
+        /*
+                if((fp = fopen("/local/data.txt", "r")) != NULL) { //data.txtの検出
+                    if(fgetc(fp)!=EOF) {
+                        dsp_t.printf("Err:data exists\nPlease move it.");
+                        return EXIT_FAILURE;
+                    }
+                    fclose(fp);
+                }
+        */
         while(1) {
             if(serialmode == true)goto BUNKI;
             /*
@@ -133,7 +139,7 @@
             //if((fp = fopen("/local/data.txt", "a")) == NULL)return EXIT_FAILURE;
             //fprintf(fp, "%lld %f %f %f\r\n", now + time_sum, voltage, resistance, offset);
             //fclose(fp);
-            if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)){
+            if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)) {
                 voltage = V/1000;
                 resistance = 10*V/C;
             }
@@ -198,7 +204,7 @@
 
 void isrRx()
 {
-    if(serialmode == false){
+    if(serialmode == false) {
         serialmode=true;
         return;
     }
@@ -210,23 +216,24 @@
 
         case 'c'://continue
             float voltage = 0;
+            
             for(int i = 0; i < SAMPLES; i++)voltage += sensor.read();
             voltage = voltage * 3.3 / SAMPLES;
             resistance = R1 / (VCC / voltage - 1);
-            if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)){
+            if((VCmonitor.getVoltage(&V) == 0) && (VCmonitor.getCurrent(&C) == 0)) {
                 voltage = (float) V/1000;
                 resistance = (float) 10*V/C;
-                
+
             }
-            pc.printf("%f,%f,%f\n", 1.1, 200.0, 0.0);
-//            pc.printf("%f,%f,%f\n", voltage, resistance, offset);
+            //pc.printf("%f,%f,%f\n", 1.1, 200.0, 0.0);
+            pc.printf("%f,%f,%f\n", voltage, resistance, offset);
             count++;
             break;
 
         case 'o'://offset
             offset = resistance;
             break;
-            
+
         case 'q'://quit serial mode
             serialmode = false;
             return;