Recording WAV files

Dependencies:   mbed SDFileSystem

Committer:
kenjiArai
Date:
Sun Apr 04 00:42:58 2010 +0000
Revision:
0:3bd1cdc1f3f4
Child:
1:484feaf2da84

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:3bd1cdc1f3f4 1 //-----------------------------------------------------------------------------------------------------------
kenjiArai 0:3bd1cdc1f3f4 2 // SD-Card Control Program
kenjiArai 0:3bd1cdc1f3f4 3 // (c)2010 Kenji Arai / JH1PJL
kenjiArai 0:3bd1cdc1f3f4 4 // http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:3bd1cdc1f3f4 5 // March 28th,2010 Started
kenjiArai 0:3bd1cdc1f3f4 6 // April 3rd,2010
kenjiArai 0:3bd1cdc1f3f4 7 //-----------------------------------------------------------------------------------------------------------
kenjiArai 0:3bd1cdc1f3f4 8 // Function
kenjiArai 0:3bd1cdc1f3f4 9 // 5 channeles ADC data records into a file which is located in SD-Card
kenjiArai 0:3bd1cdc1f3f4 10 // If USE_LCD = 1, data shows on a Text LCD
kenjiArai 0:3bd1cdc1f3f4 11 // If USE_RTC = 1, time stamp also writes in the file
kenjiArai 0:3bd1cdc1f3f4 12 // Connection
kenjiArai 0:3bd1cdc1f3f4 13 // Analog input PIN 15,16,17,19,20
kenjiArai 0:3bd1cdc1f3f4 14 // LCD PIN 22,23,24,25,26,27,28
kenjiArai 0:3bd1cdc1f3f4 15 // -> CAUTION!! pin assignment is different
kenjiArai 0:3bd1cdc1f3f4 16 // with " http://mbed.org/projects/cookbook/wiki/TextLCD "
kenjiArai 0:3bd1cdc1f3f4 17 // RTC PIN 3 needs to connect 3V Battery
kenjiArai 0:3bd1cdc1f3f4 18 // -> Please refer my program " RTC_w_COM" for time adjustment
kenjiArai 0:3bd1cdc1f3f4 19 // at " http://mbed.org/users/kenjiArai/programs/RTC_w_COM/5yi9a/ "
kenjiArai 0:3bd1cdc1f3f4 20 //-----------------------------------------------------------------------------------------------------------
kenjiArai 0:3bd1cdc1f3f4 21 #include "mbed.h"
kenjiArai 0:3bd1cdc1f3f4 22 #include "TextLCD.h"
kenjiArai 0:3bd1cdc1f3f4 23 #include "SDFileSystem.h"
kenjiArai 0:3bd1cdc1f3f4 24
kenjiArai 0:3bd1cdc1f3f4 25 #define DEBUG 1 // 1= Shows progress on PC via USB ( virtual COM line)
kenjiArai 0:3bd1cdc1f3f4 26 #define USE_LCD 1 // 1= Display the data on LCD
kenjiArai 0:3bd1cdc1f3f4 27 #define USE_RTC 1 // 1= Use RTC (need 3V supply and time adjustment before use)
kenjiArai 0:3bd1cdc1f3f4 28
kenjiArai 0:3bd1cdc1f3f4 29 #define NO_OF_SAMPLE 100 // Total recording length -> unit=sec
kenjiArai 0:3bd1cdc1f3f4 30 #define TIM_INTVL 10 // Insert time stamp in the file every ?? sec
kenjiArai 0:3bd1cdc1f3f4 31
kenjiArai 0:3bd1cdc1f3f4 32 DigitalOut myled(LED1); // Indicate the sampling period
kenjiArai 0:3bd1cdc1f3f4 33
kenjiArai 0:3bd1cdc1f3f4 34 #if USE_LCD
kenjiArai 0:3bd1cdc1f3f4 35 TextLCD lcd(p22, p28, p27, p26, p25, p24, p23, 40, 2); // rs,rw,e,d0,d1,d2,d3,40char's x 2 lines
kenjiArai 0:3bd1cdc1f3f4 36 #endif
kenjiArai 0:3bd1cdc1f3f4 37
kenjiArai 0:3bd1cdc1f3f4 38 AnalogIn ain_G_X(p15); // G Sensor
kenjiArai 0:3bd1cdc1f3f4 39 AnalogIn ain_G_Y(p16); // G Sensor
kenjiArai 0:3bd1cdc1f3f4 40 AnalogIn ain_G_Z(p17); // G Sensor
kenjiArai 0:3bd1cdc1f3f4 41 AnalogIn ain_BAT(p19); // Battery Volt
kenjiArai 0:3bd1cdc1f3f4 42 AnalogIn ain_TEMP(p20); // Temperature Sensor
kenjiArai 0:3bd1cdc1f3f4 43
kenjiArai 0:3bd1cdc1f3f4 44 SDFileSystem sd(p5, p6, p7, p8, "sd");
kenjiArai 0:3bd1cdc1f3f4 45
kenjiArai 0:3bd1cdc1f3f4 46 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 47 Serial pc(USBTX, USBRX);
kenjiArai 0:3bd1cdc1f3f4 48 #endif
kenjiArai 0:3bd1cdc1f3f4 49
kenjiArai 0:3bd1cdc1f3f4 50 int main() {
kenjiArai 0:3bd1cdc1f3f4 51 char buf[40]; // data buffer for text
kenjiArai 0:3bd1cdc1f3f4 52 int count; // count for number of record
kenjiArai 0:3bd1cdc1f3f4 53 float x,y,z,b,t; // Analog data
kenjiArai 0:3bd1cdc1f3f4 54 #if USE_RTC
kenjiArai 0:3bd1cdc1f3f4 55 time_t seconds, old_sec; // RTC data based on seconds
kenjiArai 0:3bd1cdc1f3f4 56 int i;
kenjiArai 0:3bd1cdc1f3f4 57 #endif
kenjiArai 0:3bd1cdc1f3f4 58
kenjiArai 0:3bd1cdc1f3f4 59 // Open the file
kenjiArai 0:3bd1cdc1f3f4 60 #if USE_RTC
kenjiArai 0:3bd1cdc1f3f4 61 seconds = time(NULL);
kenjiArai 0:3bd1cdc1f3f4 62 seconds %= 100000000; // Adjust 8 charcters file name
kenjiArai 0:3bd1cdc1f3f4 63 sprintf(buf,"/sd/%d.txt",seconds); // File name is defined based on time from 1970/1/1
kenjiArai 0:3bd1cdc1f3f4 64 FILE *fp = fopen(buf, "w"); // Open "out.txt" on the local file system for writing
kenjiArai 0:3bd1cdc1f3f4 65 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 66 printf("\r\n %s \r\n", buf); // File name on the screen
kenjiArai 0:3bd1cdc1f3f4 67 printf(" use RTC\r\n");
kenjiArai 0:3bd1cdc1f3f4 68 #endif
kenjiArai 0:3bd1cdc1f3f4 69 #else
kenjiArai 0:3bd1cdc1f3f4 70 FILE *fp = fopen("/sd/out.txt", "w");// Open "out.txt" on the local file system for writing
kenjiArai 0:3bd1cdc1f3f4 71 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 72 printf("\r\n /sd/out.txt\r\n"); // File name on the screen
kenjiArai 0:3bd1cdc1f3f4 73 printf(" Not use RTC\r\n");
kenjiArai 0:3bd1cdc1f3f4 74 #endif
kenjiArai 0:3bd1cdc1f3f4 75 #endif
kenjiArai 0:3bd1cdc1f3f4 76 if(fp == NULL) {
kenjiArai 0:3bd1cdc1f3f4 77 // File not open and stop
kenjiArai 0:3bd1cdc1f3f4 78 #if USE_LCD
kenjiArai 0:3bd1cdc1f3f4 79 lcd.printf(" Could not open file for write\n");
kenjiArai 0:3bd1cdc1f3f4 80 #endif
kenjiArai 0:3bd1cdc1f3f4 81 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 82 printf( "\r\n Could not open file for write\r\n");
kenjiArai 0:3bd1cdc1f3f4 83 #endif
kenjiArai 0:3bd1cdc1f3f4 84 while(1) ;
kenjiArai 0:3bd1cdc1f3f4 85 }
kenjiArai 0:3bd1cdc1f3f4 86 // Success file access
kenjiArai 0:3bd1cdc1f3f4 87 fprintf(fp, "This is a test program for logging /by JH1PJL\r\n");
kenjiArai 0:3bd1cdc1f3f4 88 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 89 printf( "This is a test program for logging /by JH1PJL\r\n");
kenjiArai 0:3bd1cdc1f3f4 90 printf("\r\nStart sampling\r\n");
kenjiArai 0:3bd1cdc1f3f4 91 #endif
kenjiArai 0:3bd1cdc1f3f4 92 #if USE_LCD
kenjiArai 0:3bd1cdc1f3f4 93 lcd.cls();
kenjiArai 0:3bd1cdc1f3f4 94 #endif
kenjiArai 0:3bd1cdc1f3f4 95
kenjiArai 0:3bd1cdc1f3f4 96 count = 0; // Initialze counter
kenjiArai 0:3bd1cdc1f3f4 97 #if USE_RTC
kenjiArai 0:3bd1cdc1f3f4 98 seconds = time(NULL); // Put time stamp in the file
kenjiArai 0:3bd1cdc1f3f4 99 old_sec = seconds;
kenjiArai 0:3bd1cdc1f3f4 100 strftime(buf,40, "Time:%I:%M:%S %p, %Y/%m/%d\r\n", localtime(&seconds));
kenjiArai 0:3bd1cdc1f3f4 101 fprintf(fp,buf);
kenjiArai 0:3bd1cdc1f3f4 102 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 103 printf(" %s \r\n", buf);
kenjiArai 0:3bd1cdc1f3f4 104 #endif
kenjiArai 0:3bd1cdc1f3f4 105 #endif
kenjiArai 0:3bd1cdc1f3f4 106 while(1) {
kenjiArai 0:3bd1cdc1f3f4 107 // check time interval (1sec)
kenjiArai 0:3bd1cdc1f3f4 108 #if USE_RTC
kenjiArai 0:3bd1cdc1f3f4 109 myled = 1;
kenjiArai 0:3bd1cdc1f3f4 110 wait(0.5);
kenjiArai 0:3bd1cdc1f3f4 111 myled = 0;
kenjiArai 0:3bd1cdc1f3f4 112 while ((seconds = time(NULL)) == old_sec) ; // Wait 1 sec for loop
kenjiArai 0:3bd1cdc1f3f4 113 old_sec = seconds;
kenjiArai 0:3bd1cdc1f3f4 114 #else
kenjiArai 0:3bd1cdc1f3f4 115 myled = 1;
kenjiArai 0:3bd1cdc1f3f4 116 wait(0.5);
kenjiArai 0:3bd1cdc1f3f4 117 myled = 0;
kenjiArai 0:3bd1cdc1f3f4 118 wait(0.5);
kenjiArai 0:3bd1cdc1f3f4 119 #endif
kenjiArai 0:3bd1cdc1f3f4 120 // Get analog data from each port
kenjiArai 0:3bd1cdc1f3f4 121 x=ain_G_X.read();
kenjiArai 0:3bd1cdc1f3f4 122 y=ain_G_Y.read();
kenjiArai 0:3bd1cdc1f3f4 123 z=ain_G_Z.read();
kenjiArai 0:3bd1cdc1f3f4 124 b=ain_BAT.read();
kenjiArai 0:3bd1cdc1f3f4 125 t=ain_TEMP.read();
kenjiArai 0:3bd1cdc1f3f4 126 // Write data into the file
kenjiArai 0:3bd1cdc1f3f4 127 sprintf(buf, "G-Sen, %f, %f, %f \r\n", x, y, z);
kenjiArai 0:3bd1cdc1f3f4 128 fprintf(fp,buf);
kenjiArai 0:3bd1cdc1f3f4 129 #if USE_LCD
kenjiArai 0:3bd1cdc1f3f4 130 lcd.locate(0, 0); // 1st line top
kenjiArai 0:3bd1cdc1f3f4 131 lcd.printf(buf);
kenjiArai 0:3bd1cdc1f3f4 132 #endif
kenjiArai 0:3bd1cdc1f3f4 133 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 134 printf(" %s", buf);
kenjiArai 0:3bd1cdc1f3f4 135 #endif
kenjiArai 0:3bd1cdc1f3f4 136 sprintf(buf, "VB, %f, T, %f \r\n", b, t);
kenjiArai 0:3bd1cdc1f3f4 137 fprintf(fp,buf);
kenjiArai 0:3bd1cdc1f3f4 138 #if USE_LCD
kenjiArai 0:3bd1cdc1f3f4 139 lcd.locate(0, 1); // 2nd line top
kenjiArai 0:3bd1cdc1f3f4 140 lcd.printf(buf);
kenjiArai 0:3bd1cdc1f3f4 141 #endif
kenjiArai 0:3bd1cdc1f3f4 142 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 143 printf(" %s", buf);
kenjiArai 0:3bd1cdc1f3f4 144 #endif
kenjiArai 0:3bd1cdc1f3f4 145 // if reach to expected data number then finsh
kenjiArai 0:3bd1cdc1f3f4 146 if (++count > NO_OF_SAMPLE){
kenjiArai 0:3bd1cdc1f3f4 147 break;
kenjiArai 0:3bd1cdc1f3f4 148 }
kenjiArai 0:3bd1cdc1f3f4 149 // Set time satmp
kenjiArai 0:3bd1cdc1f3f4 150 #if USE_RTC
kenjiArai 0:3bd1cdc1f3f4 151 i = count / TIM_INTVL;
kenjiArai 0:3bd1cdc1f3f4 152 if (count == i * TIM_INTVL){
kenjiArai 0:3bd1cdc1f3f4 153 //seconds = time(NULL); // this line is wrong! BUG
kenjiArai 0:3bd1cdc1f3f4 154 strftime(buf,40, "Time:%I:%M:%S %p, %Y/%m/%d\r\n", localtime(&seconds));
kenjiArai 0:3bd1cdc1f3f4 155 fprintf(fp, buf);
kenjiArai 0:3bd1cdc1f3f4 156 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 157 printf(" %s", buf);
kenjiArai 0:3bd1cdc1f3f4 158 #endif
kenjiArai 0:3bd1cdc1f3f4 159 }
kenjiArai 0:3bd1cdc1f3f4 160 #endif
kenjiArai 0:3bd1cdc1f3f4 161 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 162 printf("Sampling #%d/end=%d\r\n", count, NO_OF_SAMPLE);
kenjiArai 0:3bd1cdc1f3f4 163 #endif
kenjiArai 0:3bd1cdc1f3f4 164 }
kenjiArai 0:3bd1cdc1f3f4 165 // Set time satmp
kenjiArai 0:3bd1cdc1f3f4 166 #if USE_RTC
kenjiArai 0:3bd1cdc1f3f4 167 sprintf(buf,"Sampling numbers: %d\r\n", count-1);
kenjiArai 0:3bd1cdc1f3f4 168 fprintf(fp, buf);
kenjiArai 0:3bd1cdc1f3f4 169 seconds = time(NULL);
kenjiArai 0:3bd1cdc1f3f4 170 strftime(buf,40, "Time:%I:%M:%S %p, %Y/%m/%d\r\n", localtime(&seconds));
kenjiArai 0:3bd1cdc1f3f4 171 fprintf(fp, buf);
kenjiArai 0:3bd1cdc1f3f4 172 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 173 printf(" %s", buf);
kenjiArai 0:3bd1cdc1f3f4 174 #endif
kenjiArai 0:3bd1cdc1f3f4 175 #endif
kenjiArai 0:3bd1cdc1f3f4 176 fclose(fp);
kenjiArai 0:3bd1cdc1f3f4 177 // for debug
kenjiArai 0:3bd1cdc1f3f4 178 #if DEBUG
kenjiArai 0:3bd1cdc1f3f4 179 printf("\r\nFinished sampling\r\n");
kenjiArai 0:3bd1cdc1f3f4 180 #endif
kenjiArai 0:3bd1cdc1f3f4 181 }
kenjiArai 0:3bd1cdc1f3f4 182