LPC824専用プログラム
Dependencies: Ping SDFileSystem mbed-src
format.cpp@3:410a2812b0dc, 2016-01-11 (annotated)
- Committer:
- lilac0112_1
- Date:
- Mon Jan 11 17:39:38 2016 +0000
- Revision:
- 3:410a2812b0dc
- Parent:
- 2:9a51bb2694aa
- Child:
- 5:afba6bf6c408
type pin number
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 | 0:2f48b11b154f | 140 | void Ir_System(void){ |
lilac0112_1 | 0:2f48b11b154f | 141 | |
lilac0112_1 | 0:2f48b11b154f | 142 | static uint8_t const START_BIT = 0x04; |
lilac0112_1 | 0:2f48b11b154f | 143 | static uint8_t const MODE_SINGLE = 0x02; // Single-ended mode |
lilac0112_1 | 0:2f48b11b154f | 144 | //static uint8_t const MODE_DIFF = 0x00; // Differential mode |
lilac0112_1 | 0:2f48b11b154f | 145 | |
lilac0112_1 | 3:410a2812b0dc | 146 | static uint8_t const IR_NUM[IC_NUM] = {8, 8, 4}; |
lilac0112_1 | 0:2f48b11b154f | 147 | |
lilac0112_1 | 0:2f48b11b154f | 148 | uint8_t ic, ch; |
lilac0112_1 | 0:2f48b11b154f | 149 | unsigned int command_high, command_low; |
lilac0112_1 | 0:2f48b11b154f | 150 | unsigned int high_byte, low_byte; |
lilac0112_1 | 0:2f48b11b154f | 151 | |
lilac0112_1 | 3:410a2812b0dc | 152 | uint8_t i; |
lilac0112_1 | 3:410a2812b0dc | 153 | //uint8_t packet, val, order; |
lilac0112_1 | 0:2f48b11b154f | 154 | |
lilac0112_1 | 0:2f48b11b154f | 155 | uint16_t ir_data[IC_NUM][CH_NUM]; |
lilac0112_1 | 0:2f48b11b154f | 156 | |
lilac0112_1 | 3:410a2812b0dc | 157 | supply = 1; |
lilac0112_1 | 0:2f48b11b154f | 158 | ir.frequency(1000000); |
lilac0112_1 | 0:2f48b11b154f | 159 | |
lilac0112_1 | 0:2f48b11b154f | 160 | for(ic=0; ic<IC_NUM; ic++) cs[ic]=1; |
lilac0112_1 | 0:2f48b11b154f | 161 | while(1) { |
lilac0112_1 | 0:2f48b11b154f | 162 | for(ic=0; ic<IC_NUM; ic++){//IC |
lilac0112_1 | 0:2f48b11b154f | 163 | for(ch=0; ch<IR_NUM[ic]; ch++){//Ch |
lilac0112_1 | 0:2f48b11b154f | 164 | command_high = START_BIT | MODE_SINGLE | ((ch & 0x04) >> 2); |
lilac0112_1 | 0:2f48b11b154f | 165 | command_low = (ch & 0x03) << 6; |
lilac0112_1 | 1:7070fb876a2c | 166 | |
lilac0112_1 | 0:2f48b11b154f | 167 | cs[ic] = 0; |
lilac0112_1 | 1:7070fb876a2c | 168 | |
lilac0112_1 | 0:2f48b11b154f | 169 | ir.write(command_high); |
lilac0112_1 | 0:2f48b11b154f | 170 | high_byte = ir.write(command_low) & 0x0F; |
lilac0112_1 | 0:2f48b11b154f | 171 | low_byte = ir.write(0); |
lilac0112_1 | 1:7070fb876a2c | 172 | |
lilac0112_1 | 0:2f48b11b154f | 173 | cs[ic] = 1; |
lilac0112_1 | 0:2f48b11b154f | 174 | wait_us(1); |
lilac0112_1 | 1:7070fb876a2c | 175 | ir_data[ic][ch] = ((high_byte << 4) | (low_byte >> 4))&0xFF; |
lilac0112_1 | 1:7070fb876a2c | 176 | |
lilac0112_1 | 0:2f48b11b154f | 177 | } |
lilac0112_1 | 0:2f48b11b154f | 178 | } |
lilac0112_1 | 3:410a2812b0dc | 179 | for(i=0; i<8; i++){ |
lilac0112_1 | 3:410a2812b0dc | 180 | pc.printf("%d\n", ir_data[0][i]); |
lilac0112_1 | 3:410a2812b0dc | 181 | } |
lilac0112_1 | 3:410a2812b0dc | 182 | /*while(1){ |
lilac0112_1 | 0:2f48b11b154f | 183 | val = nucleo.receive(); |
lilac0112_1 | 0:2f48b11b154f | 184 | if(val){ |
lilac0112_1 | 0:2f48b11b154f | 185 | order = nucleo.read(); |
lilac0112_1 | 0:2f48b11b154f | 186 | packet = ir_data[order/10][order%10] & 0x00FF; |
lilac0112_1 | 0:2f48b11b154f | 187 | nucleo.reply(packet); |
lilac0112_1 | 3:410a2812b0dc | 188 | wait_us(1); |
lilac0112_1 | 0:2f48b11b154f | 189 | } |
lilac0112_1 | 0:2f48b11b154f | 190 | else{ |
lilac0112_1 | 0:2f48b11b154f | 191 | break; |
lilac0112_1 | 0:2f48b11b154f | 192 | } |
lilac0112_1 | 3:410a2812b0dc | 193 | }*/ |
lilac0112_1 | 0:2f48b11b154f | 194 | |
lilac0112_1 | 0:2f48b11b154f | 195 | } |
lilac0112_1 | 0:2f48b11b154f | 196 | } |
lilac0112_1 | 0:2f48b11b154f | 197 | #endif /*IR_SENSOR*/ |
lilac0112_1 | 0:2f48b11b154f | 198 | |
lilac0112_1 | 0:2f48b11b154f | 199 | |
lilac0112_1 | 0:2f48b11b154f | 200 | |
lilac0112_1 | 0:2f48b11b154f | 201 |