Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
spi_slave.cpp
00001 #include "mbed.h" 00002 00003 #include "mcp3208.h" 00004 00005 SPISlave to_cdh(p5,p6,p7,p8); // mosi, miso, sclk, ssel 00006 Serial pc(USBTX,USBRX); 00007 00008 DigitalOut myleds[4] = {LED1,LED2,LED3,LED4}; // mbed動作目視確認用LED 00009 DigitalOut ADC_on(p21); // ADC用のスイッチをONにするピン 00010 DigitalOut ADC_off(p22); // ADC用のスイッチをOFFにするピン 00011 DigitalOut COM_on(p23); 00012 DigitalOut COM_off(p24); 00013 DigitalOut myled(LED1); 00014 00015 AnalogIn v(p16); 00016 SPI spi(p11,p12,p13); 00017 MCP3208 mcp3208(spi,p14); 00018 DigitalOut dyn(p25); 00019 00020 float v_bat[5], EPS_cur[5], ADC_cur[5], dyn_cur[5]; 00021 int data; 00022 00023 int main() 00024 { 00025 pc.printf("--Hi,this is eps(slave).\r\n"); 00026 00027 to_cdh.format(8,3); 00028 to_cdh.frequency(1000000); 00029 00030 int sdummy = 0xAA; 00031 00032 while(1) { 00033 00034 if(to_cdh.receive()) { 00035 int rcmd = to_cdh.read(); //receive 1st byte 00036 int rdummy = to_cdh.read(); //receive 2nd byte 00037 00038 to_cdh.reply(sdummy); // prepare to send 1st byte (dummy) 00039 // to_cdh.reply(rcmd); 00040 00041 if(rcmd == 0x01){ 00042 to_cdh.reply(rcmd); 00043 } 00044 else if(rcmd == 0x02){ 00045 to_cdh.reply(rcmd); 00046 00047 pc.printf("ADC ON \r\n"); 00048 ADC_on = 1; 00049 wait(0.5); 00050 ADC_on = 0; 00051 } 00052 else if (rcmd == 0x05){ 00053 to_cdh.reply(rcmd); 00054 pc.printf("start initial sensing\r\n"); 00055 for(int i=0; i<5; i++){ 00056 v_bat[i] = v.read()*3.3*8.4/3.3; 00057 EPS_cur[i] = mcp3208.read_input(0)*5; //()内の数字はchナンバー 00058 ADC_cur[i] = mcp3208.read_input(1)*5; //()内の数字はchナンバー 00059 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]); 00060 wait(1.0); 00061 } 00062 pc.printf("finish initial sensing \r\n"); 00063 00064 } 00065 else if(rcmd == 0x07){ 00066 to_cdh.reply(0xF0); 00067 pc.printf("send initial sensing data\r\n"); 00068 } 00069 00070 else if(rcmd == 0x08){ 00071 to_cdh.reply(rcmd); 00072 00073 pc.printf("COM ON\r\n"); 00074 COM_on = 1; 00075 wait(0.5); 00076 COM_on = 0; 00077 } 00078 00079 else if(rcmd == 0x11){ 00080 to_cdh.reply(rcmd); 00081 00082 pc.printf("COM OFF\r\n"); 00083 COM_off = 1; 00084 wait(0.5); 00085 COM_off = 0; 00086 } 00087 00088 else if(rcmd == 0x14){ 00089 to_cdh.reply(rcmd); 00090 00091 pc.printf("Check battery voltage\r\n"); 00092 for(int i=0; i<5; i++){ 00093 v_bat[i] = v.read()*3.3*8.4/3.3; //3.3必要? 00094 pc.printf("bat_v = %f[v]\r\n ",v_bat[i]); 00095 wait(1.0); 00096 } 00097 pc.printf("finish \r\n"); 00098 } 00099 00100 else if(rcmd == 0x15){ 00101 to_cdh.reply(0xF1); 00102 pc.printf("Send battery voltage data\r\n"); 00103 } 00104 00105 else if(rcmd == 0x16){ 00106 to_cdh.reply(rcmd); 00107 00108 pc.printf("Heatcut ON\r\n"); 00109 dyn = 1; 00110 for(int i=0; i<5; i++){ 00111 dyn_cur[i]= mcp3208.read_input(3)*5; //()内の数字はchナンバー 00112 pc.printf("dyn_cur = %f[A]\r\n ",dyn_cur[i]); 00113 wait(1.0); 00114 } 00115 dyn = 0; 00116 pc.printf("heat cut OFF\r\n"); 00117 } 00118 00119 else if(rcmd == 0x17){ 00120 to_cdh.reply(0xF2); 00121 pc.printf("Send cell current data\r\n"); 00122 } 00123 else{ 00124 to_cdh.reply(0xFF); 00125 } 00126 00127 pc.printf("rcmd: %02x, dummy: %02x\r\n",rcmd, rdummy); 00128 00129 00130 } 00131 } 00132 }
Generated on Fri Jul 29 2022 09:12:09 by
1.7.2