51单片机+ds1302+DS18b20温度时钟(电路图+C语言程序) 下载本文

void get_ds1302();//读取当前时间

void w_1byte_ds1302(uchar t);//向DS1302写一个字节的数据 uchar r_1byte_ds1302();//从DS1302读一个字节的数据

//***********************************************************************

//DS18B20测温函数定义

void w_1byte_ds18b20(uchar value);//向DS18B20写一个字节 uchar r_1byte_ds18b20(void);//从DS18B20读取一个字节的数据 void rest_ds18b20(void);//DS18B20复位程序 void readtemp_ds18b20(void);//读取温度 void dis_temp();//温度显示函数

//共阳数码管断码表

const uchar tabl1[16]={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,0xff}; // 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开启,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;//从左到右分别存储温度百位,十位,个位,小数位

uchar tab23[3];//={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(); TR0=1;//

// set_ds1302();//设置DS1302的初始时间 //接下来开始编写让数码管显示的程序 while(1) {

get_ds1302();

judge_dis();//显示处理 judge_s1(); judge_s2(); judge_s3();

judge_clock();//闹钟处理程序 } }

void timer0() interrupt 1 {

TH0=(65536-50000)/256; TL0=(65536-50000)%6; t0_crycle++;

if(t0_crycle==2)// 0.1秒 {

t0_crycle=0;

msecond_flag=1; msecond_count++;

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; 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,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,zancun2/10,zancun2,12,12,1); break; case 7:

dis_san(zancun1/10,zancun1,zancun2/10,zancun2,12,12,2); break; case 8:

dis_san(zancun1/10,zancun1,zancun2/10,zancun2,12,12,3); break;

case 9://进入修改时间,时间分位个位闪烁

dis_san(zancun1/10,zancun1,zancun2/10,zancun2,12,12,4); break;

case 10://进入修改闹钟,闹钟小时十位闪烁

dis_san(zancun1/10,zancun1,zancun2/10,zancun2,12,zancun3,1); break;

case 11://进入修改闹钟,闹钟小时个位闪烁

dis_san(zancun1/10,zancun1,zancun2/10,zancun2,12,zancun3,2); break;

case 12://进入修改闹钟,闹钟小时十位闪烁

dis_san(zancun1/10,zancun1,zancun2/10,zancun2,12,zancun3,3); break;

case 13://进入修改闹钟,闹钟小时个位闪烁