Here is a project created to interface 6-axis sensor FXOS8700 and storing its data on SD card together act as data logger using K64F freedom board.
Refer to my earlier blogs which i have created separately on interfacing FXOS8700 sensor present onboard with K64 board where i have explained clearly on creating the required components for this activity using KDS + Processor expert
And one more project on exploring the SD card interface and Accessing SD card and writing some data into it using KDS + Processor expert.
[FRDM-K64F + Kinetis Design Studio (KDS)+Processor Expert(PE)] Accessing SDCARD through K64 board
Here in this tutorial i am integrating or combining both the projects and created newer one in order to act as a Data logger project which stores the sensor data to SD card.
Before we start this project below are the pre-requisites:
- KDS software tool from Freescale
- FRDM-K64F Freedom development board
- Micro-SD-Card ( I am using 1GB)
Here is an explanation of the project creation:
I have created the project with the name “K64 Sensor+SDcard” the components of this project are as shown below:
- FAT_FileSystem
- TimerInt
- FXOS8700CQ
- Utility
- LEDR-BitIO
- LEDG-BitIO
- ConsoleIO
And there are few reference components which are dependant or linked to components.
Details about these components have been provided in the earlier blogs (links given above)
Below are the lines of code for initialising the FXOS registers.
if (FX1_WriteReg8(FX1_CTRL_REG_1, 0x00) != ERR_OK) {
return ERR_FAILED;
}
if (FX1_WriteReg8(FX1_M_CTRL_REG_1, 0x1F) != ERR_OK) {
return ERR_FAILED;
}
if (FX1_WriteReg8(FX1_M_CTRL_REG_2, 0x20) != ERR_OK) {
return ERR_FAILED;
}
if (FX1_WriteReg8(FX1_XYZ_DATA_CFG, 0x00) != ERR_OK) {
return ERR_FAILED;
}
if (FX1_WriteReg8(FX1_CTRL_REG_1, 0x0D) != ERR_OK) {
return ERR_FAILED;
}
The below function call FX1_GetX() will get the accelerometer values.
x = FX1_GetX();
y = FX1_GetY();
z = FX1_GetZ();
printf("Accelerometer value \tX: %4d\t Y: %4d\t Z: %4d\n",x,y,z);
and the below function collects the magnetometer values:
if (FX1_GetMagX(&magX)!=ERR_OK) {return ERR_OK;
}
if (FX1_GetMagY(&magY)!=ERR_OK) {
return ERR_OK;
}
if (FX1_GetMagZ(&magZ)!=ERR_OK) {
return ERR_OK;
}
printf("Magnetometer value \tX: %4d\t Y: %4d\t Z: %4d\n",magX,magY,magZ);
There is a function LogToFile() in Application.c folder which does our task of storing the data to SD-card as shown below:
LogToFile(x, y, z);
LogToFile(magX,magY,magZ);
This function uses FAT component and opens a file called log.txt and append to its last edited part and writes the sensor values i.e x,y,z or magX,magY,magZ depending on the function call.
static void LogToFile(int16_t x, int16_t y, int16_t z) {
uint8_t write_buf[48];
UINT bw;
TIMEREC time;
/* open file */
if (FAT1_open(&fp, "./log.txt", FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) {
Err();
}
/* move to the end of the file */
if (FAT1_lseek(&fp, fp.fsize) != FR_OK || fp.fptr != fp.fsize) {
Err();
}
/* get time */
if (TmDt1_GetTime(&time)!=ERR_OK) {
Err();
}
/* write data */
write_buf[0] = '\0';
UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Hour);
UTIL1_chcat(write_buf, sizeof(write_buf), ':');
UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Min);
UTIL1_chcat(write_buf, sizeof(write_buf), ':');
UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Sec);
UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
UTIL1_strcatNum16s(write_buf, sizeof(write_buf), x);
UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
UTIL1_strcatNum16s(write_buf, sizeof(write_buf), y);
UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
UTIL1_strcatNum16s(write_buf, sizeof(write_buf), z);
UTIL1_strcat(write_buf, sizeof(write_buf), (unsigned char*)"\r\n");
if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) {
(void)FAT1_close(&fp);
Err();
}
/* closing file */
(void)FAT1_close(&fp);
}
Now i am connecting the SD-card to K64 board as shown below and follow these steps:
- Build and compile the project
- Open a hyper terminal (Putty in my case) with proper ‘com’ port number (CDC usb port) which is created after connecting your board to PC
- Set a Baud rate of 9600 bps
- Execute the project
- For every 1 sec the sensor data is displayed on the terminal and also stored in “Log.txt” file created in SD-card
The output seen on the terminal is as shown below:
Opening the log file shows the stored SD card data:
I have enclosed the *.SREC file, you can download this and test it directly by programming the binaries.
and also the project folder for your quick reference.