a

Dependencies:   mbed mcp3208 p_eps_sequencial_20210930

Committer:
Yanagihara
Date:
Thu Oct 07 08:31:46 2021 +0000
Revision:
1:57c53c066bb5
BBMv3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yanagihara 1:57c53c066bb5 1 #include "mbed.h"
Yanagihara 1:57c53c066bb5 2
Yanagihara 1:57c53c066bb5 3 Serial pc(USBTX,USBRX);
Yanagihara 1:57c53c066bb5 4
Yanagihara 1:57c53c066bb5 5 // I/Oピン
Yanagihara 1:57c53c066bb5 6 DigitalOut COM_on(p21); // com用のスイッチをONにするピン
Yanagihara 1:57c53c066bb5 7 DigitalOut COM_off(p22); // com用のスイッチをOFFにするピン
Yanagihara 1:57c53c066bb5 8 DigitalOut dyn(p25);
Yanagihara 1:57c53c066bb5 9
Yanagihara 1:57c53c066bb5 10 // CDH用
Yanagihara 1:57c53c066bb5 11 SPISlave to_cdh(p5,p6,p7,p8); // mosi, miso, sclk, ssel
Yanagihara 1:57c53c066bb5 12
Yanagihara 1:57c53c066bb5 13 // ADコン用
Yanagihara 1:57c53c066bb5 14 SPI mcp3208(p11,p12,p13);
Yanagihara 1:57c53c066bb5 15 DigitalOut cs_ad1(p14);
Yanagihara 1:57c53c066bb5 16
Yanagihara 1:57c53c066bb5 17 // バッテリ電圧測定用
Yanagihara 1:57c53c066bb5 18 // AnalogIn v(p16);
Yanagihara 1:57c53c066bb5 19
Yanagihara 1:57c53c066bb5 20 // グローバル変数 ***********
Yanagihara 1:57c53c066bb5 21 uint8_t EPS_cur[5];
Yanagihara 1:57c53c066bb5 22 // *************************
Yanagihara 1:57c53c066bb5 23
Yanagihara 1:57c53c066bb5 24 // ADC用関数 ******************************************
Yanagihara 1:57c53c066bb5 25 void mcp3208_Init() {
Yanagihara 1:57c53c066bb5 26 mcp3208.format(8,0);
Yanagihara 1:57c53c066bb5 27 mcp3208.frequency(2000000);
Yanagihara 1:57c53c066bb5 28 uint8_t Vref = 5;
Yanagihara 1:57c53c066bb5 29 }
Yanagihara 1:57c53c066bb5 30
Yanagihara 1:57c53c066bb5 31 uint8_t get_mcp3208() {
Yanagihara 1:57c53c066bb5 32 cs_ad1 = 0;
Yanagihara 1:57c53c066bb5 33 uint8_t d0 = mcp3208.write(0x06); // ch1
Yanagihara 1:57c53c066bb5 34 uint8_t d1 = mcp3208.write(0x40);
Yanagihara 1:57c53c066bb5 35 uint8_t d2 = mcp3208.write(0x00);
Yanagihara 1:57c53c066bb5 36 cs_ad1 = 1;
Yanagihara 1:57c53c066bb5 37 uint8_t d3 = d1 << 4;
Yanagihara 1:57c53c066bb5 38 uint8_t d = (d3 << 4) | d2;
Yanagihara 1:57c53c066bb5 39 printf("d=%d\n\r", d);
Yanagihara 1:57c53c066bb5 40 return(d);
Yanagihara 1:57c53c066bb5 41 }
Yanagihara 1:57c53c066bb5 42 // ****************************************** ADC用関数終了
Yanagihara 1:57c53c066bb5 43
Yanagihara 1:57c53c066bb5 44 // ******************* main文 ***********************
Yanagihara 1:57c53c066bb5 45 int main()
Yanagihara 1:57c53c066bb5 46 {
Yanagihara 1:57c53c066bb5 47 // ***** toCDH初期設定 *****
Yanagihara 1:57c53c066bb5 48 to_cdh.format(8,0);
Yanagihara 1:57c53c066bb5 49 to_cdh.frequency(1000000);
Yanagihara 1:57c53c066bb5 50 to_cdh.reply(0x0);
Yanagihara 1:57c53c066bb5 51
Yanagihara 1:57c53c066bb5 52 // ***** mcp3208初期設定 *****
Yanagihara 1:57c53c066bb5 53 mcp3208_Init();
Yanagihara 1:57c53c066bb5 54
Yanagihara 1:57c53c066bb5 55 // ローカル関数
Yanagihara 1:57c53c066bb5 56 uint8_t ii = 0;
Yanagihara 1:57c53c066bb5 57 // ***************************
Yanagihara 1:57c53c066bb5 58
Yanagihara 1:57c53c066bb5 59
Yanagihara 1:57c53c066bb5 60 pc.printf("--Hi,this is eps(slave).\r\n");
Yanagihara 1:57c53c066bb5 61 to_cdh.reply(0x01);
Yanagihara 1:57c53c066bb5 62
Yanagihara 1:57c53c066bb5 63 while(1) {
Yanagihara 1:57c53c066bb5 64
Yanagihara 1:57c53c066bb5 65 if(to_cdh.receive()) {
Yanagihara 1:57c53c066bb5 66 int rcmd = to_cdh.read(); //receive 1st byte
Yanagihara 1:57c53c066bb5 67
Yanagihara 1:57c53c066bb5 68 if(rcmd == 1){
Yanagihara 1:57c53c066bb5 69 pc.printf("start spi to cdh\r\n");
Yanagihara 1:57c53c066bb5 70
Yanagihara 1:57c53c066bb5 71 to_cdh.reply(0x00); // ★ 送信前データをreplyに連続で詰めたい
Yanagihara 1:57c53c066bb5 72 pc.printf("receive eps_cmd 1 to cdh\r\n ");
Yanagihara 1:57c53c066bb5 73
Yanagihara 1:57c53c066bb5 74 }
Yanagihara 1:57c53c066bb5 75 else if(rcmd == 2){
Yanagihara 1:57c53c066bb5 76 pc.printf("start survivalHK sensing\r\n");
Yanagihara 1:57c53c066bb5 77
Yanagihara 1:57c53c066bb5 78 //v_bat[ii] = v.read()*3.3*8.4/3.3;
Yanagihara 1:57c53c066bb5 79 EPS_cur[ii] = get_mcp3208();
Yanagihara 1:57c53c066bb5 80 to_cdh.reply(EPS_cur[ii]); // ★ 送信前データをreplyに連続で詰めたい
Yanagihara 1:57c53c066bb5 81 pc.printf("EPS_cur = %d[v]\r\n ",EPS_cur[ii]);
Yanagihara 1:57c53c066bb5 82 ii++;
Yanagihara 1:57c53c066bb5 83
Yanagihara 1:57c53c066bb5 84 //pc.printf("finish survivalHK sensing \r\n");
Yanagihara 1:57c53c066bb5 85
Yanagihara 1:57c53c066bb5 86
Yanagihara 1:57c53c066bb5 87 }
Yanagihara 1:57c53c066bb5 88 else if(rcmd == 3){
Yanagihara 1:57c53c066bb5 89 // 受信
Yanagihara 1:57c53c066bb5 90 pc.printf("1Hz HK Interrupt\r\n");
Yanagihara 1:57c53c066bb5 91 for(int i=0; i<4; i++){
Yanagihara 1:57c53c066bb5 92 EPS_cur[i] = get_mcp3208();
Yanagihara 1:57c53c066bb5 93 }
Yanagihara 1:57c53c066bb5 94 to_cdh.reply(EPS_cur[0]);
Yanagihara 1:57c53c066bb5 95 pc.printf("finish HK sensing \r\n");
Yanagihara 1:57c53c066bb5 96 }
Yanagihara 1:57c53c066bb5 97 /*else if(rcmd == 0x07){
Yanagihara 1:57c53c066bb5 98 to_cdh.reply(0xF0);
Yanagihara 1:57c53c066bb5 99 pc.printf("send initial sensing data\r\n");
Yanagihara 1:57c53c066bb5 100 }
Yanagihara 1:57c53c066bb5 101
Yanagihara 1:57c53c066bb5 102 else if(rcmd == 0x08){
Yanagihara 1:57c53c066bb5 103 to_cdh.reply(rcmd);
Yanagihara 1:57c53c066bb5 104
Yanagihara 1:57c53c066bb5 105 pc.printf("COM ON\r\n");
Yanagihara 1:57c53c066bb5 106 COM_on = 1;
Yanagihara 1:57c53c066bb5 107 wait(0.5);
Yanagihara 1:57c53c066bb5 108 COM_on = 0;
Yanagihara 1:57c53c066bb5 109 }
Yanagihara 1:57c53c066bb5 110
Yanagihara 1:57c53c066bb5 111 else if(rcmd == 0x11){
Yanagihara 1:57c53c066bb5 112 to_cdh.reply(rcmd);
Yanagihara 1:57c53c066bb5 113
Yanagihara 1:57c53c066bb5 114 pc.printf("COM OFF\r\n");
Yanagihara 1:57c53c066bb5 115 COM_off = 1;
Yanagihara 1:57c53c066bb5 116 wait(0.5);
Yanagihara 1:57c53c066bb5 117 COM_off = 0;
Yanagihara 1:57c53c066bb5 118 }
Yanagihara 1:57c53c066bb5 119
Yanagihara 1:57c53c066bb5 120 else if(rcmd == 0x14){
Yanagihara 1:57c53c066bb5 121 to_cdh.reply(rcmd);
Yanagihara 1:57c53c066bb5 122
Yanagihara 1:57c53c066bb5 123 pc.printf("Check battery voltage\r\n");
Yanagihara 1:57c53c066bb5 124 for(int i=0; i<5; i++){
Yanagihara 1:57c53c066bb5 125 v_bat[i] = v.read()*3.3*8.4/3.3; //3.3必要?
Yanagihara 1:57c53c066bb5 126 pc.printf("bat_v = %f[v]\r\n ",v_bat[i]);
Yanagihara 1:57c53c066bb5 127 wait(1.0);
Yanagihara 1:57c53c066bb5 128 }
Yanagihara 1:57c53c066bb5 129 pc.printf("finish \r\n");
Yanagihara 1:57c53c066bb5 130 }
Yanagihara 1:57c53c066bb5 131
Yanagihara 1:57c53c066bb5 132 else if(rcmd == 0x15){
Yanagihara 1:57c53c066bb5 133 to_cdh.reply(0xF1);
Yanagihara 1:57c53c066bb5 134 pc.printf("Send battery voltage data\r\n");
Yanagihara 1:57c53c066bb5 135 }
Yanagihara 1:57c53c066bb5 136
Yanagihara 1:57c53c066bb5 137 else if(rcmd == 0x16){
Yanagihara 1:57c53c066bb5 138 to_cdh.reply(rcmd);
Yanagihara 1:57c53c066bb5 139
Yanagihara 1:57c53c066bb5 140 pc.printf("Heatcut ON\r\n");
Yanagihara 1:57c53c066bb5 141 dyn = 1;
Yanagihara 1:57c53c066bb5 142 for(int i=0; i<5; i++){
Yanagihara 1:57c53c066bb5 143 dyn_cur[i]= mcp3208.read_input(3)*5; //()内の数字はchナンバー
Yanagihara 1:57c53c066bb5 144 pc.printf("dyn_cur = %f[A]\r\n ",dyn_cur[i]);
Yanagihara 1:57c53c066bb5 145 wait(1.0);
Yanagihara 1:57c53c066bb5 146 }
Yanagihara 1:57c53c066bb5 147 dyn = 0;
Yanagihara 1:57c53c066bb5 148 pc.printf("heat cut OFF\r\n");
Yanagihara 1:57c53c066bb5 149 }
Yanagihara 1:57c53c066bb5 150
Yanagihara 1:57c53c066bb5 151 else if(rcmd == 0x17){
Yanagihara 1:57c53c066bb5 152 to_cdh.reply(0xF2);
Yanagihara 1:57c53c066bb5 153 pc.printf("Send cell current data\r\n");
Yanagihara 1:57c53c066bb5 154 }
Yanagihara 1:57c53c066bb5 155 else{
Yanagihara 1:57c53c066bb5 156 to_cdh.reply(0xFF);
Yanagihara 1:57c53c066bb5 157 }
Yanagihara 1:57c53c066bb5 158
Yanagihara 1:57c53c066bb5 159 pc.printf("rcmd: %02x, dummy: %02x\r\n",rcmd, rdummy);
Yanagihara 1:57c53c066bb5 160 */
Yanagihara 1:57c53c066bb5 161
Yanagihara 1:57c53c066bb5 162 }
Yanagihara 1:57c53c066bb5 163 }
Yanagihara 1:57c53c066bb5 164 }