发布时间 : 星期三 文章51单片机超声波测距离(带温度补偿)更新完毕开始阅读a468ad28e2bd960590c6778f
DisplayOneChar( 1,11,speed/10+0x30); DisplayOneChar( 1,12,speed+0x30); DisplayOneChar( 1,13,'M'); DisplayOneChar( 1,14,'/'); DisplayOneChar( 1,15,'S'); }
//**************************************************** //读取温度
void readtemp_ds18b20(void) {
uchar temp32; rest_ds18b20();
w_1byte_ds18b20(0xcc); //跳过读序列号的操作 w_1byte_ds18b20(0x44); //启动温度转换 delay_8us(2); rest_ds18b20();
w_1byte_ds18b20(0xcc); //跳过读序列号的操作
w_1byte_ds18b20(0xbe); //读取温度寄存器等(共可读9个寄存器)是温度
templ=r_1byte_ds18b20(); temph=r_1byte_ds18b20();
if((temph&0xf0))//判断温度的正负性 {
flag1=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;//计算温度的个位数据 speed=331.4-0.607*(temp32 | templ); }
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;//计算温度的个位数据 flag1=1;
speed=311.4+0.607*(temp32 | templ); } }
/*====================================================================
功 能: 在1602显示一个整数数据
说 明: 显示一个整数数据-9999->32625. 从右至左显示数据5位: ======================================================================*/
void DisplayIntData(uchar X, uchar Y,int ZhengShu,uchar Digit,uchar XiaoShu) { uchar i=0,k=0, BCD[5]={0}; if(Digit>5) Digit=5; if(ZhengShu<0) {
k=1;//负数示志位
ZhengShu=-ZhengShu; }
BCD[4] = ZhengShu / 10000; //求出万位数据 ZhengShu = ZhengShu % 10000;
BCD[3] = ZhengShu / 1000; //求出千位数据 ZhengShu = ZhengShu % 1000;
BCD[2] = ZhengShu / 100; //求出百位数据 ZhengShu = ZhengShu % 100;
BCD[1] = ZhengShu / 10; //求出十位数据 BCD[0] = ZhengShu % 10; //求出个位数据 for(i=0;i if(k==1) DisplayOneChar(X,Y-1,'-');//输出负符 } //**************************************************************** //读一个字节 uchar r_1byte_ds18b20(void) { uchar i=0; uchar value = 0; for (i=0;i<8;i++) { value>>=1; dq_ds18b20=0;// DQ_L; delay_3us(); dq_ds18b20=1; //DQ_H; delay_8us(2); if(dq_ds18b20==1) value|=0x80; delay_8us(6); //延时40us } dq_ds18b20=1; return value; } //******************************************************************************* //子程序功能:向DS18B20写一字节的数据 void w_1byte_ds18b20(uchar value) { uchar i=0; for(i=0;i<8;i++) { dq_ds18b20=1; delay_3us(); dq_ds18b20=0; delay_8us(2); if (value & 0x01) dq_ds18b20=1; //DQ = 1 delay_50us(1); //延时50us 以上 delay_8us(2); value>>=1; } dq_ds18b20=1; //DQ = 1 } //;************************************************** //ds18b20复位子程序 void rest_ds18b20(void) { rest:delay_3us(); //稍做延时 delay_3us(); dq_ds18b20=1; delay_3us(); dq_ds18b20=0;// DQ_L; delay_50us(11);//480us if(dq_ds18b20==1) { return; } delay_50us(2); //延时90us if(dq_ds18b20==1) { return; } else { goto rest; } } //==============================超声波模块测试子程序================================================ /*==================================================================== 注意:是用12MHz晶振 设定延时时间:x*25us 与 产生40KHZ的脉冲 ====================================================================*/ void delay25us_40KHz(unsigned char us) { while(us--) { TxPin = 0; _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_(); _nop_(); TxPin = 1; _nop_();_nop_(); _nop_();_nop_(); } TxPin = 1; } /*=============================================================================