我使用的是STM32F407,通过485接收数据,将数据以txt文件的形式存储到SD卡里,每个文件是625k,可是程序在运行到存储了300多个文件的时候死机了。
下面是程序
[/code]
#define __SHOW_SD_PROCEDURE__
static int i = 0;
static int m = 0;
static int n = 0;
char FileName[16];
char CntBuf[64];
u8 res_open = 1;
u8 res_lseek = 1;
int open = 0;
int lseek = 0;
FIL File;
FIL* CreateNewFile(void)
{
__align(8) RTC_TimeTypeDef RTC_TimeStruct; //__align(8) 8字节对其
__align(8) RTC_DateTypeDef RTC_DateStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
LCD_ShowNum(200, 320, ++m, 8, 16);
sprintf(FileName, "%02d%02d%02d%02d.txt",
RTC_DateStruct.RTC_Date,
RTC_TimeStruct.RTC_Hours,
RTC_TimeStruct.RTC_Minutes,
RTC_TimeStruct.RTC_Seconds);
LCD_ShowNum(200, 350, ++n, 8, 16);
while(res_open!=0)
{
res_open = f_open(&File, FileName, FA_OPEN_ALWAYS | FA_WRITE);
if(++open > 2)
{
sprintf(CntBuf, "f_open = %d", res_open);
LCD_ShowString(60, 80, 210, 16, 16, CntBuf);
}
}
res_open = 1; open = 0;
while(res_lseek!=0)
{
res_lseek = f_lseek(&File, 0);
if(++lseek > 2)
{
sprintf(CntBuf, "f_lseek = %d", res_lseek);
LCD_ShowString(60, 110, 210, 16, 16, CntBuf);
}
}
res_lseek = 1; lseek = 0;
#ifdef __SHOW_SD_PROCEDURE__
LCD_ShowString(60, 140, 210, 16, 16, FileName);
#endif
LCD_ShowString(60, 170, 210, 16, 16, " CreateNewFile = ");
LCD_ShowNum(200, 170, ++i, 8, 16);
return &File;
}
#define FILE_BLOCK_CNT 100
int FileBlockCnt = 0;
int FileCnt = 0;
u8 res_close = 1;
int close = 0;
static int p= 0;
static int q = 0;
static int r = 0;
int main(void)
{
char CntBuf[64];
FIL *SDFile = NULL;
//-------------------------------
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
//-------------------------------
delay_init(168); // 初始化延时函数
uart_init(115200); // 初始化串口波特率为115200
LED_Init(); // 初始化LED
LCD_Init(); // LCD初始化
RS485_Init(115200); // 初始化RS485串口2
RTC_Set_WakeUp(RTC_WakeUpClock_CK_SPRE_16bits, 0); // 配置WAKE UP中断,1秒钟中断一次
exfuns_init(); //为fatfs相关变量申请内存
usmart_dev.init(72);
my_mem_init(SRAMIN); //初始化内部内存池
SD_InitializeCards();
f_mount(fs[0], "0:", 1); //注册工作区
// //-----------------------------
while(1)
{
if(QueueFullWithData)
{
LCD_ShowNum(200, 440, ++p, 8, 16);
if(FileBlockCnt == 0)
{
LCD_ShowNum(200, 470, ++q, 8, 16);
SDFile = CreateNewFile();
LCD_ShowNum(200, 500, ++r, 8, 16);
}
f_write(SDFile, QueueFullWithData, QUEUE_SIZE, (UINT*)&bw);
//delay_ms(10);
LCD_ShowString(60, 200, 210, 16, 16, "Saved File Block = ");
LCD_ShowNum(200, 200, ++FileBlockCnt, 8, 16);
QueueFullWithData = NULL;
// if(FileBlockCnt == FILE_BLOCK_CNT)
// {
while(res_close!= 0)
{
res_close = f_close(SDFile);
if(++close > 2)
{
sprintf(CntBuf, "f_close = %d", res_close);
LCD_ShowString(60, 230, 210, 16, 16, CntBuf);
}
}
res_close = 1; close = 0;
FileBlockCnt = 0;
LCD_ShowString(60, 260, 210, 16, 16, "Saved File Cnt = ");
LCD_ShowNum(200, 260, ++FileCnt, 8, 16);
// }
}
}
}
[/code]
经过设置标志位,发现程序是在建立新文件的时候死掉的
FIL File;
FIL* CreateNewFile(void)
{
__align(8) RTC_TimeTypeDef RTC_TimeStruct; //__align(8) 8字节对其
__align(8) RTC_DateTypeDef RTC_DateStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
LCD_ShowNum(200, 320, ++m, 8, 16);
sprintf(FileName, "%02d%02d%02d%02d.txt",
RTC_DateStruct.RTC_Date,
RTC_TimeStruct.RTC_Hours,
RTC_TimeStruct.RTC_Minutes,
RTC_TimeStruct.RTC_Seconds);
LCD_ShowNum(200, 350, ++n, 8, 16);
while(res_open!=0)
{
res_open = f_open(&File, FileName, FA_OPEN_ALWAYS | FA_WRITE);
if(++open > 2)
{
sprintf(CntBuf, "f_open = %d", res_open);
LCD_ShowString(60, 80, 210, 16, 16, CntBuf);
}
}
res_open = 1; open = 0;
while(res_lseek!=0)
{
res_lseek = f_lseek(&File, 0);
if(++lseek > 2)
{
sprintf(CntBuf, "f_lseek = %d", res_lseek);
LCD_ShowString(60, 110, 210, 16, 16, CntBuf);
}
}
res_lseek = 1; lseek = 0;
#ifdef __SHOW_SD_PROCEDURE__
LCD_ShowString(60, 140, 210, 16, 16, FileName);
#endif
LCD_ShowString(60, 170, 210, 16, 16, " CreateNewFile = ");
LCD_ShowNum(200, 170, ++i, 8, 16);
return &File;
}
复制代码
两个标志位都进行了更新,就是说标志位前的语句没有问题,是在f_open这出问题的,可是我在程序里面写的如果打开文件多于两次就输出"f_open = %d",可是在测试的时候没有输出,请问是哪里出问题了?
转载
|