Hiroaki Matsuda
/
mbed-for-SensorModule
if you send signal from pc to mbed ,mbed will send back analog data to pc
main.cpp@0:e7e366b4f7cd, 2012-12-05 (annotated)
- Committer:
- matsu
- Date:
- Wed Dec 05 15:10:17 2012 +0000
- Revision:
- 0:e7e366b4f7cd
send command from pc and mbed will send back analog data to pc.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matsu | 0:e7e366b4f7cd | 1 | #include "mbed.h" |
matsu | 0:e7e366b4f7cd | 2 | |
matsu | 0:e7e366b4f7cd | 3 | #define SENSOR_NUMBER 3 |
matsu | 0:e7e366b4f7cd | 4 | |
matsu | 0:e7e366b4f7cd | 5 | Serial device(USBTX, USBRX); // tx, rx |
matsu | 0:e7e366b4f7cd | 6 | |
matsu | 0:e7e366b4f7cd | 7 | DigitalOut myLed1(LED1); |
matsu | 0:e7e366b4f7cd | 8 | DigitalOut myLed2(LED2); |
matsu | 0:e7e366b4f7cd | 9 | DigitalOut myLed3(LED3); |
matsu | 0:e7e366b4f7cd | 10 | |
matsu | 0:e7e366b4f7cd | 11 | AnalogIn ain1(p15); |
matsu | 0:e7e366b4f7cd | 12 | AnalogIn ain2(p16); |
matsu | 0:e7e366b4f7cd | 13 | AnalogIn ain3(p17); |
matsu | 0:e7e366b4f7cd | 14 | AnalogIn ain4(p18); |
matsu | 0:e7e366b4f7cd | 15 | AnalogIn ain5(p19); |
matsu | 0:e7e366b4f7cd | 16 | AnalogIn ain6(p20); |
matsu | 0:e7e366b4f7cd | 17 | |
matsu | 0:e7e366b4f7cd | 18 | I2C i2c(p9,p10); |
matsu | 0:e7e366b4f7cd | 19 | |
matsu | 0:e7e366b4f7cd | 20 | void getInit(void); |
matsu | 0:e7e366b4f7cd | 21 | void sendData(void); |
matsu | 0:e7e366b4f7cd | 22 | |
matsu | 0:e7e366b4f7cd | 23 | typedef struct{ |
matsu | 0:e7e366b4f7cd | 24 | int id; |
matsu | 0:e7e366b4f7cd | 25 | int analogNumber; |
matsu | 0:e7e366b4f7cd | 26 | int length; |
matsu | 0:e7e366b4f7cd | 27 | }sensor_t; |
matsu | 0:e7e366b4f7cd | 28 | sensor_t sensor[SENSOR_NUMBER]; |
matsu | 0:e7e366b4f7cd | 29 | |
matsu | 0:e7e366b4f7cd | 30 | |
matsu | 0:e7e366b4f7cd | 31 | int main() { |
matsu | 0:e7e366b4f7cd | 32 | device.baud(115200); |
matsu | 0:e7e366b4f7cd | 33 | /* |
matsu | 0:e7e366b4f7cd | 34 | i2c.frequency(40000); |
matsu | 0:e7e366b4f7cd | 35 | char cmd[6]; |
matsu | 0:e7e366b4f7cd | 36 | char add[1]; |
matsu | 0:e7e366b4f7cd | 37 | char init[2]; |
matsu | 0:e7e366b4f7cd | 38 | add[0] = 0x01; |
matsu | 0:e7e366b4f7cd | 39 | init[0] = 0x2A; |
matsu | 0:e7e366b4f7cd | 40 | init[1] = 0x01; |
matsu | 0:e7e366b4f7cd | 41 | while(i2c.write(0x3A,init,2)); |
matsu | 0:e7e366b4f7cd | 42 | */ |
matsu | 0:e7e366b4f7cd | 43 | |
matsu | 0:e7e366b4f7cd | 44 | myLed1 = 0; |
matsu | 0:e7e366b4f7cd | 45 | myLed2 = 0; |
matsu | 0:e7e366b4f7cd | 46 | myLed3 = 0; |
matsu | 0:e7e366b4f7cd | 47 | |
matsu | 0:e7e366b4f7cd | 48 | getInit(); |
matsu | 0:e7e366b4f7cd | 49 | myLed1 = 1; |
matsu | 0:e7e366b4f7cd | 50 | sendData(); |
matsu | 0:e7e366b4f7cd | 51 | |
matsu | 0:e7e366b4f7cd | 52 | } |
matsu | 0:e7e366b4f7cd | 53 | |
matsu | 0:e7e366b4f7cd | 54 | |
matsu | 0:e7e366b4f7cd | 55 | void getInit() |
matsu | 0:e7e366b4f7cd | 56 | { |
matsu | 0:e7e366b4f7cd | 57 | int valueNumber = 0; |
matsu | 0:e7e366b4f7cd | 58 | int getValue = 0; |
matsu | 0:e7e366b4f7cd | 59 | int sensorCount = 0; |
matsu | 0:e7e366b4f7cd | 60 | int allSensorData = 1; |
matsu | 0:e7e366b4f7cd | 61 | |
matsu | 0:e7e366b4f7cd | 62 | while(1){ |
matsu | 0:e7e366b4f7cd | 63 | if(device.readable()){ |
matsu | 0:e7e366b4f7cd | 64 | valueNumber++; |
matsu | 0:e7e366b4f7cd | 65 | getValue = device.getc(); |
matsu | 0:e7e366b4f7cd | 66 | switch(valueNumber){ |
matsu | 0:e7e366b4f7cd | 67 | case 1: |
matsu | 0:e7e366b4f7cd | 68 | allSensorData = getValue; |
matsu | 0:e7e366b4f7cd | 69 | break; |
matsu | 0:e7e366b4f7cd | 70 | case 2: |
matsu | 0:e7e366b4f7cd | 71 | sensor[sensorCount/3].id = getValue; |
matsu | 0:e7e366b4f7cd | 72 | sensorCount++; |
matsu | 0:e7e366b4f7cd | 73 | break; |
matsu | 0:e7e366b4f7cd | 74 | case 3: |
matsu | 0:e7e366b4f7cd | 75 | sensor[(sensorCount-1)/3].analogNumber = getValue; |
matsu | 0:e7e366b4f7cd | 76 | sensorCount++; |
matsu | 0:e7e366b4f7cd | 77 | break; |
matsu | 0:e7e366b4f7cd | 78 | case 4: |
matsu | 0:e7e366b4f7cd | 79 | sensor[(sensorCount-2)/3].length = getValue; |
matsu | 0:e7e366b4f7cd | 80 | sensorCount++; |
matsu | 0:e7e366b4f7cd | 81 | valueNumber = 1; |
matsu | 0:e7e366b4f7cd | 82 | break; |
matsu | 0:e7e366b4f7cd | 83 | } |
matsu | 0:e7e366b4f7cd | 84 | } |
matsu | 0:e7e366b4f7cd | 85 | if(sensorCount > allSensorData-1)break; |
matsu | 0:e7e366b4f7cd | 86 | } |
matsu | 0:e7e366b4f7cd | 87 | } |
matsu | 0:e7e366b4f7cd | 88 | |
matsu | 0:e7e366b4f7cd | 89 | void sendData() |
matsu | 0:e7e366b4f7cd | 90 | { |
matsu | 0:e7e366b4f7cd | 91 | int valueNumber = 0; |
matsu | 0:e7e366b4f7cd | 92 | int id = 0; |
matsu | 0:e7e366b4f7cd | 93 | int getValue = 0; |
matsu | 0:e7e366b4f7cd | 94 | int checkSum = 0; |
matsu | 0:e7e366b4f7cd | 95 | int analogNumberTill = 0; |
matsu | 0:e7e366b4f7cd | 96 | int analogNumber = 0; |
matsu | 0:e7e366b4f7cd | 97 | char sendData[8]; |
matsu | 0:e7e366b4f7cd | 98 | int analogData[10] = {0}; |
matsu | 0:e7e366b4f7cd | 99 | |
matsu | 0:e7e366b4f7cd | 100 | while(1){ |
matsu | 0:e7e366b4f7cd | 101 | if(device.readable()){ |
matsu | 0:e7e366b4f7cd | 102 | valueNumber++; |
matsu | 0:e7e366b4f7cd | 103 | getValue = device.getc(); |
matsu | 0:e7e366b4f7cd | 104 | switch(valueNumber){ |
matsu | 0:e7e366b4f7cd | 105 | case 1: //get ID |
matsu | 0:e7e366b4f7cd | 106 | id = getValue; |
matsu | 0:e7e366b4f7cd | 107 | sendData[0] = id; |
matsu | 0:e7e366b4f7cd | 108 | break; |
matsu | 0:e7e366b4f7cd | 109 | case 2: //get analog data |
matsu | 0:e7e366b4f7cd | 110 | analogData[0] = ain1.read_u16(); |
matsu | 0:e7e366b4f7cd | 111 | analogData[1] = ain2.read_u16(); |
matsu | 0:e7e366b4f7cd | 112 | analogData[2] = ain3.read_u16(); |
matsu | 0:e7e366b4f7cd | 113 | analogData[3] = ain4.read_u16(); |
matsu | 0:e7e366b4f7cd | 114 | analogData[4] = ain5.read_u16(); |
matsu | 0:e7e366b4f7cd | 115 | analogData[5] = ain6.read_u16(); |
matsu | 0:e7e366b4f7cd | 116 | |
matsu | 0:e7e366b4f7cd | 117 | //calc which data to get |
matsu | 0:e7e366b4f7cd | 118 | for(int i=0; i<id; i++){ |
matsu | 0:e7e366b4f7cd | 119 | analogNumberTill += sensor[i].analogNumber; |
matsu | 0:e7e366b4f7cd | 120 | } |
matsu | 0:e7e366b4f7cd | 121 | |
matsu | 0:e7e366b4f7cd | 122 | if(getValue == 0xFE){ |
matsu | 0:e7e366b4f7cd | 123 | analogNumber = sensor[id].analogNumber; |
matsu | 0:e7e366b4f7cd | 124 | }else{ |
matsu | 0:e7e366b4f7cd | 125 | analogNumber = 1; |
matsu | 0:e7e366b4f7cd | 126 | } |
matsu | 0:e7e366b4f7cd | 127 | |
matsu | 0:e7e366b4f7cd | 128 | //analog data to send |
matsu | 0:e7e366b4f7cd | 129 | for(int i=0; i<analogNumber; i++){ |
matsu | 0:e7e366b4f7cd | 130 | if(sensor[id].length == 2){ |
matsu | 0:e7e366b4f7cd | 131 | sendData[i*2+1] = analogData[i+analogNumberTill] >> 8; |
matsu | 0:e7e366b4f7cd | 132 | sendData[i*2+2] = analogData[i+analogNumberTill] & 0xFF; |
matsu | 0:e7e366b4f7cd | 133 | }else if(sensor[id].length == 1){ |
matsu | 0:e7e366b4f7cd | 134 | sendData[i+1] = analogData[i+analogNumberTill] & 0xFF; |
matsu | 0:e7e366b4f7cd | 135 | } |
matsu | 0:e7e366b4f7cd | 136 | } |
matsu | 0:e7e366b4f7cd | 137 | |
matsu | 0:e7e366b4f7cd | 138 | //senddata |
matsu | 0:e7e366b4f7cd | 139 | for(int i=0; i < sensor[id].length*analogNumber+1; i++){ |
matsu | 0:e7e366b4f7cd | 140 | device.putc(sendData[i]); |
matsu | 0:e7e366b4f7cd | 141 | checkSum += sendData[i]; |
matsu | 0:e7e366b4f7cd | 142 | } |
matsu | 0:e7e366b4f7cd | 143 | device.putc(checkSum & 0xFF); |
matsu | 0:e7e366b4f7cd | 144 | |
matsu | 0:e7e366b4f7cd | 145 | checkSum = 0; |
matsu | 0:e7e366b4f7cd | 146 | valueNumber = 0; |
matsu | 0:e7e366b4f7cd | 147 | analogNumberTill = 0; |
matsu | 0:e7e366b4f7cd | 148 | id = 0; |
matsu | 0:e7e366b4f7cd | 149 | analogNumber = 0; |
matsu | 0:e7e366b4f7cd | 150 | |
matsu | 0:e7e366b4f7cd | 151 | break; |
matsu | 0:e7e366b4f7cd | 152 | } |
matsu | 0:e7e366b4f7cd | 153 | } |
matsu | 0:e7e366b4f7cd | 154 | } |
matsu | 0:e7e366b4f7cd | 155 | } |