A, the main task
1 configuration FATFS implementation TXT file created in the SD card;
2 to write data to branch into the SD card TXT file;
3 data read, write and print via a serial port 1,
Two, step
1 choice of chip STM32F407ZG, can directly search, click on the can,
2 the basic configuration, set the RCC to external clock,
3 configuration SYS,
4 STM32F407ZG SD card through the SDIO access chip, so need to configure the SDIO,
5.1 configuration FATFS, hook far SD Card; In Set Defines the following modifications,
5.2 configuration Platform Solutions, and set the PE2 to input mode - & gt; Click on the System View, & gt; Click GPIO, set, and
Enter FATFS configuration configuration Platform Solutions,
6 configure a serial port 1,
7 configuration clock tree,
8 set up engineering basic information: name, engineering position 4, IDE, would Setting, and recommendations will be. The c and h,.
9 configuration is complete, the generated code, create a project,
Three, the code implementation
Configure a serial port 1 printf print, in usart. Add the following code in c:
#include
Struct __FILE
{
Int handle;
};
The FILE __stdout;
Void _sys_exit (int x)
{
X=x;
}
Int fputc (int ch, FILE * f)
{
While ((USART1 - & gt; SR& 0 x40)==0);
USART1 - & gt; DR=(uint8_t) ch;
return ch;
}
2 through fatfs function file creation, open, write, read closed operation, such as
2.1 create a variable
/* XXXXXXXXXXXXXXXXXXXXXXXX storage variable XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
FATFS fs.//workspace
FIL FIL;//file
Uint32_t byteswritten;//write file count
Uint32_t bytesread;//read file count
Uint8_t wtext [10].//write the content of the
Uint8_t rtext [1024].//read the buff
Char filename []="temperature. TXT";//file name
X0d const uint8_t newline []={0, 0 x0a};//line breaks
/* xxxxxxxxxxxxxxxxxxxxxxxxDS18B20 variable XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
2.2 mount the file system and create the file
Void InitFatFas (void)
{
/* * - 1 - mount file system/
RetSD=f_mount (& amp; Fs, "", 0);
If (retSD)
{
Printf (" mount error: % d \ r \ n ", retSD);
Error_Handler ();
}
The else
Printf (" mount sucess!!!!!! \r\n");
/* - 2 - create a new file */
RetSD=f_open (& amp; Fil, filename, FA_CREATE_ALWAYS | FA_WRITE);//open the file permissions, including creating, writing (if there is no the file, creates the file)
If (retSD==FR_OK) printf (" \ r \ ncreater file sucess!!! \r\n");
The else printf (" \ r \ ncreater file error: % d \ r \ n ", retSD);
F_close (& amp; Fil);//close the file
HAL_Delay (100);
}
2.3 write data to read, and through the printf print, multi-line deposit at the same time, avoid data coverage,
Here in the file data from the collection of DS18B20,
Void FatFsTest (void)
{
HAL_Delay (1);
//read the data periodically and store
If (t % 1000==0)//once every 1000 ms read
{
Temperature=DS18B20_Get_Temp ();
If (temperature<0)
{
Temperature=- temperature;//turned positive
}
Wtext [0]=' ';
Wtext [1]=+ 48 (temperature/100);
Wtext [2]=((temperature % 100)/10) + 48.
Wtext [3]=';
Wtext [4]=(10) (100) temperature % % + 48.
Wtext [5]='C';
/* - 2 - create a new file and write data */
RetSD=f_open (& amp; Fil, filename, FA_WRITE);//open the file permissions, including creating, writing (if there is no the file, creates the file)
If (retSD==FR_OK)//the return value is zero (problems)
{
F_lseek (& amp; Fil, f_size (& amp; Fil));//the pointer to the end of the file
RetSD=f_write (& amp; Fil, wtext, sizeof (wtext), (void *) & amp; Byteswritten);//write data
If (retSD) printf (" \ r \ nwrite file error: % d \ r \ n ", retSD);//print issue code
The else
{
Printf (" \ r \ nwrite file sucess!!! \r\n");
Printf (" \ r \ nwrite Data: % s \ r \ n ", wtext);//print the content of the written
}
F_write (& amp; Fil, newline, 2, (void *) & amp; Byteswritten);//line breaks
F_close (& amp; Fil);
}
The else printf (" open file error: % d \ r \ n ", retSD);//print issue code
}
}
Four, download validation
1 serial port to check the print
2 SD card to see
CodePudding user response:
The content is very detailed, tight logic and usefulCodePudding user response:
Bloggers write very detailed, really great, thank you!CodePudding user response:
Great resources.CodePudding user response:
Clarity, bo master long handsome! A lot of supportCodePudding user response:
Educated, clarity, technical strong??