|
實(shí)物圖如下:
33.jpg (135.2 KB, 下載次數(shù): 32)
下載附件
2021-6-19 17:35 上傳
2.jpg (156.9 KB, 下載次數(shù): 33)
下載附件
2021-6-19 17:35 上傳
1.jpg (165.17 KB, 下載次數(shù): 30)
下載附件
2021-6-19 17:35 上傳
代碼如下:
#include<reg51.h>
#include"lcd.h"
#include"temp.h"
#include"ds1302.h"
sbit K1=P3^1;
sbit K2=P3^0;
sbit K3=P3^2;
sbit K4=P3^3;
sbit beep=P1^5;//蜂鳴器
void LcdDisplay(int);
void Int0Configuration();
void SetDisplay();
void Keyscan();
unsigned char SetState,SetPlace,naoz=0;
unsigned char Alarm[]={0x00,0x00}; //設(shè)定鬧鐘初值//
void Delay10ms(void); //誤差 0us
void delay(unsigned int); // 延時(shí)函數(shù),i=1時(shí),大約延時(shí)10us
void xiqi();
void xiqif();
void xiqi()
{
switch(TIME[5]&0x07)
{
case(1):OLED_ShowCHinese(54,4,2);break;
case(2):OLED_ShowCHinese(54,4,3);break;
case(3):OLED_ShowCHinese(54,4,4);break;
case(4):OLED_ShowCHinese(54,4,5);break;
case(5):OLED_ShowCHinese(54,4,6);break;
case(6):OLED_ShowCHinese(54,4,7);break;
case(7):OLED_ShowCHinese(54,4,8);break;
}
}
void xiqif()
{
switch(TIME[5]&0x07)
{
case(1):OLED_ShowCHinese1(54,4,2);break;
case(2):OLED_ShowCHinese1(54,4,3);break;
case(4):OLED_ShowCHinese1(54,4,5);break;
case(6):OLED_ShowCHinese1(54,4,7);break;
case(7):OLED_ShowCHinese1(54,4,8);break;
}
}
/*******************************************************************************
* 函數(shù)名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void main()
{
OLED_Init(); //初始化OLED
Int0Configuration();;
Ds1302Init();
Ds18b20Init();
while(1)
{if(SetState==0)
{
LcdDisplay(Ds18b20ReadTemp());
Ds1302ReadTime();
}
else
{
Keyscan();
} //else
} //while
} //main
/*******************************************************************************
* 函數(shù)名 : LcdDisplay()
* 函數(shù)功能 : LCD顯示讀取到的溫度
* 輸入 : v
* 輸出 : 無
*******************************************************************************/
void LcdDisplay(int temp) //lcd顯示
{
unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
float tp;
if(temp< 0) //當(dāng)溫度值為負(fù)數(shù)
{
OLED_ShowString(18,6,"-"); //顯示負(fù)
//因?yàn)樽x取的溫度是實(shí)際溫度的補(bǔ)碼,所以減1,再取反求出原碼
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算由?.5,還是在小數(shù)點(diǎn)后面。
}
else
{
OLED_ShowString(18,6,"+"); //顯示正
tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
//如果溫度是正的那么,那么正數(shù)的原碼就是補(bǔ)碼它本身
temp=tp*0.0625*100+0.5;
//留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
//后面的數(shù)自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)1了,小于0.5的就
//算加上0.5,還是在小數(shù)點(diǎn)后面。
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
OLED_ShowNum(26,6,datas[0],1,16);//百位
OLED_ShowNum(34,6,datas[1],1,16); //十位
OLED_ShowNum(42,6,datas[2],1,16);//個(gè)位
OLED_ShowString(50,6,".");
OLED_ShowNum(58,6,datas[3],1,16);
OLED_ShowNum(66,6,datas[4],1,16);
OLED_ShowCHinese(76,6,10);//℃
if(SetState==2)
{
OLED_Clear(); //清屏
OLED_ShowNum(20,2,Alarm[0]/16,1,16); //時(shí)
OLED_ShowNum(28,2,Alarm[0]&0x0f,1,16);
OLED_ShowString(36,2,"-");
OLED_ShowNum(44,2,Alarm[1]/16,1,16); //分
OLED_ShowNum(52,2,Alarm[1]&0x0f,1,16);
OLED_ShowString(0,0,"Alarm setting:");
}
else
{
OLED_ShowNum(20,2,TIME[2]/16,1,16); //時(shí)
OLED_ShowNum(28,2,TIME[2]&0x0f,1,16);
OLED_ShowString(36,2,":");
OLED_ShowNum(44,2,TIME[1]/16,1,16); //分
OLED_ShowNum(52,2,TIME[1]&0x0f,1,16);
OLED_ShowString(60,2,":");
OLED_ShowNum(68,2,TIME[0]/16,1,16); //秒
OLED_ShowNum(76,2,TIME[0]&0x0f,1,16);
OLED_ShowString(0,0,"2");
OLED_ShowString(8,0,"0");
OLED_ShowNum(16,0,TIME[6]/16,1,16); //年
OLED_ShowNum(24,0,TIME[6]&0x0f,1,16);
OLED_ShowCHinese(32,0,9);
OLED_ShowNum(50,0,TIME[4]/16,1,16); //月
OLED_ShowNum(58,0,TIME[4]&0x0f,1,16);
OLED_ShowCHinese(66,0,1);
OLED_ShowNum(84,0,TIME[3]/16,1,16); //日
OLED_ShowNum(92,0,TIME[3]&0x0f,1,16);
OLED_ShowCHinese(100,0,2);
OLED_ShowCHinese(36,4,0);//周
xiqi();
}
SetDisplay();
}
void SetDisplay()
{
if(SetState==2)
{
switch(SetPlace)
{
case(0):OLED_ShowNum1(20,2,Alarm[0]/16,1,16);
OLED_ShowNum1(28,2,Alarm[0]&0x0f,1,16);
break; //光標(biāo)指向--時(shí)//
case(1):OLED_ShowNum1(44,2,Alarm[1]/16,1,16);
OLED_ShowNum1(52,2,Alarm[1]&0x0f,1,16);
break; //光標(biāo)指向--分//
case(2):OLED_ShowNum1(20,2,Alarm[0]/16,1,16);
OLED_ShowNum1(28,2,Alarm[0]&0x0f,1,16);
break; //光標(biāo)指向--時(shí)//
case(3):OLED_ShowNum1(44,2,Alarm[1]/16,1,16);
OLED_ShowNum1(52,2,Alarm[1]&0x0f,1,16);
break; //光標(biāo)指向--分//
}
}
if(SetState==1)
{
switch(SetPlace)
{
case(0):OLED_ShowNum1(68,2,TIME[0]/16,1,16);
OLED_ShowNum1(76,2,TIME[0]&0x0f,1,16);
break; //光標(biāo)指向--秒//
case(1):OLED_ShowNum1(44,2,TIME[1]/16,1,16);
OLED_ShowNum1(52,2,TIME[1]&0x0f,1,16);
break; //光標(biāo)指向--分//
case(2):OLED_ShowNum1(20,2,TIME[2]/16,1,16);
OLED_ShowNum1(28,2,TIME[2]&0x0f,1,16);
break; //光標(biāo)指向--時(shí)//
case(3):OLED_ShowNum1(84,0,TIME[3]/16,1,16);
OLED_ShowNum1(92,0,TIME[3]&0x0f,1,16);
break; //光標(biāo)指向--日//
case(4):OLED_ShowNum1(50,0,TIME[4]/16,1,16);
OLED_ShowNum1(58,0,TIME[4]&0x0f,1,16);
break; //光標(biāo)指向--月//
case(5):xiqif();
break; //光標(biāo)指向--周//
case(6):OLED_ShowNum1(16,0,TIME[6]/16,1,16);
OLED_ShowNum1(24,0,TIME[6]&0x0f,1,16);
break; //光標(biāo)指向--年//
}
}
}
void Keyscan()
{
unsigned char i;
if(K4==0) //檢測按鍵K4是否按下
{
Delay10ms(); //消除抖動
if(K4==0)
{
SetPlace++;
if(SetState==1)
{if(SetPlace>=7) SetPlace=0;}
if(SetState==2)
{if(SetPlace>=4) SetPlace=0;}
}
LcdDisplay();
while((i<50)&&(K4==0)) //檢測按鍵是否松開
{
Delay10ms();
i++;
}
i=0;
}
if(K1==0) //檢測按鍵K1是否按下
{
Delay10ms(); //消除抖動
if(K1==0)
{
if(SetState==1)
{
TIME[SetPlace]++;
if((TIME[SetPlace]&0x0f)>9) //換成BCD碼。
{
TIME[SetPlace]=TIME[SetPlace]+6;
}
if((TIME[SetPlace]>=0x60)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x24)&&(SetPlace==2)) //小時(shí)只能到23
{
TIME[SetPlace]=0;
}
if((TIME[SetPlace]>=0x32)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0x01;
}
if((TIME[SetPlace]>=0x13)&&(SetPlace==4)) //月只能到12
{
TIME[SetPlace]=0x01;
}
if((TIME[SetPlace]>=0x8)&&(SetPlace==5)) //周只能到7
{
TIME[SetPlace]=0x01;
}
if((TIME[SetPlace]>=0x9a)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0;
}
}
if(SetState==2)
{
Alarm[SetPlace]++;
if((Alarm[SetPlace]&0x0f)>9) //換成BCD碼。
{
Alarm[SetPlace]=Alarm[SetPlace]+6;
}
if((Alarm[SetPlace]>=0x60)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0;
}
if((Alarm[SetPlace]>=0x24)&&(SetPlace==0)) //小時(shí)只能到23
{
Alarm[SetPlace]=0;
}
}
LcdDisplay();
while((i<50)&&(K1==0)) //檢測按鍵是否松開
{
Delay10ms();
i++;
}
i=0;
}
}
if(K2==0) //檢測按鍵K2是否按下
{
Delay10ms(); //消除抖動
if(K2==0)
{
if(SetState==1)
{
TIME[SetPlace]--;
if((TIME[SetPlace]==0x00)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0x59;
}
if((TIME[SetPlace]==0x4f)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0x49;
}
if((TIME[SetPlace]==0x3f)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0x39;
}
if((TIME[SetPlace]==0x2f)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0x29;
}
if((TIME[SetPlace]==0x1f)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0x19;
}
if((TIME[SetPlace]==0x0f)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0x09;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==2)) //小時(shí)只能到23
{
TIME[SetPlace]=0x23;
}
if((TIME[SetPlace]==0x1f)&&(SetPlace==2)) //小時(shí)只能到23
{
TIME[SetPlace]=0x19;
}
if((TIME[SetPlace]==0x0f)&&(SetPlace==2)) //小時(shí)只能到23
{
TIME[SetPlace]=0x09;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0x31;
}
if((TIME[SetPlace]==0x2f)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0x29;
}
if((TIME[SetPlace]==0x1f)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0x19;
}
if((TIME[SetPlace]==0x0f)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0x09;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==4)) //月只能到12
{
TIME[SetPlace]=0x12;
}
if((TIME[SetPlace]==0x0f)&&(SetPlace==4)) //月只能到12
{
TIME[SetPlace]=0x09;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==5)) //周只能到7
{
TIME[SetPlace]=0x06;
}
if((TIME[SetPlace]==0x00)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x99;
}
if((TIME[SetPlace]==0x8f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x89;
}
if((TIME[SetPlace]==0x7f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x79;
}
if((TIME[SetPlace]==0x6f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x69;
}
if((TIME[SetPlace]==0x5f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x59;
}
if((TIME[SetPlace]==0x4f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x49;
}
if((TIME[SetPlace]==0x3f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x39;
}
if((TIME[SetPlace]==0x2f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x29;
}
if((TIME[SetPlace]==0x1f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x19;
}
if((TIME[SetPlace]==0x0f)&&(SetPlace==6)) //年只能到99
{
TIME[SetPlace]=0x09;
}
}
if(SetState==2)
{
Alarm[SetPlace]--;
if((Alarm[SetPlace]==0x00)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0x59;
}
if((Alarm[SetPlace]==0x4f)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0x49;
}
if((Alarm[SetPlace]==0x3f)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0x39;
}
if((Alarm[SetPlace]==0x2f)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0x29;
}
if((Alarm[SetPlace]==0x1f)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0x19;
}
if((Alarm[SetPlace]==0x0f)&&(SetPlace==1)) //分秒只能到59
{
Alarm[SetPlace]=0x09;
}
if((Alarm[SetPlace]==0x00)&&(SetPlace==0)) //小時(shí)只能到23
{
Alarm[SetPlace]=0x23;
}
if((Alarm[SetPlace]==0x1f)&&(SetPlace==0)) //小時(shí)只能到23
{
Alarm[SetPlace]=0x19;
}
if((Alarm[SetPlace]==0x0f)&&(SetPlace==0)) //小時(shí)只能到23
{
Alarm[SetPlace]=0x09;
}
}
LcdDisplay();
while((i<50)&&(K2==0)) //檢測按鍵是否松開
{
Delay10ms();
i++;
}
i=0;
}
}
}
/*******************************************************************************
* 函數(shù)名 : Int0Configuration()
* 函數(shù)功能 : 配置外部中斷0
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Int0Configuration()
{
ET1 = 1; //打開定時(shí)器1中斷允許
TR1 = 1; //打開定時(shí)器1
//設(shè)置INT0
IT0=1;//跳變沿出發(fā)方式(下降沿)
EX0=1;//打開INT0的中斷允許。
EA=1;//打開總中斷
}
/*******************************************************************************
* 函數(shù)名 : Int0()
* 函數(shù)功能 : 外部中斷0 中斷函數(shù)
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Int0() interrupt 0
{
Delay10ms();
if(K3==0)
{
SetState++;
SetPlace=0;
if(SetState==2)
{
OLED_Clear(); //清屏
LcdDisplay();
}
if(SetState==3)
{ OLED_Clear(); //清屏
SetState=0;
Ds1302Init();
}
}
}
void Delay10ms(void) //誤差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void delay(unsigned int i) // 延時(shí)函數(shù),i=1時(shí),大約延時(shí)10us
{
while(i--);
}
/*定時(shí)器1函數(shù)*/
void Timer1() interrupt 3
{
TH1 = 0x0D1;
TL1 = 0x20; //給定時(shí)器1賦初值,定時(shí)1ms
if(Alarm[0]==TIME[2]&&Alarm[1]==TIME[1]&&TIME[0]==0)naoz=1;
if(Alarm[0]!=TIME[2]||Alarm[1]!=TIME[1]) naoz=0;
if(naoz==1)beep=~beep;
}
完整代碼51hei下載:
萬年歷.zip
(104.26 KB, 下載次數(shù): 76)
2021-6-19 17:37 上傳
點(diǎn)擊文件名下載附件
萬年歷源代碼 下載積分: 黑幣 -5
|
評分
-
查看全部評分
|