I am using the DS18B20 temp sensor with the stm32h7 board and am just trying to get the correct temp and display it using putty, and I want to display a message saying Heat warning when the temp is above 45 degrees.
Here is my code so far:
#include "main.h"
#include "string.h"
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
ADC_HandleTypeDef hadc3;
FDCAN_HandleTypeDef hfdcan1;
FDCAN_HandleTypeDef hfdcan2;
I2C_HandleTypeDef hi2c4;
LTDC_HandleTypeDef hltdc;
QSPI_HandleTypeDef hqspi;
RTC_HandleTypeDef hrtc;
SAI_HandleTypeDef hsai_BlockA2;
SAI_HandleTypeDef hsai_BlockB2;
MMC_HandleTypeDef hmmc1;
SPI_HandleTypeDef hspi2;
UART_HandleTypeDef huart3;
PCD_HandleTypeDef hpcd_USB_OTG_FS;
SDRAM_HandleTypeDef hsdram1;
/* USER CODE BEGIN PV */
#define BUFSIZE 256
#define DS18B20_PORT GPIOA
#define DS18B20_PIN GPIO_PIN_1
uint8_t SendBuffer[BUFSIZE];
int Counter;
int KeyState=0;
int TouchX, TouchY;
uint8_t dataBuffer[10];
uint8_t VendorID, DeviceMode, Gesture, Status, Touchxh, Touchxl, Touchyh, Touchyl;
//uint8_t temp_lsb, temp_msb;
//int16_t temp_raw;
//float temperature;
uint8_t Temp_byte1, Temp_byte2;
uint16_t SUM, TEMP;
float Temperature = 0;
float Humidity = 0;
uint8_t Presence = 0;// Store temperature value
HAL_StatusTypeDef retval;
uint8_t Answer;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void Display_Temp (float Temp);
void Set_Pin_Output (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
void Set_Pin_Input (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
uint8_t DS18B20_Start (void);
void DS18B20_Write (uint8_t data);
uint8_t DS18B20_Read (void);
void SystemClock_Config(void);
void PeriphCommonClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_ADC2_Init(void);
static void MX_ADC3_Init(void);
static void MX_FDCAN1_Init(void);
static void MX_FDCAN2_Init(void);
static void MX_FMC_Init(void);
static void MX_LTDC_Init(void);
static void MX_QUADSPI_Init(void);
static void MX_RTC_Init(void);
static void MX_SAI2_Init(void);
static void MX_SDMMC1_MMC_Init(void);
static void MX_SPI2_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_USB_OTG_FS_PCD_Init(void);
static void MX_I2C4_Init(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)
{
HAL_Init();
SystemClock_Config();
PeriphCommonClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_ADC2_Init();
MX_ADC3_Init();
MX_FDCAN1_Init();
MX_FDCAN2_Init();
MX_FMC_Init();
MX_LTDC_Init();
MX_QUADSPI_Init();
MX_RTC_Init();
MX_SAI2_Init();
MX_SPI2_Init();
MX_USART3_UART_Init();
MX_USB_OTG_FS_PCD_Init();
MX_I2C4_Init();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, 1);
DS18B20_Init();
while (1)
{
Presence = DS18B20_Start ();
DS18B20_Write (0xCC); // skip ROM
DS18B20_Write (0x44); // convert t
Presence = DS18B20_Start ();
DS18B20_Write (0xCC); // skip ROM
DS18B20_Write (0xBE); // Read Scratch-pad
Temp_byte1 = DS18B20_Read();
Temp_byte2 = DS18B20_Read();
printf("Raw Temp_byte1: %u, Temp_byte2: %un", Temp_byte1, Temp_byte2);
TEMP = ((Temp_byte2<<8))|Temp_byte1;
Temperature = (float)TEMP/16.0; // resolution is 0.0625
HAL_Delay(3000);
Display_Temp(Temperature);
}
/* USER CODE END 3 */
}
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
HAL_Delay(480); // delay according to datasheet
Set_Pin_Input(DS18B20_PORT, DS18B20_PIN);
HAL_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;
HAL_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
HAL_Delay (1); // wait for 1 us
Set_Pin_Input(DS18B20_PORT, DS18B20_PIN); // set as input
HAL_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
HAL_Delay (50); // wait for 60 us
Set_Pin_Input(DS18B20_PORT, DS18B20_PIN);
}
}
}
uint8_t DS18B20_Read (void)
{
uint8_t value=0;
Set_Pin_Input (DS18B20_PORT, DS18B20_PIN);
for (int i=0;i<8;i++)
{
Set_Pin_Output(DS18B20_PORT, DS18B20_PIN); // set as output
HAL_GPIO_WritePin (GPIOA, GPIO_PIN_1, 0); // pull the data pin LOW
HAL_Delay (2); // wait for 2 us
Set_Pin_Input(DS18B20_PORT, DS18B20_PIN); // set as input
if (HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_1)) // if the pin is HIGH
{
value |= 1<<i; // read = 1
}
HAL_Delay (60); // wait for 60 us
}
return value;
}
void Set_Pin_Output (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
void Set_Pin_Input (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
void Display_Temp (float Temp)
{
// char str[20] = {0};
// lcd_put_cur(0, 0);
//
// sprintf (str, "TEMP:- %.2f ", Temp);
// lcd_send_string(str);
// lcd_send_data('C');
// Display temperature and check for heat warning
if (Temp > 40.0) {
snprintf(SendBuffer, BUFSIZE, "Heat Warning! Current Temperature: %.2f°Cnr", Temp);
} else {
snprintf(SendBuffer, BUFSIZE, "Current Temperature: %.2f°Cnr", Temp);
}
HAL_UART_Transmit(&huart3, SendBuffer, strlen(SendBuffer), 100);
}
/**
* @brief System Clock Configuration
* @retval None
*/
and the rest of the code is just generated code
i have also tried to adjust the pin to be analog instead of output and input and these are some the readings i get:
Heat Warning! Current Temperature: 2272.25°C
Heat Warning! Current Temperature: 1055.44°C
Heat Warning! Current Temperature: 2231.31°C