打印

【求助】DS18B20测温程序不知能否测负值

[复制链接]
1973|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sunruiyeyipeng|  楼主 | 2011-4-23 09:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
////////////////////////////////////////////
///////////////////说明////////////////////
///程序参考了别人的代码///
//shownum();
//counts();
//hc138();
//以上三个函数为独创
//【我的问题:不知道这个代码能否测出负数温度】
#include<reg52.h>
#include<math.h>
#define uint unsigned int
#define uchar unsigned char
#define fu 0x40  //  "-"号的编码
#define uint unsigned int
#define uchar unsigned char
//uchar table[4];
char string[]={"The Temprature:"};
sbit hca=P2^2;//定义译码器输入端A 在P2.2管脚上
sbit hcb=P2^3;//定义译码器输入端B 在P2.3管脚上
sbit hcc=P2^4;//定义译码器输入端C 在P2.4管脚上
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//整数table

//
1
2
3
4
5
6
7
8
9
unsigned char code xs[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
//小数table
                        // 0.   1.   2.   3.   4.  5.  6.    7.    8.  9.
//sbit rw=P2^5;//读写选择端(0/1)
//sbit rs=P2^6;//数据命令选择端(1/0)
//sbit en=P2^7;//使能端口
sbit ds=P3^7;//DS18B20端口
void delay(uint a)//延时1ms
{
    uchar b;
    for(a;a>0;a--)
        for(b=125;b>0;b--);
}
uchar counts(long zs)
{

uchar c=0;

while(zs!=0)

{

zs/=10;

c++;

}

return (c);
}

void hc138(uchar id,uchar num,uchar typee)
//0,整数;1,小数
{
   switch (id)
   {

case 1:

{

hcc=0;hcb=0;hca=0;

};break;

case 2:

{

hcc=0;hcb=0;hca=1;

};break;

case 3:

{

hcc=0;hcb=1;hca=0;

};break;

case 4:

{

hcc=0;hcb=1;hca=1;


};break;

case 5:

{

hcc=1;hcb=0;hca=0;

};break;

case 6:

{

hcc=1;hcb=0;hca=1;

};break;

case 7:

{

hcc=1;hcb=1;hca=0;

};break;

case 8:

{

hcc=1;hcb=1;hca=1;

};break;
   }

if (typee==0)

{
   P0=table[num];

}

else if(typee==1)

{

P0=xs[num];

}

else if(typee==2)

{

P0=fu;


}

}

void shownum(float a)
{

uint zsx,xs,i;

long zs;

uchar zf=0;

zs=(long)a;

xs=(int)((a-zs)*100);

if(a<0)

{

zf=0;

zs*=(-1);

xs*=(-1);

}

else

{

zf=1;

}

hc138(8,xs%10,0);

delay(10);

hc138(7,xs/10,0);

delay(6);

if (zs==0) {hc138(6,0,1);}

else

{

hc138(6,zs%10,1);

delay(1);

zsx=counts(zs);

for(i=1;i<zsx;i++)

{

hc138(6-i,(zs/10)%10,0);

delay(1);

zs/=10;

}

if (zf==0)

{

hc138(6-i,0,2);

}

}

}
//=========================================
//函数功能:延时1ms
//=========================================                                                                                                                          


//=========================================
//函数功能:向1602发送命令
//=========================================
//void ledwritecom(uchar com)
//{
//   rs=0;
//   P0=com;
//   delay(5);
//   en=1;
//   delay(5);
//   en=0;
//}

//=========================================
//函数功能:向1602发送数据
//=========================================

//=========================================
//函数功能:1602初始化
//=========================================

//=========================================
//函数功能:DS18B20初始化
//=========================================
void dsinit()
//向DS18B20发送初始化信号,但是不检验其状态
{
   uint i;
   ds=0;
   i=100;
   while(i>0) i--;
   ds=1;
   i=100;
   while(i>0) i--;
}

  
//=========================================
//函数功能:DS18B20写一字节
//=========================================
void dswrite(uchar dat)
{
    uchar i,j;
    for(j=0;j<8;j++)
    {
        ds=0;
//单纯的拉低电平
        i=2;while(i>0) i--;   
        ds=dat&0x01;          //发送1位数据,记住:这里的数据是并行数据,与1,0不一样。那个1,0控制的是电平,这个是纯数据。
        i=30;while(i>0) i--;  //延时,让数据完全接受
        ds=1;                 //释放总线,等待总线恢复
        dat>>=1;              //准备下一位数据的传送
    }
}

//=========================================
//函数功能:DS18B20开始转化温度
//=========================================
void dsconvert()
{
    dsinit();         //初始化
    dswrite(0xcc);    //跳过ROM
    dswrite(0x44);    //开始温度转换
}

//=========================================
//函数功能:向DS18B20发送读指令
//=========================================
void dssendread()
{
    dsinit();
    dswrite(0xcc);    //跳过ROM
    dswrite(0xbe);    //发送读指令  
}

//=========================================
//函数功能:从DS18B20读数据
//=========================================
uchar dsread()
{
    uchar i=0,j,dat;
    for (i=8;i>0;i--)
    {
        dat>>=1;
//右移一位,左边自动空出为0,则可保留下左边数据
        ds=0;               //拉低总线,产生读信号
        for(j=2;j>0;j--);   
        ds=1;               //释放总线,准备读数据
        for(j=4;j>0;j--);   
        if(ds==1)
//如果是0,则自然而然的
        dat|=0x80;
//没有看到从ds读啊!
        for(j=30;j>0;j--);  
        ds=1;               //拉高总线,准备下一位数据的读取
    }
    return dat;
}

//=========================================
//函数功能:采集温度
//=========================================
int gettemp()
{
    uchar low,high;
    int temp,value;
    float t;
    dssendread();
    low=dsread();
    high=dsread();
    temp=high;
    temp<<=8;
    temp|=low;
    value=temp;  
    t=value*0.0625;
    value=t*100+(value>0?0.5:-0.5);
//神马意思?
    return value;  
}

void display()
{
    int temp;
//    ledwritecom(0x80);         
//
//    ledwritecom(0x80+0x40+10);
//    ledwritedat('C');
        dsconvert();                        //温度转换
        temp=gettemp();

shownum(temp/100.0);
       // table[0]=abs(temp)/10000+'0';       //百位温度
       // table[1]=abs(temp)%10000/1000+'0';  //十
       // table[2]=abs(temp)%1000/100+'0';    //个
       // table[3]=abs(temp)%100/10+'0';      //小数后1位
      //table[4]=abs(temp)%10+'0';          //小数后2位
      //  ledwritecom(0x80+0x40+5);
       // ledwritedat(table[0]);
      //  ledwritecom(0x80+0x40+6);
      //  ledwritedat(table[1]);
      //  ledwritecom(0x80+0x40+7);
      //  ledwritedat(table[2]);
      //  ledwritecom(0x80+0x40+8);
      //  ledwritedat('.');
      //  ledwritecom(0x80+0x40+9);
      //  ledwritedat(table[3]);
      //ledwritecom(0x80+0x40+10);
      //ledwritedat(table[4]);
}

//=========================================
//函数功能:主函数
//=========================================
void timerint()
//初始化计时器
{

EA=1;

  TMOD=0x01;
    TH0=(65536-10000)/256;
    TL0=(65536-10000)%256;
    ET0=1;
    TR0=1;
}
void addnum() interrupt 1
{

  TH0=(65536-10000)/256;
    TL0=(65536-10000)%256;
    display();
}
void main()
{
timerint();
    while(1){}
}

相关帖子

沙发
ayb_ice| | 2011-4-23 09:41 | 只看该作者
可以测负值

使用特权

评论回复
板凳
救火车| | 2011-4-23 11:12 | 只看该作者

使用特权

评论回复
评分
参与人数 1威望 +1 收起 理由
sunruiyeyipeng + 1
地板
ningling_21| | 2011-4-23 16:28 | 只看该作者
一看就知道LZ没看芯片资料...

使用特权

评论回复
5
xingaiking| | 2011-4-23 20:57 | 只看该作者
...楼主V5

使用特权

评论回复
6
sunruiyeyipeng|  楼主 | 2011-4-24 01:07 | 只看该作者
呵呵,期待冬天……

使用特权

评论回复
7
abin0415| | 2011-4-24 08:49 | 只看该作者
看看DATASHEET就知道了。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

21

帖子

0

粉丝