2024-10-10 08:24 AM - last edited on 2024-10-10 08:43 AM by Andrew Neil
2024-10-10 08:22 AM
Hi, where can I find a working One-Wire protocol? Is there an example provided by STM? I wrote code for a program to detect the temperature from a DS18B20 sensor, but I can’t detect the sensor, even though the debug shows everything is fine. Can someone help me? Thanks
2024-10-10 08:40 AM - edited 2024-10-10 08:40 AM
@Emanuele78 wrote:where can I find a working One-Wire protocol?
From the manufacturer:
See also:
Useful background info:
https://www.analog.com/en/resources/technical-articles/advanced-1-wire-network-driver.html
There are also UART-to-1-Wire and I2C-to-1-Wire hardware bridge chips.
@Emanuele78 wrote:even though the debug shows everything is fine.
What "debug" is that?
Did you look at the 1-Wire line with an oscilloscope to see what was actually happening?
A useful feature of 1-Wire is that the timing between "Slots" (ie, bits) is non-critical - so you can step your code one complete Slot at a time ...
#1Wire
2024-10-10 08:58 AM - last edited on 2024-10-11 12:49 AM by SofLit
Thanks, I will try to compare the links with my code.
Unfortunately, I don't have an oscilloscope, but if necessary, I will make sure to get one
I performed a debug with CUBEIDE using F6, placing breakpoints to check if the microcontroller detects the sensor, but it never finds it. The quantity variable in the following code is always zero.
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
DS18B20_ReadAll();
HAL_GPIO_WritePin(Test_GPIO_Port, Test_Pin, 1);
DS18B20_StartAll();
HAL_GPIO_WritePin(Test_GPIO_Port, Test_Pin, 0);
uint8_t ROM_tmp[8];
uint8_t i;
int quantity = DS18B20_Quantity(); // quantity always = 0
2024-10-10 09:04 AM
Please see the Posting Tips for how to properly post source code:
@Emanuele78 wrote:I performed a debug with CUBEIDE using F6, placing breakpoints to check if the microcontroller detects the sensor, but it never finds it. T= 0
Getting the correct timing on the wire is critical to 1-Wire operation - the CubeIDE debugger will not tell you anything about that.
It's also essential that your external hardware is correct - again, the CubeIDE debugger will not tell you anything about that.
This is why an oscilloscope is an essential tool for embedded firmware development: you need to be able to see what's happening in the real world of the hardware - not just the software.
2024-10-10 09:31 AM
2024-10-10 11:11 PM
2024-10-11 03:13 AM
Please post a schematic
2024-10-11 07:37 AM
2024-10-15 01:19 AM
Measuring with a multimeter (See the code snippet I provide below.), the PROVA variable correctly changes state (from 3V to zero), while OneWire_OutputLow(onewire); gives me a value of only 2.1V from 3V (when it’s high), if I keep the external 4.7kOhm pull-up resistor connected.
If I remove the pull-up resistor, I get 2.8V high and 2.1V low.
Is the program able to detect the state change with this modest variability (3V high, 2.1V low)?
I would appreciate a response to the points mentioned above as well.
I'm using the code from https://github.com/lamik/DS18B20_STM32_HAL/blob/master/Src/onewire.c.
Thanks a lot
void OneWire_Init(OneWire_t* onewire, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
HAL_TIM_Base_Start(&_DS18B20_TIMER); // Start the delay timer
onewire->GPIOx = GPIOx; // Save 1-wire bus pin
onewire->GPIO_Pin = GPIO_Pin;
// 1-Wire bit bang initialization
OneWire_BusOutputDirection(onewire);
OneWire_OutputHigh(onewire);
HAL_GPIO_WritePin(PROVA_GPIO_Port, PROVA_Pin, GPIO_PIN_SET);
HAL_Delay(100);
OneWire_OutputLow(onewire);
HAL_GPIO_WritePin(PROVA_GPIO_Port, PROVA_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
OneWire_OutputHigh(onewire);
HAL_Delay(200);
}