bbmv3

Dependencies:   mbed mcp3208

Files at this revision

API Documentation at this revision

Comitter:
Yanagihara
Date:
Thu Oct 07 08:31:46 2021 +0000
Parent:
0:38fe4bc9b7c3
Commit message:
BBMv3

Changed in this revision

eps.cpp Show annotated file Show diff for this revision Revisions of this file
spi_slave.cpp Show diff for this revision Revisions of this file
diff -r 38fe4bc9b7c3 -r 57c53c066bb5 eps.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eps.cpp	Thu Oct 07 08:31:46 2021 +0000
@@ -0,0 +1,164 @@
+#include "mbed.h"
+
+Serial pc(USBTX,USBRX);
+
+// I/Oピン
+DigitalOut COM_on(p21); // com用のスイッチをONにするピン
+DigitalOut COM_off(p22); // com用のスイッチをOFFにするピン
+DigitalOut dyn(p25);
+
+// CDH用
+SPISlave to_cdh(p5,p6,p7,p8); // mosi, miso, sclk, ssel
+
+// ADコン用
+SPI mcp3208(p11,p12,p13);
+DigitalOut cs_ad1(p14);
+
+// バッテリ電圧測定用
+// AnalogIn v(p16);
+
+// グローバル変数 ***********
+uint8_t EPS_cur[5];
+// *************************
+
+// ADC用関数 ******************************************
+void mcp3208_Init() {
+    mcp3208.format(8,0);
+    mcp3208.frequency(2000000);
+    uint8_t Vref = 5;
+    }
+
+uint8_t get_mcp3208() {
+    cs_ad1 = 0;
+    uint8_t d0 = mcp3208.write(0x06);   // ch1
+    uint8_t d1 = mcp3208.write(0x40);
+    uint8_t d2 = mcp3208.write(0x00);
+    cs_ad1 = 1;
+    uint8_t d3 = d1 << 4;
+    uint8_t d = (d3 << 4) | d2;
+    printf("d=%d\n\r", d);
+    return(d);
+    }
+// ****************************************** ADC用関数終了
+
+// ******************* main文 ***********************
+int main()
+{
+    // ***** toCDH初期設定 *****
+    to_cdh.format(8,0);
+    to_cdh.frequency(1000000);
+    to_cdh.reply(0x0);
+    
+    // ***** mcp3208初期設定 *****
+    mcp3208_Init();
+    
+    // ローカル関数
+    uint8_t ii = 0;
+    // ***************************
+    
+    
+    pc.printf("--Hi,this is eps(slave).\r\n");
+    to_cdh.reply(0x01);
+    
+    while(1) {
+
+        if(to_cdh.receive()) {
+            int rcmd = to_cdh.read();   //receive 1st byte
+
+            if(rcmd == 1){
+                pc.printf("start spi to cdh\r\n");
+
+                    to_cdh.reply(0x00);  // ★ 送信前データをreplyに連続で詰めたい
+                    pc.printf("receive eps_cmd 1 to cdh\r\n ");
+                
+            }
+            else if(rcmd == 2){
+                pc.printf("start survivalHK sensing\r\n");
+                
+                    //v_bat[ii] = v.read()*3.3*8.4/3.3;
+                    EPS_cur[ii] = get_mcp3208();
+                    to_cdh.reply(EPS_cur[ii]);  // ★ 送信前データをreplyに連続で詰めたい
+                    pc.printf("EPS_cur = %d[v]\r\n ",EPS_cur[ii]);
+                    ii++;
+                
+                //pc.printf("finish survivalHK sensing \r\n");
+                
+                
+            }
+            else if(rcmd == 3){
+                // 受信
+                pc.printf("1Hz HK Interrupt\r\n");
+                for(int i=0; i<4; i++){
+                    EPS_cur[i] = get_mcp3208();
+                }
+                to_cdh.reply(EPS_cur[0]);
+                pc.printf("finish HK sensing \r\n");
+            }
+            /*else if(rcmd == 0x07){
+               to_cdh.reply(0xF0);
+               pc.printf("send initial sensing data\r\n");
+            }
+
+            else if(rcmd == 0x08){
+                to_cdh.reply(rcmd);
+                
+                pc.printf("COM ON\r\n");
+                COM_on = 1;
+                wait(0.5);
+                COM_on = 0;
+            }                   
+
+            else if(rcmd == 0x11){
+                to_cdh.reply(rcmd);
+                
+                pc.printf("COM OFF\r\n");
+                COM_off = 1;
+                wait(0.5);
+                COM_off = 0;
+            }                      
+                     
+            else if(rcmd == 0x14){
+                to_cdh.reply(rcmd);
+                
+                pc.printf("Check battery voltage\r\n");
+                for(int i=0; i<5; i++){
+                    v_bat[i] = v.read()*3.3*8.4/3.3; //3.3必要?
+                    pc.printf("bat_v = %f[v]\r\n ",v_bat[i]);
+                    wait(1.0);
+                }
+                pc.printf("finish \r\n");                  
+            } 
+                        
+            else if(rcmd == 0x15){
+                to_cdh.reply(0xF1);
+                pc.printf("Send battery voltage data\r\n");
+            } 
+                        
+            else if(rcmd == 0x16){
+                to_cdh.reply(rcmd);
+                
+                pc.printf("Heatcut ON\r\n");
+                dyn = 1;
+                for(int i=0; i<5; i++){
+                   dyn_cur[i]= mcp3208.read_input(3)*5; //()内の数字はchナンバー
+                   pc.printf("dyn_cur = %f[A]\r\n ",dyn_cur[i]);
+                   wait(1.0);
+                }
+                dyn = 0;
+                pc.printf("heat cut OFF\r\n");                
+            } 
+                        
+            else if(rcmd == 0x17){
+                to_cdh.reply(0xF2);
+                pc.printf("Send cell current data\r\n");
+            } 
+            else{
+                to_cdh.reply(0xFF);
+            }
+                 
+            pc.printf("rcmd: %02x, dummy: %02x\r\n",rcmd, rdummy);
+            */
+
+        }
+    }
+}
\ No newline at end of file
diff -r 38fe4bc9b7c3 -r 57c53c066bb5 spi_slave.cpp
--- a/spi_slave.cpp	Sat Jan 02 09:54:43 2021 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,132 +0,0 @@
-#include "mbed.h"
-
-#include "mcp3208.h"
-
-SPISlave to_cdh(p5,p6,p7,p8); // mosi, miso, sclk, ssel
-Serial pc(USBTX,USBRX);
-
-DigitalOut myleds[4] = {LED1,LED2,LED3,LED4}; // mbed動作目視確認用LED
-DigitalOut ADC_on(p21); // ADC用のスイッチをONにするピン
-DigitalOut ADC_off(p22); // ADC用のスイッチをOFFにするピン
-DigitalOut COM_on(p23);
-DigitalOut COM_off(p24);
-DigitalOut myled(LED1);
-
-AnalogIn v(p16);
-SPI spi(p11,p12,p13);
-MCP3208 mcp3208(spi,p14);
-DigitalOut dyn(p25);
-
-float v_bat[5], EPS_cur[5], ADC_cur[5], dyn_cur[5];
-int data;
-
-int main()
-{
-    pc.printf("--Hi,this is eps(slave).\r\n");
-    
-    to_cdh.format(8,3);
-    to_cdh.frequency(1000000);
-
-    int sdummy = 0xAA;
-
-    while(1) {
-
-        if(to_cdh.receive()) {
-            int rcmd = to_cdh.read();   //receive 1st byte
-            int rdummy = to_cdh.read(); //receive 2nd byte
-            
-            to_cdh.reply(sdummy); // prepare to send 1st byte (dummy)
-//            to_cdh.reply(rcmd);
-            
-            if(rcmd == 0x01){
-                to_cdh.reply(rcmd);
-            }
-            else if(rcmd == 0x02){
-                to_cdh.reply(rcmd);
-                
-                pc.printf("ADC ON \r\n");
-                ADC_on = 1;
-                wait(0.5);
-                ADC_on = 0;
-            }
-            else if (rcmd == 0x05){
-                to_cdh.reply(rcmd);
-                pc.printf("start initial sensing\r\n");
-                for(int i=0; i<5; i++){
-                    v_bat[i] = v.read()*3.3*8.4/3.3;
-                    EPS_cur[i] = mcp3208.read_input(0)*5; //()内の数字はchナンバー
-                    ADC_cur[i] = mcp3208.read_input(1)*5; //()内の数字はchナンバー
-                    pc.printf("bat_v = %f[v] EPS_cur = %f[A] ADC_cur = %f[A]\r\n ",v_bat[i],EPS_cur[i],ADC_cur[i]);
-                    wait(1.0);
-                }
-                pc.printf("finish initial sensing \r\n");
-            
-            }
-            else if(rcmd == 0x07){
-               to_cdh.reply(0xF0);
-               pc.printf("send initial sensing data\r\n");
-            }
-
-            else if(rcmd == 0x08){
-                to_cdh.reply(rcmd);
-                
-                pc.printf("COM ON\r\n");
-                COM_on = 1;
-                wait(0.5);
-                COM_on = 0;
-            }                   
-
-            else if(rcmd == 0x11){
-                to_cdh.reply(rcmd);
-                
-                pc.printf("COM OFF\r\n");
-                COM_off = 1;
-                wait(0.5);
-                COM_off = 0;
-            }                      
-                     
-            else if(rcmd == 0x14){
-                to_cdh.reply(rcmd);
-                
-                pc.printf("Check battery voltage\r\n");
-                for(int i=0; i<5; i++){
-                    v_bat[i] = v.read()*3.3*8.4/3.3; //3.3必要?
-                    pc.printf("bat_v = %f[v]\r\n ",v_bat[i]);
-                    wait(1.0);
-                }
-                pc.printf("finish \r\n");                  
-            } 
-                        
-            else if(rcmd == 0x15){
-                to_cdh.reply(0xF1);
-                pc.printf("Send battery voltage data\r\n");
-            } 
-                        
-            else if(rcmd == 0x16){
-                to_cdh.reply(rcmd);
-                
-                pc.printf("Heatcut ON\r\n");
-                dyn = 1;
-                for(int i=0; i<5; i++){
-                   dyn_cur[i]= mcp3208.read_input(3)*5; //()内の数字はchナンバー
-                   pc.printf("dyn_cur = %f[A]\r\n ",dyn_cur[i]);
-                   wait(1.0);
-                }
-                dyn = 0;
-                pc.printf("heat cut OFF\r\n");                
-            } 
-                        
-            else if(rcmd == 0x17){
-                to_cdh.reply(0xF2);
-                pc.printf("Send cell current data\r\n");
-            } 
-            else{
-                to_cdh.reply(0xFF);
-            }
-                 
-            pc.printf("rcmd: %02x, dummy: %02x\r\n",rcmd, rdummy);
-            
-
-        }
-    }
-}
\ No newline at end of file