428 lines
11 KiB
C
Executable File
428 lines
11 KiB
C
Executable File
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2025 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include "hd44780.h"
|
|
#include "LPS22.h"
|
|
#include "HTS221.h"
|
|
#include <stdio.h>
|
|
/* USER CODE END Includes */
|
|
|
|
/* 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 ---------------------------------------------------------*/
|
|
I2C_HandleTypeDef hi2c2;
|
|
|
|
UART_HandleTypeDef huart1;
|
|
|
|
/* USER CODE BEGIN PV */
|
|
uint8_t rxByte;
|
|
char displayMode = ' '; // para los comandos
|
|
int useFahrenheit = 0; // 0 -> ºC, 1 -> ºF
|
|
float maxTemp = -100.0, minTemp = 100.0;
|
|
float maxHum = 0.0, minHum = 100.0;
|
|
float maxPress = 0.0, minPress = 2000.0;
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
static void MX_GPIO_Init(void);
|
|
static void MX_I2C2_Init(void);
|
|
static void MX_USART1_UART_Init(void);
|
|
/* USER CODE BEGIN PFP */
|
|
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
void __io_putchar(int ch)
|
|
{
|
|
uint8_t c[1];
|
|
c[0] = ch & 0x00ff;
|
|
HAL_UART_Transmit(&huart1, &*c, 1, 10);
|
|
return ch;
|
|
}
|
|
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
{
|
|
switch (rxByte) {
|
|
case 'm':
|
|
case 'n':
|
|
displayMode = rxByte;
|
|
break;
|
|
case 'c':
|
|
maxTemp = -100.0; minTemp = 100.0;
|
|
maxHum = 0.0; minHum = 100.0;
|
|
maxPress = 0.0; minPress = 2000.0;
|
|
displayMode = ' ';
|
|
break;
|
|
case 'u':
|
|
useFahrenheit = !useFahrenheit;
|
|
break;
|
|
default:
|
|
displayMode = ' ';
|
|
break;
|
|
}
|
|
HAL_UART_Receive_IT(&huart1, &rxByte, 1);
|
|
}
|
|
/* 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_I2C2_Init();
|
|
MX_USART1_UART_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
// LCD44780 INIT
|
|
lcd_reset();
|
|
lcd_display_settings(1,0,0);
|
|
lcd_clear();
|
|
|
|
// LPS22 INIT
|
|
LPS22_Init();
|
|
|
|
// HTS221 INIT
|
|
HTS221_Init();
|
|
|
|
// USART INTERRUPTIONS
|
|
HAL_UART_Receive_IT(&huart1, &rxByte, 1);
|
|
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
uint8_t str[20];
|
|
float pressure = LPS22_ReadPress();
|
|
THSample ths = HTS221_Read();
|
|
|
|
if (ths.temp > maxTemp) maxTemp = ths.temp;
|
|
if (ths.temp < minTemp) minTemp = ths.temp;
|
|
if (ths.hum > maxHum) maxHum = ths.hum;
|
|
if (ths.hum < minHum) minHum = ths.hum;
|
|
if (pressure > maxPress) maxPress = pressure;
|
|
if (pressure < minPress) minPress = pressure;
|
|
|
|
float tempToShow = useFahrenheit ? (ths.temp * 9.0 / 5.0) + 32 : ths.temp; // convertir a ºF si es necesario
|
|
float pressureToShow = useFahrenheit ? pressure * 0.75006 : pressure; // convertir a mmHg si es necesario
|
|
char tempUnit = useFahrenheit ? 'F' : 'C';
|
|
const char *pressUnit = useFahrenheit ? "mmHg" : "hPa";
|
|
|
|
switch (displayMode) {
|
|
case 'm':
|
|
sprintf(str, "Max: %.1f%c %.1f%%", maxTemp, tempUnit, maxHum);
|
|
moveToXY(0, 0);
|
|
lcd_print(str);
|
|
printf("%s\n\r", str); // Enviar a consola UART
|
|
|
|
sprintf(str, "Pres: %.1f%s", maxPress, pressUnit);
|
|
moveToXY(1, 1);
|
|
lcd_print(str);
|
|
printf("%s\n\r", str); // Enviar a consola UART
|
|
break;
|
|
|
|
case 'n':
|
|
sprintf(str, "Min: %.1f%c %.1f%%", minTemp, tempUnit, minHum);
|
|
moveToXY(0, 0);
|
|
lcd_print(str);
|
|
printf("%s\n\r", str); // Enviar a consola UART
|
|
|
|
sprintf(str, "Pres: %.1f%s", minPress, pressUnit);
|
|
moveToXY(1, 1);
|
|
lcd_print(str);
|
|
printf("%s\n\r", str); // Enviar a consola UART
|
|
break;
|
|
|
|
default:
|
|
sprintf(str, "T:%.1f%c H:%.1f%%", tempToShow, tempUnit, ths.hum);
|
|
moveToXY(0, 0);
|
|
lcd_print(str);
|
|
printf("%s\n\r", str); // Enviar a consola UART
|
|
|
|
sprintf(str, "Pres: %.1f%s", pressureToShow, pressUnit);
|
|
moveToXY(1, 1);
|
|
lcd_print(str);
|
|
printf("%s\n\r", str); // Enviar a consola UART
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Delay(500);
|
|
/* USER CODE END WHILE */
|
|
|
|
/* 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};
|
|
|
|
/** Configure the main internal regulator output voltage
|
|
*/
|
|
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
|
|
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
|
|
RCC_OscInitStruct.MSICalibrationValue = 0;
|
|
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
|
|
RCC_OscInitStruct.PLL.PLLM = 1;
|
|
RCC_OscInitStruct.PLL.PLLN = 40;
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
|
|
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
|
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_DIV1;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief I2C2 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_I2C2_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN I2C2_Init 0 */
|
|
|
|
/* USER CODE END I2C2_Init 0 */
|
|
|
|
/* USER CODE BEGIN I2C2_Init 1 */
|
|
|
|
/* USER CODE END I2C2_Init 1 */
|
|
hi2c2.Instance = I2C2;
|
|
hi2c2.Init.Timing = 0x10D19CE4;
|
|
hi2c2.Init.OwnAddress1 = 0;
|
|
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
|
hi2c2.Init.OwnAddress2 = 0;
|
|
hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
|
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
|
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Analogue filter
|
|
*/
|
|
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Digital filter
|
|
*/
|
|
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN I2C2_Init 2 */
|
|
|
|
/* USER CODE END I2C2_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief USART1 Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
huart1.Instance = USART1;
|
|
huart1.Init.BaudRate = 115200;
|
|
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart1.Init.StopBits = UART_STOPBITS_1;
|
|
huart1.Init.Parity = UART_PARITY_NONE;
|
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
if (HAL_UART_Init(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief GPIO Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_GPIO_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
|
/* USER CODE END MX_GPIO_Init_1 */
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOA, Led_LCD_Pin|D4_LCD_Pin|D7_LCD_Pin|E_LCD_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOB, D6_LCD_Pin|RS_LCD_Pin|D5_LCD_Pin, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pins : Led_LCD_Pin D4_LCD_Pin D7_LCD_Pin E_LCD_Pin */
|
|
GPIO_InitStruct.Pin = Led_LCD_Pin|D4_LCD_Pin|D7_LCD_Pin|E_LCD_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : D6_LCD_Pin RS_LCD_Pin D5_LCD_Pin */
|
|
GPIO_InitStruct.Pin = D6_LCD_Pin|RS_LCD_Pin|D5_LCD_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
|
/* USER CODE END MX_GPIO_Init_2 */
|
|
}
|
|
|
|
/* 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 */
|
|
__disable_irq();
|
|
while (1)
|
|
{
|
|
}
|
|
/* 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,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|