/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "includes.h" //包含所需的头文件
/*************************************************************************************
** Function name: main
** Descriptions:
** input parameters: 无
** output parameters: 无
** Returned value: 无
*************************************************************************************/
#define Slave_Add 0xB8 //器件地址
#define Time 600
FATFS fs;
FIL file;
FRESULT res;
uint8_t Last_time =0;
uint8_t RW_Buf[6][40];
uint8_t Rrad_Buf[10];
S_DRVRTC_TIME_DATA_T Curren_Time;
int32_t AD_Value;
UINT rr;
uint8_t Command[3] = {0x03,0x00,0x04};//功能代码:0x03读传感器的多路寄存器 0x00 起始地址 0x04读取的寄存器个数
uint8_t Recive[20]= {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};//接收代码: Recive[0]功能代码 Recive[1]返回字节数 Recive[2] 湿度高字节Recive[3]湿度低字节 Recive[4] 温度高字节Recive[5] 温度低字节
uint16_t count1 = 0;
uint16_t AM23_Temp;
uint16_t AM23_Humidity;
float x,y;
int main (void)
{
uint8_t funselect= 1,i;
Set_System(); //封装一些初始化模块
if(DrvGPIO_GetBit(E_GPA, 13)== 0) //检测SD卡是否插入
printf("SD卡已插入\n");
else printf("SD卡未插入\n");
while(SD_Init()!=0) //等待SD卡初始化完成
{
DrvSYS_Delay(600000);
printf("等待SD卡初始化\n");
}
printf("初始化成功\n");
res = f_mount(0, &fs); //注册工作区
if(res == FR_OK) //如果注册工作区成功
{
res = f_open(&file, "test2.txt", FA_OPEN_EXISTING | FA_READ); //打开文件
switch(res)
{
case FR_OK: //如果"li.txt"存在,关闭文件
printf("文件存在\n");
f_close(&file);
break;
case FR_NO_FILESYSTEM: //如果文件系统不存在,格式化SD
printf("文件系统不存在\n");
res = f_mkfs(0 , 0, _MAX_SS);
printf("%d",res);
case FR_NO_FILE: //如果"LI.txt"不存在,创建该文件并写入初值
printf("文件不存在\n");
if(f_open(&file, "test2.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK)
{
f_printf(&file, "%s\n", "温湿度记录");
f_close(&file);
}
break;
default: break;
}
f_mount(0, 0); //关闭工作区
}
res = f_mount(0, &fs);
/*f_open(&file, "li.txt", FA_OPEN_ALWAYS | FA_WRITE);//打开文件li.txt
f_lseek(&file, f_size(&file)); //将指针移到文件末尾
f_printf(&file, "%s\n", "李晓旭文本文档...");
f_close(&file);//关闭文件li.txt */
/*if(f_open(&file, "book1.txt", FA_OPEN_ALWAYS | FA_WRITE) == FR_OK)
{
f_printf(&file, "%s","内容");
}
f_close(&file);*/
f_mount(0, 0);//关闭工作区
while(1)
{
if(ShowPage == 0)
{
switch(funselect)
{
case 1:WriteStringToLCM(1,0,"->");
break;
case 2:WriteStringToLCM(2,0,"->");
break;
case 3:WriteStringToLCM(3,0,"->");
break;
case 4:WriteStringToLCM(4,0,"->");
break;
default:break;
}
WriteStringToLCM(1,1,"1.日历");
WriteStringToLCM(2,1,"2.小说");
WriteStringToLCM(3,1,"3.游戏");
WriteStringToLCM(4,1,"4.测试");
while(ShowPage == 0)
{
KeyCode = GetKey();
switch(KeyCode)
{
case KEY1_DOWN_USER:
{
if(funselect<4) funselect++;
else funselect= 1;
WriteStringToLCM(1,0," ");//清选择表头
WriteStringToLCM(2,0," ");
WriteStringToLCM(3,0," ");
WriteStringToLCM(4,0," ");
switch(funselect)
{
case 1:WriteStringToLCM(1,0,"->");
break;
case 2:WriteStringToLCM(2,0,"->");
break;
case 3:WriteStringToLCM(3,0,"->");
break;
case 4:WriteStringToLCM(4,0,"->");
break;
default:break;
}
}
break;
case KEY2_DOWN_USER:
{
switch(funselect)
{
case 1:ShowPage = DatePage; break;
case 2:ShowPage = BookPage;break;
case 3:ShowPage = GamePage;break;
case 4:ShowPage = MusicPage; break;
default:break;
}
}
break;
default:break;
}
}
}
if(ShowPage == DatePage)
{
DrvRTC_Read(DRVRTC_CURRENT_TIME, &Curren_Time); //读取当前时间
value = read_temp();//读取当前温度
value = value/10;
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
WriteStringToLCM(1,0,"星期 晴 C");
WriteStringToLCM(2,0," 年 月 日");
WriteStringToLCM(3,0," 时 分 秒");
//WriteStringToLCM(4,0,&RW_Buf[3][0]);
switch(Curren_Time.u32cDayOfWeek)
{
case 0:WriteStringToLCM(1,2,"日");break;
case 1:WriteStringToLCM(1,2,"一");break;
case 2:WriteStringToLCM(1,2,"二");break;
case 3:WriteStringToLCM(1,2,"三");break;
case 4:WriteStringToLCM(1,2,"四");break;
case 5:WriteStringToLCM(1,2,"五");break;
case 6:WriteStringToLCM(1,2,"六");break;
default:break;
}
while(ShowPage == DatePage)
{
WriteStringToLCM(3,6,"秒 ");
DrvRTC_Read(DRVRTC_CURRENT_TIME, &Curren_Time); //读取当前时间
value = read_temp();//读取当前温度
value = value/10;
if(value >100)
value = 99;
WriteValueToLCM(1,5,"%1f",value);
//WriteValueToLCM(4,0,"%2f",value);
WriteValueToLCM(2,0,"%2d",Curren_Time.u32Year);
WriteValueToLCM(2,3,"%2d",Curren_Time.u32cMonth);
WriteValueToLCM(2,5,"%2d",Curren_Time.u32cDay);
WriteValueToLCM(3,1,"%2d",Curren_Time.u32cHour);
WriteValueToLCM(3,3,"%2d",Curren_Time.u32cMinute);
WriteValueToLCM(3,5,"%2d",Curren_Time.u32cSecond);
KeyCode = GetKey();
switch(KeyCode)
{
case KEY1_DOWN_USER:
{
ShowPage = 0;
funselect = 1;
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
}
break;
case KEY2_DOWN_USER:
{}
break;
default:break;
}
}/*while(ShowPage == DatePage)*/
}/*if(ShowPage == DatePage)*/
if(ShowPage == BookPage)
{
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
WriteStringToLCM(1,2,"电子书");
WriteStringToLCM(2,2,"人生.txt");
WriteStringToLCM(3,3,"作者—路遥");
f_mount(0, &fs); //注册工作区//注册工作区
f_open(&file, "book1.txt", FA_OPEN_EXISTING | FA_READ);//打开电子书记事本
while(ShowPage == BookPage)
{
KeyCode = GetKey();
switch(KeyCode)
{
case KEY1_DOWN_USER:
{
f_close(&file);//关闭电子书记事本
f_mount(0, 0);//关闭工作区
ShowPage = 0;
funselect = 2;
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
}
break;
case KEY2_DOWN_USER:
{
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
for(i=1;i<=4;i++)//
{
f_read(&file,&RW_Buf[i][0],16,&rr);
printf("%s\n",&RW_Buf[i][0]);
WriteStringToLCM(i,0,&RW_Buf[i][0]);
}
}
break;
default:break;
}
} /*while(ShowPage == BookPage)*/
}/*if(ShowPage == BookPage)*/
if(ShowPage == GamePage)
{
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
WriteStringToLCM(1,3,"游戏");
WriteStringToLCM(2,0,"->1.贪吃蛇");
WriteStringToLCM(3,1,"2.俄罗斯方块");
while(ShowPage == GamePage)
{
KeyCode = GetKey();
switch(KeyCode)
{
case KEY1_DOWN_USER:
{
ShowPage = 0;
funselect = 3;
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
}
break;
case KEY2_DOWN_USER:
{}
break;
default:break;
}
} /*while(ShowPage == MusicPage)*/
}/*if(ShowPage == MusicPage)*/
if(ShowPage == MusicPage)
{
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
WriteStringToLCM(1,3,"测试");
WriteStringToLCM(2,0,"湿度:");
WriteStringToLCM(3,0,"温度:");
count1 = Time-3; //为了每第一次进来都读一次
Last_time = 80;
while(ShowPage == MusicPage)
{
DrvRTC_Read(DRVRTC_CURRENT_TIME, &Curren_Time); //读取当前时间
if(Last_time !=Curren_Time.u32cSecond)
{
Last_time = Curren_Time.u32cSecond;
count1++;
}
if(count1>=Time-2) //每3分钟采集一次温湿度值
{
if(count1 == Time-2) //长时间(大于4秒)没读取温度值 待下次再要读取时 必须读两次
{
I2C_Write_N_Byte(Slave_Add,Command,3);
delay_ms(10);//等待
I2C_Read_NByte(Slave_Add,Recive,8);
delay_ms(10);//等待
}
if(count1==Time)
{
count1 = 0;
I2C_Write_N_Byte(Slave_Add,Command,3);
delay_ms(10);//等待
I2C_Read_NByte(Slave_Add,Recive,8);
delay_ms(100);//等待
AM23_Humidity = (Recive[2]*0x100+Recive[3]);
AM23_Temp = (Recive[4]*0x100+Recive[5]);
x = AM23_Humidity;
y = AM23_Temp;
printf("%d",AM23_Humidity/10);
printf(".%d\n",AM23_Humidity%10);
printf("%d",AM23_Temp/10);
printf(".%d\n",AM23_Temp%10);
WriteValueToLCM(2,3,"%1f",x/10);
WriteValueToLCM(3,3,"%1f",y/10);
res = f_mount(0, &fs);//注册工作区
f_open(&file, "test2.txt", FA_OPEN_ALWAYS | FA_WRITE);//打开文件TEST2.txt 写属性
f_lseek(&file, f_size(&file)); //将指针移到文件末尾
f_printf(&file, " %02d",AM23_Humidity/10);//AM23_Humidity/10
f_printf(&file, ".");
f_printf(&file, " %d\n",AM23_Humidity%10);//AM23_Humidity%10
f_printf(&file, " %02d",AM23_Temp/10);//AM23_Temp/10
f_printf(&file, ".");
f_printf(&file, " %d\n",AM23_Temp%10);//AM23_Temp%10
f_close(&file);//关闭文件test2.txt */
/* f_open(&file, "test2.txt", FA_OPEN_EXISTING | FA_READ);//打开文件TEST2.txt 读属性
while( f_gets(Rrad_Buf, 10, &file) )
{
//WriteStringToLCM(4,0,Rrad_Buf);
printf("%s\n",Rrad_Buf);
}
f_close(&file);//关闭文件test.txt */
f_mount(0, 0);//关闭工作区
}
}
KeyCode = GetKey();
switch(KeyCode)
{
case KEY1_DOWN_USER:
{
ShowPage = 0;
funselect = 4;
WriteCommandToLCM(0x01); //清屏指令
delay_ms(10);
}
break;
case KEY2_DOWN_USER:
{}
break;
default:break;
}
} /*while(ShowPage == MusicPage)*/
}/*if(ShowPage == MusicPage)*/
}/*while*/
}/*main*/