DS18B20 CRC 校验

DS18B20 CRC 校验,帮我看看这个CRC校验 写对没有  为什么会有 150度  感谢大家了 #include”temp…

DS18B20 CRC 校验,帮我看看这个CRC校验 写对没有  为什么会有 150度  感谢大家了
#include”temp.h”
uchar scratbuf[9]; //存从18B20高速缓存中读取的9B数据
uchar code crc_tab[256]={
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
};
/*******************************************************************************
* 函 数 名         : Delay1ms
* 函数功能                   : 延时函数
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void Delay1ms(uint y)
{
        uint x;
        for( ; y>0; y–)
        {
                for(x=110; x>0; x–);
        }
}
/*******************************************************************************
* 函 数 名         : Ds18b20Init
* 函数功能                   : 初始化
* 输    入         : 无
* 输    出         : 初始化成功返回1,失败返回0
*******************************************************************************/

uchar Ds18b20Init()
{
        uchar i;
        DSPORT = 0;                         //将总线拉低480us~960us
        i = 70;       
        while(i–);         //延时642us
        DSPORT = 1;                        //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
        i = 0;
        while(DSPORT)        //等待DS18B20拉低总线
        {
                i++;
                if(i>5)//等待>5MS
                {
                        return 0;//初始化失败
                }
                Delay1ms(1);       
        }
        return 1;//初始化成功
}

/*******************************************************************************
* 函 数 名         : Ds18b20WriteByte
* 函数功能                   : 向18B20写入一个字节
* 输    入         : com
* 输    出         : 无
*******************************************************************************/

void Ds18b20WriteByte(uchar dat)
{
        uint i, j;

        for(j=0; j<8; j++)
        {
                DSPORT = 0;                       //每写入一位数据之前先把总线拉低1us
                i++;
                DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
                i=6;
                while(i–); //延时68us,持续时间最少60us
                DSPORT = 1;        //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
                dat >>= 1;
        }
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadByte
* 函数功能                   : 读取一个字节
* 输    入         : com
* 输    出         : 无
*******************************************************************************/

uchar Ds18b20ReadByte()
{
        uchar byte, bi;
        uint i, j;       
        for(j=8; j>0; j–)
        {
                DSPORT = 0;//先将总线拉低1us
                i++;
                DSPORT = 1;//然后释放总线
                i++;
                i++;//延时6us等待数据稳定
                bi = DSPORT;         //读取数据,从最低位开始读取
                /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
                byte = (byte >> 1) | (bi << 7);                                                  
                i = 4;                //读取完之后等待48us再接着读取下一个数
                while(i–);
        }                               
        return byte;
}
/*******************************************************************************
* 函 数 名         : Ds18b20ChangTemp
* 函数功能                   : 让18b20开始转换温度
* 输    入         : com
* 输    出         : 无
*******************************************************************************/

void  Ds18b20ChangTemp()
{
        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);                //跳过ROM操作命令                 
        Ds18b20WriteByte(0x44);            //温度转换命令
        Delay1ms(100);        //等待转换成功,而如果你是一直刷着的话,就不用这个延时了

}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTempCom
* 函数功能                   : 发送读取温度命令
* 输    入         : com
* 输    出         : 无
*******************************************************************************/

void  Ds18b20ReadTempCom()
{       

        Ds18b20Init();
        Delay1ms(1);
        Ds18b20WriteByte(0xcc);         //跳过ROM操作命令
        Ds18b20WriteByte(0xbe);         //发送读取温度命令
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTemp
* 函数功能                   : 读取温度
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
uchar do_crc8(uchar *p,uchar n) //8位CRC校验,p指向校验数据,n为字节数
{
        uchar i,crc8=0;
        for(i=0;i<n;i++)
        crc8=crc_tab[crc8^p[i]]; //连续查表计算CRC
        return crc8;
}
int Ds18b20ReadTemp()
{
        int temp = 0;
        char i;
        Ds18b20ChangTemp();                                 //先写入转换命令
        Ds18b20ReadTempCom();                        //然后等待转换完后发送读取温度命令
        for(i=0;i<9;i++)
        scratbuf[i]=Ds18b20ReadByte();
        if(!do_crc8(scratbuf,9))
         {
                  temp = scratbuf[1];
                temp <<= 8;
                temp |= scratbuf[0];
                return temp;
         }
}

作者: soufei

为您推荐


Fatal error: Can't use function return value in write context in /www/wwwroot/www.qianrushi.com.cn/wp-content/themes/news/footer.php on line 115