LPC824専用プログラム
Dependencies: Ping SDFileSystem mbed-src
format.cpp@7:9b1ac6fbf32c, 2016-01-15 (annotated)
- Committer:
- lilac0112_1
- Date:
- Fri Jan 15 15:30:57 2016 +0000
- Revision:
- 7:9b1ac6fbf32c
- Parent:
- 6:79dc74e4926a
- Child:
- 8:a2eadba03040
succeed in connecting to 3208
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:2f48b11b154f | 77 | void Usw_System(void){ |
lilac0112_1 | 2:9a51bb2694aa | 78 | uint8_t packet, val, order; |
lilac0112_1 | 0:2f48b11b154f | 79 | uint16_t front_dis=0, rear_dis=0; |
lilac0112_1 | 0:2f48b11b154f | 80 | |
lilac0112_1 | 0:2f48b11b154f | 81 | while(1){ |
lilac0112_1 | 0:2f48b11b154f | 82 | front.Send(); |
lilac0112_1 | 0:2f48b11b154f | 83 | wait_ms(30); |
lilac0112_1 | 0:2f48b11b154f | 84 | front_dis = front.Read_cm(); |
lilac0112_1 | 0:2f48b11b154f | 85 | |
lilac0112_1 | 0:2f48b11b154f | 86 | rear.Send(); |
lilac0112_1 | 0:2f48b11b154f | 87 | wait_ms(30); |
lilac0112_1 | 0:2f48b11b154f | 88 | rear_dis = rear.Read_cm(); |
lilac0112_1 | 0:2f48b11b154f | 89 | |
lilac0112_1 | 0:2f48b11b154f | 90 | while(1){ |
lilac0112_1 | 0:2f48b11b154f | 91 | val = nucleo.receive(); |
lilac0112_1 | 0:2f48b11b154f | 92 | if(val){ |
lilac0112_1 | 0:2f48b11b154f | 93 | order = nucleo.read(); |
lilac0112_1 | 0:2f48b11b154f | 94 | switch(order){ |
lilac0112_1 | 0:2f48b11b154f | 95 | case 0x01: |
lilac0112_1 | 0:2f48b11b154f | 96 | packet = (front_dis&0x00FF);break; |
lilac0112_1 | 0:2f48b11b154f | 97 | case 0x02: |
lilac0112_1 | 0:2f48b11b154f | 98 | packet = (front_dis&0xFF00)>>8;break; |
lilac0112_1 | 0:2f48b11b154f | 99 | case 0x03: |
lilac0112_1 | 0:2f48b11b154f | 100 | packet = (rear_dis&0x00FF);break; |
lilac0112_1 | 0:2f48b11b154f | 101 | case 0x04: |
lilac0112_1 | 0:2f48b11b154f | 102 | packet = (rear_dis&0xFF00)>>8;break; |
lilac0112_1 | 0:2f48b11b154f | 103 | default: |
lilac0112_1 | 0:2f48b11b154f | 104 | packet = 0x00;break; |
lilac0112_1 | 0:2f48b11b154f | 105 | } |
lilac0112_1 | 0:2f48b11b154f | 106 | nucleo.reply(packet); |
lilac0112_1 | 0:2f48b11b154f | 107 | } |
lilac0112_1 | 0:2f48b11b154f | 108 | else{ |
lilac0112_1 | 0:2f48b11b154f | 109 | break; |
lilac0112_1 | 0:2f48b11b154f | 110 | } |
lilac0112_1 | 0:2f48b11b154f | 111 | } |
lilac0112_1 | 0:2f48b11b154f | 112 | } |
lilac0112_1 | 0:2f48b11b154f | 113 | } |
lilac0112_1 | 0:2f48b11b154f | 114 | #endif /*ULTRA_SONIC*/ |
lilac0112_1 | 0:2f48b11b154f | 115 | |
lilac0112_1 | 0:2f48b11b154f | 116 | #ifdef COLOR_SENSOR |
lilac0112_1 | 0:2f48b11b154f | 117 | void Color_System(void){ |
lilac0112_1 | 1:7070fb876a2c | 118 | uint8_t val, order, packet; |
lilac0112_1 | 1:7070fb876a2c | 119 | uint16_t data; |
lilac0112_1 | 1:7070fb876a2c | 120 | |
lilac0112_1 | 0:2f48b11b154f | 121 | while(1){ |
lilac0112_1 | 1:7070fb876a2c | 122 | while(1){ |
lilac0112_1 | 1:7070fb876a2c | 123 | val = nucleo.receive(); |
lilac0112_1 | 1:7070fb876a2c | 124 | if(val){ |
lilac0112_1 | 1:7070fb876a2c | 125 | order = nucleo.read(); |
lilac0112_1 | 1:7070fb876a2c | 126 | order %= SENSOR_X_COLOR; |
lilac0112_1 | 2:9a51bb2694aa | 127 | data = ColorSensor[order].read_u16(); |
lilac0112_1 | 1:7070fb876a2c | 128 | packet = (data & 0xFF00)>>8; |
lilac0112_1 | 1:7070fb876a2c | 129 | nucleo.reply(packet); |
lilac0112_1 | 1:7070fb876a2c | 130 | } |
lilac0112_1 | 1:7070fb876a2c | 131 | else{ |
lilac0112_1 | 1:7070fb876a2c | 132 | break; |
lilac0112_1 | 1:7070fb876a2c | 133 | } |
lilac0112_1 | 1:7070fb876a2c | 134 | } |
lilac0112_1 | 0:2f48b11b154f | 135 | } |
lilac0112_1 | 0:2f48b11b154f | 136 | } |
lilac0112_1 | 0:2f48b11b154f | 137 | #endif /*COLOR_SENSOR*/ |
lilac0112_1 | 0:2f48b11b154f | 138 | |
lilac0112_1 | 0:2f48b11b154f | 139 | #ifdef IR_SENSOR |
lilac0112_1 | 7:9b1ac6fbf32c | 140 | void Ir_System(void){ |
lilac0112_1 | 6:79dc74e4926a | 141 | int i;//ic |
lilac0112_1 | 6:79dc74e4926a | 142 | int j;//ch |
lilac0112_1 | 6:79dc74e4926a | 143 | uint8_t ch_num[3]={8, 8, 4}; |
lilac0112_1 | 7:9b1ac6fbf32c | 144 | uint16_t ir_data[IC_NUM][IR_NUM]={0}; |
lilac0112_1 | 6:79dc74e4926a | 145 | |
lilac0112_1 | 7:9b1ac6fbf32c | 146 | //nucleo.reply(0x00); |
lilac0112_1 | 7:9b1ac6fbf32c | 147 | ir.format(8, 3); |
lilac0112_1 | 6:79dc74e4926a | 148 | ir.frequency(1000000); |
lilac0112_1 | 7:9b1ac6fbf32c | 149 | int mod=1; |
lilac0112_1 | 6:79dc74e4926a | 150 | |
lilac0112_1 | 6:79dc74e4926a | 151 | for(i=0; i<IC_NUM; i++) cs[i]=1; |
lilac0112_1 | 6:79dc74e4926a | 152 | while(1) { |
lilac0112_1 | 7:9b1ac6fbf32c | 153 | /*cs[1]=1; |
lilac0112_1 | 7:9b1ac6fbf32c | 154 | |
lilac0112_1 | 7:9b1ac6fbf32c | 155 | mod = ir.write(mod); |
lilac0112_1 | 7:9b1ac6fbf32c | 156 | //ir.write(0)&0xFF; |
lilac0112_1 | 7:9b1ac6fbf32c | 157 | |
lilac0112_1 | 7:9b1ac6fbf32c | 158 | cs[1]=0; |
lilac0112_1 | 7:9b1ac6fbf32c | 159 | wait_us(1); |
lilac0112_1 | 7:9b1ac6fbf32c | 160 | |
lilac0112_1 | 7:9b1ac6fbf32c | 161 | pc.printf("%Value is "); |
lilac0112_1 | 7:9b1ac6fbf32c | 162 | pc.printf("%d\t", mod); |
lilac0112_1 | 7:9b1ac6fbf32c | 163 | pc.printf("\r\n"); |
lilac0112_1 | 7:9b1ac6fbf32c | 164 | */ |
lilac0112_1 | 6:79dc74e4926a | 165 | for(i=0; i<IC_NUM; i++){//IC |
lilac0112_1 | 6:79dc74e4926a | 166 | for(j=0; j<ch_num[i]; j++){//Ch |
lilac0112_1 | 7:9b1ac6fbf32c | 167 | ir_data[i][j] = read_input(i, j); |
lilac0112_1 | 6:79dc74e4926a | 168 | } |
lilac0112_1 | 6:79dc74e4926a | 169 | } |
lilac0112_1 | 6:79dc74e4926a | 170 | pc.printf("%Value is "); |
lilac0112_1 | 7:9b1ac6fbf32c | 171 | pc.printf("%d\t", ir_data[1][0]); |
lilac0112_1 | 7:9b1ac6fbf32c | 172 | pc.printf("%d\t", ir_data[1][1]); |
lilac0112_1 | 6:79dc74e4926a | 173 | pc.printf("%d\t", ir_data[1][2]); |
lilac0112_1 | 7:9b1ac6fbf32c | 174 | pc.printf("%d\t", ir_data[1][3]); |
lilac0112_1 | 6:79dc74e4926a | 175 | pc.printf("%d\t", ir_data[1][4]); |
lilac0112_1 | 7:9b1ac6fbf32c | 176 | pc.printf("%d\t", ir_data[1][5]); |
lilac0112_1 | 7:9b1ac6fbf32c | 177 | pc.printf("%d\t", ir_data[1][6]); |
lilac0112_1 | 7:9b1ac6fbf32c | 178 | pc.printf("%d\t", ir_data[1][7]); |
lilac0112_1 | 6:79dc74e4926a | 179 | pc.printf("\r\n"); |
lilac0112_1 | 6:79dc74e4926a | 180 | } |
lilac0112_1 | 6:79dc74e4926a | 181 | } |
lilac0112_1 | 7:9b1ac6fbf32c | 182 | int read_input(int ic, int channel) |
lilac0112_1 | 7:9b1ac6fbf32c | 183 | { |
lilac0112_1 | 0:2f48b11b154f | 184 | |
lilac0112_1 | 7:9b1ac6fbf32c | 185 | int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2); |
lilac0112_1 | 7:9b1ac6fbf32c | 186 | int command_low = (channel & 0x03) << 6; |
lilac0112_1 | 0:2f48b11b154f | 187 | |
lilac0112_1 | 7:9b1ac6fbf32c | 188 | cs[ic] = 0; |
lilac0112_1 | 0:2f48b11b154f | 189 | |
lilac0112_1 | 7:9b1ac6fbf32c | 190 | ir.write(command_high); |
lilac0112_1 | 7:9b1ac6fbf32c | 191 | int high_byte = ir.write(command_low) & 0x0F; |
lilac0112_1 | 7:9b1ac6fbf32c | 192 | int low_byte = ir.write(0); |
lilac0112_1 | 0:2f48b11b154f | 193 | |
lilac0112_1 | 7:9b1ac6fbf32c | 194 | cs[ic] = 1; |
lilac0112_1 | 7:9b1ac6fbf32c | 195 | wait_us(1); |
lilac0112_1 | 0:2f48b11b154f | 196 | |
lilac0112_1 | 7:9b1ac6fbf32c | 197 | return (high_byte << 8) | low_byte;//0x0000...0xFFFF |
lilac0112_1 | 7:9b1ac6fbf32c | 198 | /*int static mac=0; |
lilac0112_1 | 7:9b1ac6fbf32c | 199 | int static mast[10][10] = |
lilac0112_1 | 7:9b1ac6fbf32c | 200 | { |
lilac0112_1 | 7:9b1ac6fbf32c | 201 | {1,2,3,4,5,6,7,8,9,10}, |
lilac0112_1 | 7:9b1ac6fbf32c | 202 | {2,4,6,8,10,12,14,16,18,20}, |
lilac0112_1 | 7:9b1ac6fbf32c | 203 | {3,6,9,12,15,18,21,24,27,30}, |
lilac0112_1 | 7:9b1ac6fbf32c | 204 | {4,8,12,16,20,24,28,32,36,40}, |
lilac0112_1 | 7:9b1ac6fbf32c | 205 | {5,10,15,20,25,30,35,40,45,50}, |
lilac0112_1 | 7:9b1ac6fbf32c | 206 | {6,12,18,24,30,36,42,48,54,60}, |
lilac0112_1 | 7:9b1ac6fbf32c | 207 | {7,14,21,28,35,42,49,56,63,70}, |
lilac0112_1 | 7:9b1ac6fbf32c | 208 | {8,16,24,32,40,48,56,64,72,80}, |
lilac0112_1 | 7:9b1ac6fbf32c | 209 | {9,18,27,36,45,54,63,72,81,90}, |
lilac0112_1 | 7:9b1ac6fbf32c | 210 | {10,20,30,40,50,60,70,80,90,100}, |
lilac0112_1 | 7:9b1ac6fbf32c | 211 | }; |
lilac0112_1 | 7:9b1ac6fbf32c | 212 | if(channel==0) mac++; |
lilac0112_1 | 7:9b1ac6fbf32c | 213 | mac = mac%10; |
lilac0112_1 | 7:9b1ac6fbf32c | 214 | return mast[mac][channel];*/ |
lilac0112_1 | 0:2f48b11b154f | 215 | } |
lilac0112_1 | 0:2f48b11b154f | 216 | #endif /*IR_SENSOR*/ |
lilac0112_1 | 0:2f48b11b154f | 217 | |
lilac0112_1 | 0:2f48b11b154f | 218 | |
lilac0112_1 | 0:2f48b11b154f | 219 | |
lilac0112_1 | 0:2f48b11b154f | 220 |