Skip to content

Validate DS18B20 CRC #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 52 additions & 19 deletions STM32CubeExpansion_LRWAN/Drivers/BSP/Components/ds18b20/ds18b20.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,27 @@ void DS18B20_SkipRom(uint8_t num)
DS18B20_Presence(num);
DS18B20_WriteByte(0XCC,num);
}

float DS18B20_GetTemp_SkipRom (uint8_t num)
unsigned char DS18B20_crc8(const unsigned char * data, const unsigned int size)
{
unsigned char crc = 0;
for ( unsigned int i = 0; i < size; ++i )
{
unsigned char inbyte = data[i];
for ( unsigned char j = 0; j < 8; ++j )
{
unsigned char mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if ( mix ) crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}
float DS18B20_GetTemp_SkipRom (uint8_t num, bool *success)
{
uint8_t tpmsb, tplsb;
short s_tem;
float f_tem;

uint8_t scratchpad[9];

DS18B20_SkipRom(num);
DS18B20_WriteByte(0X44,num);
Expand All @@ -531,32 +545,51 @@ float DS18B20_GetTemp_SkipRom (uint8_t num)
DS18B20_SkipRom (num);
DS18B20_WriteByte(0XBE,num);

tplsb = DS18B20_ReadByte(num);
tpmsb = DS18B20_ReadByte(num);
scratchpad[0] = DS18B20_ReadByte(num); // LSB
scratchpad[1] = DS18B20_ReadByte(num); // MSB

if((tpmsb==5)&&(tplsb==80)) //1360= 00000101 01010000,tpmsb=00000101=5,tplsb=01010000=80;
if((scratchpad[0]==5)&&(scratchpad[1]==80)) //1360= 00000101 01010000,tpmsb=00000101=5,tplsb=01010000=80;
{
DS18B20_SkipRom(num);
DS18B20_WriteByte(0X44,num);

DelayMs(750);

DS18B20_SkipRom (num);
DS18B20_WriteByte(0XBE,num);
// The power-on reset value of the temperature register is 00000101 01010000, so read it again
DS18B20_SkipRom(num);
DS18B20_WriteByte(0X44,num);

tplsb = DS18B20_ReadByte(num);
tpmsb = DS18B20_ReadByte(num);
DelayMs(750);

DS18B20_SkipRom (num);
DS18B20_WriteByte(0XBE,num);

scratchpad[0] = DS18B20_ReadByte(num); // LSB
scratchpad[1] = DS18B20_ReadByte(num); // MSB
}

s_tem = tpmsb<<8;
s_tem = s_tem | tplsb;
// Receive the rest of the scratchpad
scratchpad[2] = DS18B20_ReadByte(num);// User byte 1
scratchpad[3] = DS18B20_ReadByte(num);// User byte 2
scratchpad[4] = DS18B20_ReadByte(num);// Configuration register
scratchpad[5] = DS18B20_ReadByte(num);// Reserved FFh
scratchpad[6] = DS18B20_ReadByte(num);// Reserved
scratchpad[7] = DS18B20_ReadByte(num);// Reserved 10h
scratchpad[8] = DS18B20_ReadByte(num);// CRC

// output scratchpad for debugging purposes
//PPRINTF("Scratchpad contents: %02x%02x%02x%02x%02x%02x%02x%02x%02x\r\n",
// scratchpad[0], scratchpad[1], scratchpad[2], scratchpad[3],
// scratchpad[4], scratchpad[5], scratchpad[6], scratchpad[7],
// scratchpad[8]);

// calculate CRC and match it against reported one
(*success) = DS18B20_crc8(scratchpad, 8) == scratchpad[8]; // Reserved FFh

s_tem = scratchpad[1]<<8;
s_tem = s_tem | scratchpad[0];

if( s_tem < 0 )
f_tem = (~s_tem+1) * -0.0625;
else
f_tem = s_tem * 0.0625;

// PPRINTF("temp is %f\r\n",f_tem);
//PPRINTF("temp is %f, status %i\r\n",f_tem, (*success));
return f_tem;
}
/*******END OF FILE********/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uint8_t DS18B20_ReadByte(uint8_t num);
void DS18B20_WriteBit(uint8_t dat,uint8_t num);
void DS18B20_WriteByte(uint8_t dat,uint8_t num);
void DS18B20_SkipRom(uint8_t num);
float DS18B20_GetTemp_SkipRom (uint8_t num);
float DS18B20_GetTemp_SkipRom (uint8_t num, bool *success);
#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,22 @@ Maintainer: Miguel Luis and Gregory Cristian
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include <stdbool.h>

/* Exported types ------------------------------------------------------------*/

typedef struct{

int in1;/*GPIO Digital Input 0 or 1*/

float temp1;//DS18B20-1
bool temp1_ok;//DS18B20-1

float temp2;//DS18B20-2
bool temp2_ok;//DS18B20-2

float temp3;//DS18B20-3
bool temp3_ok;//DS18B20-3

float oil; //oil float

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void BSP_sensor_Read( sensor_t *sensor_data)

sensor_data->in1=HAL_GPIO_ReadPin(GPIO_INPUT_PORT,GPIO_INPUT_PIN1);

sensor_data->temp1=DS18B20_GetTemp_SkipRom(1);
sensor_data->temp1=DS18B20_GetTemp_SkipRom(1, &sensor_data->temp1_ok);

if((mode==1)||(mode==3))
{
Expand Down Expand Up @@ -197,8 +197,8 @@ void BSP_sensor_Read( sensor_t *sensor_data)

else if(mode==4)
{
sensor_data->temp2=DS18B20_GetTemp_SkipRom(2);
sensor_data->temp3=DS18B20_GetTemp_SkipRom(3);
sensor_data->temp2=DS18B20_GetTemp_SkipRom(2, &sensor_data->temp2_ok);
sensor_data->temp3=DS18B20_GetTemp_SkipRom(3, &sensor_data->temp3_ok);
}

else if(mode==5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,13 @@ static void Send( void )
{
AppData.Buff[i++] =(batteryLevel_mV>>8); //level of battery in mV
AppData.Buff[i++] =batteryLevel_mV & 0xFF;

AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);

if(sensor_data.temp1_ok){
AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}
AppData.Buff[i++] =(int)(sensor_data.oil)>>8; //oil float
AppData.Buff[i++] =(int)sensor_data.oil;

Expand Down Expand Up @@ -479,8 +482,13 @@ static void Send( void )
AppData.Buff[i++] =(batteryLevel_mV>>8); //level of battery in mV
AppData.Buff[i++] =batteryLevel_mV & 0xFF;

AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
if(sensor_data.temp1_ok){
AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}

AppData.Buff[i++] =(int)(sensor_data.oil)>>8; //oil float
AppData.Buff[i++] =(int)sensor_data.oil;
Expand Down Expand Up @@ -556,8 +564,13 @@ static void Send( void )
AppData.Buff[i++] =(batteryLevel_mV>>8); //level of battery in mV
AppData.Buff[i++] =batteryLevel_mV & 0xFF;

AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
if(sensor_data.temp1_ok){
AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}

AppData.Buff[i++] =(int)(sensor_data.oil)>>8; //oil float
AppData.Buff[i++] =(int)sensor_data.oil;
Expand All @@ -572,21 +585,34 @@ static void Send( void )
switch_status=HAL_GPIO_ReadPin(GPIO_EXTI_PORT,GPIO_EXTI_PIN);
AppData.Buff[i++]=(switch_status<<7)|(sensor_data.in1<<1)|0x0C;
}

AppData.Buff[i++]=(int)(sensor_data.temp2*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp2*10);
AppData.Buff[i++]=(int)(sensor_data.temp3*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp3*10);
if(sensor_data.temp2_ok){
AppData.Buff[i++]=(int)(sensor_data.temp2*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp2*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}
if(sensor_data.temp3_ok){
AppData.Buff[i++]=(int)(sensor_data.temp3*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp3*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}

}

else if(mode==5)
{
AppData.Buff[i++] =(batteryLevel_mV>>8); //level of battery in mV
AppData.Buff[i++] =batteryLevel_mV & 0xFF;

AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
if(sensor_data.temp1_ok){
AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}

AppData.Buff[i++] =(int)(sensor_data.oil)>>8; //oil float
AppData.Buff[i++] =(int)sensor_data.oil;
Expand All @@ -613,8 +639,13 @@ static void Send( void )
AppData.Buff[i++] =(batteryLevel_mV>>8); //level of battery in mV
AppData.Buff[i++] =batteryLevel_mV & 0xFF;

AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
if(sensor_data.temp1_ok){
AppData.Buff[i++]=(int)(sensor_data.temp1*10)>>8; //DS18B20
AppData.Buff[i++]=(int)(sensor_data.temp1*10);
}else{
AppData.Buff[i++]=0x80;
AppData.Buff[i++]=0x00;
}

AppData.Buff[i++] =(int)(sensor_data.oil)>>8; //oil float
AppData.Buff[i++] =(int)sensor_data.oil;
Expand Down