LPC824専用プログラム
Dependencies: Ping SDFileSystem mbed-src
format.cpp@6:79dc74e4926a, 2016-01-12 (annotated)
- Committer:
- lilac0112_1
- Date:
- Tue Jan 12 14:59:09 2016 +0000
- Revision:
- 6:79dc74e4926a
- Parent:
- 5:afba6bf6c408
- Child:
- 7:9b1ac6fbf32c
added a library
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 | 6:79dc74e4926a | 140 | void Ir_System2(void){ |
lilac0112_1 | 6:79dc74e4926a | 141 | |
lilac0112_1 | 6:79dc74e4926a | 142 | static uint8_t const START_BIT = 0x04; |
lilac0112_1 | 6:79dc74e4926a | 143 | static uint8_t const MODE_SINGLE = 0x02; // Single-ended mode |
lilac0112_1 | 6:79dc74e4926a | 144 | //static uint8_t const MODE_DIFF = 0x00; // Differential mode |
lilac0112_1 | 6:79dc74e4926a | 145 | |
lilac0112_1 | 6:79dc74e4926a | 146 | int i;//ic |
lilac0112_1 | 6:79dc74e4926a | 147 | int j;//ch |
lilac0112_1 | 6:79dc74e4926a | 148 | uint8_t ch_num[3]={8, 8, 4}; |
lilac0112_1 | 6:79dc74e4926a | 149 | int command_high; |
lilac0112_1 | 6:79dc74e4926a | 150 | int command_low; |
lilac0112_1 | 6:79dc74e4926a | 151 | int high_byte; |
lilac0112_1 | 6:79dc74e4926a | 152 | int low_byte; |
lilac0112_1 | 6:79dc74e4926a | 153 | |
lilac0112_1 | 6:79dc74e4926a | 154 | uint16_t ir_data[3][8]={0}; |
lilac0112_1 | 6:79dc74e4926a | 155 | //clock_t stime,etime; |
lilac0112_1 | 6:79dc74e4926a | 156 | |
lilac0112_1 | 6:79dc74e4926a | 157 | supply = 1; |
lilac0112_1 | 6:79dc74e4926a | 158 | ir.frequency(1000000); |
lilac0112_1 | 6:79dc74e4926a | 159 | |
lilac0112_1 | 6:79dc74e4926a | 160 | for(i=0; i<IC_NUM; i++) cs[i]=1; |
lilac0112_1 | 6:79dc74e4926a | 161 | while(1) { |
lilac0112_1 | 6:79dc74e4926a | 162 | //stime = clock(); |
lilac0112_1 | 6:79dc74e4926a | 163 | for(i=0; i<IC_NUM; i++){//IC |
lilac0112_1 | 6:79dc74e4926a | 164 | for(j=0; j<ch_num[i]; j++){//Ch |
lilac0112_1 | 6:79dc74e4926a | 165 | command_high = START_BIT | MODE_SINGLE | ((j & 0x04) >> 2); |
lilac0112_1 | 6:79dc74e4926a | 166 | command_low = (j & 0x03) << 6; |
lilac0112_1 | 6:79dc74e4926a | 167 | |
lilac0112_1 | 6:79dc74e4926a | 168 | cs[i] = 0; |
lilac0112_1 | 6:79dc74e4926a | 169 | |
lilac0112_1 | 6:79dc74e4926a | 170 | ir.write(command_high); |
lilac0112_1 | 6:79dc74e4926a | 171 | high_byte = ir.write(command_low) & 0x0F; |
lilac0112_1 | 6:79dc74e4926a | 172 | low_byte = ir.write(0); |
lilac0112_1 | 6:79dc74e4926a | 173 | |
lilac0112_1 | 6:79dc74e4926a | 174 | cs[i] = 1; |
lilac0112_1 | 6:79dc74e4926a | 175 | wait_us(1); |
lilac0112_1 | 6:79dc74e4926a | 176 | |
lilac0112_1 | 6:79dc74e4926a | 177 | ir_data[i][j] = (high_byte << 8) | low_byte; |
lilac0112_1 | 6:79dc74e4926a | 178 | } |
lilac0112_1 | 6:79dc74e4926a | 179 | } |
lilac0112_1 | 6:79dc74e4926a | 180 | //etime = clock(); |
lilac0112_1 | 6:79dc74e4926a | 181 | //pc.printf("%.4f\n", (float)(etime-stime)/CLOCKS_PER_SEC); |
lilac0112_1 | 6:79dc74e4926a | 182 | pc.printf("%Value is "); |
lilac0112_1 | 6:79dc74e4926a | 183 | pc.printf("%d\t", ir_data[1][2]); |
lilac0112_1 | 6:79dc74e4926a | 184 | pc.printf("%d\t", ir_data[1][4]); |
lilac0112_1 | 6:79dc74e4926a | 185 | pc.printf("%d\t", ir_data[0][0]); |
lilac0112_1 | 6:79dc74e4926a | 186 | pc.printf("%d\t", ir_data[0][4]); |
lilac0112_1 | 6:79dc74e4926a | 187 | pc.printf("\r\n"); |
lilac0112_1 | 6:79dc74e4926a | 188 | } |
lilac0112_1 | 6:79dc74e4926a | 189 | } |
lilac0112_1 | 0:2f48b11b154f | 190 | void Ir_System(void){ |
lilac0112_1 | 0:2f48b11b154f | 191 | |
lilac0112_1 | 0:2f48b11b154f | 192 | static uint8_t const START_BIT = 0x04; |
lilac0112_1 | 0:2f48b11b154f | 193 | static uint8_t const MODE_SINGLE = 0x02; // Single-ended mode |
lilac0112_1 | 0:2f48b11b154f | 194 | //static uint8_t const MODE_DIFF = 0x00; // Differential mode |
lilac0112_1 | 0:2f48b11b154f | 195 | |
lilac0112_1 | 3:410a2812b0dc | 196 | static uint8_t const IR_NUM[IC_NUM] = {8, 8, 4}; |
lilac0112_1 | 0:2f48b11b154f | 197 | |
lilac0112_1 | 0:2f48b11b154f | 198 | uint8_t ic, ch; |
lilac0112_1 | 0:2f48b11b154f | 199 | unsigned int command_high, command_low; |
lilac0112_1 | 0:2f48b11b154f | 200 | unsigned int high_byte, low_byte; |
lilac0112_1 | 0:2f48b11b154f | 201 | |
lilac0112_1 | 5:afba6bf6c408 | 202 | //uint8_t i; |
lilac0112_1 | 3:410a2812b0dc | 203 | //uint8_t packet, val, order; |
lilac0112_1 | 0:2f48b11b154f | 204 | |
lilac0112_1 | 0:2f48b11b154f | 205 | uint16_t ir_data[IC_NUM][CH_NUM]; |
lilac0112_1 | 0:2f48b11b154f | 206 | |
lilac0112_1 | 6:79dc74e4926a | 207 | supply = 1; |
lilac0112_1 | 0:2f48b11b154f | 208 | ir.frequency(1000000); |
lilac0112_1 | 0:2f48b11b154f | 209 | |
lilac0112_1 | 0:2f48b11b154f | 210 | for(ic=0; ic<IC_NUM; ic++) cs[ic]=1; |
lilac0112_1 | 0:2f48b11b154f | 211 | while(1) { |
lilac0112_1 | 0:2f48b11b154f | 212 | for(ic=0; ic<IC_NUM; ic++){//IC |
lilac0112_1 | 0:2f48b11b154f | 213 | for(ch=0; ch<IR_NUM[ic]; ch++){//Ch |
lilac0112_1 | 0:2f48b11b154f | 214 | command_high = START_BIT | MODE_SINGLE | ((ch & 0x04) >> 2); |
lilac0112_1 | 0:2f48b11b154f | 215 | command_low = (ch & 0x03) << 6; |
lilac0112_1 | 1:7070fb876a2c | 216 | |
lilac0112_1 | 0:2f48b11b154f | 217 | cs[ic] = 0; |
lilac0112_1 | 1:7070fb876a2c | 218 | |
lilac0112_1 | 0:2f48b11b154f | 219 | ir.write(command_high); |
lilac0112_1 | 0:2f48b11b154f | 220 | high_byte = ir.write(command_low) & 0x0F; |
lilac0112_1 | 0:2f48b11b154f | 221 | low_byte = ir.write(0); |
lilac0112_1 | 1:7070fb876a2c | 222 | |
lilac0112_1 | 0:2f48b11b154f | 223 | cs[ic] = 1; |
lilac0112_1 | 0:2f48b11b154f | 224 | wait_us(1); |
lilac0112_1 | 1:7070fb876a2c | 225 | ir_data[ic][ch] = ((high_byte << 4) | (low_byte >> 4))&0xFF; |
lilac0112_1 | 1:7070fb876a2c | 226 | |
lilac0112_1 | 0:2f48b11b154f | 227 | } |
lilac0112_1 | 0:2f48b11b154f | 228 | } |
lilac0112_1 | 6:79dc74e4926a | 229 | pc.printf("%d\t%d\t%d\t%d\t\r\n", ir_data[1][0], ir_data[1][1], ir_data[1][2], ir_data[1][3]); |
lilac0112_1 | 3:410a2812b0dc | 230 | /*while(1){ |
lilac0112_1 | 0:2f48b11b154f | 231 | val = nucleo.receive(); |
lilac0112_1 | 0:2f48b11b154f | 232 | if(val){ |
lilac0112_1 | 0:2f48b11b154f | 233 | order = nucleo.read(); |
lilac0112_1 | 0:2f48b11b154f | 234 | packet = ir_data[order/10][order%10] & 0x00FF; |
lilac0112_1 | 0:2f48b11b154f | 235 | nucleo.reply(packet); |
lilac0112_1 | 3:410a2812b0dc | 236 | wait_us(1); |
lilac0112_1 | 0:2f48b11b154f | 237 | } |
lilac0112_1 | 0:2f48b11b154f | 238 | else{ |
lilac0112_1 | 0:2f48b11b154f | 239 | break; |
lilac0112_1 | 0:2f48b11b154f | 240 | } |
lilac0112_1 | 3:410a2812b0dc | 241 | }*/ |
lilac0112_1 | 0:2f48b11b154f | 242 | |
lilac0112_1 | 0:2f48b11b154f | 243 | } |
lilac0112_1 | 0:2f48b11b154f | 244 | } |
lilac0112_1 | 0:2f48b11b154f | 245 | #endif /*IR_SENSOR*/ |
lilac0112_1 | 0:2f48b11b154f | 246 | |
lilac0112_1 | 0:2f48b11b154f | 247 | |
lilac0112_1 | 0:2f48b11b154f | 248 | |
lilac0112_1 | 0:2f48b11b154f | 249 |