这是我前段时间弄好的,现在整理一下,分享给大家里面包含源程序和仿真。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "reg52.h"
- #include "lcd1602.h"
- #include "ds18b20.h"
- #include "ds1302.h"
- /*********按键*********/
- sbit s1=P0^0;
- sbit s2=P0^1;
- sbit s3=P0^2;
- sbit s4=P0^3;
- /****时间结构体变量****/
- Ds1302_Date Date;
- /*********继电器*********/
- sbit ji_dian_qi=P3^6; //1动作ji_dian_qi
- unsigned char SetTemp =25,t;
- /*********标志位*************/
- unsigned char flag=0;
- unsigned char flag1=0;
- unsigned char flag2=0;
-
- int wendu;
- //十进制转化为BC码
- unsigned char dectobcd(unsigned char dec)
- {
- unsigned char bcd;
- bcd = 0;
- while(dec >= 10)
- {
- dec -= 10;
- bcd++;
- }
- bcd <<= 4;
- bcd |= dec;
- return bcd;
- }
- void Delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void LCD_SetTemp(unsigned char X,unsigned char Y,unsigned char temp)
- {
- unsigned char ge,shi;
- unsigned char tab1[3];
- ge =temp%10;
- shi=temp/10%10;
-
- tab1[1] ='0'+ge;
- tab1[0] ='0'+shi;
- tab1[2] ='\0';
- Lcd1602_Write_Cmd(0x0c);//取消光标闪烁
- LcdShowStr(X,Y,tab1);
- }
- void key()
- {
- if(s1==0)
- {
- Delay(1);
- if(s1==0) //
- {
- while(!s1);
- if(flag2==0)
- {
- flag=~flag;
- }
- else if(flag2==1)
- {
- Date.Minute++;
- if(Date.Minute==60)
- Date.Minute=0;
- Date.Minute=dectobcd(Date.Minute);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_MINUTE,Date.Minute); //分钟初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- }
- else if(flag2==2)
- {
- Date.Hour++;
- if(Date.Hour==24)
- Date.Hour=0;
- Date.Hour=dectobcd(Date.Hour);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_HOUR,Date.Hour); //小时初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- }
- else if(flag2==3)
- {
- Date.Year++;
- if(Date.Year==80)
- Date.Year=0;
- Date.Year=dectobcd(Date.Year);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_YEAR,Date.Year); //年初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
-
- }
- else if(flag2==4)
- {
- Date.Month++;
- if(Date.Month==13)
- Date.Month=0;
- Date.Month=dectobcd(Date.Month);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_MONTH,Date.Month); //月初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- }
- else if(flag2==5)
- {
- Date.Day++;
- if(Date.Month==2)
- {
- if(Date.Day==29)
- Date.Day=0;
- }else if(Date.Month==4 || Date.Month==6 || Date.Month==9 || Date.Month==11)
- {
- if(Date.Day==31)
- Date.Day=0;
- }
- else
- {
- if(Date.Day==32)
- Date.Day=0;
- }
-
- Date.Day=dectobcd(Date.Day);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_DAY,Date.Day); //日初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
-
- }
- else if(flag2==6)
- {
- SetTemp++;
- LCD_SetTemp(1,1,SetTemp);
- }
-
-
- }
-
-
- }
- if(s2==0)
- {
- Delay(1);
- if(s2==0) //功能键
- {
- while(!s2);
- if(flag2==0)
- {
- flag1=~flag1;
- }
- else if(flag2==1)
- {
- Date.Minute--;
- if(Date.Minute==255)
- Date.Minute=59;
- Date.Minute=dectobcd(Date.Minute);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_MINUTE,Date.Minute); //分钟初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- } else if(flag2==2)
- {
- Date.Hour--;
- if(Date.Hour==255)
- Date.Hour=23;
- Date.Hour=dectobcd(Date.Hour);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_HOUR,Date.Hour); //小时初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- }
- else if(flag2==3)
- {
- Date.Year--;
- if(Date.Year==255)
- Date.Year=79;
- Date.Year=dectobcd(Date.Year);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_YEAR,Date.Year); //年初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- }
- else if(flag2==4)
- {
- Date.Month--;
- if(Date.Month==255)
- Date.Month=12;
- Date.Month=dectobcd(Date.Month);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_MONTH,Date.Month); //月初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
- }
- else if(flag2==5)
- {
- Date.Day--;
-
- if(Date.Month==2)
- {
- if(Date.Day == 255)
- Date.Day=28;
- }else if(Date.Month==4 || Date.Month==6 || Date.Month==9 || Date.Month==11)
- {
- if(Date.Day == 255)
- Date.Day=30;
- }
- else
- {
- if(Date.Day == 255)
- Date.Day=31;
- }
-
-
- Date.Day=dectobcd(Date.Day);
- Write1302 (WRITE_PROTECT,0X00); //禁止写保护
- Write1302 (WRITE_DAY,Date.Day); //天初始化
- Write1302 (WRITE_PROTECT,0x80); //允许写保护
-
- }
- else if(flag2==6)
- {
- SetTemp--;
- LCD_SetTemp(1,1,SetTemp);
- }
-
- }
- }
- if(s3==0)
- {
- Delay(1);
- if(s3==0)
- {
- while(!s3);
- flag2++;
- if(flag2==7)
- flag2=0;
- }
- }
- switch(flag2)
- {
- case 0:Lcd1602_Write_Cmd(0x0c); break;//取消光标闪烁
- case 1:LcdShowCursor(13,1) ; break;//光标闪烁
- case 2:LcdShowCursor(10,1) ; break;//光标闪烁
- case 3:LcdShowCursor(2,0) ; break;//光标闪烁
- case 4:LcdShowCursor(5,0) ; break;//光标闪烁
- case 5:LcdShowCursor(8,0) ; break;//光标闪烁
- case 6:LcdShowCursor(3,1) ; break;//光标闪烁
-
- }
- }
-
- void Lcd_Ds1302_Display(unsigned char Hour,unsigned char Minute,unsigned char Second,unsigned char X,unsigned char Y)
- {
- unsigned char Hour_Shi,Hour_Ge;
- unsigned char Minute_Shi,Minute_Ge;
- unsigned char Second_Shi,Second_Ge;
- unsigned char tab[8];
-
- Hour_Shi = Hour/10;
- Hour_Ge = Hour%10;
- Minute_Shi = Minute/10;
- Minute_Ge = Minute%10;
- Second_Shi = Second/10;
- Second_Ge = Second%10;
-
-
- tab[0] ='0'+Hour_Shi;
- tab[1] ='0'+Hour_Ge;
- tab[2] ='-';
- tab[3] ='0'+Minute_Shi;
- tab[4] ='0'+Minute_Ge;
- tab[5] ='-';
- tab[6] ='0'+Second_Shi;
- tab[7] ='0'+Second_Ge;
-
- Lcd1602_Write_Cmd(0x0c); //取消光标闪烁
- LcdShowStr(X,Y,tab);
-
-
-
- }
- void Lcd_Ds18b20_Display(unsigned char X,unsigned char Y,int i)
- {
- unsigned char ge,shi,bai,qian;
- unsigned char tab6[6];
- if(flag2==0)
- {
- if(i>SetTemp*100)
- ji_dian_qi=0;
- else if(i<SetTemp*100)
- ji_dian_qi=1;
- }
- if(i<0)
- {
- tab6[0] ='-';
- i=-i;
- }
- else
- tab6[0] ='+';
-
- ge =i%10;
- shi=i/10%10;
- bai=i/100%10;
- qian=i/1000%10;
- tab6[5] ='0'+ge;
- tab6[4] ='0'+shi;
- tab6[3] ='.';
- tab6[2] ='0'+bai;
- tab6[1] ='0'+qian;
- Lcd1602_Write_Cmd(0x0c);//取消光标闪烁
- LcdShowStr(X,Y,tab6);
- }
- void Init_Tim()
- {
- TMOD=0X01; //启动定时器0为工作方式1
- TH0=(65536-50000)/256; //求模
- TL0=(65536-50000)%256; //求余
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
LCD DS18B20.zip
(157.35 KB, 下载次数: 95)
|