LPC824専用プログラム

Dependencies:   Ping SDFileSystem mbed-src

Committer:
lilac0112_1
Date:
Sun Jan 24 09:09:49 2016 +0000
Revision:
8:a2eadba03040
Parent:
7:9b1ac6fbf32c
Child:
9:7f98d4415425
spi communication was succeeded

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 8:a2eadba03040 78 //InterruptIn vol(P0_15);
lilac0112_1 8:a2eadba03040 79 void fx(void){
lilac0112_1 8:a2eadba03040 80 uint8_t packet, val, order;
lilac0112_1 8:a2eadba03040 81 uint8_t front_dis=0, rear_dis=0;
lilac0112_1 8:a2eadba03040 82 front_dis = 200;
lilac0112_1 8:a2eadba03040 83 rear_dis = 150;
lilac0112_1 8:a2eadba03040 84 val = nucleo.receive();
lilac0112_1 8:a2eadba03040 85 if(val==1){
lilac0112_1 8:a2eadba03040 86 order = nucleo.read();
lilac0112_1 8:a2eadba03040 87 if(order == 0xAB){
lilac0112_1 8:a2eadba03040 88 packet = 0xAA;nucleo.reply(0xAA);
lilac0112_1 8:a2eadba03040 89 }
lilac0112_1 8:a2eadba03040 90 else{
lilac0112_1 8:a2eadba03040 91 packet = 0x55;nucleo.reply(0x55);
lilac0112_1 8:a2eadba03040 92 }
lilac0112_1 8:a2eadba03040 93 pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
lilac0112_1 8:a2eadba03040 94 }
lilac0112_1 8:a2eadba03040 95 else{
lilac0112_1 8:a2eadba03040 96 pc.printf("FFFF%d\r\n", 0);
lilac0112_1 8:a2eadba03040 97 }
lilac0112_1 8:a2eadba03040 98 }
lilac0112_1 8:a2eadba03040 99 void fx2(void){
lilac0112_1 8:a2eadba03040 100 uint16_t packet, val, order;
lilac0112_1 8:a2eadba03040 101 uint16_t front_dis=0, rear_dis=0;
lilac0112_1 8:a2eadba03040 102 front_dis = 200;
lilac0112_1 8:a2eadba03040 103 rear_dis = 150;
lilac0112_1 8:a2eadba03040 104 val = nucleo.receive();
lilac0112_1 8:a2eadba03040 105 if(val==1){
lilac0112_1 8:a2eadba03040 106 order = nucleo.read();
lilac0112_1 8:a2eadba03040 107 if(order == 0xABCD){
lilac0112_1 8:a2eadba03040 108 packet = 0xAAAA;nucleo.reply(0xAAAA);
lilac0112_1 8:a2eadba03040 109 }
lilac0112_1 8:a2eadba03040 110 else{
lilac0112_1 8:a2eadba03040 111 packet = 0x5555;nucleo.reply(0x5555);
lilac0112_1 8:a2eadba03040 112 }
lilac0112_1 8:a2eadba03040 113 nucleo.reply(0x5555);
lilac0112_1 8:a2eadba03040 114 pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
lilac0112_1 8:a2eadba03040 115 }
lilac0112_1 8:a2eadba03040 116 else{
lilac0112_1 8:a2eadba03040 117 pc.printf("FFFF%d\r\n", 0);
lilac0112_1 8:a2eadba03040 118 }
lilac0112_1 8:a2eadba03040 119 }
lilac0112_1 0:2f48b11b154f 120 void Usw_System(void){
lilac0112_1 8:a2eadba03040 121 int packet, val, order;
lilac0112_1 0:2f48b11b154f 122 uint16_t front_dis=0, rear_dis=0;
lilac0112_1 0:2f48b11b154f 123
lilac0112_1 8:a2eadba03040 124 nucleo.format(8, 3);
lilac0112_1 8:a2eadba03040 125 nucleo.frequency(1000000);
lilac0112_1 8:a2eadba03040 126 nucleo.reply(0x00);
lilac0112_1 8:a2eadba03040 127 /*
lilac0112_1 8:a2eadba03040 128 nucleo.format(16, 3);
lilac0112_1 8:a2eadba03040 129 nucleo.frequency(1000000);
lilac0112_1 8:a2eadba03040 130 nucleo.reply(0x0000);
lilac0112_1 8:a2eadba03040 131 */
lilac0112_1 8:a2eadba03040 132 vol.fall(&fx);
lilac0112_1 8:a2eadba03040 133 //vol.fall(&fx2);
lilac0112_1 8:a2eadba03040 134
lilac0112_1 8:a2eadba03040 135 led[0]=led[1]=1;
lilac0112_1 0:2f48b11b154f 136 while(1){
lilac0112_1 8:a2eadba03040 137 /*
lilac0112_1 0:2f48b11b154f 138 front.Send();
lilac0112_1 0:2f48b11b154f 139 wait_ms(30);
lilac0112_1 0:2f48b11b154f 140 front_dis = front.Read_cm();
lilac0112_1 8:a2eadba03040 141 if(front_dis>0xFF) front_dis=0xFF;
lilac0112_1 0:2f48b11b154f 142
lilac0112_1 0:2f48b11b154f 143 rear.Send();
lilac0112_1 0:2f48b11b154f 144 wait_ms(30);
lilac0112_1 0:2f48b11b154f 145 rear_dis = rear.Read_cm();
lilac0112_1 8:a2eadba03040 146 if(rear_dis>0xFF) rear_dis=0xFF;
lilac0112_1 8:a2eadba03040 147 */
lilac0112_1 0:2f48b11b154f 148
lilac0112_1 8:a2eadba03040 149
lilac0112_1 8:a2eadba03040 150 front_dis = 200;
lilac0112_1 8:a2eadba03040 151 rear_dis = 150;
lilac0112_1 8:a2eadba03040 152
lilac0112_1 8:a2eadba03040 153 //pc.printf("%d\t%d\t\r\n", front_dis, rear_dis);
lilac0112_1 8:a2eadba03040 154 //pc.printf("%d\r\n", vol.read());
lilac0112_1 8:a2eadba03040 155 //fx();
lilac0112_1 8:a2eadba03040 156 //fx2();
lilac0112_1 8:a2eadba03040 157 while(0){
lilac0112_1 0:2f48b11b154f 158 val = nucleo.receive();
lilac0112_1 8:a2eadba03040 159 if(val==1){
lilac0112_1 0:2f48b11b154f 160 order = nucleo.read();
lilac0112_1 8:a2eadba03040 161 if(order == 0xAB){
lilac0112_1 8:a2eadba03040 162 packet = 0xAA;nucleo.reply(packet);
lilac0112_1 0:2f48b11b154f 163 }
lilac0112_1 8:a2eadba03040 164 else{
lilac0112_1 8:a2eadba03040 165 packet = 0x55;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 166 }
lilac0112_1 8:a2eadba03040 167
lilac0112_1 8:a2eadba03040 168 pc.printf("SSSSSSSSSSSSSSSSS%d\r\n", order);
lilac0112_1 8:a2eadba03040 169 break;
lilac0112_1 0:2f48b11b154f 170 }
lilac0112_1 0:2f48b11b154f 171 else{
lilac0112_1 8:a2eadba03040 172 pc.printf("FFFF%d\r\n", 0);
lilac0112_1 0:2f48b11b154f 173 break;
lilac0112_1 0:2f48b11b154f 174 }
lilac0112_1 0:2f48b11b154f 175 }
lilac0112_1 0:2f48b11b154f 176 }
lilac0112_1 0:2f48b11b154f 177 }
lilac0112_1 0:2f48b11b154f 178 #endif /*ULTRA_SONIC*/
lilac0112_1 0:2f48b11b154f 179
lilac0112_1 0:2f48b11b154f 180 #ifdef COLOR_SENSOR
lilac0112_1 0:2f48b11b154f 181 void Color_System(void){
lilac0112_1 1:7070fb876a2c 182 uint8_t val, order, packet;
lilac0112_1 1:7070fb876a2c 183 uint16_t data;
lilac0112_1 1:7070fb876a2c 184
lilac0112_1 0:2f48b11b154f 185 while(1){
lilac0112_1 1:7070fb876a2c 186 while(1){
lilac0112_1 1:7070fb876a2c 187 val = nucleo.receive();
lilac0112_1 1:7070fb876a2c 188 if(val){
lilac0112_1 1:7070fb876a2c 189 order = nucleo.read();
lilac0112_1 1:7070fb876a2c 190 order %= SENSOR_X_COLOR;
lilac0112_1 2:9a51bb2694aa 191 data = ColorSensor[order].read_u16();
lilac0112_1 1:7070fb876a2c 192 packet = (data & 0xFF00)>>8;
lilac0112_1 1:7070fb876a2c 193 nucleo.reply(packet);
lilac0112_1 1:7070fb876a2c 194 }
lilac0112_1 1:7070fb876a2c 195 else{
lilac0112_1 1:7070fb876a2c 196 break;
lilac0112_1 1:7070fb876a2c 197 }
lilac0112_1 1:7070fb876a2c 198 }
lilac0112_1 0:2f48b11b154f 199 }
lilac0112_1 0:2f48b11b154f 200 }
lilac0112_1 0:2f48b11b154f 201 #endif /*COLOR_SENSOR*/
lilac0112_1 0:2f48b11b154f 202
lilac0112_1 0:2f48b11b154f 203 #ifdef IR_SENSOR
lilac0112_1 7:9b1ac6fbf32c 204 void Ir_System(void){
lilac0112_1 6:79dc74e4926a 205 int i;//ic
lilac0112_1 6:79dc74e4926a 206 int j;//ch
lilac0112_1 6:79dc74e4926a 207 uint8_t ch_num[3]={8, 8, 4};
lilac0112_1 7:9b1ac6fbf32c 208 uint16_t ir_data[IC_NUM][IR_NUM]={0};
lilac0112_1 6:79dc74e4926a 209
lilac0112_1 8:a2eadba03040 210 int order,packet,val;
lilac0112_1 8:a2eadba03040 211 //nucleo...chipselect will be defined interruptpin.
lilac0112_1 8:a2eadba03040 212 //nucleo...will be high clock and 16bit
lilac0112_1 8:a2eadba03040 213 //nucleo.format(16, 0);
lilac0112_1 8:a2eadba03040 214 //nucleo.frequency(5000000);
lilac0112_1 8:a2eadba03040 215 nucleo.format(8, 0);
lilac0112_1 8:a2eadba03040 216 nucleo.frequency(1000000);
lilac0112_1 8:a2eadba03040 217 nucleo.reply(0x00);
lilac0112_1 8:a2eadba03040 218
lilac0112_1 7:9b1ac6fbf32c 219 ir.format(8, 3);
lilac0112_1 6:79dc74e4926a 220 ir.frequency(1000000);
lilac0112_1 7:9b1ac6fbf32c 221 int mod=1;
lilac0112_1 6:79dc74e4926a 222
lilac0112_1 6:79dc74e4926a 223 for(i=0; i<IC_NUM; i++) cs[i]=1;
lilac0112_1 6:79dc74e4926a 224 while(1) {
lilac0112_1 7:9b1ac6fbf32c 225 /*cs[1]=1;
lilac0112_1 7:9b1ac6fbf32c 226
lilac0112_1 7:9b1ac6fbf32c 227 mod = ir.write(mod);
lilac0112_1 7:9b1ac6fbf32c 228 //ir.write(0)&0xFF;
lilac0112_1 7:9b1ac6fbf32c 229
lilac0112_1 7:9b1ac6fbf32c 230 cs[1]=0;
lilac0112_1 7:9b1ac6fbf32c 231 wait_us(1);
lilac0112_1 7:9b1ac6fbf32c 232
lilac0112_1 7:9b1ac6fbf32c 233 pc.printf("%Value is ");
lilac0112_1 7:9b1ac6fbf32c 234 pc.printf("%d\t", mod);
lilac0112_1 7:9b1ac6fbf32c 235 pc.printf("\r\n");
lilac0112_1 7:9b1ac6fbf32c 236 */
lilac0112_1 6:79dc74e4926a 237 for(i=0; i<IC_NUM; i++){//IC
lilac0112_1 6:79dc74e4926a 238 for(j=0; j<ch_num[i]; j++){//Ch
lilac0112_1 7:9b1ac6fbf32c 239 ir_data[i][j] = read_input(i, j);
lilac0112_1 6:79dc74e4926a 240 }
lilac0112_1 6:79dc74e4926a 241 }
lilac0112_1 8:a2eadba03040 242 while(0){
lilac0112_1 8:a2eadba03040 243 val = nucleo.receive();
lilac0112_1 8:a2eadba03040 244 if(val==1){
lilac0112_1 8:a2eadba03040 245 //order = nucleo.read();
lilac0112_1 8:a2eadba03040 246 order = nucleo.read();
lilac0112_1 8:a2eadba03040 247 //if(order != 0xAA) break;
lilac0112_1 8:a2eadba03040 248 //packet = ir_data[order/10][order%10] & 0x00FF;
lilac0112_1 8:a2eadba03040 249 //nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 250 packet = (ir_data[1][0] & 0x0FF0)>>4;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 251 /*packet = (ir_data[1][1] & 0xFF00)>>8;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 252 packet = (ir_data[1][2] & 0xFF00)>>8;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 253 packet = (ir_data[1][3] & 0xFF00)>>8;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 254 packet = (ir_data[1][4] & 0xFF00)>>8;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 255 packet = (ir_data[1][5] & 0xFF00)>>8;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 256 packet = (ir_data[1][6] & 0xFF00)>>8;nucleo.reply(packet);
lilac0112_1 8:a2eadba03040 257 packet = (ir_data[1][7] & 0xFF00)>>8;nucleo.reply(packet);wait_us(1);*/
lilac0112_1 8:a2eadba03040 258
lilac0112_1 8:a2eadba03040 259 pc.printf("SSSSSSSSSSSSSSSSS\r\n");
lilac0112_1 8:a2eadba03040 260 break;
lilac0112_1 8:a2eadba03040 261 }
lilac0112_1 8:a2eadba03040 262 else{
lilac0112_1 8:a2eadba03040 263 pc.printf("FFFFF\r\n");
lilac0112_1 8:a2eadba03040 264 break;
lilac0112_1 8:a2eadba03040 265 }
lilac0112_1 8:a2eadba03040 266 }
lilac0112_1 6:79dc74e4926a 267 pc.printf("%Value is ");
lilac0112_1 7:9b1ac6fbf32c 268 pc.printf("%d\t", ir_data[1][0]);
lilac0112_1 7:9b1ac6fbf32c 269 pc.printf("%d\t", ir_data[1][1]);
lilac0112_1 6:79dc74e4926a 270 pc.printf("%d\t", ir_data[1][2]);
lilac0112_1 7:9b1ac6fbf32c 271 pc.printf("%d\t", ir_data[1][3]);
lilac0112_1 6:79dc74e4926a 272 pc.printf("%d\t", ir_data[1][4]);
lilac0112_1 7:9b1ac6fbf32c 273 pc.printf("%d\t", ir_data[1][5]);
lilac0112_1 7:9b1ac6fbf32c 274 pc.printf("%d\t", ir_data[1][6]);
lilac0112_1 7:9b1ac6fbf32c 275 pc.printf("%d\t", ir_data[1][7]);
lilac0112_1 6:79dc74e4926a 276 pc.printf("\r\n");
lilac0112_1 6:79dc74e4926a 277 }
lilac0112_1 6:79dc74e4926a 278 }
lilac0112_1 7:9b1ac6fbf32c 279 int read_input(int ic, int channel)
lilac0112_1 7:9b1ac6fbf32c 280 {
lilac0112_1 0:2f48b11b154f 281
lilac0112_1 7:9b1ac6fbf32c 282 int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2);
lilac0112_1 7:9b1ac6fbf32c 283 int command_low = (channel & 0x03) << 6;
lilac0112_1 0:2f48b11b154f 284
lilac0112_1 7:9b1ac6fbf32c 285 cs[ic] = 0;
lilac0112_1 0:2f48b11b154f 286
lilac0112_1 7:9b1ac6fbf32c 287 ir.write(command_high);
lilac0112_1 7:9b1ac6fbf32c 288 int high_byte = ir.write(command_low) & 0x0F;
lilac0112_1 7:9b1ac6fbf32c 289 int low_byte = ir.write(0);
lilac0112_1 0:2f48b11b154f 290
lilac0112_1 8:a2eadba03040 291
lilac0112_1 8:a2eadba03040 292 wait_us(1);
lilac0112_1 7:9b1ac6fbf32c 293 cs[ic] = 1;
lilac0112_1 0:2f48b11b154f 294
lilac0112_1 7:9b1ac6fbf32c 295 return (high_byte << 8) | low_byte;//0x0000...0xFFFF
lilac0112_1 0:2f48b11b154f 296 }
lilac0112_1 0:2f48b11b154f 297 #endif /*IR_SENSOR*/
lilac0112_1 0:2f48b11b154f 298
lilac0112_1 0:2f48b11b154f 299
lilac0112_1 0:2f48b11b154f 300
lilac0112_1 0:2f48b11b154f 301