51单片机ds1302DS18b20温度时钟电路图C语言程序文档格式.docx
- 文档编号:16845883
- 上传时间:2022-11-26
- 格式:DOCX
- 页数:38
- 大小:2.49MB
51单片机ds1302DS18b20温度时钟电路图C语言程序文档格式.docx
《51单片机ds1302DS18b20温度时钟电路图C语言程序文档格式.docx》由会员分享,可在线阅读,更多相关《51单片机ds1302DS18b20温度时钟电路图C语言程序文档格式.docx(38页珍藏版)》请在冰豆网上搜索。
sbitspeak=P3^7;
//定义蜂鸣器控制口
sbitclk_ds1302=P3^6;
//定义控制DS1302的时钟线
sbitio_ds1302=P3^5;
//定义控制DS1302的串行数据
sbitrest_ds1302=P3^4;
#definesmg_dataP0//定义数码管数据口
voiddelay_3us();
//3US的延时程序
voiddelay_8us(uintt);
//8US延时基准程序
voiddelay_50us(uintt);
//延时50*T微妙函数的声明
voiddisplay1(uchardis_data);
//数码管1显示子程序
voiddisplay2(uchardis_data);
//数码管2显示子程序
voiddisplay3(uchardis_data);
//数码管3显示子程序
voiddisplay4(uchardis_data);
//数码管4显示子程序
voiddisplay5(uchardis_data);
//数码管5显示子程序
voiddisplay6(uchardis_data);
//数码管6显示子程序
voidinit_t0();
//定时器0初始化函数
voiddis_led();
//LED处理函数
voidjudge_s1();
//S1按键处理函数
voidjudge_s2();
//S2按键处理函数
voidjudge_s3();
//S3按键处理函数
voiddis(uchars6,uchars5,uchars4,uchars3,uchars2,uchars1);
//显示子程序
voiddis_san(uchars6,uchars5,uchars4,uchars3,uchars2,uchars1,ucharsan);
//闪烁显示子程序
voidjudge_dis();
//显示处理函数
voidjudge_clock();
voidset_ds1302();
//设置时间
voidget_ds1302();
//读取当前时间
voidw_1byte_ds1302(uchart);
//向DS1302写一个字节的数据
ucharr_1byte_ds1302();
//从DS1302读一个字节的数据
//***********************************************************************
//DS18B20测温函数定义
voidw_1byte_ds18b20(ucharvalue);
//向DS18B20写一个字节
ucharr_1byte_ds18b20(void);
//从DS18B20读取一个字节的数据
voidrest_ds18b20(void);
//DS18B20复位程序
voidreadtemp_ds18b20(void);
//读取温度
voiddis_temp();
//温度显示函数
//共阳数码管断码表
constuchartabl1[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
//012345
0x82,0xf8,0x80,0x90,0x86,0x87,0xFF,
//6789ETB
0xc6,0xbf,0xff};
//C-
constuchartabl3[]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
uchart0_crycle;
ucharhour_count,minute_count,second_count,msecond_count;
ucharclock_hour,clock_minute;
ucharcountdown_second;
ucharcountdown_hour,countdown_minute;
ucharclock_en;
//闹钟关闭和开启的标志,1开启,0关闭
ucharflag1,second_flag,zancun1,zancun2,zancun3;
ucharzancun4,zancun5,zancun6,zancun7;
ucharclock_flag,countdown_flag;
ucharmsecond_minute,msecond_second,msecond_msecond,msecond_flag;
//秒表相关参数
uintspeak_count;
uchartempl,temph,temp_flag;
uchart_b,t_s,t_g,t_x,temp_flag2;
//从左到右分别存储温度百位,十位,个位,小数位
uchartab23[3];
//={0x40,0x59,0x23,0x28,0x11,0x06,0x09};
//上电时默认的时间
//主程序
voidmain()
{
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();
TR0=1;
//
//set_ds1302();
//设置DS1302的初始时间
//接下来开始编写让数码管显示的程序
while
(1)
{
get_ds1302();
judge_dis();
//显示处理
judge_s1();
judge_s2();
judge_s3();
judge_clock();
//闹钟处理程序
}
}
voidtimer0()interrupt1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0_crycle++;
if(t0_crycle==2)//秒
t0_crycle=0;
msecond_flag=1;
msecond_count++;
if(msecond_count==10)//1秒
{
msecond_count=0;
second_flag=1;
}
//**************************************************
voidjudge_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;
zancun4>
=4;
zancun5=minute_count&
zancun5>
zancun6=second_count&
zancun6>
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)
case1:
dis(5,10,11,1,12,12);
//显示SET1
led1_bit=1;
led2_bit=1;
break;
case2:
dis(5,10,11,2,12,12);
//显示SET2
case3:
dis(5,10,11,3,12,12);
//显示SET3
case4:
dis(5,10,11,4,12,12);
//显示SET4
case5:
dis(5,10,11,5,12,12);
//显示SET5
case6:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,1);
case7:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,2);
case8:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,3);
case9:
//进入修改时间,时间分位个位闪烁
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,4);
case10:
//进入修改闹钟,闹钟小时十位闪烁
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,1);
case11:
//进入修改闹钟,闹钟小时个位闪烁
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,2);
case12:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,3);
case13:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,4);
case14:
//进入修改闹钟的开关
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,6);
case15:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,1);
case16:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,2);
case17:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,3);
case18:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,4);
case19:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,5);
case20:
dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,6);
case21:
countdown_second--;
if(countdown_second==255)
countdown_second=59;
countdown_minute--;
if(countdown_minute==255)
countdown_minute=59;
countdown_hour--;
if(countdown_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);
case22:
if(countdown_flag>
0&
&
countdown_flag<
7)
speak=0;
countdown_flag++;
else
case23:
dis(msecond_minute/10,msecond_minute%10,msecond_second/10,msecond_second%10,msecond_msecond%10,12);
case24:
if(msecond_flag==1)
msecond_flag=0;
msecond_msecond++;
if(msecond_msecond==10)
msecond_second++;
if(msecond_second==60)
msecond_minute++;
if(msecond_minute==100)
msecond_minute=99;
flag1=23;
case25:
dis(zancun3/10,zancun3%10,zancun2/10,zancun2%10,zancun1/10,zancun1%10);
default:
voidjudge_s1()
s1_bit=1;
//置IO为1,准备读入收据
if(s1_bit==0)//判断是否有按键按下
delay_50us
(1);
//延时,去除机械抖动
if(s1_bit==0)
case0:
flag1++;
flag1=6;
flag1=10;
flag1=15;
//系统从秒表状态复位
//系统从计数器复位
while(s1_bit==0)
}//等待按键释放
voidjudge_s2()
s2_bit=1;
if(s2_bit==0)//判断是否有按键按下
if(s2_bit==0)
switch(flag1)
//在显示SET1状态下按S2件,进入修改时间
zancun6=hour_count&
0x0f;
zancun1=zancun4*10+zancun6;
//zancun1=hour_count;
zancun6=minute_count&
zancun2=zancun5*10+zancun6;
//zancun2=minute_count;
//在显示SET2状态下按S2,进入设置闹钟
zancun1=clock_hour;
zancun2=clock_minute;
//修改时钟小时十位状态下按S2件
//修改时钟小时个位状态下按S2件
//修改时钟分钟十位状态下按S2件
//修改时钟分钟个位状态下按S2件
//zancun4=zancun1/10;
tab23[2]=zancun1/10*16+zancun1%10;
//zancun5=zancun2&
//zancun5>
tab23[1]=zancun2/10*16+zancun2%10;
hour_count=tab23[2];
minute_count=tab23[1];
second_count=0;
tab23[0]=0;
set_ds1302();
//修改闹钟小时十位状态下按S2
//修改闹钟小时个位状态下按S2
//修改闹钟分钟十位状态下按S2
//修改闹钟分钟个位状态下按S2
//修改闹钟使能状态下按S2
clock_hour=zancun1;
clock_minute=zancun2;
clock_en=zancun3;
zancun1=countdown_hour;
zancun2=countdown_minute;
zancun3=countdown_second;
countdown_hour=zancun1;
countdown_minute=zancun2;
countdown_second=zancun3;
flag1=21;
//秒表暂停
msecond
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 ds1302DS18b20 温度 时钟 电路图 语言 程序