Quellcode durchsuchen

[20220818_154836_user] D2_main.c

rita vor 2 Jahren
Ursprung
Commit
5462239fb5
1 geänderte Dateien mit 248 neuen und 0 gelöschten Zeilen
  1. 248 0
      [20220818_154836_user] D2_main.c

+ 248 - 0
[20220818_154836_user] D2_main.c

@@ -0,0 +1,248 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file           : main.c
+  * @brief          : Main program body
+  * @attention
+  *
+  * <h2><center>&copy; Copyright (c) 2019 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:
+  *                        www.st.com/SLA0044
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+#include "adc.h"
+#include "usart.h"
+#include "i2c.h"
+#include "tim.h"
+#include "stm32f4xx_hal.h"
+#include "gpio.h"
+#include "stdio.h"
+
+/* Private variables ---------------------------------------------------------*/
+
+/* Private variables ---------------------------------------------------------*/
+#define VECT_TAB_OFFSET  0x10000
+int tankstatus = 0;
+uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
+uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
+uint8_t zigbeestatu[20];
+uint8_t zigbeestatu1[5];
+uint8_t zigbeecon[7]="connect";
+uint8_t zigbeenotcon[11]="not connect";
+uint8_t zigbeechang[14]="change is down";
+uint16_t M4_AD_Value = 0;
+float M4_voltage_V =0;
+float M4_pH_mid = 1.500;
+float M4_pH_low = 2.030;
+float M4_pH_high =0.975;
+float M4_PH=0;
+/* USER CODE BEGIN PV */
+typedef  void (*pFunction)(void);
+/* USER CODE END PV */
+/* Private function prototypes -----------------------------------------------*/
+void SystemClock_Config(void);
+void MX_ADC1_Init1(char pin);
+void sensor(void);
+/* USER CODE BEGIN PFP */
+/* USER CODE END PFP */
+/* Private user code ---------------------------------------------------------*/
+/* USER CODE BEGIN 0 */
+int _write(int fd, char *ch, int len)
+{
+   HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
+   HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
+   return len;
+}
+void print(char* s, float c)
+{   int w;
+    int w_d;
+    int w_f;
+    if(c<0)
+    {
+       w = c*100;
+       w_d = w/100;
+       w_f = (-1*w)%100;
+    }
+    else
+    {
+      w = c*100;
+      w_d = w/100;
+      w_f = w%100;
+    }
+    printf("%s = %d.%d\n",s,w_d,w_f);
+}
+/* USER CODE END 0 */
+/**
+* @brief  The application entry point.
+* @retval int
+*/
+int main(void)
+{
+  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
+  HAL_Init();
+  SystemClock_Config();
+  MX_GPIO_Init();
+  MX_USART2_UART_Init();
+  MX_UART4_Init();
+  MX_USART1_UART_Init();
+  HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
+  HAL_Delay(1000);
+  HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
+  HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
+  HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
+  if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
+  {
+     HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
+  }
+  else if(zigbeestatu[18]== 0x05)
+  {
+     HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
+  }
+  while (1)
+  {
+    sensor();
+    if(M4_PH<16)
+    {
+         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
+
+         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
+
+         HAL_Delay(5000);
+
+    }
+  }
+}
+void SystemClock_Config(void)
+{
+  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
+  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
+  __HAL_RCC_PWR_CLK_ENABLE();
+  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
+  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
+  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
+  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
+  RCC_OscInitStruct.PLL.PLLM = 8;
+  RCC_OscInitStruct.PLL.PLLN = 72;
+  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
+  RCC_OscInitStruct.PLL.PLLQ = 3;
+  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  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();
+  }
+}
+void user_pwm_setvalue(uint16_t value)
+{
+    TIM_OC_InitTypeDef sConfigOC;
+    sConfigOC.OCMode = TIM_OCMODE_PWM1;
+    sConfigOC.Pulse = value;
+    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
+    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
+    HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
+    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
+}
+void user_pwm_setvalue2(uint16_t value)
+{
+    TIM_OC_InitTypeDef sConfigOC;
+    sConfigOC.OCMode = TIM_OCMODE_PWM1;
+    sConfigOC.Pulse = value;
+    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
+    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
+    HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
+    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
+}
+void sensor(void)
+{
+    MX_ADC1_Init1(ADC_CHANNEL_0);
+    HAL_ADC_Start(&hadc1);
+    HAL_ADC_PollForConversion(&hadc1, 50);
+    if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
+      {
+          M4_AD_Value = HAL_ADC_GetValue(&hadc1);
+          M4_voltage_V = M4_AD_Value*3.3f/4096;
+          if (M4_voltage_V > M4_pH_mid)
+           {
+               M4_PH = 7.0 - 3.0 / (M4_pH_low - M4_pH_mid) * (M4_voltage_V - M4_pH_mid);
+           }
+          else
+           {
+               M4_PH = 7.0 - 3.0 / (M4_pH_mid - M4_pH_high) * (M4_voltage_V - M4_pH_mid);
+           }
+      }
+    print("M4_PH",M4_PH);
+    HAL_Delay(10000);
+}
+void MX_ADC1_Init1(char pin)
+{
+  ADC_ChannelConfTypeDef sConfig = {0};
+  hadc1.Instance = ADC1;
+  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
+  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
+  hadc1.Init.ScanConvMode = DISABLE;
+  hadc1.Init.ContinuousConvMode = DISABLE;
+  hadc1.Init.DiscontinuousConvMode = DISABLE;
+  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
+  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
+  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
+  hadc1.Init.NbrOfConversion = 1;
+  hadc1.Init.DMAContinuousRequests = DISABLE;
+  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
+    if (HAL_ADC_Init(&hadc1) != HAL_OK)
+    {
+      Error_Handler();
+    }
+  sConfig.Channel = pin;
+  sConfig.Rank = 1;
+  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
+  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
+  {
+    Error_Handler();
+  }
+}
+void MX_GPIO_Input1(unsigned long pin)
+{
+  GPIO_InitTypeDef GPIO_InitStruct = {0};
+  __HAL_RCC_GPIOE_CLK_ENABLE();
+  GPIO_InitStruct.Pin = pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
+  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+}
+void Error_Handler(void)
+{
+}