八八八八
//8563時(shí)鐘模塊、DHT11模塊。
//在LCD上顯示時(shí)鐘和日歷,溫度和濕度。通過確認(rèn)鍵切換顯示內(nèi)容。#include <Rtc Pcf8563.h>
#include <Liquidcrystal.h>
#include"dht11.h”
Rtc_Pcf8563rtc; //定義一個對象rtc
dht11 DHT11;
#define DHT11PIN2 //定義DHT11模塊引腳
//LCD1602 引腳
const intrs=8, en=9, d4= 4, d5= 5,d6= 6,d7=7;//LCD1602引腳
LiquidCrystallcd(rs, en, d4, d5, d6, d7);
byte day, weekday, month, century, year; //定義日期變量
byte hr, minute, sec; //定義時(shí)間變量
char time_str[16]; //時(shí)間
char data str[16] ; //日歷
char strout [8];
char time_Out[16];
intkey_in = A0; //模擬量按鍵
intkey_v[4]= (0x2D,0x13,0x8,0x00 );//預(yù)存鍵值
intflag=0; //顯示時(shí)鐘狀態(tài)
intflag1= 0xff; //初始沒有按鍵狀態(tài)
int key; //按鍵
int key1; //防止重鍵
String week[7] ={"Mon","Tue ", "Wed ","Thur", "Fri","sat ", "Sun "};
byte Centigrade[8]={ //定義C顯示符號
? B10000,
? B00110,
? B01001,
? B01000,
? B01000,
? B01001,
? B00110,
? B00000
};
void setup() {
? lcd.begin(16,2); //液晶初始化
? lcd.createChar(0, Centigrade); //在地址0創(chuàng)造C字符
? getclock(); //讀取當(dāng)前時(shí)鐘
}
void loop() { //主循環(huán)函數(shù)
? read key();
? if(key != 0)
{
? ? switch (key)(
? ? ? case l: Select(); break; //選擇鍵
? ? ? case 2: Reduce(); break; //增加鍵
? ? ? case3:Increase(); break; //減少鍵
? ? ? case 4: Return(); break; //確認(rèn)鍵
? ? ? ?}
? ? key =0;
}
? if (flag == 0)
? ? time display();
? else if (flag ==1)
? ? temp display();
? else if(flag == 2)
? ? set display();
}
void getclock(){ //讀日歷、時(shí)鐘
? rtc.getDateTime();
? sec = rtc.getSecond();
? minute = rtc.getMinute();
? hr=rtc.getHour();
? day = rtc.getDay();
? month = rtc.getMonth();
? year = rtc.getYear();
? weekday = rtc.getWeekday();
}
voidset display() {//參數(shù)修改狀態(tài),在相應(yīng)位置光標(biāo)閃爍
? lcd.cursor();
? lcd.blink();
? if (flagl != 0xff) {
? ? switch (flag1) {
? ? case 0: 1cd.setCursor(10,0);lcd.print(sec /10);lcd.print(sec % 10);1cd.setCursor(11, 0); break;
? ? case 1: 1cd.setCursor(7, 0);1cd.print(minute / 10); 1cd.print(minute /10);lcd.setCursor(8, 0);break;
? ? case 2: lcd.setCursor(4, 0);lcd.print(hr /10); lcd.print(hr % 10); 1cd.setCursor(5, 0); break;
? ? case 3: lcd.setCursor(12,1);1cd.print(week[weekday -1]);1cd.setCursor(15,1);break;
? ? case 4:lcd.setCursor(8, 1);lcd.print(day/ 10);lcd.print(day % 10);1cd.setCursor(9, 1);break;
? ? case5:lcd.setCursor(5, 1);1cd.print(month /10);lcd.print(month % 10);lcd.setCursor(6, 1); break;
? ? case6:lcd.setCursor(0,1);lcd.print("20");lcd.print(year/10);lcd.print(year810);lcd.setCursor(3,1); break;
? ? }
? }
}
void time_display() //顯示日歷、時(shí)鐘
{
? lcd.noCursor();
? lcd.noBlink();
? lcd.setCursor(4, 0);
? lcd.print(rtc.formatTime());
? lcd.setCursor(0,1);
? lcd.print(rtc.formatDate(RTCC_DATE_ASIA));
? lcd.setCursor(12,1);
? lcd.print(week[rtc.getWeekday() -1]);
}
void temp_display() { //溫濕度顯示
? DHT11.read(DHT11PIN);
? int temperature = DHT11.temperature; //溫度
? int humidity = DHT11.humidity; //濕度
? lcd.setCursor(4, 0);
? lcd.print("T=");
? lcd.print(temperature);
? lcd.write(byte(0));
? lcd.print("");
? lcd.setCursor(4, 1);
? lcd.print("H=");
? lcd.print(humiditv);
? lcd.print("% ");
}
void read_key() {? //讀按鍵
? int key_Value = analogRead(A0) >> 4;? //讀取AD高8位
? if(key Value != 0x3f) {? ? ? ? ? ? ? ? //無鍵按下時(shí)鍵值是0x3f
? ? delay(120);
? ? key Value = analogRead(key_in) >> 4;
? ? if(key_Value != 0x3f) {
? ? ? for (int i= 0; i < 4; i++) {
if (key_Value >= key_v[i] -1 && key_Value <= key_v[i]+ 1) //取一個范圍
? ? ? ? ? ? key=i +1; //鍵值從1開始?
? ? ? }
? ? }
? ? if (key1 != key) //防止連鍵
? ? ? keyl = key;
? ? else
? ? ? key=0;
? }
}
void Select() { //參數(shù)修改鍵
? getclock();
? rtc.setDateTime (day, weekday, month, 0, year, hr, minute, sec);?
? if (flag == 1) {
? ? flag = 0;
? ? }
? else? {
? ? flag=2;
? ? if? (flag1==0xff)
? ? ? flaq1= 0;
? ? elseif (flag1 < 6){
? ? ? flag1 +=1;
? ? ? rtc.setDateTime(day, weekday, month, 0, year, hr, minute, sec);
? ? }
? ? else flagl=0;
? }
}
void Reduce() { //減少鍵
? if (flag1 !=0xff) {
? ? switch (flag1) {
? ? ? case 0: if(sec == 0) sec = 59; else sec -= 1; break;
? ? ? case l: if(minute==0) minute = 59; else minute -= 1; break;
? ? ? case 2: if(hr== 0) hr=23; else hr -= 1; break;
? ? ? case 3: if (weekday==1) weekday =7; else weekday -= 1;break;
? ? ? case 4: if (day==1) day = 31;else day -= 1; break;
? ? ? case 5: if(month == 1) month = 12;else month -= 1; break;
? ? ? case 6: if(year == 0) year = 99;else year -= 1; break;
? ? }
? ? rtc.setDateTime (day, weekday, month, 0, year, hr, minute, sec);
? ? }
}
void Increase() {//增加鍵
? if (flag1!=0xff) {
? ? switch(flag1) {
? ? ? case 0: if (sec == 59) sec =0; else sec += 1; break;
? ? ? case 1:if (minute ==59) minute = 0; else minute += 1; break;
? ? ? case 2:if (hr == 23) hr=0; else hr +=1;break;
? ? ? case 3: if (weekday ==7) weekday = 1; else weekday += 1;break;
? ? ? case 4:if (day ==31) day =1;else day += 1; break;
? ? ? case 5: if (month == 12)month = 1; else month += 1; break;
? ? ? case 6: if (year == 99) year = 0; else year += 1; break;
? ? }
? ? rtc.setDateTime(day, weekday, month, 0, year, hr, minute, sec);一
? }
}
void Return() {//確認(rèn)鍵
? if (flag1 != Oxff) {
? ? rtc.setDateTime (day, weekday, month, 0, year, hr, minute, sec);
? ? flagl = 0xff;
? ? flag = 0;
? }
? else {
? ? flag =!flag;
? ? lcd.clear();
? ? flagl=0xff;
? }
}