MPX4250AP-Pressure-gauge-main.c


/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "LiquidCrystal.h"
#include"stdio.h"
#include"math.h"
/* USER CODE END Includes */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* Calibration Parameters */
#define ADC_RESOLUTION 4095     // 12-bit ADC resolution
#define ADC_VOLTAGE_RANGE 5.0f  // Voltage range of ADC in volts
#define PRESSURE_MIN 0.0        // Minimum pressure in kPa
#define VOLTAGE_MIN 0.25        // Corresponding minimum voltage in V
#define PRESSURE_MAX 250.0f     // Maximum pressure in kPa
#define VOLTAGE_MAX 4.87        // Corresponding maximum voltage in V

/* UART Message Strings */
#define MSG_OK "Operation successful.\n\r"
#define MSG_NOT_OK "Operation failed.\n\r"

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */
/* Global Variables */
char uartMsg[50]; // UART message buffer
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_ADC1_Init(void);

int main(void)
{
  /* USER CODE BEGIN 1 */
  float sensitivity = (PRESSURE_MAX - PRESSURE_MIN) / (VOLTAGE_MAX - VOLTAGE_MIN);
  float offset = PRESSURE_MIN - (sensitivity * VOLTAGE_MIN);
  float resolution = ADC_VOLTAGE_RANGE / ADC_RESOLUTION;
  /* USER CODE END 1 */


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

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_ADC1_Init();
  
  /* USER CODE BEGIN 2 */
  LiquidCrystal(GPIOB, GPIO_PIN_3,GPIO_PIN_4,GPIO_PIN_5,GPIO_PIN_6 ,GPIO_PIN_7,GPIO_PIN_8,GPIO_PIN_9);
  begin(20,4);
  /* USER CODE END 2 */

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_ADC_Start(&hadc1);

    // Wait for the conversion to complete (timeout: 1 second)
    if (HAL_ADC_PollForConversion(&hadc1, 1000) != HAL_OK)
      HAL_UART_Transmit(&huart1, (uint8_t *)MSG_NOT_OK, sizeof(MSG_NOT_OK), 100);
    else
    {
     HAL_UART_Transmit(&huart1, (uint8_t *)MSG_OK, sizeof(MSG_OK), 100);
     // Read the ADC value
     int adc_value = HAL_ADC_GetValue(&hadc1);

     // Convert ADC value to pressure using calibration parameters
     float pressure_kPa = (adc_value * resolution * sensitivity) + offset;
     float pressure_mbar = pressure_kPa * 10; // Convert kPa to mbar
     
     setCursor(0, 0);
     print("Pressure Value=");
     setCursor(0, 1);
     sprintf(uartMsg, "%.1f", pressure_kPa);
     print(uartMsg);
     setCursor(7, 1);
     print("kPa");
     setCursor(0, 2);
     sprintf(uartMsg, "%.1f", pressure_mbar);
     print(uartMsg);
     setCursor(7, 2);
     print("Mbar");
     HAL_Delay(1000);
     clear();

    }
    HAL_ADC_Stop(&hadc1);
  }
  /* USER CODE END 3 */
}