0% found this document useful (0 votes)
44 views

Code Ok

The document contains code for reading temperature from a DS18B20 temperature sensor connected to a microcontroller over 1-Wire interface. It initializes GPIO, I2C and Timer peripherals, reads temperature from the sensor and displays it on an LCD screen connected over I2C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Code Ok

The document contains code for reading temperature from a DS18B20 temperature sensor connected to a microcontroller over 1-Wire interface. It initializes GPIO, I2C and Timer peripherals, reads temperature from the sensor and displays it on an LCD screen connected over I2C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

/* USER CODE BEGIN Header */

/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "tim.h"
#include "gpio.h"
#include "stdio.h"
#include "string.h"
#include "i2c-lcd.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
char dulieu[20];
uint8_t Presence = 0;
float Temperature = 0;
uint8_t Temp_byte1;
uint8_t Temp_byte2;
uint16_t TEMP;

//float t = 30.57;
/* USER CODE END Includes */
#define DS18B20_PORT GPIOB
#define DS18B20_PIN GPIO_PIN_9

void delay (uint32_t us)


{
__HAL_TIM_SET_COUNTER(&htim4,0);
while ((__HAL_TIM_GET_COUNTER(&htim4))<us);
}

void Set_Pin_Output(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)


{
GPIO_InitTypeDef GPIO_IntStruct ={0};
GPIO_IntStruct.Pin = GPIO_Pin;
GPIO_IntStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_IntStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOx, &GPIO_IntStruct);

void Set_Pin_Intput(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)


{
GPIO_InitTypeDef GPIO_IntStruct ={0};
GPIO_IntStruct.Pin = GPIO_Pin;
GPIO_IntStruct.Mode = GPIO_MODE_INPUT;
GPIO_IntStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOx, &GPIO_IntStruct);

uint8_t DS18B20_Start (void)


{
uint8_t Response = 0;
Set_Pin_Output(DS18B20_PORT, DS18B20_PIN); // set the pin as output
HAL_GPIO_WritePin (DS18B20_PORT, DS18B20_PIN, 0); // pull the pin low
delay(480); // delay according to datasheet

Set_Pin_Intput(DS18B20_PORT, DS18B20_PIN); // set the pin as input


delay(80); // delay according to datasheet

if (!(HAL_GPIO_ReadPin (DS18B20_PORT, DS18B20_PIN))) Response = 1; // if


the pin is low i.e the presence pulse is detected
else Response = -1;

delay(400); // 480 us delay totally.

return Response;
}

void DS18B20_Write (uint8_t data)


{
Set_Pin_Output(DS18B20_PORT, DS18B20_PIN); // set as output

for (int i=0; i<8; i++)


{

if ((data & (1<<i))!=0) // if the bit is high


{
// write 1

Set_Pin_Output(DS18B20_PORT, DS18B20_PIN); // set as output


HAL_GPIO_WritePin (DS18B20_PORT, DS18B20_PIN, 0); // pull the
pin LOW
delay(1); // wait for 1 us

Set_Pin_Intput(DS18B20_PORT, DS18B20_PIN); // set as input


delay(50); // wait for 60 us
}

else // if the bit is low


{
// write 0

Set_Pin_Output(DS18B20_PORT, DS18B20_PIN);
HAL_GPIO_WritePin (DS18B20_PORT, DS18B20_PIN, 0); // pull the
pin LOW
delay(50); // wait for 60 us

Set_Pin_Intput(DS18B20_PORT, DS18B20_PIN);
}
}
}

uint8_t DS18B20_Read (void)


{
uint8_t value=0;
Set_Pin_Intput (DS18B20_PORT,DS18B20_PIN);

for (int i=0;i<8;i++)


{
Set_Pin_Output(DS18B20_PORT,DS18B20_PIN); // set as output

HAL_GPIO_WritePin (DS18B20_PORT,DS18B20_PIN, 0); // pull the data pin


LOW
delay(2); // wait for 2 us

Set_Pin_Intput(DS18B20_PORT,DS18B20_PIN); // set as input


if (HAL_GPIO_ReadPin (DS18B20_PORT,DS18B20_PIN)) // if the pin is HIGH
{
value |= 1<<i; // read = 1
}
delay(60); // wait for 60 us
}
return value;
}
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/


/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/


/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/


void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/


/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */


SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */


MX_GPIO_Init();
MX_I2C1_Init();
MX_TIM4_Init();
HAL_TIM_Base_Start(&htim4);
lcd_init();
/* USER CODE BEGIN 2 */
// lcd_put_cur(0,2);
// printf("Hello World\r\n");
// HAL_Delay(5000);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
lcd_put_cur(0,4);
lcd_send_string("DS18B20");
lcd_put_cur(1,2);
lcd_send_string("T = ");
lcd_put_cur(1,11);
lcd_send_data(223);
lcd_put_cur(1,12);
lcd_send_string("C");

DS18B20_Start();
Presence = DS18B20_Start ();
delay (1);
DS18B20_Write (0xCC); // skip ROM
DS18B20_Write (0x44); // convert t
delay (800);

Presence = DS18B20_Start ();


delay(1);
DS18B20_Write (0xCC); // skip ROM
DS18B20_Write (0xBE); // Read Scratch-pad

Temp_byte1 = DS18B20_Read();
Temp_byte2 = DS18B20_Read();
TEMP = (Temp_byte2 <<8)|Temp_byte1;
Temperature = (float)TEMP/16;

sprintf(dulieu,"%2.2f",Temperature);
lcd_send_cmd(0x80|0x46);
lcd_send_string(dulieu);
HAL_Delay(1000);

/* USER CODE BEGIN 3 */


}
/* USER CODE END 3 */
}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Initializes the RCC Oscillators according to the specified parameters


* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)


{
Error_Handler();
}
}

/* USER CODE BEGIN 4 */


/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */

/* USER CODE END Error_Handler_Debug */


}

#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

You might also like