Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ELEC351 by
SD_CARD.cpp@49:d51f96a46cc3, 2018-01-09 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Jan 09 11:41:07 2018 +0000
- Revision:
- 49:d51f96a46cc3
- Parent:
- 48:244d6d81bb52
- Child:
- 50:3d61ca637399
SD Card Working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 47:6d128e500875 | 1 | #include "SD_CARD.hpp" |
thomasmorris | 48:244d6d81bb52 | 2 | FILE* fp; |
thomasmorris | 48:244d6d81bb52 | 3 | InterruptIn SD_CARD_DETECT(D0); |
thomasmorris | 49:d51f96a46cc3 | 4 | |
thomasmorris | 49:d51f96a46cc3 | 5 | |
thomasmorris | 48:244d6d81bb52 | 6 | |
thomasmorris | 48:244d6d81bb52 | 7 | bool SD_Write = 1; |
thomasmorris | 47:6d128e500875 | 8 | void SD_Init()//Initialised the SD CARD |
thomasmorris | 47:6d128e500875 | 9 | { |
thomasmorris | 47:6d128e500875 | 10 | |
thomasmorris | 47:6d128e500875 | 11 | } |
thomasmorris | 48:244d6d81bb52 | 12 | void SD_Card_Eject() |
thomasmorris | 49:d51f96a46cc3 | 13 | {/* |
thomasmorris | 48:244d6d81bb52 | 14 | SD_Write = 0; |
thomasmorris | 48:244d6d81bb52 | 15 | if(Log_Value==5){pc.printf("Closing File Ejecting SD Card\n");} |
thomasmorris | 49:d51f96a46cc3 | 16 | //fclose(fp);//Close File |
thomasmorris | 48:244d6d81bb52 | 17 | sd.deinit();//Close down |
thomasmorris | 48:244d6d81bb52 | 18 | LCD.Display_Clear(); |
thomasmorris | 48:244d6d81bb52 | 19 | LCD.DDRAM_Address(0x00); |
thomasmorris | 48:244d6d81bb52 | 20 | LCD.Write_String("SD Card"); |
thomasmorris | 48:244d6d81bb52 | 21 | LCD.DDRAM_Address(0x40); |
thomasmorris | 48:244d6d81bb52 | 22 | LCD.Write_String("Unmounted"); |
thomasmorris | 48:244d6d81bb52 | 23 | LCD.DDRAM_Address(0x00); |
thomasmorris | 48:244d6d81bb52 | 24 | pc.printf("SD Card Unmounted\n"); |
thomasmorris | 49:d51f96a46cc3 | 25 | */ |
thomasmorris | 48:244d6d81bb52 | 26 | } |
thomasmorris | 47:6d128e500875 | 27 | void SD_Card_Write() |
thomasmorris | 47:6d128e500875 | 28 | { |
thomasmorris | 48:244d6d81bb52 | 29 | if(SD_Write == 0) |
thomasmorris | 48:244d6d81bb52 | 30 | { |
thomasmorris | 48:244d6d81bb52 | 31 | if(Log_Value==5){pc.printf("SD Has Been Ejected Please do a Hardware Restart\n");} |
thomasmorris | 48:244d6d81bb52 | 32 | return; |
thomasmorris | 48:244d6d81bb52 | 33 | } |
thomasmorris | 47:6d128e500875 | 34 | if(Write_Pointer == mailsize - 1) |
thomasmorris | 47:6d128e500875 | 35 | { |
thomasmorris | 48:244d6d81bb52 | 36 | if(Log_Value==5){pc.printf("In SD_Card Thread\n");} |
thomasmorris | 49:d51f96a46cc3 | 37 | //Open to WRITE |
thomasmorris | 49:d51f96a46cc3 | 38 | FATFileSystem fs("sd", &sd); |
thomasmorris | 49:d51f96a46cc3 | 39 | fp = fopen("/sd/test.csv","w"); |
thomasmorris | 47:6d128e500875 | 40 | |
thomasmorris | 49:d51f96a46cc3 | 41 | if (fp == NULL) |
thomasmorris | 49:d51f96a46cc3 | 42 | { |
thomasmorris | 49:d51f96a46cc3 | 43 | error("Could not open file for write in SD_Card\n"); |
thomasmorris | 49:d51f96a46cc3 | 44 | LCD.Display_Clear(); |
thomasmorris | 49:d51f96a46cc3 | 45 | LCD.Write_String("CANNOT OPEN FILE"); |
thomasmorris | 49:d51f96a46cc3 | 46 | errorCode(FATAL); |
thomasmorris | 49:d51f96a46cc3 | 47 | } |
thomasmorris | 49:d51f96a46cc3 | 48 | //FILE* fp = fopen("/sd/hello.csv","w"); |
thomasmorris | 48:244d6d81bb52 | 49 | if(Log_Value==5){pc.printf("File Opened\n");} |
thomasmorris | 47:6d128e500875 | 50 | |
thomasmorris | 47:6d128e500875 | 51 | for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++) |
thomasmorris | 47:6d128e500875 | 52 | { |
thomasmorris | 49:d51f96a46cc3 | 53 | if(Log_Value==5){pc.printf("Copying from address: %d\n", SD_Card_Data_Pointer);} |
thomasmorris | 49:d51f96a46cc3 | 54 | |
thomasmorris | 49:d51f96a46cc3 | 55 | //ofstream outfile ("Hello Tom.txt"); |
thomasmorris | 47:6d128e500875 | 56 | time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time(); |
thomasmorris | 47:6d128e500875 | 57 | tm* Time_Pointer = localtime(&Time); |
thomasmorris | 47:6d128e500875 | 58 | int temp_day = Time_Pointer->tm_mday; |
thomasmorris | 47:6d128e500875 | 59 | int temp_month = (Time_Pointer->tm_mon+1);//Set to current month |
thomasmorris | 47:6d128e500875 | 60 | int temp_year = (Time_Pointer->tm_year+1900);//Set to current year |
thomasmorris | 47:6d128e500875 | 61 | |
thomasmorris | 49:d51f96a46cc3 | 62 | int temp_hours = Time_Pointer->tm_hour; |
thomasmorris | 47:6d128e500875 | 63 | int temp_minute = Time_Pointer->tm_min; |
thomasmorris | 47:6d128e500875 | 64 | int temp_seconds = Time_Pointer->tm_sec; |
thomasmorris | 47:6d128e500875 | 65 | |
thomasmorris | 47:6d128e500875 | 66 | float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature(); |
thomasmorris | 47:6d128e500875 | 67 | float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure(); |
thomasmorris | 47:6d128e500875 | 68 | float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light(); |
thomasmorris | 47:6d128e500875 | 69 | |
thomasmorris | 47:6d128e500875 | 70 | fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);//Date |
thomasmorris | 47:6d128e500875 | 71 | fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);//Time |
thomasmorris | 47:6d128e500875 | 72 | fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature |
thomasmorris | 47:6d128e500875 | 73 | fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure |
thomasmorris | 47:6d128e500875 | 74 | fprintf(fp, "Light: %5.3f,",temp_light);//Light |
thomasmorris | 47:6d128e500875 | 75 | fprintf(fp, "End of Data \n");//End of data |
thomasmorris | 47:6d128e500875 | 76 | } |
thomasmorris | 48:244d6d81bb52 | 77 | if(Log_Value==5){pc.printf("Dumped data to SD Card\n");} |
thomasmorris | 47:6d128e500875 | 78 | //Close File |
thomasmorris | 47:6d128e500875 | 79 | fclose(fp); |
thomasmorris | 47:6d128e500875 | 80 | } |
thomasmorris | 47:6d128e500875 | 81 | } |
thomasmorris | 49:d51f96a46cc3 | 82 |