当前位置:首页 > 基于单片机的数字电子时钟 - 图文
{ if(addr&0x01) { sda=1;
} else { sda=0; } sck=1; sck=0; addr=addr>>1;
}
for(i=0;i<8;i++) { if(d&0x01) { sda=1; } else { sda=0; } sck=1; sck=0; d=d>>1;
} rst=0;
}
unsigned char ds1302readbyte(unsigned char addr) { unsigned char i; unsigned char temp; rst=1;
addr=addr|0x01; for(i=0;i<8;i++) { if(addr&0x01) { sda=1;
} else {
sda=0;
22
}
}
} sck=1; sck=0; addr=addr>>1;
for(i=0;i<8;i++) { } rst=0; return temp;
temp=temp>>1; if(sda) { } else { } sck=1; sck=0;
temp&=0x7F; temp|=0x80;
void ds1302write(void) {
unsigned char i,tmp; }
void ds1302read(void)
23
for(i=0;i<8;i++) { }
ds1302writebyte(ds1302_control_add,0x00); ds1302writebyte(ds1302_sec_add,0x80);
ds1302writebyte(ds1302_year_add,time_buf[1]); ds1302writebyte(ds1302_date_add,time_buf[3]); ds1302writebyte(ds1302_day_add,time_buf[7]); ds1302writebyte(ds1302_hr_add,time_buf[4]); ds1302writebyte(ds1302_min_add,time_buf[5]); ds1302writebyte(ds1302_sec_add,time_buf[6]); ds1302writebyte(ds1302_day_add,time_buf[7]); ds1302writebyte(ds1302_control_add,0x80);
//关闭写保护 //年 //日 //周 //时 //分 //秒 //周
//暂停
tmp=time_buf1[i]/10; time_buf[i]=time_buf1[i]; time_buf[i]=time_buf[i]+tmp*16;
ds1302writebyte(ds1302_month_add,time_buf[2]); //月
{
unsigned char i,tmp; time_buf[1]=ds1302readbyte(ds1302_year_add); time_buf[3]=ds1302readbyte(ds1302_date_add); time_buf[4]=ds1302readbyte(ds1302_hr_add); time_buf[5]=ds1302readbyte(ds1302_min_add); time_buf[7]=ds1302readbyte(ds1302_day_add);
//年 //月 //日 //时 //分 //周
time_buf[2]=ds1302readbyte(ds1302_month_add);
time_buf[6]=(ds1302readbyte(ds1302_sec_add))&0x7F;//秒
for(i=0;i<8;i++) { tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]; time_buf1[i]=time_buf1[i]+tmp*10;
}
}
void ds1302init(void) { rst=0; sck=0;
ds1302writebyte(ds1302_sec_add,0x00); }
//DS18B20模块程序 #include\#include\bit Init_DS18B20(void) { bit dat=0; DQ = 1; DelayUs2x(5); DQ = 0; DelayUs2x(200); DelayUs2x(200); DQ = 1; DelayUs2x(50); dat=DQ; DelayUs2x(25); return dat; }
unsigned char ReadOneChar(void) {
unsigned char i=0; unsigned char dat = 0; for (i=8;i>0;i--)
24
{ DQ = 0; dat>>=1; DQ = 1; if(DQ) dat|=0x80; DelayUs2x(25); }
return(dat); }
void WriteOneChar(unsigned char dat) {
unsigned char i=0; for (i=8; i>0; i--) { DQ = 0; DQ = dat&0x01; DelayUs2x(25); DQ = 1; dat>>=1; }
DelayUs2x(25); }
unsigned int ReadTemperature(void) { unsigned char a=0; unsigned int b=0; float t=0; Init_DS18B20(); WriteOneChar(0xCC); WriteOneChar(0x44); delay(10); Init_DS18B20(); WriteOneChar(0xCC); WriteOneChar(0xBE); a=ReadOneChar(); b=ReadOneChar(); b<<=8; t=a+b; return(t);
}
//按键模块程序
25
共分享92篇相关文档