51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc

上传人:scccc 文档编号:12013344 上传时间:2021-12-01 格式:DOC 页数:55 大小:2.88MB
返回 下载 相关 举报
51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc_第1页
第1页 / 共55页
51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc_第2页
第2页 / 共55页
51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc_第3页
第3页 / 共55页
51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc_第4页
第4页 / 共55页
51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc_第5页
第5页 / 共55页
点击查看更多>>
资源描述

《51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc》由会员分享,可在线阅读,更多相关《51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序).doc(55页珍藏版)》请在三一文库上搜索。

1、时钟电路图:PCB板:推荐精选单片机程序:/*= 调试要求: 1.MCU:AT89S52芯片或AT89C52 2.晶振:12MHz 功能:多功能时钟+温度计 =*/#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned intsbit dis_bit1=P27;/定义数码管控制口sbit dis_bit2=P26;/定义数码管控制口sbit dis_bit3=P24;/定义数码管控制口sbit dis_bit4=P23;/定义数码管控制口sbit dis

2、_bit5=P21;/定义数码管控制口sbit dis_bit6=P20;/定义数码管控制口sbit led1_bit=P22;/定时LED的控制口推荐精选sbit led2_bit=P25;/定时LED的控制口sbit s1_bit=P10; /定义S1控制口sbit s2_bit=P11; /定义S2控制口sbit s3_bit=P12; /定义S3控制口sbit dq_ds18b20=P33;/定义控制DS18B20sbit speak=P37; /定义蜂鸣器控制口sbit clk_ds1302=P36;/定义控制DS1302的时钟线sbit io_ds1302=P35;/定义控制DS1

3、302的串行数据sbit rest_ds1302=P34;#define smg_data P0/定义数码管数据口void delay_3us();/3US的延时程序void delay_8us(uint t);/8US延时基准程序void delay_50us(uint t);/延时50*T微妙函数的声明void display1(uchar dis_data);/数码管1显示子程序void display2(uchar dis_data);/数码管2显示子程序void display3(uchar dis_data);/数码管3显示子程序void display4(uchar dis_da

4、ta);/数码管4显示子程序void display5(uchar dis_data);/数码管5显示子程序void display6(uchar dis_data);/数码管6显示子程序void init_t0();/定时器0初始化函数void dis_led();/LED处理函数void judge_s1();/S1按键处理函数void judge_s2();/S2按键处理函数void judge_s3();/S3按键处理函数void dis(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1);/显示子程序void dis_san(

5、uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1,uchar san);/闪烁显示子程序void judge_dis();/显示处理函数void judge_clock();/显示处理函数void set_ds1302();/设置时间void get_ds1302();/读取当前时间void w_1byte_ds1302(uchar t);/向DS1302写一个字节的数据uchar r_1byte_ds1302();/从DS1302读一个字节的数据/*/DS18B20测温函数定义void w_1byte_ds18b20(uchar va

6、lue);/向DS18B20写一个字节 uchar r_1byte_ds18b20(void);/从DS18B20读取一个字节的数据void rest_ds18b20(void);/DS18B20复位程序void readtemp_ds18b20(void);/读取温度 void dis_temp();/温度显示函数推荐精选/共阳数码管断码表const uchar tabl116=0xc0,0xf9,0xa4,0xb0,0x99,0x92, / 0 1 2 3 4 5 0x82,0xf8,0x80,0x90,0x86,0x87,0xFF, /6 7 8 9 E T B 0xc6,0xbf,0x

7、ff; / C -const uchar tabl3=0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09; uchar t0_crycle;uchar hour_count,minute_count,second_count,msecond_count;uchar clock_hour,clock_minute;uchar countdown_second;uchar countdown_hour,countdown_minute;uchar clock_en;/闹钟关闭和开启的标志,1

8、开启,0关闭uchar flag1,second_flag,zancun1,zancun2,zancun3;uchar zancun4,zancun5,zancun6,zancun7;uchar clock_flag,countdown_flag;uchar msecond_minute,msecond_second,msecond_msecond,msecond_flag;/秒表相关参数uint speak_count;uchar templ,temph,temp_flag;uchar t_b,t_s,t_g,t_x,temp_flag2;/从左到右分别存储温度百位,十位,个位,小数位uch

9、ar tab233;/=0x40,0x59,0x23,0x28,0x11,0x06,0x09;/上电时默认的时间/主程序void main() P3=0x00; flag1=0; zancun3=0; msecond_minute=0;/置秒表相关参数为0 msecond_second=0; msecond_msecond=0; speak=1;/关闭蜂鸣器 speak_count=0; clock_hour=0; clock_minute=0;推荐精选 clock_flag=0; countdown_flag=0;/倒计时标志位为0 clock_en=0;/开机时默认关闭闹钟 init_t0

10、(); TR0=1;/ / set_ds1302();/设置DS1302的初始时间 /接下来开始编写让数码管显示的程序 while(1) get_ds1302(); judge_dis();/显示处理 judge_s1(); judge_s2(); judge_s3(); judge_clock();/闹钟处理程序 void timer0() interrupt 1TH0=(65536-50000)/256;TL0=(65536-50000)%256;t0_crycle+;if(t0_crycle=2)/ 0.1秒 t0_crycle=0; msecond_flag=1; msecond_co

11、unt+; if(msecond_count=10)/1秒 msecond_count=0; second_flag=1; /*/显示处理函数void judge_dis() if(flag1=0) if(second_flag=1) zancun7+; second_flag=0;推荐精选 if(zancun7<1) if(temp_flag2=1) readtemp_ds18b20();/读取温度 temp_flag2=0; dis_temp();/温度显示函数 if(zancun7>=1) temp_flag2=1; zancun4=hour_count&0xf0;

12、zancun4>>=4; zancun5=minute_count&0xf0; zancun5>>=4; zancun6=second_count&0xf0; zancun6>>=4; dis(zancun4,hour_count&0x0f,zancun5,minute_count&0x0f,zancun6,second_count&0x0f); dis_led(); if(zancun7=5)zancun7=0; if(flag1!=0) switch(flag1) case 1: dis(5,10,11,1,12,

13、12);/显示SET1 led1_bit=1; led2_bit=1; break; case 2: dis(5,10,11,2,12,12);/显示SET2 break; case 3: dis(5,10,11,3,12,12);/显示SET3 break; case 4: dis(5,10,11,4,12,12);/显示SET4推荐精选 break; case 5: dis(5,10,11,5,12,12);/显示SET5 break; case 6: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,1); break;

14、case 7: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,2); break; case 8: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,3); break; case 9:/进入修改时间,时间分位个位闪烁 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,4); break; case 10:/进入修改闹钟,闹钟小时十位闪烁 dis_san(zancun1/10,zancun1%1

15、0,zancun2/10,zancun2%10,12,zancun3,1); break; case 11:/进入修改闹钟,闹钟小时个位闪烁 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,2); break; case 12:/进入修改闹钟,闹钟小时十位闪烁 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,3); break; case 13:/进入修改闹钟,闹钟小时个位闪烁 dis_san(zancun1/10,zancun1%10,z

16、ancun2/10,zancun2%10,12,zancun3,4); break; case 14:/进入修改闹钟的开关 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,6); break; case 15: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,1);推荐精选 break; case 16: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancu

17、n3/10,zancun3%10,2); break; case 17: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,3); break; case 18: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,4); break; case 19: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,5

18、); break; case 20: dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,6); break; case 21: if(second_flag=1) second_flag=0; countdown_second-; if(countdown_second=255) countdown_second=59; countdown_minute-; if(countdown_minute=255) countdown_minute=59; countdown_hour-; if(coun

19、tdown_hour=255) flag1=22; countdown_minute=0; countdown_hour=0;推荐精选 countdown_second=0; countdown_flag=1; dis(countdown_hour/10,countdown_hour%10,countdown_minute/10,countdown_minute%10,countdown_second/10,countdown_second%10);/ break; case 22: if(countdown_flag>0 && countdown_flag<7)

20、speak=0; if(second_flag=1) second_flag=0; countdown_flag+; else speak=1; dis(countdown_hour/10,countdown_hour%10,countdown_minute/10,countdown_minute%10,countdown_second/10,countdown_second%10);/ break; case 23: dis(msecond_minute/10,msecond_minute%10,msecond_second/10,msecond_second%10,msecond_msec

21、ond%10,12); break; case 24: if(msecond_flag=1) msecond_flag=0; msecond_msecond+; if(msecond_msecond=10) msecond_msecond=0; msecond_second+; if(msecond_second=60)推荐精选 msecond_second=0; msecond_minute+; if(msecond_minute=100) msecond_minute=99; flag1=23; dis(msecond_minute/10,msecond_minute%10,msecond

22、_second/10,msecond_second%10,msecond_msecond%10,12); break; case 25: dis(zancun3/10,zancun3%10,zancun2/10,zancun2%10,zancun1/10,zancun1%10); break; default: break; /*/S1按键处理函数void judge_s1() s1_bit=1;/置IO为1,准备读入收据 if(s1_bit=0)/判断是否有按键按下 delay_50us(1);/ 延时,去除机械抖动 if(s1_bit=0) switch(flag1) case 0: ca

23、se 1: case 2: case 3: case 4: case 6: case 7:推荐精选 case 8: case 10: case 11: case 12: case 13: case 15: case 16: case 17: case 18: case 19: flag1+; break; case 9: flag1=6; break; case 14: flag1=10; break; case 20: flag1=15; break; case 5: case 21: case 22: case 23:/系统从秒表状态复位 case 24:/系统从秒表状态复位 case 2

24、5:/系统从计数器复位 flag1=0; break; default: break; while(s1_bit=0) judge_dis(); /等待按键释放 /*/S2按键处理函数void judge_s2() s2_bit=1;/置IO为1,准备读入收据推荐精选 if(s2_bit=0)/判断是否有按键按下 delay_50us(1);/ 延时,去除机械抖动 if(s2_bit=0) switch (flag1) case 1: /在显示SET1状态下按S2件,进入修改时间 flag1=6; zancun4=hour_count&0xf0; zancun4>>=4;

25、zancun6=hour_count&0x0f; zancun1=zancun4*10+zancun6; /zancun1=hour_count; zancun5=minute_count&0xf0; zancun5>>=4; zancun6=minute_count&0x0f; zancun2=zancun5*10+zancun6; / zancun2=minute_count; break; case 2:/在显示SET2状态下按S2,进入设置闹钟 zancun1=clock_hour; zancun2=clock_minute; flag1=10; b

26、reak; case 6:/修改时钟小时十位状态下按S2件 case 7:/修改时钟小时个位状态下按S2件 case 8:/修改时钟分钟十位状态下按S2件 case 9:/修改时钟分钟个位状态下按S2件 /zancun4=zancun1/10; tab232=zancun1/10*16+zancun1%10; /zancun5=zancun2&0xf0; /zancun5>>=4; tab231=zancun2/10*16+zancun2%10; hour_count=tab232; minute_count=tab231; second_count=0; tab230=0

27、; set_ds1302();/设置DS1302的初始时间 flag1=0; break; case 10:/修改闹钟小时十位状态下按S2推荐精选 case 11:/修改闹钟小时个位状态下按S2 case 12:/修改闹钟分钟十位状态下按S2 case 13:/修改闹钟分钟个位状态下按S2 case 14:/修改闹钟使能状态下按S2 clock_hour=zancun1; clock_minute=zancun2; clock_en=zancun3; flag1=0; break; case 3: flag1=15; zancun1=countdown_hour; zancun2=countd

28、own_minute; zancun3=countdown_second; break; case 15: case 16: case 17: case 18: case 19: case 20: countdown_hour=zancun1; countdown_minute=zancun2; countdown_second=zancun3; flag1=21; countdown_flag=0; break; case 22: flag1=21; break; case 21: flag1=22; break; case 4: flag1=23;/秒表暂停 msecond_minute=0; msecond_second=0; msecond_msecond=0; break; case 23: flag1=24; break; case 24: flag1=23;推荐精选 break; case 5: flag1=25;/进入计数器模式

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1