cancel
Showing results for 
Search instead for 
Did you mean: 

DS18b20 temperature sensor does not work with STM32L073RZ microcontroller, Can anyone help me to find the error

Thanustiya. T
Associate

I am working with DS18b20 temperature sensor in STM32L073RZ board. DS18b20 works using one wire communication. I used one GPIO pin for communicate with DS18b20. i couldn't get the sensor reading. i have attached my code below. Thanks in advance.

static void DS18B20_Mode_OUT_PP(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin = BSP_DS18B20_PIN;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(BSP_DS18B20_PORT, &GPIO_InitStruct);

}

static void DS18B20_Mode_IN_NP(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.Pin = BSP_DS18B20_PIN;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(BSP_DS18B20_PORT, &GPIO_InitStruct);

}

static void DS18B20_Reset(void)

{

DS18B20_Mode_OUT_PP();

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, 0);

bsp_delay_us (480);

HAL_GPIO_WritePin(BSP_DS18B20_PORT, BSP_DS18B20_PIN, GPIO_PIN_SET);

bsp_delay_us(80);

}

static uint8_t DS18B20_Presence(void)

{

uint8_t pulse_time = 0;

DS18B20_Mode_IN_NP();

bsp_delay_us(60);

while (!(DS18B20_IN) && (pulse_time <100))

{

pulse_time++;

bsp_delay_us(1);

}

//bsp_delay_us(350);

if (pulse_time >= 99)

{

DS18B20_Mode_OUT_PP();

return 1;

}

else

{

//pulse_time = 0;

return 0;

}

}

uint8_t DS18B20_Init(void)

{

DS18B20_Reset();

return DS18B20_Presence();

}

static uint8_t DS18B20_ReadBit(void)

{

uint8_t dat;

DS18B20_Mode_OUT_PP();

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, 0);

bsp_delay_us(2);

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, SET);

DS18B20_Mode_IN_NP();

bsp_delay_us(8);

if (DS18B20_IN)

{

dat = 1;

count1=count1+1;

}

else

{

dat = 0;

count2=count2+1;

}

bsp_delay_us(60);

d=dat;

return dat;

}

static uint8_t DS18B20_ReadByte(void)

{

uint8_t i, j, dat = 0;

for(i = 0; i < 8; i++)

{

j = DS18B20_ReadBit();

dat = (dat) | (j << i);

}

  n=dat;

return dat;

}

static void DS18B20_WriteByte(uint8_t dat)

{

uint8_t i ;//testb

DS18B20_Mode_OUT_PP(); // set as output

for (int i=0; i<8; i++)

{

if ((dat & (1<<i))!=0) // if the bit is high

{

// write 1

DS18B20_Mode_OUT_PP(); // set as output

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, 0); // pull the pin LOW

bsp_delay_us (5); // wait for 1 us

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, SET); // set as input

bsp_delay_us (60); // wait for 60 us

}

else // if the bit is low

{

count4 = count4+1;

DS18B20_Mode_OUT_PP();

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, 0); // pull the pin LOW

bsp_delay_us(60); // wait for 60 us

HAL_GPIO_WritePin (BSP_DS18B20_PORT, BSP_DS18B20_PIN, SET);

bsp_delay_us(5);

}

}

}

static void DS18B20_SkipRom(void)

{

DS18B20_WriteByte(0XCC); /* Skip ROM */

}

float DS18B20_GetTemp_SkipRom(void)

{

uint8_t tpmsb, tplsb;

float s_tem;

DS18B20_SkipRom();

DS18B20_WriteByte(0X44); /* Start conversion */

bsp_delay_us(75000);

DS18B20_Reset();

  DS18B20_Presence();

DS18B20_SkipRom();

   DS18B20_WriteByte(0XBE); /* read temperature value */

tplsb = DS18B20_ReadByte();

tpmsb = DS18B20_ReadByte();

s_tem = (tpmsb<<8)|tplsb;

s_tem = s_tem/16;

m=tplsb;

return s_tem;

}

void DS18B20_ReadId(uint8_t *ds18b20_id)

{

uint8_t uc;

DS18B20_WriteByte(0x33); //Read the serial number

for (uc = 0; uc < 8; uc++)

{

ds18b20_id[uc] = DS18B20_ReadByte();

}

}

void bsp_delay_us(uint16_t us)

{

__HAL_TIM_SET_COUNTER(&htim6, 0);

HAL_TIM_Base_Start(&htim6);

  while (__HAL_TIM_GET_COUNTER(&htim6) != us);

HAL_TIM_Base_Stop(&htim6);

}

float ReadTemperature(void){

  DS18B20_Init();

  DS18B20_ReadId(ucDs18b20Id);

float value = 0;

float temp_val;

temp_val=DS18B20_GetTemp_SkipRom();

value+=temp_val;

count=count+1;

return value;

}

1 REPLY 1
NghiaTd
Associate

Sir, I now having the same issue, did you find a solution for this, i really need help