本文主要是介绍stc15--znfat,往SD卡创建文件,写数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/****************《51单片机轻松入门-基于STC15W4K系列》配套例程 *************★★★★★★★★★★★★★★★★★★★★★★★★《51单片机轻松入门-基于STC15W4K系列》 一书已经由北航出版社正式出版发行。作者亲手创作的与教材配套的51双核实验板(2个MCU)对程序下载、调试、仿真方便,不需要外部仿真器与编程器,这种设计方式彻底解决了系统中多个最高优先级谁也不能让谁的中断竞争问题。淘宝店地址:https://shop117387413.taobao.comQQ群:STC51-STM32(3) :515624099 或 STC51-STM32(2):99794374。验证信息:STC15单片机邮箱:xgliyouquan@126.com★★★★★★★★★★★★★★★★★★★★★★★★*/#include "znfat/znFAT.h"
#include "sd.h"
#include "uart.h"
#include "myfun.h"//===============================
struct znFAT_Init_Args idata Init_Args; // 初始化参数集合
struct FileInfo idata fileinfo; // 文件信息集合
struct DateTime idata dt; // 日期与时间
unsigned char buf[100];
unsigned char tempint[11]; // 长整数最大值4294967295,转ASCII码后占用10字节// 由于后面程序要使用strlen()库函数计算长度,需增加1个字节存放结束符0void port_mode() // 端口模式
{P0M1=0xff; P0M0=0xff;P1M1=0x0C;P1M0=0x0C;P4M1=0x80;P4M0=0x80;
}
int main()
{ unsigned int res=0,i=0;unsigned char ti;unsigned long len=0;unsigned char monthG,dayG,hourG,minG,secG;unsigned char wenduG,shiduG,shuiliangG,dianliangG;wenduG=20;shiduG=35;shuiliangG=54;dianliangG=20;monthG=4;dayG=5;hourG=10;minG=50;secG=32;port_mode();UART_Init(); UART_Send_Str("串口设置完毕\r\n"); znFAT_Device_Init(); // 存储设备初始化UART_Send_Str("SD卡初始化完毕\r\n");znFAT_Select_Device(0,&Init_Args); // 选择设备res=znFAT_Init(); // 文件系统初始化 if(!res) //文件系统初始化成功{UART_Send_Str("Suc. to init FS\r\n");UART_Send_StrNum("BPB_Sector_No:",Init_Args.BPB_Sector_No); UART_Send_StrNum("Total_SizeKB:",Init_Args.Total_SizeKB); UART_Send_StrNum("BytesPerSector:",Init_Args.BytesPerSector); UART_Send_StrNum("FATsectors:",Init_Args.FATsectors); UART_Send_StrNum("SectorsPerClust:",Init_Args.SectorsPerClust); UART_Send_StrNum("FirstFATSector:",Init_Args.FirstFATSector); UART_Send_StrNum("FirstDirSector:",Init_Args.FirstDirSector); UART_Send_StrNum("FSsec:",Init_Args.FSINFO_Sec);UART_Send_StrNum("Next_Free_Cluster:",Init_Args.Next_Free_Cluster);UART_Send_StrNum("FreenCluster:",Init_Args.Free_nCluster); }else //文件系统初始化失败{UART_Send_StrNum("Fail to init FS, Err Code:",res);}//==================================================================dt.date.year=2014; dt.date.month=01; dt.date.day=20;dt.time.hour=22; dt.time.min=38; dt.time.sec=41;res=znFAT_Create_File(&fileinfo,"/test14.txt",&dt); //创建文件if(!res) // 创建文件成功{delay(30000);UART_Send_Str("Suc. to create file.\r\n");UART_Send_Str("================================\n");UART_Send_Str("File_Name(Short 8.3):");UART_Send_Str(fileinfo.File_Name);UART_Send_Enter();UART_Send_StrNum("File_Size:",fileinfo.File_Size);UART_Send_Str("File_CDate:");UART_Send_Num(fileinfo.File_CDate.year); UART_Send_Str("年");UART_Send_Num(fileinfo.File_CDate.month);UART_Send_Str("月"); UART_Send_Num(fileinfo.File_CDate.day); UART_Send_Str("日");UART_Send_Num(fileinfo.File_CTime.hour); UART_Send_Str("时");UART_Send_Num(fileinfo.File_CTime.min); UART_Send_Str("分");UART_Send_Num(fileinfo.File_CTime.sec); UART_Send_Str("秒\r\n");UART_Send_StrNum("File_StartClust:",fileinfo.File_StartClust);UART_Send_StrNum("File_CurClust:",fileinfo.File_CurClust);UART_Send_StrNum("File_CurSec:",fileinfo.File_CurSec);UART_Send_StrNum("File_CurPos:",fileinfo.File_CurPos);UART_Send_StrNum("File_CurOffset:",fileinfo.File_CurOffset);UART_Send_Str("================================\n");// for(i=0;i<22;i++)
// {
// //buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧
// buf[i]="0123456789abcdef中国\r\n"[i]; // 字符数组赋值技巧
// } //// for(i=0;i<10;i++)
// {
// len=znFAT_WriteData(&fileinfo,22,buf); //向文件写入数据
// if(len==ERR_OVER_FILE_MAX_SIZE || len==ERR_OVER_DISK_SPACE)
// {
// UART_Send_StrNum("Have some Err :",len);
// }
// else
// {
// UART_Send_StrNum("Write Data,times:",i);
// UART_Send_StrNum("Current file offset is ",fileinfo.File_CurOffset);
// }
// //将数据变成字符串形式,拼接成整串,然后再一次性写入到SD卡文件中。//字符串格式为//时间2021/02/03/15:30:wendu:35;shidu:25;shuiliang:20,dianliang:25;//时间2021/02/03/,15:30,温度35,湿度25%,水量25%,电量30%;for(ti=0;ti<5;ti++) //连续写5次内容,测试换行对不对{for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="时间"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据//2021的20//将20转变成字符串len=znFAT_WriteData(&fileinfo,1,":"); //向文件写入数据Long_Str(monthG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,"/"); //向文件写入数据Long_Str(dayG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,"/"); //向文件写入数据Long_Str(monthG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,"/"); //向文件写入数据Long_Str(dayG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,"/"); //向文件写入数据Long_Str(hourG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,":"); //向文件写入数据Long_Str(minG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,"]"); //向文件写入数据len=znFAT_WriteData(&fileinfo,1,":"); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"\r\n"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="温度"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(wenduG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,4,"deg;"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="湿度"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(shiduG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%/"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="湿度"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(shiduG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%/"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="湿度"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(shiduG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%/"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="湿度"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(shiduG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%/"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="湿度"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(shiduG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%;"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="水量"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(shuiliangG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%;"); //向文件写入数据for(i=0;i<4;i++){//buf[i]="0123456789abcdefghij"[i]; // 字符数组赋值技巧buf[i]="电量"[i]; // 字符数组赋值技巧}len=znFAT_WriteData(&fileinfo,4,buf); //向文件写入数据Long_Str(dianliangG,tempint);len=znFAT_WriteData(&fileinfo,2,tempint); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"%;"); //向文件写入数据len=znFAT_WriteData(&fileinfo,2,"\r\n"); //向文件写入数据if(len==ERR_OVER_FILE_MAX_SIZE || len==ERR_OVER_DISK_SPACE){UART_Send_StrNum("Have some Err :",len);}else{UART_Send_StrNum("Write Data,times:",i);UART_Send_StrNum("Current file offset is ",fileinfo.File_CurOffset);}} znFAT_Close_File(&fileinfo); //关闭文件}else{UART_Send_StrNum("Fail to create file, Err Code:",res);}znFAT_Flush_FS(); //刷新文件系统while(1);return 0;
}
这篇关于stc15--znfat,往SD卡创建文件,写数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!