rest_ds18b20();
w_1byte_ds18b20(0xcc); //跳过读序列号的操作 w_1byte_ds18b20(0xbe); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
templ=r_1byte_ds18b20(); temph=r_1byte_ds18b20();
if((temph&0xf0))//判断温度的正负性 {
temp_flag=0;//温度为负数标志 temph=-temph; templ=-templ;
t_x=tabl3[templ & 0x0f];//计算温度的小数 temp32=temph & 0x0f; temp32<<=4; templ>>=4;
temp32=temp32 | templ;
t_b=temp32/100;//计算温度的百位数据 t_s=temp32/10;//计算温度的十位数据 t_g=temp32;//计算温度的个位数据 }
else//为正数 {
t_x=tabl3[templ & 0x0f];//计算温度的小数 temp32=temph & 0x0f; temp32<<=4; templ>>=4;
temp32=temp32 | templ;
t_b=temp32/100;//计算温度的百位数据 t_s=temp32/10;//计算温度的十位数据 t_g=temp32;//计算温度的个位数据 temp_flag=1; } }
void dis_temp()//温度显示函数 {
if(temp_flag=1) {
if(t_b==0) {
dis(12,12,t_s,t_g,13,12); } else {
dis(12,t_b,t_s,t_g,13,12);
} } else {
dis(14,t_b,t_s,t_g,13,12); } }
//;##############################################################################
//;子程序名:w_1byte_ds1302
//;功能: 向DS1302写一个字节的数据 void w_1byte_ds1302(uchar t) {
uchar i;
for(i=0;i<8;i++) {
if(t & 0x01)
{io_ds1302=1;} else
{io_ds1302=0;} clk_ds1302=1; delay_3us(); delay_3us(); clk_ds1302=0; delay_3us(); delay_3us(); t>>=1; } }
//;########################################################################
//;子程序名:r_1byte_ds1302()
//;功能: 从DS1302读一个字节的数据 uchar r_1byte_ds1302() {
uchar i,temp11=0;
io_ds1302=1;//置IO为1,准备读入数据 for(i=0;i<8;i++) {
temp11>>=1;
if(io_ds1302) temp11 |= 0x80; clk_ds1302=1; delay_3us(); delay_3us();
clk_ds1302=0; delay_3us(); }
return(temp11); }
//;#################################################################################
//;子程序名:setbds1302
//;功能: 设置DS1302初始时间,并启动计时 void set_ds1302() {
uchar i,j;
rest_ds1302=0; delay_3us(); clk_ds1302=0; delay_3us(); rest_ds1302=1; delay_3us();
w_1byte_ds1302(0x8e);//写控制命令字 delay_3us();
w_1byte_ds1302(0x00);//写保护关闭 clk_ds1302=1; delay_3us(); rest_ds1302=0;
for(i=0,j=0x80;i<7;i++,j+=2) {
rest_ds1302=0; delay_3us(); clk_ds1302=0; delay_3us(); rest_ds1302=1; delay_3us();
w_1byte_ds1302(j); delay_3us();
w_1byte_ds1302(tab23[i]); delay_3us(); delay_3us(); clk_ds1302=1; delay_3us(); rest_ds1302=0; delay_3us(); delay_3us(); }
rest_ds1302=0;
delay_3us(); clk_ds1302=0; delay_3us(); rest_ds1302=1; delay_3us();
w_1byte_ds1302(0x8e); delay_3us();
w_1byte_ds1302(0x80); clk_ds1302=1; delay_3us(); rest_ds1302=0; delay_3us(); }
//;#################################################################-------
//;子程序名:get1302 void get_ds1302() {
uchar temp11[7],i,j; for(i=0;i<7;i++) {temp11[i]=0;}
for(i=0,j=0x81;i<7;i++,j+=2) {
rest_ds1302=0; delay_3us(); clk_ds1302=0; delay_3us(); rest_ds1302=1; delay_3us();
w_1byte_ds1302(j);
temp11[i]=r_1byte_ds1302(); delay_3us(); clk_ds1302=1; delay_3us(); rest_ds1302=0; delay_3us(); }
if(temp11[0]!=0xff)
{second_count=temp11[0];} if(temp11[1]!=0xff)// 数据验证 {minute_count=temp11[1];} if(temp11[2]!=0xff)//数据验证 {hour_count=temp11[2];} // date=temp[3];