/*############################################################# Program Name : ex3_F446RE_SDCardLoggerWrite Author : Grant Phillips Date Modified : 19/09/2016 Compiler : ARMmbed Tested On : NUCLEO-F446RE Description : Example program that demonstrates the use of a SDCard with the NUCLEO board. Everytime the button is pushed, the current time and analog input value gets logged to a file. Requirements : * NUCLEO-F446RE Board Circuit : * a potentiometer (POT) connected to PC0 * the USER pushbutton connected to PC13 * The SDCard connected as: VCC - +3V or +5V (depending on module) GND - GND SS/CS - PB_6 MOSI - PB_5 (SPI3_MOSI) MISO - PB_4 (SPI3_MISO) SCK - PB_3 (SPI3_SCK) ##############################################################*/ #include "mbed.h" #include "SDFileSystem.h" //Niel Thiessen's library Serial pc(SERIAL_TX, SERIAL_RX); //to use the PC as a console (display output) DigitalOut myled(LED1); //LED to indicate system operation SDFileSystem sd(PB_5, PB_4, PB_3, PB_6, "sd"); //create a SDFileSystem object // mosi, miso, sck , cs , name AnalogIn ain(PC_0); //analog input pin DigitalIn pb(PC_13); //USER pushbutton int main() { char date[32]; //string for storing the current date char timeofday[32]; //string for storing the current time time_t seconds; //time_t variable to read the number of seconds since January 1, 1970 for time calculations struct tm t; //time structure for setting the time //set time structure to 20 Sep 2016 00:31:01 t.tm_year = 2016 - 1900; t.tm_mon = 9 - 1; t.tm_mday = 20; t.tm_hour = 0; t.tm_min = 31; t.tm_sec = 1; //use the time structure and set the real-time clock (RTC) time set_time(mktime(&t)); pc.printf("ex3_F446RE_SDCardLoggerWrite\n\n"); pc.printf("Press button to log a analog input value...\n"); while(1) { if(pb == 0) { //when the button is pressed... FILE *fp = fopen("/sd/log.txt", "a"); //open a file for appending/adding("a"); if the file doesn't exist //it will be created; if it exists text will be added/appended to the //end of the file if(fp == NULL) { //if error opening printf("Could not open file for write!\n"); } else { seconds = time(NULL); //read the new time value strftime(date, 32, "%d/%m/%y", localtime(&seconds)); //create a date string from the time value strftime(timeofday, 32, "%H:%M:%S", localtime(&seconds)); //create a time string from the time value fprintf(fp, "%s %s %d\r\n", date, timeofday, ain.read_u16()); //write the date string, time string, and analog integer value to the file //the /r/n combination puts the entries on different lines in the file fclose(fp); //close the file to prevent corrupt files pc.printf("Temperature recorded.\n"); wait(0.5); //delay to debounce the pushbutton } } myled = !myled; //flash led wait(0.1); } }