小白一枚 不是特别懂C 认为是程序的问题 但不知道怎么改 求助各位大神
DHT11单片机程序:
主函数:- #include "sys.h"
- #include "GPIO.h"
- #include "LCD1602.h"
- #include "dht11.h"
- #include "delay.h"
- #include "sys.h"
- #include "stdio.h"
- #include "usart.h"
- #include "string.h"
- #include "ds1302.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char temperature,humidity;
- unsigned char setn = 0;
- unsigned char p_r=0; //平年/润年 =0表示平年,=1表示润年
- u8 set_shi=0,set_fen=0,set_miao=0,set_nian=0,set_yue=0,set_ri=0,set_week=0;
- unsigned char see_NZ = 0;
- unsigned char nz_shi=12,nz_fen=0,nz_miao=0;
- void Display_Time()//显示时间及温湿度
- {
- uint nian_temp;
-
- LCD1602_write_com(0x80);
- LCD1602_write_data('2');
- LCD1602_write_data('0');
- LCD1602_write_data(SysDate.year/10 + 0x30);
- LCD1602_write_data(SysDate.year%10 + 0x30);
- LCD1602_write_data('-');
- LCD1602_write_data(SysDate.mon/10 + 0x30);
- LCD1602_write_data(SysDate.mon%10 + 0x30);
- LCD1602_write_data('-');
- LCD1602_write_data(SysDate.day/10 + 0x30);
- LCD1602_write_data(SysDate.day%10 + 0x30);
- LCD1602_write_data(' ');
- switch(SysDate.week)
- {
- case 1:
- LCD1602_write_data('M');//星期数为1时,显示
- LCD1602_write_data('O');
- LCD1602_write_data('N');
- break;
- case 2:
- LCD1602_write_data('T');//星期数据为2时显示
- LCD1602_write_data('U');
- LCD1602_write_data('E');
- break;
- case 3:
- LCD1602_write_data('W');//星期数据为3时显示
- LCD1602_write_data('E');
- LCD1602_write_data('D');
- break;
- case 4:
- LCD1602_write_data('T');//星期数据为4是显示
- LCD1602_write_data('H');
- LCD1602_write_data('U');
- break;
- case 5:
- LCD1602_write_data('F');//星期数据为5时显示
- LCD1602_write_data('R');
- LCD1602_write_data('I');
- break;
- case 6:
- LCD1602_write_data('S');//星期数据为6时显示
- LCD1602_write_data('A');
- LCD1602_write_data('T');
- break;
- case 7:
- LCD1602_write_data('S');//星期数据为7时显示
- LCD1602_write_data('U');
- LCD1602_write_data('N');
- break;
- }
- LCD1602_write_data(' ');
-
- nian_temp=2000+SysDate.year;
- if((nian_temp%400==0)||((nian_temp%100!=0)&&(nian_temp%4==0))) //判断是否为闰年
- p_r=1;
- else
- p_r=0;
- if(p_r == 1)
- {
- LCD1602_write_data('R');
- }
- else
- {
- LCD1602_write_data('P');
- }
-
- LCD1602_write_com(0x80+0x40);
- LCD1602_write_data(SysDate.hour/10 + 0x30);
- LCD1602_write_data(SysDate.hour%10 + 0x30);
- LCD1602_write_data(':');
- LCD1602_write_data(SysDate.min/10 + 0x30);
- LCD1602_write_data(SysDate.min%10 + 0x30);
- LCD1602_write_data(':');
- LCD1602_write_data(SysDate.sec/10 + 0x30);
- LCD1602_write_data(SysDate.sec%10 + 0x30);
- LCD1602_write_data(' ');
- LCD1602_write_data(temperature/10 + 0x30);//显示温度的十位
- LCD1602_write_data(temperature%10 + 0x30);//显示温度的个位
- LCD1602_write_data('C');
- LCD1602_write_data(' ');
- LCD1602_write_data(humidity/10 + 0x30);//显示湿度的十位
- LCD1602_write_data(humidity%10 + 0x30);//湿度个位
- LCD1602_write_data('%');
- }
- void Display_alarm_clock()//显示闹钟
- {
- LCD1602_write_com(0x80+0x40+4);
- LCD1602_write_data(nz_shi/10 + 0x30);
- LCD1602_write_data(nz_shi%10 + 0x30);
- LCD1602_write_data(':');
- LCD1602_write_data(nz_fen/10 + 0x30);
- LCD1602_write_data(nz_fen%10 + 0x30);
- LCD1602_write_data(':');
- LCD1602_write_data(nz_miao/10 + 0x30);
- LCD1602_write_data(nz_miao%10 + 0x30);
- LCD1602_write_data(' ');
- }
- void Display_setTime()//显示设置时间
- {
- LCD1602_write_com(0x80);
- LCD1602_write_data('2');
- LCD1602_write_data('0');
- LCD1602_write_data(set_nian/10 + 0x30);
- LCD1602_write_data(set_nian%10 + 0x30);
- LCD1602_write_data('-');
- LCD1602_write_data(set_yue/10 + 0x30);
- LCD1602_write_data(set_yue%10 + 0x30);
- LCD1602_write_data('-');
- LCD1602_write_data(set_ri/10 + 0x30);
- LCD1602_write_data(set_ri%10 + 0x30);
- LCD1602_write_data(' ');
- switch(set_week)
- {
- case 1:
- LCD1602_write_data('M');//星期数为1时,显示
- LCD1602_write_data('O');
- LCD1602_write_data('N');
- break;
- case 2:
- LCD1602_write_data('T');//星期数据为2时显示
- LCD1602_write_data('U');
- LCD1602_write_data('E');
- break;
- case 3:
- LCD1602_write_data('W');//星期数据为3时显示
- LCD1602_write_data('E');
- LCD1602_write_data('D');
- break;
- case 4:
- LCD1602_write_data('T');//星期数据为4是显示
- LCD1602_write_data('H');
- LCD1602_write_data('U');
- break;
- case 5:
- LCD1602_write_data('F');//星期数据为5时显示
- LCD1602_write_data('R');
- LCD1602_write_data('I');
- break;
- case 6:
- LCD1602_write_data('S');//星期数据为6时显示
- LCD1602_write_data('A');
- LCD1602_write_data('T');
- break;
- case 7:
- LCD1602_write_data('S');//星期数据为7时显示
- LCD1602_write_data('U');
- LCD1602_write_data('N');
- break;
- }
- LCD1602_write_data(' ');
-
- LCD1602_write_com(0x80+0x40);
- LCD1602_write_data(set_shi/10 + 0x30);
- LCD1602_write_data(set_shi%10 + 0x30);
- LCD1602_write_data(':');
- LCD1602_write_data(set_fen/10 + 0x30);
- LCD1602_write_data(set_fen%10 + 0x30);
- LCD1602_write_data(':');
- LCD1602_write_data(set_miao/10 + 0x30);
- LCD1602_write_data(set_miao%10 + 0x30);
- LCD1602_write_data(' ');
- }
- void keyscan()
- {
- if(KEY1 == 0)
- {
- delay_ms(20);
- if(KEY1 == 0)
- {
- BEEP = 1;
- delay_ms(200);
- BEEP = 0;
- while(KEY1 == 0);
-
- set_shi = SysDate.hour;
- set_fen = SysDate.min;
- set_miao = SysDate.sec;
- set_nian = SysDate.year;
- set_yue = SysDate.mon;
- set_ri = SysDate.day;
- set_week = SysDate.week;
- if(see_NZ == 0)
- {
- setn++;
- if(setn > 7)
- {
- setn = 0;
- LCD1602_write_com(0x0C);
- }
- if(setn == 1)
- {
- Display_setTime();
- LCD1602_write_com(0x80+3);
- LCD1602_write_com(0x0F);
- }
- if(setn == 2)
- {
- LCD1602_write_com(0x80+6);
- LCD1602_write_com(0x0F);
- }
- if(setn == 3)
- {
- LCD1602_write_com(0x80+9);
- LCD1602_write_com(0x0F);
- }
- if(setn == 4)
- {
-
- LCD1602_write_com(0x80+13);
- LCD1602_write_com(0x0F);
- }
- if(setn == 5)
- {
- LCD1602_write_com(0x80+0x40+1);
- LCD1602_write_com(0x0F);
- }
- if(setn == 6)
- {
- LCD1602_write_com(0x80+0x40+4);
- LCD1602_write_com(0x0F);
- }
- if(setn == 7)
- {
- LCD1602_write_com(0x80+0x40+7);
- LCD1602_write_com(0x0F);
- }
- }else
- {
- setn++;
- if(setn > 3)
- {
- setn = 0;
- LCD1602_write_com(0x0C);
- }
- if(setn == 1)
- {
- LCD1602_write_com(0x80+0x40+5);
- LCD1602_write_com(0x0F);
- }
- if(setn == 2)
- {
- LCD1602_write_com(0x80+0x40+8);
- LCD1602_write_com(0x0F);
- }
- if(setn == 3)
- {
- LCD1602_write_com(0x80+0x40+11);
- LCD1602_write_com(0x0F);
- }
- }
- }
- }
- if(KEY2 == 0)
- {
- delay_ms(20);
- if(KEY2 == 0)
- {
- BEEP = 1;
- delay_ms(200);
- BEEP = 0;
- while(KEY2 == 0);
-
- if(see_NZ == 0)
- {
- if(setn == 1)
- {
- set_nian ++;
- if(set_nian == 100)
- {
- set_nian = 0;
- }
- SysDate.year = set_nian;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+3);
- LCD1602_write_com(0x0F);
- }
- if(setn == 2)
- {
- set_yue ++;
- if(set_yue == 13)
- {
- set_yue = 1;
- }
- if((set_yue==4)||(set_yue==6)||(set_yue==9)||(set_yue==11))
- {
- if(set_ri>30)
- set_ri=1;
- }
- else
- {
- if(set_yue==2)
- {
- if(p_r==1)
- {
- if(set_ri>29)
- set_ri=1;
- }
- else
- {
- if(set_ri>28)
- set_ri=1;
- }
- }
- }
- SysDate.mon = set_yue;
- SysDate.day = set_ri;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+6);
- LCD1602_write_com(0x0F);
- }
- if(setn == 3)
- {
- set_ri ++;
- if((set_yue==1)||(set_yue==3)||(set_yue==5)||(set_yue==7)||(set_yue==8)||(set_yue==10)||(set_yue==12))
- {
- if(set_ri==32)
- set_ri=1;
- }
- else
- {
- if(set_yue==2)
- {
- if(p_r==1)
- {
- if(set_ri==30)
- set_ri=1;
- }
- else
- {
- if(set_ri==29)
- set_ri=1;
- }
- }
- else
- {
- if(set_ri==31)
- set_ri=1;
- }
- }
- SysDate.day = set_ri;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+9);
- LCD1602_write_com(0x0F);
- }
- if(setn == 4)
- {
- set_week ++;
- if(set_week == 8)
- {
- set_week = 1;
- }
- SysDate.week = set_week;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+13);
- LCD1602_write_com(0x0F);
- }
- if(setn == 5)
- {
- set_shi ++;
- if(set_shi == 24)
- {
- set_shi = 0;
- }
- SysDate.hour = set_shi;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+0x40+1);
- LCD1602_write_com(0x0F);
- }
- if(setn == 6)
- {
- set_fen ++;
- if(set_fen == 60)
- {
- set_fen = 0;
- }
- SysDate.min = set_fen;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+0x40+4);
- LCD1602_write_com(0x0F);
- }
- if(setn == 7)
- {
- set_miao ++;
- if(set_miao == 60)
- {
- set_miao = 0;
- }
- SysDate.sec = set_miao;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+0x40+7);
- LCD1602_write_com(0x0F);
- }
- }else
- {
- if(setn == 1)
- {
- nz_shi ++;
- if(nz_shi == 24)
- {
- nz_shi = 0;
- }
- Display_alarm_clock();
- LCD1602_write_com(0x80+0x40+5);
- LCD1602_write_com(0x0F);
- }
- if(setn == 2)
- {
- nz_fen ++;
- if(nz_fen == 60)
- {
- nz_fen = 0;
- }
- Display_alarm_clock();
- LCD1602_write_com(0x80+0x40+8);
- LCD1602_write_com(0x0F);
- }
- if(setn == 3)
- {
- nz_miao ++;
- if(nz_miao == 60)
- {
- nz_miao = 0;
- }
- Display_alarm_clock();
- LCD1602_write_com(0x80+0x40+11);
- LCD1602_write_com(0x0F);
- }
- }
- }
- }
- if(KEY3 == 0)
- {
- delay_ms(20);
- if(KEY3 == 0)
- {
- BEEP = 1;
- delay_ms(200);
- BEEP = 0;
- while(KEY3 == 0);
-
- if(see_NZ == 0)
- {
- if(setn == 1)
- {
- if(set_nian == 0)
- {
- set_nian = 100;
- }
- set_nian --;
- SysDate.year = set_nian;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+3);
- LCD1602_write_com(0x0F);
- }
- if(setn == 2)
- {
- if(set_yue == 1)
- {
- set_yue = 13;
- }
- set_yue --;
- if((set_yue==4)||(set_yue==6)||(set_yue==9)||(set_yue==11))
- {
- if(set_ri>30)
- set_ri=1;
- }
- else
- {
- if(set_yue==2)
- {
- if(p_r==1)
- {
- if(set_ri>29)
- set_ri=1;
- }
- else
- {
- if(set_ri>28)
- set_ri=1;
- }
- }
- }
- SysDate.mon = set_yue;
- SysDate.day = set_ri;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+6);
- LCD1602_write_com(0x0F);
- }
- if(setn == 3)
- {
- set_ri --;
- if((set_yue==1)||(set_yue==3)||(set_yue==5)||(set_yue==7)||(set_yue==8)||(set_yue==10)||(set_yue==12))
- {
- if(set_ri==0)
- set_ri=31;
- }
- else
- {
- if(set_yue==2)
- {
- if(p_r==1)
- {
- if(set_ri==0)
- set_ri=29;
- }
- else
- {
- if(set_ri==0)
- set_ri=28;
- }
- }
- else
- {
- if(set_ri==0)
- set_ri=30;
- }
- }
- SysDate.day = set_ri;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+9);
- LCD1602_write_com(0x0F);
- }
- if(setn == 4)
- {
- if(set_week == 1)
- {
- set_week = 8;
- }
- set_week --;
- SysDate.week = set_week;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+13);
- LCD1602_write_com(0x0F);
- }
- if(setn == 5)
- {
- if(set_shi == 0)
- {
- set_shi = 24;
- }
- set_shi --;
- SysDate.hour = set_shi;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+0x40+1);
- LCD1602_write_com(0x0F);
- }
- if(setn == 6)
- {
- if(set_fen == 0)
- {
- set_fen = 60;
- }
- set_fen --;
- SysDate.min = set_fen;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+0x40+4);
- LCD1602_write_com(0x0F);
- }
- if(setn == 7)
- {
- if(set_miao == 0)
- {
- set_miao = 60;
- }
- set_miao --;
- SysDate.sec = set_miao;
- Display_setTime();
- DS1302_DateSet(&SysDate);
-
- LCD1602_write_com(0x80+0x40+7);
- LCD1602_write_com(0x0F);
- }
- }else
- {
- if(setn == 1)
- {
- if(nz_shi == 0)
- {
- nz_shi = 24;
- }
- nz_shi--;
- Display_alarm_clock();
- LCD1602_write_com(0x80+0x40+5);
- LCD1602_write_com(0x0F);
- }
- if(setn == 2)
- {
- if(nz_fen == 0)
- {
- nz_fen = 60;
- }
- nz_fen--;
- Display_alarm_clock();
- LCD1602_write_com(0x80+0x40+8);
- LCD1602_write_com(0x0F);
- }
- if(setn == 3)
- {
- if(nz_miao == 0)
- {
- nz_miao = 60;
- }
- nz_miao--;
- Display_alarm_clock();
- LCD1602_write_com(0x80+0x40+11);
- LCD1602_write_com(0x0F);
- }
- }
- }
- }
- if(KEY4 == 0 &&setn==0)
- {
- delay_ms(20);
- if(KEY4 == 0 && setn==0)
- {
- BEEP = 1;
- delay_ms(200);
- BEEP = 0;
- while(KEY4 == 0);
- see_NZ = !see_NZ;
- LCD1602_write_com(0x80); //指针设置
- LCD1602_write_word(" ");
- LCD1602_write_com(0x80+0x40); //指针设置
- LCD1602_write_word(" ");
- if(see_NZ == 1)
- {
- LCD1602_write_com(0x80); //指针设置
- LCD1602_write_word(" Set Alarm Clock");
- Display_alarm_clock();
- }
- }
- }
- }
- int main(void)
- {
- u8 test_interval = 0;
- u8 flag = 0;
-
- delay_init(); //延时函数初始化
- KEY_GPIO_Init(); //按键初始化
- DS1302_Init(&SysDate);
- delay_ms(100);
- DS1302_DateRead(&SysDate);
- delay_ms(500); //上电瞬间加入一定延时在初始化
- LCD_Init(); //屏幕初始化
- delay_ms(100);
- LCD1602_write_com(0x80); //指针设置
- LCD1602_write_word(" ");
- LCD1602_write_com(0x80+0x40); //指针设置
- LCD1602_write_word(" ");
- while(1)
- {
- if (test_interval ++ > 10 && setn==0)
- {
- test_interval = 0;
- DS1302_DateRead(&SysDate);//读取时间
- DHT11_Read_Data(&temperature,&humidity);//读取温湿度
- if(see_NZ == 0)
- {
- Display_Time();
- }
- if(SysDate.hour==nz_shi && SysDate.min==nz_fen && SysDate.sec==nz_miao)
- {
- flag = 1;
- }
- if(flag == 1)
- {
- if(SysDate.hour==nz_shi && SysDate.min==nz_fen)
- {
- BEEP = ~BEEP;
- delay_ms(80);
- }else
- {
- BEEP = 0;
- flag = 0;
- }
- }
- }
- keyscan();//按键扫描
- delay_ms(1);
- }
-
- }
-
复制代码
|