LPC824専用プログラム

Dependencies:   Ping SDFileSystem mbed-src

Committer:
lilac0112_1
Date:
Tue Jan 26 15:03:56 2016 +0000
Revision:
11:a5023f22f25a
Parent:
10:641da47d4a14
Child:
12:c90a124169e6
added Bubble Sort.

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 11:a5023f22f25a 126 wait_us(100);
lilac0112_1 9:7f98d4415425 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 10:641da47d4a14 160 static uint8_t const ir_key=0xA;//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 9:7f98d4415425 174 packet = ((ir_key<<12)|(ir_notice<<8)|(ir_position[1]<<4)|(ir_position[0]<<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 11:a5023f22f25a 189 //uint8_t num_long, num_short;
lilac0112_1 11:a5023f22f25a 190 //uint16_t ir_val, ir_val_long[8], ir_val_short[12];
lilac0112_1 11:a5023f22f25a 191 //uint8_t ir_posi, ir_posi_long[8], ir_posi_short[12];
lilac0112_1 10:641da47d4a14 192 static uint8_t const ch_num[3]={8, 8, 4};
lilac0112_1 10:641da47d4a14 193
lilac0112_1 10:641da47d4a14 194 uint16_t ir_data[IC_NUM][IR_NUM]={0};
lilac0112_1 10:641da47d4a14 195
lilac0112_1 9:7f98d4415425 196 nucleo.format(16, 3);
lilac0112_1 8:a2eadba03040 197 nucleo.frequency(1000000);
lilac0112_1 9:7f98d4415425 198 nucleo.reply(0x0000);
lilac0112_1 8:a2eadba03040 199
lilac0112_1 7:9b1ac6fbf32c 200 ir.format(8, 3);
lilac0112_1 6:79dc74e4926a 201 ir.frequency(1000000);
lilac0112_1 9:7f98d4415425 202
lilac0112_1 11:a5023f22f25a 203 //遠距離用電源
lilac0112_1 10:641da47d4a14 204 supply.period(0.010);//T=10[ms]
lilac0112_1 10:641da47d4a14 205 supply.write(0.9);//L...10%,H...90%
lilac0112_1 10:641da47d4a14 206
lilac0112_1 10:641da47d4a14 207 //call.fall(&emergency);
lilac0112_1 6:79dc74e4926a 208
lilac0112_1 6:79dc74e4926a 209 for(i=0; i<IC_NUM; i++) cs[i]=1;
lilac0112_1 6:79dc74e4926a 210 while(1) {
lilac0112_1 10:641da47d4a14 211
lilac0112_1 11:a5023f22f25a 212 //num_short=0;
lilac0112_1 11:a5023f22f25a 213 //num_long=0;
lilac0112_1 6:79dc74e4926a 214 for(i=0; i<IC_NUM; i++){//IC
lilac0112_1 6:79dc74e4926a 215 for(j=0; j<ch_num[i]; j++){//Ch
lilac0112_1 10:641da47d4a14 216 ir_data[i][j] = read_input(i, j);
lilac0112_1 10:641da47d4a14 217 /*
lilac0112_1 10:641da47d4a14 218 ir_val = read_input(i, j);
lilac0112_1 10:641da47d4a14 219 ir_posi = read_position(i, j);
lilac0112_1 10:641da47d4a14 220
lilac0112_1 10:641da47d4a14 221 if(i==0){
lilac0112_1 10:641da47d4a14 222 ir_val_long[num_long] = ir_val;
lilac0112_1 10:641da47d4a14 223 ir_posi_long[num_long] = ir_posi;
lilac0112_1 10:641da47d4a14 224 num_long++;
lilac0112_1 10:641da47d4a14 225 }
lilac0112_1 10:641da47d4a14 226 else{
lilac0112_1 10:641da47d4a14 227 ir_val_short[num_short] = ir_val;
lilac0112_1 10:641da47d4a14 228 ir_posi_short[num_short] = ir_posi;
lilac0112_1 10:641da47d4a14 229 num_short++;
lilac0112_1 10:641da47d4a14 230 }*/
lilac0112_1 6:79dc74e4926a 231 }
lilac0112_1 6:79dc74e4926a 232 }
lilac0112_1 10:641da47d4a14 233
lilac0112_1 10:641da47d4a14 234 //BubbleSort(ir_val_short, ir_posi_short, 12);
lilac0112_1 10:641da47d4a14 235 //BubbleSort(ir_val_long, ir_posi_long, 8);
lilac0112_1 11:a5023f22f25a 236 /*
lilac0112_1 10:641da47d4a14 237 ir_position[0] = ir_posi_short[0];
lilac0112_1 10:641da47d4a14 238 ir_position[1] = ir_posi_long[0];
lilac0112_1 10:641da47d4a14 239 ir_notice=0;
lilac0112_1 11:a5023f22f25a 240 */
lilac0112_1 6:79dc74e4926a 241 pc.printf("%Value is ");
lilac0112_1 7:9b1ac6fbf32c 242 pc.printf("%d\t", ir_data[1][0]);
lilac0112_1 7:9b1ac6fbf32c 243 pc.printf("%d\t", ir_data[1][1]);
lilac0112_1 6:79dc74e4926a 244 pc.printf("%d\t", ir_data[1][2]);
lilac0112_1 7:9b1ac6fbf32c 245 pc.printf("%d\t", ir_data[1][3]);
lilac0112_1 6:79dc74e4926a 246 pc.printf("%d\t", ir_data[1][4]);
lilac0112_1 7:9b1ac6fbf32c 247 pc.printf("%d\t", ir_data[1][5]);
lilac0112_1 7:9b1ac6fbf32c 248 pc.printf("%d\t", ir_data[1][6]);
lilac0112_1 7:9b1ac6fbf32c 249 pc.printf("%d\t", ir_data[1][7]);
lilac0112_1 6:79dc74e4926a 250 pc.printf("\r\n");
lilac0112_1 9:7f98d4415425 251 //emergency();
lilac0112_1 6:79dc74e4926a 252 }
lilac0112_1 6:79dc74e4926a 253 }
lilac0112_1 10:641da47d4a14 254 uint16_t read_input(uint8_t ic, uint8_t channel)
lilac0112_1 7:9b1ac6fbf32c 255 {
lilac0112_1 10:641da47d4a14 256 uint16_t command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
lilac0112_1 10:641da47d4a14 257 uint16_t command_low = (channel & 0x03) << 6;
lilac0112_1 0:2f48b11b154f 258
lilac0112_1 7:9b1ac6fbf32c 259 cs[ic] = 0;
lilac0112_1 0:2f48b11b154f 260
lilac0112_1 7:9b1ac6fbf32c 261 ir.write(command_high);
lilac0112_1 10:641da47d4a14 262 uint16_t high_byte = ir.write(command_low) & 0x0F;
lilac0112_1 10:641da47d4a14 263 uint16_t low_byte = ir.write(0);
lilac0112_1 8:a2eadba03040 264
lilac0112_1 8:a2eadba03040 265 wait_us(1);
lilac0112_1 7:9b1ac6fbf32c 266 cs[ic] = 1;
lilac0112_1 0:2f48b11b154f 267
lilac0112_1 7:9b1ac6fbf32c 268 return (high_byte << 8) | low_byte;//0x0000...0xFFFF
lilac0112_1 0:2f48b11b154f 269 }
lilac0112_1 10:641da47d4a14 270 uint8_t read_position(uint8_t ic, uint8_t channel){
lilac0112_1 10:641da47d4a14 271 //DigitalOut cs[IC_NUM] = {cs1, cs2, cs3};
lilac0112_1 10:641da47d4a14 272 static uint8_t const ir_location[3][8]=
lilac0112_1 10:641da47d4a14 273 {//0,1,2...7(ch)
lilac0112_1 10:641da47d4a14 274 {1, 2, 3, 4, 5, 6, 7, 8},//cs1,Long*8
lilac0112_1 10:641da47d4a14 275 {1, 2, 3, 4, 5, 6, 7, 8},//cs2,Short*8
lilac0112_1 10:641da47d4a14 276 {9, 10, 11, 12},//cs3,Short*4
lilac0112_1 10:641da47d4a14 277 };
lilac0112_1 10:641da47d4a14 278 return ir_location[ic][channel];
lilac0112_1 10:641da47d4a14 279 }
lilac0112_1 10:641da47d4a14 280 void BubbleSort(uint16_t *data, uint8_t *data2, uint8_t n)//昇順にする
lilac0112_1 9:7f98d4415425 281 {
lilac0112_1 9:7f98d4415425 282 bool flag;
lilac0112_1 10:641da47d4a14 283 uint8_t i, j;//inclement
lilac0112_1 10:641da47d4a14 284 uint16_t temp;//temporary for value
lilac0112_1 10:641da47d4a14 285 uint8_t temp2;//temporary for position
lilac0112_1 9:7f98d4415425 286
lilac0112_1 10:641da47d4a14 287 j=0;
lilac0112_1 9:7f98d4415425 288 do{
lilac0112_1 10:641da47d4a14 289 j++;
lilac0112_1 9:7f98d4415425 290 flag=0;
lilac0112_1 10:641da47d4a14 291 for (i=0; i<n-j; i++) {
lilac0112_1 10:641da47d4a14 292 if (data[i]<data[i+1]) {
lilac0112_1 10:641da47d4a14 293 // 左右の並びがおかしければ入れ替える
lilac0112_1 9:7f98d4415425 294 flag=1;
lilac0112_1 9:7f98d4415425 295 temp=data[i];
lilac0112_1 9:7f98d4415425 296 data[i]=data[i+1];
lilac0112_1 9:7f98d4415425 297 data[i+1]=temp;
lilac0112_1 10:641da47d4a14 298
lilac0112_1 10:641da47d4a14 299 temp2=data2[i];
lilac0112_1 10:641da47d4a14 300 data2[i]=data2[i+1];
lilac0112_1 10:641da47d4a14 301 data2[i+1]=temp2;
lilac0112_1 9:7f98d4415425 302 }
lilac0112_1 9:7f98d4415425 303 }
lilac0112_1 10:641da47d4a14 304 } while (flag==1); //入れ替えがある間,繰り返す.
lilac0112_1 9:7f98d4415425 305 }
lilac0112_1 0:2f48b11b154f 306 #endif /*IR_SENSOR*/