main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include"main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. #include "stdio.h"
  25. /* Private variables ---------------------------------------------------------*/
  26. uint16_t M4_AD_Value = 0;
  27. float M4_voltage_V =0;
  28. float M4_pH_mid = 1.500;
  29. float M4_pH_low = 2.030;
  30. float M4_pH_high =0.975;
  31. float M4_PH=0;
  32. /* USER CODE BEGIN PV */
  33. typedef void (*pFunction)(void);
  34. /* USER CODE END PV */
  35. /* Private function prototypes -----------------------------------------------*/
  36. void SystemClock_Config(void);
  37. /* USER CODE BEGIN PFP */
  38. /* USER CODE END PFP */
  39. /* Private user code ---------------------------------------------------------*/
  40. /* USER CODE BEGIN 0 */
  41. /* USER CODE END 0 */
  42. /**
  43. * @brief The application entry point.
  44. * @retval int
  45. */
  46. int main(void)
  47. {
  48. HAL_Init();
  49. SystemClock_Config();
  50. MX_GPIO_Init();
  51. MX_USART2_UART_Init();
  52. MX_UART4_Init();
  53. MX_USART1_UART_Init();
  54. MX_ADC1_Init();
  55. while (1)
  56. {
  57. HAL_ADC_Start(&hadc1);
  58. HAL_ADC_PollForConversion(&hadc1, 50);
  59. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  60. {
  61. M4_AD_Value = HAL_ADC_GetValue(&hadc1);
  62. M4_voltage_V = M4_AD_Value*3.3f/4096;
  63. if (M4_voltage_V > M4_pH_mid)
  64. {
  65. M4_PH = 7.0 - 3.0 / (M4_pH_low - M4_pH_mid) * (M4_voltage_V - M4_pH_mid);
  66. }
  67. else
  68. {
  69. M4_PH = 7.0 - 3.0 / (M4_pH_mid - M4_pH_high) * (M4_voltage_V - M4_pH_mid);
  70. }
  71. printf("M4_PH: %1.1f\n",M4_PH);
  72. }
  73. HAL_Delay(10000);
  74. if(>=20 and tankstatus == Waiting and M4_PH < 14)
  75. {
  76. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  77. HAL_Delay(30);
  78. }
  79. }
  80. }
  81. void SystemClock_Config(void)
  82. {
  83. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  84. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  85. __HAL_RCC_PWR_CLK_ENABLE();
  86. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  87. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  88. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  89. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  90. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  91. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  92. RCC_OscInitStruct.PLL.PLLM = 8;
  93. RCC_OscInitStruct.PLL.PLLN = 72;
  94. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  95. RCC_OscInitStruct.PLL.PLLQ = 3;
  96. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  97. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  98. {
  99. Error_Handler();
  100. }
  101. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  102. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  103. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  104. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  105. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  106. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  107. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  108. {
  109. Error_Handler();
  110. }
  111. }
  112. void Error_Handler(void)
  113. {
  114. }