實物圖如下:
代碼如下:
#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}; //設定鬧鐘初值//
void Delay10ms(void); //誤差 0us
void delay(unsigned int); // 延時函數,i=1時,大約延時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;
}
}
/*******************************************************************************
* 函數名 : main
* 函數功能 : 主函數
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void main()
{
OLED_Init(); //初始化OLED
Int0Configuration();;
Ds1302Init();
Ds18b20Init();
while(1)
{if(SetState==0)
{
LcdDisplay(Ds18b20ReadTemp());
Ds1302ReadTime();
}
else
{
Keyscan();
} //else
} //while
} //main
/*******************************************************************************
* 函數名 : LcdDisplay()
* 函數功能 : LCD顯示讀取到的溫度
* 輸入 : v
* 輸出 : 無
*******************************************************************************/
void LcdDisplay(int temp) //lcd顯示
{
unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定義數組
float tp;
if(temp< 0) //當溫度值為負數
{
OLED_ShowString(18,6,"-"); //顯示負
//因為讀取的溫度是實際溫度的補碼,所以減1,再取反求出原碼
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
//留兩個小數點就*100,+0.5是四舍五入,因為C語言浮點數轉換為整型的時候把小數點
//后面的數自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進1了,小于0.5的就
//算由?.5,還是在小數點后面。
}
else
{
OLED_ShowString(18,6,"+"); //顯示正
tp=temp;//因為數據處理有小數點所以將溫度賦給一個浮點型變量
//如果溫度是正的那么,那么正數的原碼就是補碼它本身
temp=tp*0.0625*100+0.5;
//留兩個小數點就*100,+0.5是四舍五入,因為C語言浮點數轉換為整型的時候把小數點
//后面的數自動去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進1了,小于0.5的就
//算加上0.5,還是在小數點后面。
}
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);//個位
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); //時
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); //時
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; //光標指向--時//
case(1):OLED_ShowNum1(44,2,Alarm[1]/16,1,16);
OLED_ShowNum1(52,2,Alarm[1]&0x0f,1,16);
break; //光標指向--分//
case(2):OLED_ShowNum1(20,2,Alarm[0]/16,1,16);
OLED_ShowNum1(28,2,Alarm[0]&0x0f,1,16);
break; //光標指向--時//
case(3):OLED_ShowNum1(44,2,Alarm[1]/16,1,16);
OLED_ShowNum1(52,2,Alarm[1]&0x0f,1,16);
break; //光標指向--分//
}
}
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; //光標指向--秒//
case(1):OLED_ShowNum1(44,2,TIME[1]/16,1,16);
OLED_ShowNum1(52,2,TIME[1]&0x0f,1,16);
break; //光標指向--分//
case(2):OLED_ShowNum1(20,2,TIME[2]/16,1,16);
OLED_ShowNum1(28,2,TIME[2]&0x0f,1,16);
break; //光標指向--時//
case(3):OLED_ShowNum1(84,0,TIME[3]/16,1,16);
OLED_ShowNum1(92,0,TIME[3]&0x0f,1,16);
break; //光標指向--日//
case(4):OLED_ShowNum1(50,0,TIME[4]/16,1,16);
OLED_ShowNum1(58,0,TIME[4]&0x0f,1,16);
break; //光標指向--月//
case(5):xiqif();
break; //光標指向--周//
case(6):OLED_ShowNum1(16,0,TIME[6]/16,1,16);
OLED_ShowNum1(24,0,TIME[6]&0x0f,1,16);
break; //光標指向--年//
}
}
}
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)) //小時只能到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)) //小時只能到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)) //小時只能到23
{
TIME[SetPlace]=0x23;
}
if((TIME[SetPlace]==0x1f)&&(SetPlace==2)) //小時只能到23
{
TIME[SetPlace]=0x19;
}
if((TIME[SetPlace]==0x0f)&&(SetPlace==2)) //小時只能到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)) //小時只能到23
{
Alarm[SetPlace]=0x23;
}
if((Alarm[SetPlace]==0x1f)&&(SetPlace==0)) //小時只能到23
{
Alarm[SetPlace]=0x19;
}
if((Alarm[SetPlace]==0x0f)&&(SetPlace==0)) //小時只能到23
{
Alarm[SetPlace]=0x09;
}
}
LcdDisplay();
while((i<50)&&(K2==0)) //檢測按鍵是否松開
{
Delay10ms();
i++;
}
i=0;
}
}
}
/*******************************************************************************
* 函數名 : Int0Configuration()
* 函數功能 : 配置外部中斷0
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void Int0Configuration()
{
ET1 = 1; //打開定時器1中斷允許
TR1 = 1; //打開定時器1
//設置INT0
IT0=1;//跳變沿出發方式(下降沿)
EX0=1;//打開INT0的中斷允許。
EA=1;//打開總中斷
}
/*******************************************************************************
* 函數名 : Int0()
* 函數功能 : 外部中斷0 中斷函數
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
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) // 延時函數,i=1時,大約延時10us
{
while(i--);
}
/*定時器1函數*/
void Timer1() interrupt 3
{
TH1 = 0x0D1;
TL1 = 0x20; //給定時器1賦初值,定時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, 下載次數: 76)
2021-6-19 17:37 上傳
點擊文件名下載附件
萬年歷源代碼
|