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
}