实物图如下:
代码如下:
#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)
|