if you send signal from pc to mbed ,mbed will send back analog data to pc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define SENSOR_NUMBER 3
00004 
00005 Serial device(USBTX, USBRX);  // tx, rx
00006 
00007 DigitalOut myLed1(LED1);
00008 DigitalOut myLed2(LED2);
00009 DigitalOut myLed3(LED3);
00010 
00011 AnalogIn ain1(p15);
00012 AnalogIn ain2(p16);
00013 AnalogIn ain3(p17);
00014 AnalogIn ain4(p18);
00015 AnalogIn ain5(p19);
00016 AnalogIn ain6(p20);
00017 
00018 I2C i2c(p9,p10);
00019 
00020 void getInit(void);
00021 void sendData(void);
00022 
00023 typedef struct{
00024     int id;
00025     int analogNumber;
00026     int length;
00027 }sensor_t;
00028 sensor_t sensor[SENSOR_NUMBER];
00029 
00030 
00031 int main() {
00032     device.baud(115200);
00033     /*
00034     i2c.frequency(40000);
00035     char cmd[6];
00036     char add[1];
00037     char init[2];
00038     add[0] = 0x01;
00039     init[0] = 0x2A;
00040     init[1] = 0x01;
00041     while(i2c.write(0x3A,init,2));
00042     */
00043 
00044     myLed1 = 0;
00045     myLed2 = 0;
00046     myLed3 = 0;    
00047 
00048     getInit();
00049     myLed1 = 1;
00050     sendData();
00051 
00052 }
00053 
00054 
00055 void getInit()
00056 {
00057     int valueNumber = 0;
00058     int getValue = 0;
00059     int sensorCount = 0;
00060     int allSensorData = 1;
00061     
00062     while(1){
00063         if(device.readable()){
00064             valueNumber++;
00065             getValue = device.getc();
00066             switch(valueNumber){
00067             case 1:
00068                 allSensorData = getValue;
00069                 break;
00070             case 2:
00071                 sensor[sensorCount/3].id = getValue;
00072                 sensorCount++;
00073                 break;
00074             case 3:
00075                 sensor[(sensorCount-1)/3].analogNumber = getValue;
00076                 sensorCount++;
00077                 break;
00078             case 4:
00079                 sensor[(sensorCount-2)/3].length = getValue;                
00080                 sensorCount++;
00081                 valueNumber = 1;
00082                 break;
00083             } 
00084         }
00085         if(sensorCount > allSensorData-1)break;        
00086     }
00087 }
00088 
00089 void sendData()
00090 {
00091     int valueNumber = 0;
00092     int id = 0;
00093     int getValue = 0;
00094     int checkSum = 0;
00095     int analogNumberTill = 0;
00096     int analogNumber = 0;
00097     char sendData[8];
00098     int analogData[10] = {0};
00099 
00100     while(1){
00101         if(device.readable()){
00102             valueNumber++;
00103             getValue = device.getc();                        
00104             switch(valueNumber){
00105                 case 1: //get ID
00106                     id = getValue;
00107                     sendData[0] = id;
00108                     break;
00109                 case 2:                     //get analog data
00110                     analogData[0] = ain1.read_u16();
00111                     analogData[1] = ain2.read_u16();
00112                     analogData[2] = ain3.read_u16();
00113                     analogData[3] = ain4.read_u16();
00114                     analogData[4] = ain5.read_u16();
00115                     analogData[5] = ain6.read_u16();
00116 
00117                     //calc which data to get
00118                     for(int i=0; i<id; i++){
00119                         analogNumberTill += sensor[i].analogNumber;
00120                     }
00121                     
00122                     if(getValue == 0xFE){
00123                         analogNumber = sensor[id].analogNumber;                        
00124                     }else{
00125                         analogNumber = 1;
00126                     }
00127                     
00128                     //analog data to send
00129                     for(int i=0; i<analogNumber; i++){
00130                         if(sensor[id].length == 2){
00131                              sendData[i*2+1] = analogData[i+analogNumberTill] >> 8;
00132                              sendData[i*2+2] = analogData[i+analogNumberTill] & 0xFF;
00133                         }else if(sensor[id].length == 1){
00134                              sendData[i+1] = analogData[i+analogNumberTill] & 0xFF;                        
00135                         }
00136                     }
00137                     
00138                     //senddata
00139                     for(int i=0; i < sensor[id].length*analogNumber+1; i++){
00140                         device.putc(sendData[i]);
00141                         checkSum += sendData[i];
00142                     }
00143                     device.putc(checkSum & 0xFF);
00144                     
00145                     checkSum = 0;
00146                     valueNumber = 0;
00147                     analogNumberTill = 0;
00148                     id = 0;
00149                     analogNumber = 0;
00150                     
00151                     break;
00152             }
00153         }
00154     }
00155 }