-
- 实现上述功能的重要代码如下:
- #include "avr/io.h"
- #include "pcf8563.h"
- #include "uart.h"
- #include "myfun.h"
- #include "adc.h"
- #include "ds18b20.h"
- #include "znfat/znfat.h"
- //=============================================================
- struct Time2 time;
- struct znFAT_Init_Args Init_Args; //初始化参数集合
- struct FileInfo fileinfo; //文件信息集合
- unsigned char str[100]; //用于存储转换后的字符串
- unsigned int format_data(char *s,struct Time2 *ptime,unsigned int adc,
- unsigned int temprature)
- {
- //将时间、AD值及温度值合成字符串
- }
- int main()
- {
- int i=0,len=0,cur_sec=0;
- DDRD=0XE3; //设置IO,PCF8563 使用 PB0与PB1 DS18B20使用PC0
- //模拟量使用ADC0 SD卡使用 PA7-4
- DDRA=0XEe;
- DDRB=0X83;
- DDRC=0XFF;
- //===============================================================
- UART_Init();
- UART_Send_Str("uart init ok.\r\n");
- znFAT_Device_Init(); //存储设备初始化
- UART_Send_Str("sd init ok.\r\n");
- znFAT_Select_Device(0,&Init_Args); //选择设备
- znFAT_Init(); //文件系统初始化
- UART_Send_Str("znFAT init OK.\r\n");
- //输出文件系统相关信息
- time.year =12; //向time中装入要设置的时间数据 2012.8.29 23:18:50 星期3
- time.month = 8;
- time.day =29;
- time.hour =23;
- time.minute=18;
- time.second=50;
- time.week = 3;
- P8563_Set_Time(); //设置时间,即将time中的时间数据写入PCF8563
- adc_enable(); //打开AVR内部ADC采样
- //===============================================================
- if(!znFAT_Open_File(&fileinfo,"/znmcu.txt",0,1)) //打开SD卡根目录下的znmcu.txt文件
- {
- UART_Send_Str("suc. to open file.\n");
- //输出文件相关信息
-
- znFAT_WriteData(&fileinfo,26,"年 月 日 时 分 秒 ADC 温度"); //写入数据头
- str[0]=0x0d;str[1]=0x0a;
- znFAT_WriteData(&fileinfo,2,str); //回车换行
-
- for(i=0;i<5000;i++) //进行5000次循环,不过只有在秒值变化时才会写入数据
- //即每秒写一次
- {
- P8563_Read_Time(); //读取时间
- if(time.second!=cur_sec)
- {
- ds1820_start(); //启动DS18B20温度采样
-
- len=format_data(str,&time,read_adc(),ds1820_read_temp()); //将时间、AD值与
- //温度值转为字符串
-
- znFAT_WriteData(&fileinfo,len,str); //将字符串写入文件
- cur_sec=time.second; //更新当前秒值
- }
- }
-
- znFAT_Close_File(&fileinfo); //关闭文件
-
- znFAT_Flush_FS(); //刷新文件系统
- }
- else
- {
- UART_Send_Str("fail to open file.\r\n");
- }
- while(1);
- }
硬件平台: