a
Dependencies: mbed mcp3208 p_eps_sequencial_20210930
eps.cpp
00001 #include "mbed.h" 00002 00003 Serial pc(USBTX,USBRX); 00004 00005 // I/Oピン 00006 DigitalOut COM_on(p21); // com用のスイッチをONにするピン 00007 DigitalOut COM_off(p22); // com用のスイッチをOFFにするピン 00008 DigitalOut dyn(p25); 00009 00010 // CDH用 00011 SPISlave to_cdh(p5,p6,p7,p8); // mosi, miso, sclk, ssel 00012 00013 // ADコン用 00014 SPI mcp3208(p11,p12,p13); 00015 DigitalOut cs_ad1(p14); 00016 00017 // バッテリ電圧測定用 00018 // AnalogIn v(p16); 00019 00020 // グローバル変数 *********** 00021 uint8_t EPS_cur[5]; 00022 // ************************* 00023 00024 // ADC用関数 ****************************************** 00025 void mcp3208_Init() { 00026 mcp3208.format(8,0); 00027 mcp3208.frequency(2000000); 00028 uint8_t Vref = 5; 00029 } 00030 00031 uint8_t get_mcp3208() { 00032 cs_ad1 = 0; 00033 uint8_t d0 = mcp3208.write(0x06); // ch1 00034 uint8_t d1 = mcp3208.write(0x40); 00035 uint8_t d2 = mcp3208.write(0x00); 00036 cs_ad1 = 1; 00037 uint8_t d3 = d1 << 4; 00038 uint8_t d = (d3 << 4) | d2; 00039 printf("d=%d\n\r", d); 00040 return(d); 00041 } 00042 // ****************************************** ADC用関数終了 00043 00044 // ******************* main文 *********************** 00045 int main() 00046 { 00047 // ***** toCDH初期設定 ***** 00048 to_cdh.format(8,0); 00049 to_cdh.frequency(1000000); 00050 to_cdh.reply(0x0); 00051 00052 // ***** mcp3208初期設定 ***** 00053 mcp3208_Init(); 00054 00055 // ローカル関数 00056 uint8_t ii = 0; 00057 // *************************** 00058 00059 00060 pc.printf("--Hi,this is eps(slave).\r\n"); 00061 to_cdh.reply(0x01); 00062 00063 while(1) { 00064 00065 if(to_cdh.receive()) { 00066 int rcmd = to_cdh.read(); //receive 1st byte 00067 00068 if(rcmd == 1){ 00069 pc.printf("start spi to cdh\r\n"); 00070 00071 to_cdh.reply(0x00); // ★ 送信前データをreplyに連続で詰めたい 00072 pc.printf("receive eps_cmd 1 to cdh\r\n "); 00073 00074 } 00075 else if(rcmd == 2){ 00076 pc.printf("start survivalHK sensing\r\n"); 00077 00078 //v_bat[ii] = v.read()*3.3*8.4/3.3; 00079 EPS_cur[ii] = get_mcp3208(); 00080 to_cdh.reply(EPS_cur[ii]); // ★ 送信前データをreplyに連続で詰めたい 00081 pc.printf("EPS_cur = %d[v]\r\n ",EPS_cur[ii]); 00082 ii++; 00083 00084 //pc.printf("finish survivalHK sensing \r\n"); 00085 00086 00087 } 00088 else if(rcmd == 3){ 00089 // 受信 00090 pc.printf("1Hz HK Interrupt\r\n"); 00091 for(int i=0; i<4; i++){ 00092 EPS_cur[i] = get_mcp3208(); 00093 } 00094 to_cdh.reply(EPS_cur[0]); 00095 pc.printf("finish HK sensing \r\n"); 00096 } 00097 /*else if(rcmd == 0x07){ 00098 to_cdh.reply(0xF0); 00099 pc.printf("send initial sensing data\r\n"); 00100 } 00101 00102 else if(rcmd == 0x08){ 00103 to_cdh.reply(rcmd); 00104 00105 pc.printf("COM ON\r\n"); 00106 COM_on = 1; 00107 wait(0.5); 00108 COM_on = 0; 00109 } 00110 00111 else if(rcmd == 0x11){ 00112 to_cdh.reply(rcmd); 00113 00114 pc.printf("COM OFF\r\n"); 00115 COM_off = 1; 00116 wait(0.5); 00117 COM_off = 0; 00118 } 00119 00120 else if(rcmd == 0x14){ 00121 to_cdh.reply(rcmd); 00122 00123 pc.printf("Check battery voltage\r\n"); 00124 for(int i=0; i<5; i++){ 00125 v_bat[i] = v.read()*3.3*8.4/3.3; //3.3必要? 00126 pc.printf("bat_v = %f[v]\r\n ",v_bat[i]); 00127 wait(1.0); 00128 } 00129 pc.printf("finish \r\n"); 00130 } 00131 00132 else if(rcmd == 0x15){ 00133 to_cdh.reply(0xF1); 00134 pc.printf("Send battery voltage data\r\n"); 00135 } 00136 00137 else if(rcmd == 0x16){ 00138 to_cdh.reply(rcmd); 00139 00140 pc.printf("Heatcut ON\r\n"); 00141 dyn = 1; 00142 for(int i=0; i<5; i++){ 00143 dyn_cur[i]= mcp3208.read_input(3)*5; //()内の数字はchナンバー 00144 pc.printf("dyn_cur = %f[A]\r\n ",dyn_cur[i]); 00145 wait(1.0); 00146 } 00147 dyn = 0; 00148 pc.printf("heat cut OFF\r\n"); 00149 } 00150 00151 else if(rcmd == 0x17){ 00152 to_cdh.reply(0xF2); 00153 pc.printf("Send cell current data\r\n"); 00154 } 00155 else{ 00156 to_cdh.reply(0xFF); 00157 } 00158 00159 pc.printf("rcmd: %02x, dummy: %02x\r\n",rcmd, rdummy); 00160 */ 00161 00162 } 00163 } 00164 }
Generated on Tue Nov 1 2022 05:07:33 by
1.7.2