LPC824専用プログラム

Dependencies:   Ping SDFileSystem mbed-src

Committer:
lilac0112_1
Date:
Thu Feb 25 06:13:50 2016 +0000
Revision:
15:b1ff150cbb18
Parent:
14:61505c5dbc2b
Hokushinetsu(70:1)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lilac0112_1 0:2f48b11b154f 1 #include "mbed.h"
lilac0112_1 0:2f48b11b154f 2 #include "extern.h"
lilac0112_1 0:2f48b11b154f 3
lilac0112_1 0:2f48b11b154f 4 #ifdef SD_CARD
lilac0112_1 0:2f48b11b154f 5 void Sd_System(void){
lilac0112_1 0:2f48b11b154f 6
lilac0112_1 0:2f48b11b154f 7 char buf[BUFSIZE];
lilac0112_1 0:2f48b11b154f 8 uint16_t FileData[FDATA_NUM]={0}, num, data;
lilac0112_1 2:9a51bb2694aa 9 uint8_t spi_num, h_byte, l_byte, val, packet;
lilac0112_1 0:2f48b11b154f 10 int i;
lilac0112_1 0:2f48b11b154f 11 FILE *fp;
lilac0112_1 1:7070fb876a2c 12 char *fname="/sd/mydir/sdtest.txt";
lilac0112_1 1:7070fb876a2c 13 char *dname="/sd/mydir";
lilac0112_1 1:7070fb876a2c 14
lilac0112_1 1:7070fb876a2c 15 mkdir(dname, 0777);
lilac0112_1 1:7070fb876a2c 16
lilac0112_1 0:2f48b11b154f 17 pc.printf("Hello World!\n");
lilac0112_1 0:2f48b11b154f 18
lilac0112_1 1:7070fb876a2c 19 fp = fopen(fname, "r");
lilac0112_1 0:2f48b11b154f 20 if(fp == NULL) {
lilac0112_1 0:2f48b11b154f 21 error("Could not open file for read\n");
lilac0112_1 0:2f48b11b154f 22 }
lilac0112_1 0:2f48b11b154f 23 while(fgets(buf, sizeof(buf), fp) != NULL){
lilac0112_1 0:2f48b11b154f 24 if(buf[0] == '#') continue;
lilac0112_1 0:2f48b11b154f 25
lilac0112_1 0:2f48b11b154f 26 num = atol(strtok(buf, ","));
lilac0112_1 0:2f48b11b154f 27 data = atol(strtok(NULL, "\r\n\0"));
lilac0112_1 0:2f48b11b154f 28
lilac0112_1 2:9a51bb2694aa 29 if(num<FDATA_NUM){
lilac0112_1 0:2f48b11b154f 30 FileData[num] = data;
lilac0112_1 0:2f48b11b154f 31 pc.printf("%d, %ld\n", num, FileData[num]);
lilac0112_1 0:2f48b11b154f 32 }
lilac0112_1 0:2f48b11b154f 33 else{
lilac0112_1 0:2f48b11b154f 34 continue;
lilac0112_1 0:2f48b11b154f 35 }
lilac0112_1 0:2f48b11b154f 36 }
lilac0112_1 0:2f48b11b154f 37 fclose(fp);
lilac0112_1 0:2f48b11b154f 38
lilac0112_1 1:7070fb876a2c 39 while(1){
lilac0112_1 1:7070fb876a2c 40
lilac0112_1 1:7070fb876a2c 41 val = nucleo.receive();
lilac0112_1 1:7070fb876a2c 42 if(!val) continue;
lilac0112_1 1:7070fb876a2c 43
lilac0112_1 2:9a51bb2694aa 44 spi_num = nucleo.read();
lilac0112_1 2:9a51bb2694aa 45 h_byte = nucleo.read();
lilac0112_1 2:9a51bb2694aa 46 l_byte = nucleo.read();
lilac0112_1 1:7070fb876a2c 47
lilac0112_1 1:7070fb876a2c 48 if((spi_num&0x80)>>7 == 1){//writing to sd
lilac0112_1 1:7070fb876a2c 49 spi_num = spi_num & 0x7F;
lilac0112_1 2:9a51bb2694aa 50 if(spi_num<FDATA_NUM){
lilac0112_1 1:7070fb876a2c 51 FileData[spi_num] = (h_byte<<8) | l_byte;
lilac0112_1 1:7070fb876a2c 52 //pc.printf("%d, %ld\n", num, FileData[num]);
lilac0112_1 1:7070fb876a2c 53 fp = fopen(fname, "w");
lilac0112_1 1:7070fb876a2c 54 for(i=0; i<FDATA_NUM; i++) fprintf(fp, "%d, %ld\n", i, FileData[i]);
lilac0112_1 1:7070fb876a2c 55 fclose(fp);
lilac0112_1 1:7070fb876a2c 56 }
lilac0112_1 1:7070fb876a2c 57 }
lilac0112_1 1:7070fb876a2c 58 else{//reading from sd
lilac0112_1 1:7070fb876a2c 59 spi_num = spi_num & 0x7F;
lilac0112_1 2:9a51bb2694aa 60 if(spi_num<FDATA_NUM){
lilac0112_1 1:7070fb876a2c 61 h_byte = (FileData[spi_num] & 0xFF00)>>8;
lilac0112_1 1:7070fb876a2c 62 l_byte = (FileData[spi_num] & 0x00FF);
lilac0112_1 1:7070fb876a2c 63
lilac0112_1 1:7070fb876a2c 64 packet = h_byte;
lilac0112_1 1:7070fb876a2c 65 nucleo.reply(packet);
lilac0112_1 1:7070fb876a2c 66
lilac0112_1 1:7070fb876a2c 67 packet = l_byte;
lilac0112_1 1:7070fb876a2c 68 nucleo.reply(packet);
lilac0112_1 1:7070fb876a2c 69 }
lilac0112_1 1:7070fb876a2c 70 }
lilac0112_1 0:2f48b11b154f 71 //pc.printf("Goodbye World!\n");
lilac0112_1 0:2f48b11b154f 72 }
lilac0112_1 0:2f48b11b154f 73 }
lilac0112_1 0:2f48b11b154f 74 #endif /*SD_CARD*/
lilac0112_1 0:2f48b11b154f 75
lilac0112_1 0:2f48b11b154f 76 #ifdef ULTRA_SONIC
lilac0112_1 8:a2eadba03040 77
lilac0112_1 9:7f98d4415425 78
lilac0112_1 9:7f98d4415425 79 uint16_t packet, val, order;
lilac0112_1 9:7f98d4415425 80 uint16_t front_dis=0, rear_dis=0;
lilac0112_1 9:7f98d4415425 81
lilac0112_1 9:7f98d4415425 82 void emergency(void){
lilac0112_1 9:7f98d4415425 83 led[1]=1;
lilac0112_1 9:7f98d4415425 84 //front_dis = 0xAA;
lilac0112_1 9:7f98d4415425 85 //rear_dis = 0xBB;
lilac0112_1 8:a2eadba03040 86 val = nucleo.receive();
lilac0112_1 8:a2eadba03040 87 if(val==1){
lilac0112_1 8:a2eadba03040 88 order = nucleo.read();
lilac0112_1 8:a2eadba03040 89 if(order == 0xABCD){
lilac0112_1 9:7f98d4415425 90 packet = ((front_dis<<8)|(rear_dis))&0xFFFF;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 91 }
lilac0112_1 8:a2eadba03040 92 else{
lilac0112_1 9:7f98d4415425 93 packet = 0xFFFF;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 94 }
lilac0112_1 9:7f98d4415425 95 //pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
lilac0112_1 8:a2eadba03040 96 }
lilac0112_1 8:a2eadba03040 97 else{
lilac0112_1 9:7f98d4415425 98 //pc.printf("FFFF\r\n");
lilac0112_1 8:a2eadba03040 99 }
lilac0112_1 11:a5023f22f25a 100 wait_us(100);
lilac0112_1 9:7f98d4415425 101 led[1]=0;
lilac0112_1 8:a2eadba03040 102 }
lilac0112_1 0:2f48b11b154f 103 void Usw_System(void){
lilac0112_1 0:2f48b11b154f 104
lilac0112_1 8:a2eadba03040 105 nucleo.format(16, 3);
lilac0112_1 8:a2eadba03040 106 nucleo.frequency(1000000);
lilac0112_1 8:a2eadba03040 107 nucleo.reply(0x0000);
lilac0112_1 9:7f98d4415425 108
lilac0112_1 9:7f98d4415425 109 call.fall(&emergency);
lilac0112_1 8:a2eadba03040 110
lilac0112_1 9:7f98d4415425 111 //led[0]=led[1]=1;
lilac0112_1 0:2f48b11b154f 112 while(1){
lilac0112_1 9:7f98d4415425 113
lilac0112_1 9:7f98d4415425 114 led[0]=1;
lilac0112_1 0:2f48b11b154f 115 front.Send();
lilac0112_1 0:2f48b11b154f 116 wait_ms(30);
lilac0112_1 0:2f48b11b154f 117 front_dis = front.Read_cm();
lilac0112_1 9:7f98d4415425 118 if(front_dis>0xFE) front_dis=0xFE;
lilac0112_1 9:7f98d4415425 119
lilac0112_1 0:2f48b11b154f 120
lilac0112_1 0:2f48b11b154f 121 rear.Send();
lilac0112_1 0:2f48b11b154f 122 wait_ms(30);
lilac0112_1 0:2f48b11b154f 123 rear_dis = rear.Read_cm();
lilac0112_1 9:7f98d4415425 124 if(rear_dis>0xFE) rear_dis=0xFE;
lilac0112_1 9:7f98d4415425 125 led[0]=0;
lilac0112_1 12:c90a124169e6 126 wait_us(1000);
lilac0112_1 12:c90a124169e6 127 //pc.printf("%d\t%d\t\r\n", front_dis, rear_dis);
lilac0112_1 9:7f98d4415425 128
lilac0112_1 9:7f98d4415425 129 //emergency();
lilac0112_1 0:2f48b11b154f 130 }
lilac0112_1 0:2f48b11b154f 131 }
lilac0112_1 0:2f48b11b154f 132 #endif /*ULTRA_SONIC*/
lilac0112_1 0:2f48b11b154f 133
lilac0112_1 0:2f48b11b154f 134 #ifdef COLOR_SENSOR
lilac0112_1 0:2f48b11b154f 135 void Color_System(void){
lilac0112_1 1:7070fb876a2c 136 uint8_t val, order, packet;
lilac0112_1 1:7070fb876a2c 137 uint16_t data;
lilac0112_1 1:7070fb876a2c 138
lilac0112_1 0:2f48b11b154f 139 while(1){
lilac0112_1 1:7070fb876a2c 140 while(1){
lilac0112_1 1:7070fb876a2c 141 val = nucleo.receive();
lilac0112_1 1:7070fb876a2c 142 if(val){
lilac0112_1 1:7070fb876a2c 143 order = nucleo.read();
lilac0112_1 1:7070fb876a2c 144 order %= SENSOR_X_COLOR;
lilac0112_1 2:9a51bb2694aa 145 data = ColorSensor[order].read_u16();
lilac0112_1 1:7070fb876a2c 146 packet = (data & 0xFF00)>>8;
lilac0112_1 1:7070fb876a2c 147 nucleo.reply(packet);
lilac0112_1 1:7070fb876a2c 148 }
lilac0112_1 1:7070fb876a2c 149 else{
lilac0112_1 1:7070fb876a2c 150 break;
lilac0112_1 1:7070fb876a2c 151 }
lilac0112_1 1:7070fb876a2c 152 }
lilac0112_1 0:2f48b11b154f 153 }
lilac0112_1 0:2f48b11b154f 154 }
lilac0112_1 0:2f48b11b154f 155 #endif /*COLOR_SENSOR*/
lilac0112_1 0:2f48b11b154f 156
lilac0112_1 0:2f48b11b154f 157 #ifdef IR_SENSOR
lilac0112_1 10:641da47d4a14 158
lilac0112_1 9:7f98d4415425 159 uint8_t ir_notice=0;//4bit
lilac0112_1 12:c90a124169e6 160 static uint8_t const ir_key=IR_KEY;//4bit
lilac0112_1 9:7f98d4415425 161 uint8_t ir_position[2]={0};//4bit*2
lilac0112_1 9:7f98d4415425 162
lilac0112_1 9:7f98d4415425 163 void emergency(void){
lilac0112_1 10:641da47d4a14 164 uint16_t order,packet,val;
lilac0112_1 9:7f98d4415425 165 //ir_key=0xA;
lilac0112_1 9:7f98d4415425 166 //ir_notice=0xC;
lilac0112_1 9:7f98d4415425 167 //ir_position[1]=0xE;
lilac0112_1 9:7f98d4415425 168 //ir_position[0]=0xF;
lilac0112_1 9:7f98d4415425 169 val = nucleo.receive();
lilac0112_1 9:7f98d4415425 170 if(val==1){
lilac0112_1 9:7f98d4415425 171 order = nucleo.read();
lilac0112_1 9:7f98d4415425 172 if(order == 0xABCD){
lilac0112_1 9:7f98d4415425 173 //key/notice/long/short
lilac0112_1 12:c90a124169e6 174 packet = ((ir_key<<12)|(ir_notice<<8)|(ir_position[LONG_SPOT]<<4)|(ir_position[SHORT_SPOT]<<0))&0xFFFF;nucleo.reply(packet);
lilac0112_1 9:7f98d4415425 175 }
lilac0112_1 9:7f98d4415425 176 else{
lilac0112_1 9:7f98d4415425 177 packet = 0xFFFF;nucleo.reply(packet);
lilac0112_1 9:7f98d4415425 178 }
lilac0112_1 9:7f98d4415425 179 //pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
lilac0112_1 9:7f98d4415425 180 }
lilac0112_1 9:7f98d4415425 181 else{
lilac0112_1 9:7f98d4415425 182 //pc.f("FFFF\r\n");
lilac0112_1 9:7f98d4415425 183 }
lilac0112_1 9:7f98d4415425 184 }
lilac0112_1 7:9b1ac6fbf32c 185 void Ir_System(void){
lilac0112_1 10:641da47d4a14 186
lilac0112_1 10:641da47d4a14 187 uint8_t i;//ic
lilac0112_1 10:641da47d4a14 188 uint8_t j;//ch
lilac0112_1 12:c90a124169e6 189
lilac0112_1 12:c90a124169e6 190 uint8_t num_long;
lilac0112_1 12:c90a124169e6 191 uint8_t num_short;
lilac0112_1 12:c90a124169e6 192
lilac0112_1 12:c90a124169e6 193 uint16_t ir_val_long[8];
lilac0112_1 12:c90a124169e6 194 uint16_t ir_val_short[12];
lilac0112_1 12:c90a124169e6 195
lilac0112_1 12:c90a124169e6 196 uint16_t ir_val, ir_val_diff;
lilac0112_1 12:c90a124169e6 197
lilac0112_1 12:c90a124169e6 198 uint8_t ir_posi;
lilac0112_1 12:c90a124169e6 199 uint8_t ir_posi_long[8];
lilac0112_1 12:c90a124169e6 200 uint8_t ir_posi_short[12];
lilac0112_1 10:641da47d4a14 201 static uint8_t const ch_num[3]={8, 8, 4};
lilac0112_1 10:641da47d4a14 202
lilac0112_1 9:7f98d4415425 203 nucleo.format(16, 3);
lilac0112_1 8:a2eadba03040 204 nucleo.frequency(1000000);
lilac0112_1 9:7f98d4415425 205 nucleo.reply(0x0000);
lilac0112_1 8:a2eadba03040 206
lilac0112_1 7:9b1ac6fbf32c 207 ir.format(8, 3);
lilac0112_1 6:79dc74e4926a 208 ir.frequency(1000000);
lilac0112_1 9:7f98d4415425 209
lilac0112_1 11:a5023f22f25a 210 //遠距離用電源
lilac0112_1 10:641da47d4a14 211 supply.period(0.010);//T=10[ms]
lilac0112_1 10:641da47d4a14 212 supply.write(0.9);//L...10%,H...90%
lilac0112_1 10:641da47d4a14 213
lilac0112_1 14:61505c5dbc2b 214 //Nucleoとの通信用のピン変化割り込み
lilac0112_1 14:61505c5dbc2b 215 //Nucleoとの通信をする際はTeraTermへの出力は控えるべき
lilac0112_1 15:b1ff150cbb18 216 call.fall(&emergency);
lilac0112_1 6:79dc74e4926a 217
lilac0112_1 6:79dc74e4926a 218 for(i=0; i<IC_NUM; i++) cs[i]=1;
lilac0112_1 6:79dc74e4926a 219 while(1) {
lilac0112_1 14:61505c5dbc2b 220 //全ての素子の値を検出
lilac0112_1 12:c90a124169e6 221 num_short=0;
lilac0112_1 12:c90a124169e6 222 num_long=0;
lilac0112_1 6:79dc74e4926a 223 for(i=0; i<IC_NUM; i++){//IC
lilac0112_1 6:79dc74e4926a 224 for(j=0; j<ch_num[i]; j++){//Ch
lilac0112_1 10:641da47d4a14 225 ir_val = read_input(i, j);
lilac0112_1 10:641da47d4a14 226 ir_posi = read_position(i, j);
lilac0112_1 10:641da47d4a14 227
lilac0112_1 10:641da47d4a14 228 if(i==0){
lilac0112_1 10:641da47d4a14 229 ir_val_long[num_long] = ir_val;
lilac0112_1 10:641da47d4a14 230 ir_posi_long[num_long] = ir_posi;
lilac0112_1 10:641da47d4a14 231 num_long++;
lilac0112_1 12:c90a124169e6 232
lilac0112_1 10:641da47d4a14 233 }
lilac0112_1 10:641da47d4a14 234 else{
lilac0112_1 10:641da47d4a14 235 ir_val_short[num_short] = ir_val;
lilac0112_1 10:641da47d4a14 236 ir_posi_short[num_short] = ir_posi;
lilac0112_1 10:641da47d4a14 237 num_short++;
lilac0112_1 12:c90a124169e6 238 }
lilac0112_1 12:c90a124169e6 239
lilac0112_1 6:79dc74e4926a 240 }
lilac0112_1 6:79dc74e4926a 241 }
lilac0112_1 14:61505c5dbc2b 242 //バブルソート
lilac0112_1 15:b1ff150cbb18 243 BubbleSort(ir_val_short, ir_posi_short, 12);
lilac0112_1 15:b1ff150cbb18 244 BubbleSort(ir_val_long, ir_posi_long, 8);
lilac0112_1 10:641da47d4a14 245
lilac0112_1 14:61505c5dbc2b 246 //検出した最大値と最小値の差
lilac0112_1 12:c90a124169e6 247 ir_val_diff = ir_val_short[0]-ir_val_short[11];//After bubble
lilac0112_1 14:61505c5dbc2b 248 //最もボールがあるとされる位置を代入
lilac0112_1 12:c90a124169e6 249 ir_position[SHORT_SPOT] = ir_posi_short[11];//After bubble
lilac0112_1 12:c90a124169e6 250 ir_position[LONG_SPOT] = ir_posi_long[7];//After bubble
lilac0112_1 12:c90a124169e6 251
lilac0112_1 11:a5023f22f25a 252 /*
lilac0112_1 12:c90a124169e6 253 #define IR_NOTE_NONE 0x0
lilac0112_1 12:c90a124169e6 254 #define IR_NOTE_FAR 0x1
lilac0112_1 12:c90a124169e6 255 #define IR_NOTE_CLOSE 0x2
lilac0112_1 12:c90a124169e6 256 #define IR_NOTE_CLOSER 0x3
lilac0112_1 11:a5023f22f25a 257 */
lilac0112_1 14:61505c5dbc2b 258 //ボールが遠くにあるか,近くか,フィールドにないかを判断
lilac0112_1 12:c90a124169e6 259 ir_notice = IR_NOTE_NONE;//none
lilac0112_1 13:938a2dda0626 260
lilac0112_1 13:938a2dda0626 261 if((ir_val_short[11]>=3900)&&(1)){
lilac0112_1 13:938a2dda0626 262 if((ir_val_long[7]<=1700)&&(1)){
lilac0112_1 13:938a2dda0626 263 ir_notice = IR_NOTE_FAR;//long_data
lilac0112_1 13:938a2dda0626 264 }
lilac0112_1 13:938a2dda0626 265 else{
lilac0112_1 13:938a2dda0626 266 ir_notice = IR_NOTE_NONE;//none
lilac0112_1 13:938a2dda0626 267 }
lilac0112_1 13:938a2dda0626 268 }
lilac0112_1 13:938a2dda0626 269 else{
lilac0112_1 13:938a2dda0626 270 if((ir_val_short[11]<=2400)&&(1)){
lilac0112_1 13:938a2dda0626 271 if(ir_val_long[7]<=1000){
lilac0112_1 13:938a2dda0626 272 ir_notice = IR_NOTE_CLOSER;//short_data
lilac0112_1 13:938a2dda0626 273 }
lilac0112_1 13:938a2dda0626 274 else{
lilac0112_1 13:938a2dda0626 275 ir_notice = IR_NOTE_FAR;//long_data
lilac0112_1 13:938a2dda0626 276 }
lilac0112_1 13:938a2dda0626 277
lilac0112_1 13:938a2dda0626 278 }
lilac0112_1 13:938a2dda0626 279 else{
lilac0112_1 13:938a2dda0626 280 ir_notice = IR_NOTE_CLOSE;//short_data
lilac0112_1 13:938a2dda0626 281 }
lilac0112_1 13:938a2dda0626 282 }
lilac0112_1 13:938a2dda0626 283
lilac0112_1 13:938a2dda0626 284
lilac0112_1 13:938a2dda0626 285 /*ir_notice = IR_NOTE_NONE;//none
lilac0112_1 12:c90a124169e6 286 if((DIS_MORE_CLOSE<=ir_val_diff)&&(1)){
lilac0112_1 12:c90a124169e6 287 ir_notice = IR_NOTE_CLOSER;//short_data
lilac0112_1 12:c90a124169e6 288 }
lilac0112_1 12:c90a124169e6 289 else if((DIS_CLOSE<=ir_val_diff)&&(ir_val_diff<DIS_MORE_CLOSE)){
lilac0112_1 12:c90a124169e6 290 ir_notice = IR_NOTE_CLOSE;//short_data
lilac0112_1 12:c90a124169e6 291 }
lilac0112_1 12:c90a124169e6 292 else if((DIS_FAR<=ir_val_diff)&&(ir_val_diff<DIS_CLOSE)){
lilac0112_1 12:c90a124169e6 293 ir_notice = IR_NOTE_FAR;//long_data
lilac0112_1 12:c90a124169e6 294 }
lilac0112_1 12:c90a124169e6 295 else if((1)&&(ir_val_diff<DIS_FAR)){
lilac0112_1 12:c90a124169e6 296 ir_notice = IR_NOTE_NONE;//none
lilac0112_1 13:938a2dda0626 297 if(((ir_val_long[0]-ir_val_long[7])>900)&&1){
lilac0112_1 12:c90a124169e6 298 ir_notice = IR_NOTE_FAR;//long_data
lilac0112_1 12:c90a124169e6 299 }
lilac0112_1 13:938a2dda0626 300 }*/
lilac0112_1 14:61505c5dbc2b 301
lilac0112_1 14:61505c5dbc2b 302 //デバッグ用のTeraTermへの出力
lilac0112_1 14:61505c5dbc2b 303
lilac0112_1 12:c90a124169e6 304 /*
lilac0112_1 14:61505c5dbc2b 305 pc.printf("val_short:%d\t", ir_val_short[11]);//最小検出値(近)
lilac0112_1 14:61505c5dbc2b 306 pc.printf("val_long:%d\t", ir_val_long[7]);//最小検出値(遠)
lilac0112_1 14:61505c5dbc2b 307 pc.printf("state_short:%d\t", ir_position[0]);//ボール位置(近)
lilac0112_1 14:61505c5dbc2b 308 pc.printf("state_long:%d\t", ir_position[1]);//ボール位置(遠)
lilac0112_1 14:61505c5dbc2b 309 pc.printf("Diff_short:%d\t", ir_val_short[0]-ir_val_short[11]);//検出した最大値と最小値の差(近)
lilac0112_1 14:61505c5dbc2b 310 pc.printf("Diff_long:%d\t", ir_val_long[0]-ir_val_long[7]);//検出した最大値と最小値の差(遠)
lilac0112_1 14:61505c5dbc2b 311 pc.printf("notice:%d\t", ir_notice);//判定されたボールの状況
lilac0112_1 12:c90a124169e6 312 pc.printf("\r\n");
lilac0112_1 12:c90a124169e6 313 */
lilac0112_1 12:c90a124169e6 314 /*
lilac0112_1 12:c90a124169e6 315 pc.printf("%Value is ");
lilac0112_1 14:61505c5dbc2b 316 pc.printf("Dirr_short is %d\t", ir_val_short[0]-ir_val_short[11]);//検出した最大値と最小値の差(近)
lilac0112_1 14:61505c5dbc2b 317 pc.printf("Dirr_long is %d\t", ir_val_long[0]-ir_val_long[7]);//検出した最大値と最小値の差(遠)
lilac0112_1 6:79dc74e4926a 318 pc.printf("\r\n");
lilac0112_1 12:c90a124169e6 319 */
lilac0112_1 15:b1ff150cbb18 320
lilac0112_1 14:61505c5dbc2b 321 //検出した値全てを出力(バブルソートすると位置が変わるので注意)
lilac0112_1 15:b1ff150cbb18 322 /*pc.printf("%Value is ");
lilac0112_1 12:c90a124169e6 323 for(i=0; i<8; i++){
lilac0112_1 14:61505c5dbc2b 324 pc.printf("%d\t", ir_val_long[i]);//遠
lilac0112_1 12:c90a124169e6 325 }
lilac0112_1 12:c90a124169e6 326 for(i=0; i<12; i++){
lilac0112_1 14:61505c5dbc2b 327 pc.printf("%d\t", ir_val_short[i]);//近
lilac0112_1 12:c90a124169e6 328 }
lilac0112_1 12:c90a124169e6 329
lilac0112_1 12:c90a124169e6 330 pc.printf("\r\n");
lilac0112_1 15:b1ff150cbb18 331 */
lilac0112_1 6:79dc74e4926a 332 }
lilac0112_1 6:79dc74e4926a 333 }
lilac0112_1 10:641da47d4a14 334 uint16_t read_input(uint8_t ic, uint8_t channel)
lilac0112_1 7:9b1ac6fbf32c 335 {
lilac0112_1 10:641da47d4a14 336 uint16_t command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
lilac0112_1 10:641da47d4a14 337 uint16_t command_low = (channel & 0x03) << 6;
lilac0112_1 0:2f48b11b154f 338
lilac0112_1 7:9b1ac6fbf32c 339 cs[ic] = 0;
lilac0112_1 0:2f48b11b154f 340
lilac0112_1 7:9b1ac6fbf32c 341 ir.write(command_high);
lilac0112_1 10:641da47d4a14 342 uint16_t high_byte = ir.write(command_low) & 0x0F;
lilac0112_1 10:641da47d4a14 343 uint16_t low_byte = ir.write(0);
lilac0112_1 8:a2eadba03040 344
lilac0112_1 8:a2eadba03040 345 wait_us(1);
lilac0112_1 7:9b1ac6fbf32c 346 cs[ic] = 1;
lilac0112_1 0:2f48b11b154f 347
lilac0112_1 7:9b1ac6fbf32c 348 return (high_byte << 8) | low_byte;//0x0000...0xFFFF
lilac0112_1 0:2f48b11b154f 349 }
lilac0112_1 10:641da47d4a14 350 uint8_t read_position(uint8_t ic, uint8_t channel){
lilac0112_1 10:641da47d4a14 351 //DigitalOut cs[IC_NUM] = {cs1, cs2, cs3};
lilac0112_1 10:641da47d4a14 352 static uint8_t const ir_location[3][8]=
lilac0112_1 10:641da47d4a14 353 {//0,1,2...7(ch)
lilac0112_1 10:641da47d4a14 354 {1, 2, 3, 4, 5, 6, 7, 8},//cs1,Long*8
lilac0112_1 10:641da47d4a14 355 {1, 2, 3, 4, 5, 6, 7, 8},//cs2,Short*8
lilac0112_1 10:641da47d4a14 356 {9, 10, 11, 12},//cs3,Short*4
lilac0112_1 10:641da47d4a14 357 };
lilac0112_1 10:641da47d4a14 358 return ir_location[ic][channel];
lilac0112_1 10:641da47d4a14 359 }
lilac0112_1 10:641da47d4a14 360 void BubbleSort(uint16_t *data, uint8_t *data2, uint8_t n)//昇順にする
lilac0112_1 9:7f98d4415425 361 {
lilac0112_1 9:7f98d4415425 362 bool flag;
lilac0112_1 10:641da47d4a14 363 uint8_t i, j;//inclement
lilac0112_1 10:641da47d4a14 364 uint16_t temp;//temporary for value
lilac0112_1 10:641da47d4a14 365 uint8_t temp2;//temporary for position
lilac0112_1 9:7f98d4415425 366
lilac0112_1 10:641da47d4a14 367 j=0;
lilac0112_1 9:7f98d4415425 368 do{
lilac0112_1 10:641da47d4a14 369 j++;
lilac0112_1 9:7f98d4415425 370 flag=0;
lilac0112_1 10:641da47d4a14 371 for (i=0; i<n-j; i++) {
lilac0112_1 10:641da47d4a14 372 if (data[i]<data[i+1]) {
lilac0112_1 10:641da47d4a14 373 // 左右の並びがおかしければ入れ替える
lilac0112_1 9:7f98d4415425 374 flag=1;
lilac0112_1 9:7f98d4415425 375 temp=data[i];
lilac0112_1 9:7f98d4415425 376 data[i]=data[i+1];
lilac0112_1 9:7f98d4415425 377 data[i+1]=temp;
lilac0112_1 10:641da47d4a14 378
lilac0112_1 10:641da47d4a14 379 temp2=data2[i];
lilac0112_1 10:641da47d4a14 380 data2[i]=data2[i+1];
lilac0112_1 10:641da47d4a14 381 data2[i+1]=temp2;
lilac0112_1 9:7f98d4415425 382 }
lilac0112_1 9:7f98d4415425 383 }
lilac0112_1 10:641da47d4a14 384 } while (flag==1); //入れ替えがある間,繰り返す.
lilac0112_1 9:7f98d4415425 385 }
lilac0112_1 0:2f48b11b154f 386 #endif /*IR_SENSOR*/