
mbed base board 2016 Check program check for SD card read&write (3 switches, 6 leds, I2C LCD, SD card)
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@1:a41c274f95e2, 2016-10-17 (annotated)
- Committer:
- tamaki
- Date:
- Mon Oct 17 00:20:38 2016 +0000
- Revision:
- 1:a41c274f95e2
- Parent:
- 0:bdbd3d6fc5d5
- Child:
- 2:0ee1c0236a11
mbed board check program for SD card(read, write)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tamaki | 1:a41c274f95e2 | 1 | #define CPP1 0 |
tamaki | 1:a41c274f95e2 | 2 | #define C1 1 |
tamaki | 1:a41c274f95e2 | 3 | |
tamaki | 1:a41c274f95e2 | 4 | |
tamaki | 1:a41c274f95e2 | 5 | #if CPP1 |
mbed_official | 0:bdbd3d6fc5d5 | 6 | #include "mbed.h" |
mbed_official | 0:bdbd3d6fc5d5 | 7 | #include "SDFileSystem.h" |
tamaki | 1:a41c274f95e2 | 8 | #include "ACM1602NI.h" |
tamaki | 1:a41c274f95e2 | 9 | DigitalOut led1(p21); |
tamaki | 1:a41c274f95e2 | 10 | DigitalOut led2(p22); |
tamaki | 1:a41c274f95e2 | 11 | DigitalOut led3(p23); |
tamaki | 1:a41c274f95e2 | 12 | DigitalOut led4(p24); |
tamaki | 1:a41c274f95e2 | 13 | DigitalOut led5(p25); |
tamaki | 1:a41c274f95e2 | 14 | DigitalOut led6(p26); |
tamaki | 1:a41c274f95e2 | 15 | //DigitalIn sw1(p14); |
tamaki | 1:a41c274f95e2 | 16 | //DigitalIn sw2(p15); |
tamaki | 1:a41c274f95e2 | 17 | //DigitalIn sw3(p16); |
tamaki | 1:a41c274f95e2 | 18 | AnalogIn vr(p17); |
tamaki | 1:a41c274f95e2 | 19 | |
tamaki | 1:a41c274f95e2 | 20 | FILE *fp; |
mbed_official | 0:bdbd3d6fc5d5 | 21 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
tamaki | 1:a41c274f95e2 | 22 | ACM1602NI lcd(p28, p27); //sda scl |
tamaki | 1:a41c274f95e2 | 23 | |
tamaki | 1:a41c274f95e2 | 24 | class cChkSw |
tamaki | 1:a41c274f95e2 | 25 | { |
tamaki | 1:a41c274f95e2 | 26 | public: |
tamaki | 1:a41c274f95e2 | 27 | int st[3]; |
tamaki | 1:a41c274f95e2 | 28 | int counter[3];//past counter |
tamaki | 1:a41c274f95e2 | 29 | cChkSw(PinName, PinName, PinName); |
tamaki | 1:a41c274f95e2 | 30 | DigitalIn *sw1; |
tamaki | 1:a41c274f95e2 | 31 | DigitalIn *sw2; |
tamaki | 1:a41c274f95e2 | 32 | DigitalIn *sw3; |
tamaki | 1:a41c274f95e2 | 33 | void init(); |
tamaki | 1:a41c274f95e2 | 34 | void chksw(); |
mbed_official | 0:bdbd3d6fc5d5 | 35 | |
tamaki | 1:a41c274f95e2 | 36 | protected: |
tamaki | 1:a41c274f95e2 | 37 | }; |
tamaki | 1:a41c274f95e2 | 38 | |
tamaki | 1:a41c274f95e2 | 39 | void cChkSw::init() |
tamaki | 1:a41c274f95e2 | 40 | { |
tamaki | 1:a41c274f95e2 | 41 | } |
tamaki | 1:a41c274f95e2 | 42 | |
tamaki | 1:a41c274f95e2 | 43 | cChkSw::cChkSw(PinName s1, PinName s2, PinName s3) |
tamaki | 1:a41c274f95e2 | 44 | { |
tamaki | 1:a41c274f95e2 | 45 | sw1 = new DigitalIn(s1); |
tamaki | 1:a41c274f95e2 | 46 | sw2 = new DigitalIn(s2); |
tamaki | 1:a41c274f95e2 | 47 | sw3 = new DigitalIn(s3); |
tamaki | 1:a41c274f95e2 | 48 | sw1->mode(PullUp); |
tamaki | 1:a41c274f95e2 | 49 | sw2->mode(PullUp); |
tamaki | 1:a41c274f95e2 | 50 | sw3->mode(PullUp); |
tamaki | 1:a41c274f95e2 | 51 | } |
tamaki | 1:a41c274f95e2 | 52 | |
tamaki | 1:a41c274f95e2 | 53 | void cChkSw::chksw() |
tamaki | 1:a41c274f95e2 | 54 | //switch status 0:off, 1:posEdge, 2:on, 3:negEdge |
tamaki | 1:a41c274f95e2 | 55 | { |
tamaki | 1:a41c274f95e2 | 56 | static int ssw[3], psw[3], cnt[3], tcnt[3]; |
tamaki | 1:a41c274f95e2 | 57 | int i; |
tamaki | 1:a41c274f95e2 | 58 | psw[0] = sw1->read(); |
tamaki | 1:a41c274f95e2 | 59 | psw[1] = sw2->read(); |
tamaki | 1:a41c274f95e2 | 60 | psw[2] = sw3->read(); |
tamaki | 1:a41c274f95e2 | 61 | //switch status 0:off, 1:posEdge, 2:on, 3:negEdge |
tamaki | 1:a41c274f95e2 | 62 | for(i = 0; i < 3; i++){ |
tamaki | 1:a41c274f95e2 | 63 | if((ssw[i] == 0)&&(psw[i] == 0)){ |
tamaki | 1:a41c274f95e2 | 64 | st[i] = 2;//on |
tamaki | 1:a41c274f95e2 | 65 | tcnt[i] ++; |
tamaki | 1:a41c274f95e2 | 66 | counter[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 67 | } |
tamaki | 1:a41c274f95e2 | 68 | if((ssw[i] == 0)&&(psw[i] == 1)){ |
tamaki | 1:a41c274f95e2 | 69 | st[i] = 3;//negEdge |
tamaki | 1:a41c274f95e2 | 70 | cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 71 | tcnt[i] = 0; |
tamaki | 1:a41c274f95e2 | 72 | counter[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 73 | } |
tamaki | 1:a41c274f95e2 | 74 | if((ssw[i] == 1)&&(psw[i] == 1)){ |
tamaki | 1:a41c274f95e2 | 75 | st[i] = 0;//off |
tamaki | 1:a41c274f95e2 | 76 | tcnt[i] ++; |
tamaki | 1:a41c274f95e2 | 77 | } |
tamaki | 1:a41c274f95e2 | 78 | if((ssw[i] == 1)&&(psw[i] == 0)){ |
tamaki | 1:a41c274f95e2 | 79 | st[i] = 1;//posEdge |
tamaki | 1:a41c274f95e2 | 80 | cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 81 | tcnt[i] = 0; |
tamaki | 1:a41c274f95e2 | 82 | counter[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 83 | } |
tamaki | 1:a41c274f95e2 | 84 | ssw[i] = psw[i]; |
tamaki | 1:a41c274f95e2 | 85 | } |
tamaki | 1:a41c274f95e2 | 86 | } |
tamaki | 1:a41c274f95e2 | 87 | |
tamaki | 1:a41c274f95e2 | 88 | int main() { |
tamaki | 1:a41c274f95e2 | 89 | char str[80]; |
tamaki | 1:a41c274f95e2 | 90 | |
tamaki | 1:a41c274f95e2 | 91 | |
tamaki | 1:a41c274f95e2 | 92 | lcd.cls(); |
tamaki | 1:a41c274f95e2 | 93 | lcd.locate(0,0); |
tamaki | 1:a41c274f95e2 | 94 | lcd.printf("SD Card chkpgm"); |
tamaki | 1:a41c274f95e2 | 95 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 96 | |
tamaki | 1:a41c274f95e2 | 97 | // mkdir("/sd/mydir", 0777); |
tamaki | 1:a41c274f95e2 | 98 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 99 | int counter = 0; |
tamaki | 1:a41c274f95e2 | 100 | |
tamaki | 1:a41c274f95e2 | 101 | cChkSw cs(p14,p15,p16); |
tamaki | 1:a41c274f95e2 | 102 | while(1){ |
tamaki | 1:a41c274f95e2 | 103 | cs.chksw(); |
tamaki | 1:a41c274f95e2 | 104 | led6 = 1; |
tamaki | 1:a41c274f95e2 | 105 | |
tamaki | 1:a41c274f95e2 | 106 | if(cs.st[0] == 1){ |
tamaki | 1:a41c274f95e2 | 107 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 108 | led4 = 1; |
tamaki | 1:a41c274f95e2 | 109 | counter++; |
tamaki | 1:a41c274f95e2 | 110 | FILE *fp = fopen("/sd/sdtest.txt", "w"); |
tamaki | 1:a41c274f95e2 | 111 | if(fp == NULL) { |
tamaki | 1:a41c274f95e2 | 112 | error("Could not open file for write\n"); |
tamaki | 1:a41c274f95e2 | 113 | led2 = 1; |
tamaki | 1:a41c274f95e2 | 114 | }else{ |
tamaki | 1:a41c274f95e2 | 115 | fprintf(fp, "SDcard#%d\n", counter); |
tamaki | 1:a41c274f95e2 | 116 | fclose(fp); |
tamaki | 1:a41c274f95e2 | 117 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 118 | lcd.printf(" "); |
tamaki | 1:a41c274f95e2 | 119 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 120 | lcd.printf("counter = %d", counter); |
tamaki | 1:a41c274f95e2 | 121 | led1 = 1; |
tamaki | 1:a41c274f95e2 | 122 | } |
tamaki | 1:a41c274f95e2 | 123 | } |
tamaki | 1:a41c274f95e2 | 124 | if(cs.st[1] == 1){ |
tamaki | 1:a41c274f95e2 | 125 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 126 | led5 = 1; |
tamaki | 1:a41c274f95e2 | 127 | fp = fopen("/sd/sdtest.txt", "r"); |
tamaki | 1:a41c274f95e2 | 128 | if(fp == NULL) { |
tamaki | 1:a41c274f95e2 | 129 | error("Could not open file for write\n\r"); |
tamaki | 1:a41c274f95e2 | 130 | led2 = 1; |
tamaki | 1:a41c274f95e2 | 131 | }else{ |
tamaki | 1:a41c274f95e2 | 132 | fscanf(fp, "%s\n", str);//read from SD card |
tamaki | 1:a41c274f95e2 | 133 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 134 | lcd.printf(" ");//output string to LCD |
tamaki | 1:a41c274f95e2 | 135 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 136 | lcd.printf("text = %s", str);//output string to USB serial |
tamaki | 1:a41c274f95e2 | 137 | fclose(fp); |
tamaki | 1:a41c274f95e2 | 138 | led1 = 1; |
tamaki | 1:a41c274f95e2 | 139 | } |
tamaki | 1:a41c274f95e2 | 140 | } |
tamaki | 1:a41c274f95e2 | 141 | if(cs.st[2] == 1){ |
tamaki | 1:a41c274f95e2 | 142 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 143 | led6 = 1; |
tamaki | 1:a41c274f95e2 | 144 | counter = 0; |
tamaki | 1:a41c274f95e2 | 145 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 146 | lcd.printf(" "); |
tamaki | 1:a41c274f95e2 | 147 | } |
mbed_official | 0:bdbd3d6fc5d5 | 148 | } |
tamaki | 1:a41c274f95e2 | 149 | } |
tamaki | 1:a41c274f95e2 | 150 | #endif |
tamaki | 1:a41c274f95e2 | 151 | |
tamaki | 1:a41c274f95e2 | 152 | #if C1 |
tamaki | 1:a41c274f95e2 | 153 | // |
tamaki | 1:a41c274f95e2 | 154 | // |
tamaki | 1:a41c274f95e2 | 155 | // |
tamaki | 1:a41c274f95e2 | 156 | #include "mbed.h" |
tamaki | 1:a41c274f95e2 | 157 | #include "SDFileSystem.h" |
tamaki | 1:a41c274f95e2 | 158 | #include "ACM1602NI.h" |
tamaki | 1:a41c274f95e2 | 159 | |
tamaki | 1:a41c274f95e2 | 160 | DigitalOut led1(p21); |
tamaki | 1:a41c274f95e2 | 161 | DigitalOut led2(p22); |
tamaki | 1:a41c274f95e2 | 162 | DigitalOut led3(p23); |
tamaki | 1:a41c274f95e2 | 163 | DigitalOut led4(p24); |
tamaki | 1:a41c274f95e2 | 164 | DigitalOut led5(p25); |
tamaki | 1:a41c274f95e2 | 165 | DigitalOut led6(p26); |
tamaki | 1:a41c274f95e2 | 166 | DigitalIn sw1(p14); |
tamaki | 1:a41c274f95e2 | 167 | DigitalIn sw2(p15); |
tamaki | 1:a41c274f95e2 | 168 | DigitalIn sw3(p16); |
tamaki | 1:a41c274f95e2 | 169 | AnalogIn vr(p17); |
tamaki | 1:a41c274f95e2 | 170 | |
tamaki | 1:a41c274f95e2 | 171 | FILE *fp; |
tamaki | 1:a41c274f95e2 | 172 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
tamaki | 1:a41c274f95e2 | 173 | ACM1602NI lcd(p28, p27); //sda scl |
tamaki | 1:a41c274f95e2 | 174 | |
tamaki | 1:a41c274f95e2 | 175 | //switch status 0:off, 1:posEdge, 2:on, 3:negEdge |
tamaki | 1:a41c274f95e2 | 176 | int sw_st[3]; |
tamaki | 1:a41c274f95e2 | 177 | int sw_cnt[3];//past counter |
tamaki | 1:a41c274f95e2 | 178 | |
tamaki | 1:a41c274f95e2 | 179 | void chksw(void) |
tamaki | 1:a41c274f95e2 | 180 | { |
tamaki | 1:a41c274f95e2 | 181 | static int ssw[3], psw[3], cnt[3], tcnt[3]; |
tamaki | 1:a41c274f95e2 | 182 | int i; |
tamaki | 1:a41c274f95e2 | 183 | psw[0] = sw1; |
tamaki | 1:a41c274f95e2 | 184 | psw[1] = sw2; |
tamaki | 1:a41c274f95e2 | 185 | psw[2] = sw3; |
tamaki | 1:a41c274f95e2 | 186 | //switch status 0:off, 1:posEdge, 2:on, 3:negEdge |
tamaki | 1:a41c274f95e2 | 187 | for(i = 0; i < 3; i++){ |
tamaki | 1:a41c274f95e2 | 188 | if((ssw[i] == 0)&&(psw[i] == 0)){ |
tamaki | 1:a41c274f95e2 | 189 | sw_st[i] = 2;//on |
tamaki | 1:a41c274f95e2 | 190 | tcnt[i] ++; |
tamaki | 1:a41c274f95e2 | 191 | sw_cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 192 | } |
tamaki | 1:a41c274f95e2 | 193 | if((ssw[i] == 0)&&(psw[i] == 1)){ |
tamaki | 1:a41c274f95e2 | 194 | sw_st[i] = 3;//negEdge |
tamaki | 1:a41c274f95e2 | 195 | cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 196 | tcnt[i] = 0; |
tamaki | 1:a41c274f95e2 | 197 | sw_cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 198 | } |
tamaki | 1:a41c274f95e2 | 199 | if((ssw[i] == 1)&&(psw[i] == 1)){ |
tamaki | 1:a41c274f95e2 | 200 | sw_st[i] = 0;//off |
tamaki | 1:a41c274f95e2 | 201 | tcnt[i] ++; |
tamaki | 1:a41c274f95e2 | 202 | } |
tamaki | 1:a41c274f95e2 | 203 | if((ssw[i] == 1)&&(psw[i] == 0)){ |
tamaki | 1:a41c274f95e2 | 204 | sw_st[i] = 1;//posEdge |
tamaki | 1:a41c274f95e2 | 205 | cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 206 | tcnt[i] = 0; |
tamaki | 1:a41c274f95e2 | 207 | sw_cnt[i] = tcnt[i]; |
tamaki | 1:a41c274f95e2 | 208 | } |
tamaki | 1:a41c274f95e2 | 209 | ssw[i] = psw[i]; |
tamaki | 1:a41c274f95e2 | 210 | } |
tamaki | 1:a41c274f95e2 | 211 | } |
tamaki | 1:a41c274f95e2 | 212 | |
tamaki | 1:a41c274f95e2 | 213 | int main() { |
tamaki | 1:a41c274f95e2 | 214 | char str[80]; |
tamaki | 1:a41c274f95e2 | 215 | |
tamaki | 1:a41c274f95e2 | 216 | |
tamaki | 1:a41c274f95e2 | 217 | lcd.cls(); |
tamaki | 1:a41c274f95e2 | 218 | lcd.locate(0,0); |
tamaki | 1:a41c274f95e2 | 219 | lcd.printf("SD Card chkpgm"); |
tamaki | 1:a41c274f95e2 | 220 | lcd.locate(0,1); |
mbed_official | 0:bdbd3d6fc5d5 | 221 | |
tamaki | 1:a41c274f95e2 | 222 | // mkdir("/sd/mydir", 0777); |
tamaki | 1:a41c274f95e2 | 223 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 224 | int counter = 0; |
tamaki | 1:a41c274f95e2 | 225 | |
tamaki | 1:a41c274f95e2 | 226 | while(1){ |
tamaki | 1:a41c274f95e2 | 227 | chksw(); |
tamaki | 1:a41c274f95e2 | 228 | led6 = 1; |
tamaki | 1:a41c274f95e2 | 229 | |
tamaki | 1:a41c274f95e2 | 230 | if(sw_st[0] == 1){ |
tamaki | 1:a41c274f95e2 | 231 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 232 | led4 = 1; |
tamaki | 1:a41c274f95e2 | 233 | counter++; |
tamaki | 1:a41c274f95e2 | 234 | FILE *fp = fopen("/sd/sdtest.txt", "w"); |
tamaki | 1:a41c274f95e2 | 235 | if(fp == NULL) { |
tamaki | 1:a41c274f95e2 | 236 | error("Could not open file for write\n"); |
tamaki | 1:a41c274f95e2 | 237 | led2 = 1; |
tamaki | 1:a41c274f95e2 | 238 | }else{ |
tamaki | 1:a41c274f95e2 | 239 | fprintf(fp, "SDcard#%d\n", counter); |
tamaki | 1:a41c274f95e2 | 240 | fclose(fp); |
tamaki | 1:a41c274f95e2 | 241 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 242 | lcd.printf(" "); |
tamaki | 1:a41c274f95e2 | 243 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 244 | lcd.printf("counter = %d", counter); |
tamaki | 1:a41c274f95e2 | 245 | led1 = 1; |
tamaki | 1:a41c274f95e2 | 246 | } |
tamaki | 1:a41c274f95e2 | 247 | } |
tamaki | 1:a41c274f95e2 | 248 | if(sw_st[1] == 1){ |
tamaki | 1:a41c274f95e2 | 249 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 250 | led5 = 1; |
tamaki | 1:a41c274f95e2 | 251 | fp = fopen("/sd/sdtest.txt", "r"); |
tamaki | 1:a41c274f95e2 | 252 | if(fp == NULL) { |
tamaki | 1:a41c274f95e2 | 253 | error("Could not open file for write\n\r"); |
tamaki | 1:a41c274f95e2 | 254 | led2 = 1; |
tamaki | 1:a41c274f95e2 | 255 | }else{ |
tamaki | 1:a41c274f95e2 | 256 | fscanf(fp, "%s\n", str);//read from SD card |
tamaki | 1:a41c274f95e2 | 257 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 258 | lcd.printf(" ");//output string to LCD |
tamaki | 1:a41c274f95e2 | 259 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 260 | lcd.printf("text = %s", str);//output string to USB serial |
tamaki | 1:a41c274f95e2 | 261 | fclose(fp); |
tamaki | 1:a41c274f95e2 | 262 | led1 = 1; |
tamaki | 1:a41c274f95e2 | 263 | } |
tamaki | 1:a41c274f95e2 | 264 | } |
tamaki | 1:a41c274f95e2 | 265 | if(sw_st[2] == 1){ |
tamaki | 1:a41c274f95e2 | 266 | led1=led2=led3=led4=led5=led6 = 0; |
tamaki | 1:a41c274f95e2 | 267 | led6 = 1; |
tamaki | 1:a41c274f95e2 | 268 | counter = 0; |
tamaki | 1:a41c274f95e2 | 269 | lcd.locate(0,1); |
tamaki | 1:a41c274f95e2 | 270 | lcd.printf(" "); |
tamaki | 1:a41c274f95e2 | 271 | } |
tamaki | 1:a41c274f95e2 | 272 | } |
mbed_official | 0:bdbd3d6fc5d5 | 273 | } |
tamaki | 1:a41c274f95e2 | 274 | #endif |