Sterling
Sterling
DIIDevHeads IoT Integration Server
Created by Sterling on 9/27/2024 in #firmware-and-baremetal
Issues with MATCH ROM Command in DS18B20 Sensor Communication on STM32 Using UART
I am working on reading temperatures from multiple DS18B20 sensors connected to a shared 1-Wire bus on an STM32 NUCLEO-F103RB board. The sensors operate in parasitic power mode, with a 4.7k pull-up resistor on the data line, and I’m using UART in half-duplex mode to emulate 1-Wire communication. I can read data from a single sensor using the SKIP ROM command and retrieve sensor IDs with the READ ROM command. However, when I try to communicate with a specific sensor using the MATCH ROM command, I don’t receive any response. My code :
uint8_t DS18B20_Init(void)
{
uint8_t ResetByte = 0xF0, PresenceByte;
huart1.Init.BaudRate = 9600; // Set to 9600 for reset pulse
HAL_HalfDuplex_Init(&huart1);
HAL_UART_Transmit(&huart1, &ResetByte, 1, 1); // Send reset pulse
HAL_UART_Receive(&huart1, &PresenceByte, 1, 1); // Wait for presence pulse
huart1.Init.BaudRate = 115200; // Set baud rate back to 115200 for data communication
HAL_HalfDuplex_Init(&huart1);
return (PresenceByte != ResetByte) ? 1 : 0;
}

void DS18B20_MatchROM(uint8_t rom[8])
{
DS18B20_Init();
DS18B20_WriteByte(0x55); // Send MATCH ROM command
for (int i = 0; i < 8; i++) {
DS18B20_WriteByte(rom[i]); // Send each byte of the ROM code
}
DS18B20_WriteByte(0xBE); // Send Read Scratchpad command
}
uint8_t DS18B20_Init(void)
{
uint8_t ResetByte = 0xF0, PresenceByte;
huart1.Init.BaudRate = 9600; // Set to 9600 for reset pulse
HAL_HalfDuplex_Init(&huart1);
HAL_UART_Transmit(&huart1, &ResetByte, 1, 1); // Send reset pulse
HAL_UART_Receive(&huart1, &PresenceByte, 1, 1); // Wait for presence pulse
huart1.Init.BaudRate = 115200; // Set baud rate back to 115200 for data communication
HAL_HalfDuplex_Init(&huart1);
return (PresenceByte != ResetByte) ? 1 : 0;
}

void DS18B20_MatchROM(uint8_t rom[8])
{
DS18B20_Init();
DS18B20_WriteByte(0x55); // Send MATCH ROM command
for (int i = 0; i < 8; i++) {
DS18B20_WriteByte(rom[i]); // Send each byte of the ROM code
}
DS18B20_WriteByte(0xBE); // Send Read Scratchpad command
}
I’ve verified the ROM codes using the READ ROM command, and they are correct. I switch the UART baud rate from 9600 (for the reset pulse) to 115200 (for data), but suspect this may cause timing or initialization issues. Problems: 1. MATCH ROM Command Failure: The sensor doesn't respond after the MATCH ROM command. 2. Baud Rate Switching: Switching between 9600 and 115200 baud might cause communication issues. 3. Error Handling: My code lacks handling for failed transmissions or invalid responses. What could be causing the MATCH ROM failure, and how can I ensure correct UART baud rate switching and timing for multiple sensors?
3 replies