I connected the temperature sensor DS18B20.
My observations: delayMicroseconds() - does not work correctly.
pinMode(), digitalRead() - work very slowly.
I used the microDS18B20 library using HAL (changed the microOneWire.cpp file):
Temperature sensor connected to pin PB5
void pinModeOutput(){
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void pinModeInput(){
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULL_UP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void delayMicros(int m){
uint32_t d = 0;
while ( d < 9 * m) {
asm ("\t nop");
d++;
}
}
bool oneWire_reset(uint8_t pin) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
pinModeOutput();
delayMicros(600);
pinModeInput();
MOW_CLI();
delayMicros(60);
bool pulse = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET;
MOW_SEI();
delayMicros(600);
return !pulse;
}
void oneWire_write(uint8_t data, uint8_t pin) {
for (uint8_t i = 8; i; i–) {
pinModeOutput();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
MOW_CLI();
if (data & 1) {
delayMicros(5);
pinModeInput();
delayMicros(60);
} else {
delayMicros(60);
pinModeInput();
delayMicros(5);
}
MOW_SEI();
data >>= 1;
}
}
uint8_t oneWire_read(uint8_t pin) {
uint8_t data = 0;
for (uint8_t i = 8; i; i–) {
data >>= 1;
MOW_CLI();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
pinModeOutput();
delayMicros(2);
pinModeInput();
delayMicros(8);
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET) data |= (1 << 7);
delayMicros(60);
MOW_SEI();
}
return data;
}