![](/media/cache/profiles/f0fcf351df4eb6786e9bb6fc4e2dee02.jpg.50x50_q85.jpg)
SD-Card Control Program / Using Micro-SD / based on SDCardTest Program (http://mbed.org/users/simon/programs/SDCardTest/gpdz4x/)
Dependencies: mbed SDFileSystem
Please refer following my Notebook page.
/users/kenjiArai/notebook/sd-card-control-new/#
org_main.cpp@7:4467fbfa888b, 2020-05-02 (annotated)
- Committer:
- kenjiArai
- Date:
- Sat May 02 03:45:17 2020 +0000
- Revision:
- 7:4467fbfa888b
- Parent:
- 2:1397a54382ec
changed starting process(wait key input)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 2:1397a54382ec | 1 | #if 0 |
kenjiArai | 2:1397a54382ec | 2 | /* |
kenjiArai | 2:1397a54382ec | 3 | * mbed Application program |
kenjiArai | 2:1397a54382ec | 4 | * SD-Card Control Program |
kenjiArai | 2:1397a54382ec | 5 | * |
kenjiArai | 2:1397a54382ec | 6 | * Copyright (c) 2010-2014 Kenji Arai / JH1PJL |
kenjiArai | 2:1397a54382ec | 7 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 2:1397a54382ec | 8 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 2:1397a54382ec | 9 | * March 28th, 2010 Started |
kenjiArai | 2:1397a54382ec | 10 | * April 3rd, 2010 |
kenjiArai | 2:1397a54382ec | 11 | * October 3rd, 2014 use latest lib. |
kenjiArai | 2:1397a54382ec | 12 | * |
kenjiArai | 2:1397a54382ec | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
kenjiArai | 2:1397a54382ec | 14 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
kenjiArai | 2:1397a54382ec | 15 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 2:1397a54382ec | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 2:1397a54382ec | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 2:1397a54382ec | 18 | */ |
kenjiArai | 2:1397a54382ec | 19 | |
kenjiArai | 2:1397a54382ec | 20 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 21 | // Function |
kenjiArai | 2:1397a54382ec | 22 | // 5 channeles ADC data records into a file which is located in SD-Card |
kenjiArai | 2:1397a54382ec | 23 | // If USE_LCD = 1, data shows on a Text LCD |
kenjiArai | 2:1397a54382ec | 24 | // If USE_RTC = 1, time stamp also writes in the file |
kenjiArai | 2:1397a54382ec | 25 | // Connection |
kenjiArai | 2:1397a54382ec | 26 | // Analog input PIN 15,16,17,19,20 |
kenjiArai | 2:1397a54382ec | 27 | // LCD PIN 22,21,8,7,6,5 |
kenjiArai | 2:1397a54382ec | 28 | // -> CAUTION!! pin assignment is different |
kenjiArai | 2:1397a54382ec | 29 | // with " http://mbed.org/projects/cookbook/wiki/TextLCD " |
kenjiArai | 2:1397a54382ec | 30 | // RTC PIN 3 needs to connect 3V Battery |
kenjiArai | 2:1397a54382ec | 31 | // -> Please refer my program " RTC_w_COM" for time adjustment |
kenjiArai | 2:1397a54382ec | 32 | // at " http://mbed.org/users/kenjiArai/programs/RTC_w_COM/5yi9a/ " |
kenjiArai | 2:1397a54382ec | 33 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 34 | |
kenjiArai | 2:1397a54382ec | 35 | // Include --------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 36 | #include "mbed.h" |
kenjiArai | 2:1397a54382ec | 37 | #include "TextLCD.h" |
kenjiArai | 2:1397a54382ec | 38 | #include "SDFileSystem.h" |
kenjiArai | 2:1397a54382ec | 39 | |
kenjiArai | 2:1397a54382ec | 40 | // Definition ------------------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 41 | #define DEBUG 1 // 1= Shows progress on PC via USB ( virtual COM line) |
kenjiArai | 2:1397a54382ec | 42 | #define USE_LCD 1 // 1= Display the data on LCD |
kenjiArai | 2:1397a54382ec | 43 | #define USE_RTC 1 // 1= Use RTC (need 3V supply and time adjustment before use) |
kenjiArai | 2:1397a54382ec | 44 | |
kenjiArai | 2:1397a54382ec | 45 | #define NO_OF_SAMPLE 100 // Total recording length -> unit=sec |
kenjiArai | 2:1397a54382ec | 46 | #define TIM_INTVL 10 // Insert time stamp in the file every ?? sec |
kenjiArai | 2:1397a54382ec | 47 | |
kenjiArai | 2:1397a54382ec | 48 | // Object ---------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 49 | DigitalOut myled(LED1); // Indicate the sampling period |
kenjiArai | 2:1397a54382ec | 50 | #if USE_LCD |
kenjiArai | 2:1397a54382ec | 51 | TextLCD lcd(p22, p21, p8, p7, p6, p5, TextLCD::LCD40x2); // rs, e, d4-d7 |
kenjiArai | 2:1397a54382ec | 52 | #endif |
kenjiArai | 2:1397a54382ec | 53 | AnalogIn ain_G_X(p15); // G Sensor |
kenjiArai | 2:1397a54382ec | 54 | AnalogIn ain_G_Y(p16); // G Sensor |
kenjiArai | 2:1397a54382ec | 55 | AnalogIn ain_G_Z(p17); // G Sensor |
kenjiArai | 2:1397a54382ec | 56 | AnalogIn ain_BAT(p19); // Battery Volt |
kenjiArai | 2:1397a54382ec | 57 | AnalogIn ain_TEMP(p20); // Temperature Sensor |
kenjiArai | 2:1397a54382ec | 58 | SDFileSystem sd(p11, p12, p13, p14, "sd"); // do,di,clk,cs |
kenjiArai | 2:1397a54382ec | 59 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 60 | Serial pc(USBTX, USBRX); |
kenjiArai | 2:1397a54382ec | 61 | #endif |
kenjiArai | 2:1397a54382ec | 62 | |
kenjiArai | 2:1397a54382ec | 63 | // RAM ------------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 64 | |
kenjiArai | 2:1397a54382ec | 65 | // ROM / Constant data --------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 66 | |
kenjiArai | 2:1397a54382ec | 67 | // Function prototypes --------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 68 | |
kenjiArai | 2:1397a54382ec | 69 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 70 | // Control Program |
kenjiArai | 2:1397a54382ec | 71 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 72 | int main() |
kenjiArai | 2:1397a54382ec | 73 | { |
kenjiArai | 2:1397a54382ec | 74 | char buf[40]; // data buffer for text |
kenjiArai | 2:1397a54382ec | 75 | int count; // count for number of record |
kenjiArai | 2:1397a54382ec | 76 | float x,y,z,b,t; // Analog data |
kenjiArai | 2:1397a54382ec | 77 | #if USE_RTC |
kenjiArai | 2:1397a54382ec | 78 | time_t seconds, old_sec; // RTC data based on seconds |
kenjiArai | 2:1397a54382ec | 79 | int i; |
kenjiArai | 2:1397a54382ec | 80 | #endif |
kenjiArai | 2:1397a54382ec | 81 | |
kenjiArai | 2:1397a54382ec | 82 | // Open the file |
kenjiArai | 2:1397a54382ec | 83 | #if USE_RTC |
kenjiArai | 2:1397a54382ec | 84 | seconds = time(NULL); |
kenjiArai | 2:1397a54382ec | 85 | seconds %= 100000000; // Adjust 8 charcters file name |
kenjiArai | 2:1397a54382ec | 86 | sprintf(buf,"/sd/%d.txt",seconds); // File name is defined based on time from 1970/1/1 |
kenjiArai | 2:1397a54382ec | 87 | FILE *fp = fopen(buf, "w"); // Open "out.txt" on the local file system for writing |
kenjiArai | 2:1397a54382ec | 88 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 89 | printf("\r\n %s \r\n", buf); // File name on the screen |
kenjiArai | 2:1397a54382ec | 90 | printf(" use RTC\r\n"); |
kenjiArai | 2:1397a54382ec | 91 | #endif |
kenjiArai | 2:1397a54382ec | 92 | #else |
kenjiArai | 2:1397a54382ec | 93 | FILE *fp = fopen("/sd/out.txt", "w");// Open "out.txt" on the local file system for writing |
kenjiArai | 2:1397a54382ec | 94 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 95 | printf("\r\n /sd/out.txt\r\n"); // File name on the screen |
kenjiArai | 2:1397a54382ec | 96 | printf(" Not use RTC\r\n"); |
kenjiArai | 2:1397a54382ec | 97 | #endif |
kenjiArai | 2:1397a54382ec | 98 | #endif |
kenjiArai | 2:1397a54382ec | 99 | if(fp == NULL) { |
kenjiArai | 2:1397a54382ec | 100 | // File not open and stop |
kenjiArai | 2:1397a54382ec | 101 | #if USE_LCD |
kenjiArai | 2:1397a54382ec | 102 | lcd.printf(" Could not open file for write\n"); |
kenjiArai | 2:1397a54382ec | 103 | #endif |
kenjiArai | 2:1397a54382ec | 104 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 105 | printf( "\r\n Could not open file for write\r\n"); |
kenjiArai | 2:1397a54382ec | 106 | #endif |
kenjiArai | 2:1397a54382ec | 107 | while(1) ; |
kenjiArai | 2:1397a54382ec | 108 | } |
kenjiArai | 2:1397a54382ec | 109 | // Success file access |
kenjiArai | 2:1397a54382ec | 110 | fprintf(fp, "This is a test program for logging /by JH1PJL\r\n"); |
kenjiArai | 2:1397a54382ec | 111 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 112 | printf( "This is a test program for logging /by JH1PJL\r\n"); |
kenjiArai | 2:1397a54382ec | 113 | printf("\r\nStart sampling\r\n"); |
kenjiArai | 2:1397a54382ec | 114 | #endif |
kenjiArai | 2:1397a54382ec | 115 | #if USE_LCD |
kenjiArai | 2:1397a54382ec | 116 | lcd.cls(); |
kenjiArai | 2:1397a54382ec | 117 | #endif |
kenjiArai | 2:1397a54382ec | 118 | |
kenjiArai | 2:1397a54382ec | 119 | count = 0; // Initialze counter |
kenjiArai | 2:1397a54382ec | 120 | #if USE_RTC |
kenjiArai | 2:1397a54382ec | 121 | seconds = time(NULL); // Put time stamp in the file |
kenjiArai | 2:1397a54382ec | 122 | old_sec = seconds; |
kenjiArai | 2:1397a54382ec | 123 | strftime(buf,40, "Time:%I:%M:%S %p, %Y/%m/%d\r\n", localtime(&seconds)); |
kenjiArai | 2:1397a54382ec | 124 | fprintf(fp,buf); |
kenjiArai | 2:1397a54382ec | 125 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 126 | printf(" %s \r\n", buf); |
kenjiArai | 2:1397a54382ec | 127 | #endif |
kenjiArai | 2:1397a54382ec | 128 | #endif |
kenjiArai | 2:1397a54382ec | 129 | while(1) { |
kenjiArai | 2:1397a54382ec | 130 | // check time interval (1sec) |
kenjiArai | 2:1397a54382ec | 131 | #if USE_RTC |
kenjiArai | 2:1397a54382ec | 132 | myled = 1; |
kenjiArai | 2:1397a54382ec | 133 | wait(0.5); |
kenjiArai | 2:1397a54382ec | 134 | myled = 0; |
kenjiArai | 2:1397a54382ec | 135 | while ((seconds = time(NULL)) == old_sec) ; // Wait 1 sec for loop |
kenjiArai | 2:1397a54382ec | 136 | old_sec = seconds; |
kenjiArai | 2:1397a54382ec | 137 | #else |
kenjiArai | 2:1397a54382ec | 138 | myled = 1; |
kenjiArai | 2:1397a54382ec | 139 | wait(0.5); |
kenjiArai | 2:1397a54382ec | 140 | myled = 0; |
kenjiArai | 2:1397a54382ec | 141 | wait(0.5); |
kenjiArai | 2:1397a54382ec | 142 | #endif |
kenjiArai | 2:1397a54382ec | 143 | // Get analog data from each port |
kenjiArai | 2:1397a54382ec | 144 | x=ain_G_X.read(); |
kenjiArai | 2:1397a54382ec | 145 | y=ain_G_Y.read(); |
kenjiArai | 2:1397a54382ec | 146 | z=ain_G_Z.read(); |
kenjiArai | 2:1397a54382ec | 147 | b=ain_BAT.read(); |
kenjiArai | 2:1397a54382ec | 148 | t=ain_TEMP.read(); |
kenjiArai | 2:1397a54382ec | 149 | // Write data into the file |
kenjiArai | 2:1397a54382ec | 150 | sprintf(buf, "G-Sen, %f, %f, %f \r\n", x, y, z); |
kenjiArai | 2:1397a54382ec | 151 | fprintf(fp,buf); |
kenjiArai | 2:1397a54382ec | 152 | #if USE_LCD |
kenjiArai | 2:1397a54382ec | 153 | lcd.locate(0, 0); // 1st line top |
kenjiArai | 2:1397a54382ec | 154 | lcd.printf(buf); |
kenjiArai | 2:1397a54382ec | 155 | #endif |
kenjiArai | 2:1397a54382ec | 156 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 157 | printf(" %s", buf); |
kenjiArai | 2:1397a54382ec | 158 | #endif |
kenjiArai | 2:1397a54382ec | 159 | sprintf(buf, "VB, %f, T, %f \r\n", b, t); |
kenjiArai | 2:1397a54382ec | 160 | fprintf(fp,buf); |
kenjiArai | 2:1397a54382ec | 161 | #if USE_LCD |
kenjiArai | 2:1397a54382ec | 162 | lcd.locate(0, 1); // 2nd line top |
kenjiArai | 2:1397a54382ec | 163 | lcd.printf(buf); |
kenjiArai | 2:1397a54382ec | 164 | #endif |
kenjiArai | 2:1397a54382ec | 165 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 166 | printf(" %s", buf); |
kenjiArai | 2:1397a54382ec | 167 | #endif |
kenjiArai | 2:1397a54382ec | 168 | // if reach to expected data number then finsh |
kenjiArai | 2:1397a54382ec | 169 | if (++count > NO_OF_SAMPLE) { |
kenjiArai | 2:1397a54382ec | 170 | break; |
kenjiArai | 2:1397a54382ec | 171 | } |
kenjiArai | 2:1397a54382ec | 172 | // Set time satmp |
kenjiArai | 2:1397a54382ec | 173 | #if USE_RTC |
kenjiArai | 2:1397a54382ec | 174 | i = count / TIM_INTVL; |
kenjiArai | 2:1397a54382ec | 175 | if (count == i * TIM_INTVL) { |
kenjiArai | 2:1397a54382ec | 176 | //seconds = time(NULL); // this line is wrong! BUG |
kenjiArai | 2:1397a54382ec | 177 | strftime(buf,40, "Time:%I:%M:%S %p, %Y/%m/%d\r\n", localtime(&seconds)); |
kenjiArai | 2:1397a54382ec | 178 | fprintf(fp, buf); |
kenjiArai | 2:1397a54382ec | 179 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 180 | printf(" %s", buf); |
kenjiArai | 2:1397a54382ec | 181 | #endif |
kenjiArai | 2:1397a54382ec | 182 | } |
kenjiArai | 2:1397a54382ec | 183 | #endif |
kenjiArai | 2:1397a54382ec | 184 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 185 | printf("Sampling #%d/end=%d\r\n", count, NO_OF_SAMPLE); |
kenjiArai | 2:1397a54382ec | 186 | #endif |
kenjiArai | 2:1397a54382ec | 187 | } |
kenjiArai | 2:1397a54382ec | 188 | // Set time satmp |
kenjiArai | 2:1397a54382ec | 189 | #if USE_RTC |
kenjiArai | 2:1397a54382ec | 190 | sprintf(buf,"Sampling numbers: %d\r\n", count-1); |
kenjiArai | 2:1397a54382ec | 191 | fprintf(fp, buf); |
kenjiArai | 2:1397a54382ec | 192 | seconds = time(NULL); |
kenjiArai | 2:1397a54382ec | 193 | strftime(buf,40, "Time:%I:%M:%S %p, %Y/%m/%d\r\n", localtime(&seconds)); |
kenjiArai | 2:1397a54382ec | 194 | fprintf(fp, buf); |
kenjiArai | 2:1397a54382ec | 195 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 196 | printf(" %s", buf); |
kenjiArai | 2:1397a54382ec | 197 | #endif |
kenjiArai | 2:1397a54382ec | 198 | #endif |
kenjiArai | 2:1397a54382ec | 199 | fclose(fp); |
kenjiArai | 2:1397a54382ec | 200 | // for debug |
kenjiArai | 2:1397a54382ec | 201 | #if DEBUG |
kenjiArai | 2:1397a54382ec | 202 | printf("\r\nFinished sampling\r\n"); |
kenjiArai | 2:1397a54382ec | 203 | #endif |
kenjiArai | 2:1397a54382ec | 204 | } |
kenjiArai | 2:1397a54382ec | 205 | #endif |