Timer Basic : Digital Temperature Sensor

void _delay_us(uint8_t delay)
{
HAL_TIM_Base_Start(&htim17);
__HAL_TIM_SET_COUNTER(&htim17,0);  // set the counter value a 0
while (__HAL_TIM_GET_COUNTER(&htim17) < delay);  // wait for ARR value
} 

HAL_TIM_Base_Start(TIM_HandleTypeDef *htim);      // start timer 
HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim);       // stop timer
HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim); // start timer with interrupt
HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim);  // stop timer with interrupt

#define DS18B20_GPIO              GPIOC
#define DS18B20_PIN               GPIO_PIN_0
void output(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitStruct.Pin = DS18B20_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(DS18B20_GPIO, &GPIO_InitStruct);
} 
void input(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  GPIO_InitStruct.Pin =  DS18B20_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(DS18B20_GPIO, &GPIO_InitStruct);

}
#define CONFIG_OUTPUT             HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,GPIO_PIN_RESET)
#define READ_INPUT                HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_0)
uint8_t DS18B20Reset(void)
{
	//Pull Output line low and wait for 480uS
    output();//Output low for 480us   
    CONFIG_OUTPUT;//Configure as Output
	_delay_us(480);
	//Release line and wait for 60uS
    input(); //Configure as Input   
	_delay_us(60);
	//Store line value and wait until the completion of 480uS period
	uint8_t i;
	i=(READ_INPUT);
	_delay_us(420);
	return i; //Return the value read from the presence pulse (0=OK, 1=WRONG)
}
void DS18B20Write(uint8_t byte)
{
	for(uint8_t i=0;i<8;i++)
	{
	  //Pull line low for 2uS   
      output();  //Configure as Output  
      CONFIG_OUTPUT;//Output low 
	  _delay_us(2);
	  //If we want to write 1, release the line (if not will keep low)
	  if(byte & 0x01) input(); 
      //Configure as Input
	  _delay_us(60);  
      input(); //Configure as Input
	  _delay_us(1);
	  byte=byte>>1;
	}
}

uint8_t DS18B20Read()
{
	uint8_t byte=0x00;
	for(uint8_t i=0;i<8;i++)
	{
	  byte=byte>>1;
	  //Pull line low for 1uS  
      output(); //Configure as Output
      CONFIG_OUTPUT; //Output low for 2us
	  _delay_us(2);
	  //Release line and wait for 14uS
      input();
	  _delay_us(14); //Configure as Input
	  if(READ_INPUT) byte|=0x80;
	  _delay_us(45);
	}
	return byte;}