12864液晶ds1302时间和ds18b20温度程序仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- #include <ds18b20.h>
- #include <word.h>
- #include <12864display.h>
- #include <ds1302.h>
- //声明外部温度读数数组
- extern unsigned char data temperature[2];
- //声明外部高低温报警
- extern char Alarm_Temp_HL[2];
- //存放分解的7个ASCII码温度数据
- unsigned char temp[7];
- bit HI_Alarm = 0,LO_Alarm = 0; //高温低温报警标志位
- sbit HI_LED = P1^4; //高温报警灯
- sbit LO_LED = P1^5; //低温报警灯
- sbit BUZZER = P2^6; //喇叭
- sbit button = P1^0; //按钮切换显示
- sbit button_shi=P1^1; //调节时
- sbit button_fen=P1^2; //分
- sbit button_miao=P1^3; //秒
- sbit button_year=P1^6; //年
- sbit button_month=P1^7; //月
- sbit button_day=P3^0; //日
- sbit button_week=P3^7; //星期
- //按钮的7个变量
- unsigned char count1,count2,count3,count4,count5,count6,count7;
- //根据输入的tem在12864的第1行的pos位置显示相应的0~9
- void trans(unsigned char hang,unsigned char tem,unsigned int pos)
- {
- switch(tem)
- {
- case '0':vShowOneChar(hang,pos,uca_0);break;
- case '1':vShowOneChar(hang,pos,uca_1);break;
- case '2':vShowOneChar(hang,pos,uca_2);break;
- case '3':vShowOneChar(hang,pos,uca_3);break;
- case '4':vShowOneChar(hang,pos,uca_4);break;
- case '5':vShowOneChar(hang,pos,uca_5);break;
- case '6':vShowOneChar(hang,pos,uca_6);break;
- case '7':vShowOneChar(hang,pos,uca_7);break;
- case '8':vShowOneChar(hang,pos,uca_8);break;
- case '9':vShowOneChar(hang,pos,uca_9);break;
- default:break;
- }
- }
- //l2864显示
- void display()
- {
- unsigned char temp_data,temp_data2;
- unsigned int TempDec; //用来存放4位小数
- temp_data = temperature[1]&0xf8;//取高5位
-
- if (temp_data==0xf8) //负温度读数
- {
- temperature[0]=~temperature[0]+1;
- temperature[1]=~temperature[1];
- if (temperature[0]==0x00)
- temperature[1]++;
- temp_data = (temperature[1]&0x07)<<4; //取高字节低3位(温度读数高3位)
- temp_data2 = (temperature[0]&0xf0)>>4; //取低字节高4位(温度读数低4位)
- temp_data= temp_data|temp_data2; //组合成完整数据
- if(temp_data>=Alarm_Temp_HL[1])
- {
- TF0=1;
- LO_Alarm=1;
- HI_Alarm=0;
- }
- else
- {
- TF0=1;
- LO_Alarm=0;
- HI_Alarm=0;
- }
- vShowOneChar(0,40,uca_fu);
- }
- else
- {
- temp_data = (temperature[1]&0x07)<<4; //取高字节低3位(温度读数高3位)
- temp_data2 = (temperature[0]&0xf0)>>4; //取低字节高4位(温度读数低4位)
- temp_data= temp_data|temp_data2; //组合成完整数据
- if(temp_data>=Alarm_Temp_HL[0])
- {
- TF0=1;
- HI_Alarm=1;
- LO_Alarm=0;
- }
- else
- {
- TF0=1;
- HI_Alarm=0;
- LO_Alarm=0;
- }
- vShowOneChar(0,40,uca_zheng);
- }
- temp[0] = temp_data/100+0x30; //取百位转换为ASCII码
- temp[1] = (temp_data%100)/10+0x30; //取十位转换为ASCII码
- temp[2] = (temp_data%100)%10+0x30; //取个位转换为ASCII码
- temperature[0]&=0x0f; //取小数位转换为ASCII码
- TempDec = (temperature[0]*0.0625)*10000;//小数部分,扩大1万倍,1/16,最低分辨率 取四位小数
- temp[3] = TempDec/1000+0x30; //取小数个位转换为ASCII码
- temp[4] = (TempDec%1000)/100+0x30; //取小数十位转换为ASCII码
- temp[5] = ((TempDec%1000)%100)/10+0x30;//取小数百位转换为ASCII码
- temp[6] = ((TempDec%1000)%100)%10+0x30;//取小数千位转换为ASCII码
- trans(0,temp[0],48);
- trans(0,temp[1],56);
- trans(0,temp[2],64);
- vShowOneChar(0,72,uca_dian); //小数点
- trans(0,temp[3],80);
- trans(0,temp[4],88);
- trans(0,temp[5],96);
- trans(0,temp[6],104);
- }
- //中断,温度过高过低对应的灯亮
- void T0_INT() interrupt 1
- {
- TF0=0;
- if(HI_Alarm)
- {
- HI_LED=0;
- BUZZER=1;
- }
- else
- {
- HI_LED=1;
- }
- if(LO_Alarm)
- {
- LO_LED=0;
- BUZZER=1;
- }
- else
- {
- LO_LED=1;
- }
- }
- void show_time(unsigned char time,unsigned char hang,unsigned char pos1,unsigned char pos2)
- {
- unsigned char i,j;
- i=time/10+0x30;
- j=time%10+0x30;
- trans(hang,i,pos1);
- trans(hang,j,pos2);
- }
- //主函数
- void main()
- {
- unsigned char shi,fen,miao,year,month,day,week;
- DS1820_Reset();//DS1820初始化
- Set_Alarm_Temp_Value();
- IE=0x82;TMOD=0x02; //开中断
- vLCDInitialize();
- delay100us();
- while(button==1)
- {
- vShowOneChin(6,16,uca_kb);
- vShowOneChin(6,32,uca_kb);
- vShowOneChin(6,80,uca_kb);
- vShowOneChin(6,96,uca_kb);
- vShowOneChin(4,32,uca_kb);
- vShowOneChin(4,48,uca_kb);
- vShowOneChin(4,64,uca_kb);
- vShowOneChin(4,80,uca_kb);
- vShowOneChin(4,16,uca_kb);
- vShowOneChin(4,96,uca_kb);
- vShowOneChin(2,32,uca_kb);
- vShowOneChin(2,48,uca_kb);
- vShowOneChin(2,64,uca_kb);
- vShowOneChin(2,80,uca_kb);
-
- vShowOneChin(0,0,uca_wen);
- vShowOneChin(0,16,uca_du);
- vShowOneChar(0,32,uca_mh);
- vShowOneChar(0,112,uca_o);
- vShowOneChar(0,120,uca_C);
- read_temp(); //读取温度值
- delay100us();
- display(); //显示温度值
- delay100us();
- BUZZER=0;
- }
-
- while(button==0)
- {
- //判断按键
- if(button_shi==0)
- {
- while(button_shi==0);
- count1++;
- }
- if(button_fen==0)
- {
- while(button_fen==0);
- count2++;
- }
- if(button_miao==0)
- {
- while(button_miao==0);
- count3++;
- }
- if(button_year==0)
- {
- while(button_year==0);
- count4++;
- }
- if(button_month==0)
- {
- while(button_month==0);
- count5++;
- }
- if(button_day==0)
- {
- while(button_day==0);
- count6++;
- }
- if(button_week==0)
- {
- while(button_week==0);
- count7++;
- }
- shi=ReadSet1302(0x85);
- shi=((shi&0x70)>>4)*10+(shi&0x0f)+count1; //高三个字节是十位,低4个字节为个位
- if(shi>=24)
- shi=shi-24;
- fen=ReadSet1302(0x83);
- fen=((fen&0x70)>>4)*10+(fen&0x0f)+count2;
- if(fen>=60)
- fen=fen-60;
- miao=ReadSet1302(0x81);
- miao=((miao&0x70)>>4)*10+(miao&0x0f)+count3;
- if(miao>=60)
- miao=miao-60;
- year=ReadSet1302(0x8d);
- year=((year&0x70)>>4)*10+(year&0x0f)+count4;
- month=ReadSet1302(0x89);
- month=((month&0x70)>>4)*10+(month&0x0f)+count5;
- if(month>=13)
- month=month-12;
- day=ReadSet1302(0x87);
- day=((day&0x70)>>4)*10+(day&0x0f)+count6;
- if(day>=32)
- day=day-31;
- week=ReadSet1302(0x8b);
- week=((week&0x70)>>4)*10+(week&0x0f)+0x30+count7;
- if(week>=0x38)
- {
- week=week-0x07;
- count7=0;
- }
- if(week==0x31)
- week=week+6;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
12864ds1302时间和ds18b20温度程序和仿真图.zip
(235.82 KB, 下载次数: 199)
|