




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Diap.c#include reg52.h#include intrins.h#define uchar unsigned char bit Display_Flag=0;sbit SH_CP=P14;sbit DS=P15;sbit ST_CP=P16;void write_74hc595(uchar *content) uchar i=18,j;while(i-)for(j=0;j8;j+)DS=(bit)(contentij&0x80);SH_CP=0;_nop_();SH_CP=1; /移位时钟脉冲上升沿移位ST_CP=0;_nop_();ST_CP=1; /上升沿将数据送到数据锁存器_nop_();ST_CP=0; /锁存显示数据void show(uchar *content)uchar code data_code=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;uchar Disp_CODE18;uchar i;for(i=0;i17;i+)if(i=14&content14=0)Disp_CODE14=0x80;continue;Disp_CODEi=data_codecontenti;Disp_CODE17=content17;write_74hc595(Disp_CODE);uchar Disp_temp18=2,0,1,2, 0,1, 2,0, 2,2, 0,9, 3,0, 5, 2,7, 0xff;ds18b20.c#include reg52.h#include intrins.h#define uchar unsigned char #define uint unsigned int #define delayNOP() _nop_();_nop_();_nop_();_nop_();sbit DQ=P13; /DS18B20数据线bit DS18B20_IS_OK=1; /传感器正常标志bit DS18B20_Flag=0; /温度负数标志void delayus(uchar us)while(us)-us;/初始化DS18B20uchar initialize_ds18b20(void)uchar status;DQ=1; delayus(8);DQ=0; delayus(90);DQ=1; delayus(8);status=DQ;delayus(100);DQ=1;return status; /初始化成功时返回0/读一字节uchar readbyte(void)uchar i,dat=0;DQ=1; _nop_();for(i=0;i=1; DQ=1; _nop_(); _nop_();if(DQ)dat|=0x80; delayus(30); DQ=1;return dat;/写一字节void writebyte(uchar dat)uchar i;for(i=0;i=1;void convert(void)initialize_ds18b20();DS18B20_IS_OK=0;if(initialize_ds18b20()writebyte(0xcc);writebyte(0x44);DS18B20_IS_OK=1;char readtemp(void)uchar temp1,temp2,temp3;convert();if(DS18B20_IS_OK)initialize_ds18b20();writebyte(0xcc);writebyte(0xbe);temp1=(readbyte()&0xf0)4;temp2=(readbyte()&0x07)4;temp3=temp1+temp2;DS18B20_Flag=0;if(temp2&0x80)temp3=256-temp3; DS18B20_Flag=1;return(temp3);Ds1302.c#include reg52.h#define uchar unsigned char #define uint unsigned int sbit SDA=P10;sbit CLK=P11;sbit RST=P12;uchar DateTime7; /所读取的日期时间/一年中每个月的天数,2月的天数由年份决定uchar MonthsDays=0,31,0,31,30,31,30,31,31,30,31,30,31;/向DS1302写入一字节void Write_A_Byte_TO_DS1302(uchar x)uchar i;for(i=0;i=1;/从DS1302读取一字节uchar Get_A_Byte_FROM_DS1302(void)uchar i,b,t;for(i=0;i=1; t=SDA; b|=t7; CLK=1; CLK=0;/BCD码转换return(b/16*10+b%16);/从DS1302指定位置读数据uchar Read_Data(uchar addr)uchar dat;RST=0; CLK=0; RST=1;Write_A_Byte_TO_DS1302(addr);dat=Get_A_Byte_FROM_DS1302();CLK=1; RST=0;return(dat);/向DS1302某地址写入数据void Write_DS1302(uchar addr,uchar dat)CLK=0; RST=1;Write_A_Byte_TO_DS1302(addr);Write_A_Byte_TO_DS1302(dat);CLK=0; RST=0;/设置时间void SET_DS1302(void)uchar i;/写控制字,取消写保护Write_DS1302(0x8e,0x00);/分时日月年依次写入for(i=1;i7;i+)/分的起始地址1000 0010(0x82),后面续依次是时,日,月,年,写入地址每次递增2Write_DS1302(0x80+2*i,(DateTimei/104)|(DateTimei%10);Write_DS1302(0x8e,0x80); /加保护 /读取当前日期时间void GetTime(void)uchar i;for(i=0;i7;i+)DateTimei=Read_Data(0x81+2*i);/判断是否为闰年uchar isLeapYear(uint y)return(y%4=0&y%100!=0)|(y%400=0);/-/求自2000.1.1开始的任何一天是星期几/函数没有通过,求出总天数后在求出星期几/因为求总天数可能会月出uint的范围/-void RefreshWeekDay(void)uint i,d,w=5; /已知1999.12.31是周五for(i=2000;i2000+DateTime6;i+)d=isLeapYear(i)?366:365;w=(w+d)%7;d=0;for(i=1;iDateTime4;i+)d+=MonthsDaysi;d+=DateTime3;/保存星期,06表示星期日,星期一,二,、六,为了与DS1302的星期格式匹配,返回值需要加1DateTime5=(w+d)%7+1;/日期与时间转换为数字字符void Format_DateTime(uchar d,uchar *a)a0=d/10; a1=d%10;Main.c#include disp.c#include DS1302.c#include string.h#include DS18B20.cuchar tCount=0;uchar Adjust_Index=-1;sbit sb1=P33;sbit sb2=P34;sbit sb3=P35;sbit sb4=P36;/年月日时分秒+/-void DateTime_Adjust(char x)switch(Adjust_Index)case 6: /年00-99if(x=1&DateTime60)DateTime6-;/获取2月天数MonthsDays2=isLeapYear(2000+DateTime6)?29:28;/如果年份变化后当前月份的天数大于上限则设为上限if(DateTime3MonthsDaysDateTime4)DateTime3=MonthsDaysDateTime4;break;case 4: /月01-12if(x=1&DateTime41)DateTime4-;/获取2月份天数MonthsDays2=isLeapYear(2000+DateTime6)?29:28;/如果月份变化后当前月份的天数大于上限则设为上限if(DateTime3MonthsDaysDateTime4)DateTime3=MonthsDaysDateTime4;break;case 3: /日00-28/29/30/31;调节之前首先根据年份得出该年中2月的天数MonthsDays2=isLeapYear(2000+DateTime6)?29:28;/根据当前月份决定调节日期的上限if(x=1&DateTime31)DateTime3-;break;case 2: /时if(x=1&DateTime21)DateTime2-;break;case 1: /分if(x=1&DateTime11)DateTime1-;break;case 0: /秒if(x=1&DateTime01)DateTime0-;/键盘中断(INT0)void EX_INT0(void) interrupt 0if(sb1=0) /选择调整对象if(Adjust_Index=-1|Adjust_Index=0)Adjust_Index=7;Adjust_Index-;if(Adjust_Index=5)Adjust_Index=4; /跳过对星期的调整else if(sb2=0)DateTime_Adjust(1); /加else if(sb3=0)DateTime_Adjust(-1); /减else if(sb4=0) /确定SET_DS1302(); /将调整后的时间写入DS1302Adjust_Index=-1; /操作索引重设为-1,时间继续正常显示/定时器0每秒刷新LCD显示void T0_INT(void) interrupt 1TH0=(65536-50000)/256;TL0=(65536-50000)%256;if(+tCount10)return;tCount=0;Display_Flag=Display_Flag;if(Adjust_Index=-1) /如果未执行调整操作则正常读取当前时间Disp_temp17=Disp_temp17; GetTime();/更新星期RefreshWeekDay();/按指定格式生成待显示的日期时间串Format_DateTime(DateTime6,Disp_temp+2);Format_DateTime(DateTime4,Disp_temp+4);Format_DateTime(DateTime3,Disp_temp+6);/星期Disp_temp14=DateTime5-1;/时分秒Format_DateTime(DateTime2,Disp_temp+8);Format_DateTime(DateTime1,Disp_temp+10);Format_DateTime(DateTime0,Disp_temp+12);/读取温度if(DS18B20_IS_OK)Disp_temp15=readtemp()/10;Disp_temp16=readtemp()%10;if(Adjust_Index=6) /年if(Display_Flag=1)Disp_temp2=Disp_temp3=10;if(Adjust_Index=4) /月if(Display_Flag=1)Disp_temp4=Disp_temp5=10;if(Adjust_Index=3) /日if(Display_Flag=1)Disp_temp6=Disp_temp7=10;if(Adjust_Index=2) /时if(Display_Flag=1)Disp_temp8
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年 广州医科大学附属第一医院招聘笔试试题附答案
- 2025年 东莞市望牛墩镇招聘机关事业单位考试试题附答案
- “丝绸之路”丝路文化戏剧商业计划书
- 天然气化工项目可行性研究报告
- 中国苗圃行业市场运营现状及投资战略咨询报告
- 2025-2030年中国席卡夹项目投资可行性研究分析报告
- 中国单反行业市场全景监测及投资前景展望报告
- 中国蜂制品行业市场深度研究及投资规划建议报告
- 信息与计算机工程学院080400仪器科学与技术报录数据分析报告
- 中国清洁能源行业市场调查报告
- 水电工培训试题及答案
- 乌鲁木齐市既有建筑改造消防设计审查工作指南
- 2025至2030中国混凝土外加剂市场供需发展及经营管理风险预警报告
- 青海中考地理试题及答案
- 《中心静脉导管的护理》课件
- 城市轨道交通应急处理自然灾害应急处理课件
- 新疆维吾尔自治区2024年普通高校招生普通类国家及地方专项、南疆单列、对口援疆计划 本科二批次投档情况 (理工)
- 基础会计教学质量分析报告
- 《宏观经济学原理》课件
- 2025新人教版七下英语单词默写表
- 2024年保山市小升初英语考试模拟试题及答案解析
评论
0/150
提交评论