1
0

main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. /* Private variables ---------------------------------------------------------*/
  28. /* Private variables ---------------------------------------------------------*/
  29. #define VECT_TAB_OFFSET 0x10000
  30. int tankstatus = 0;
  31. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  32. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  33. uint8_t zigbeestatu[20];
  34. uint8_t zigbeestatu1[5];
  35. uint8_t zigbeecon[7]="connect";
  36. uint8_t zigbeenotcon[11]="not connect";
  37. uint8_t zigbeechang[14]="change is down";
  38. uint16_t M4_AD_Value = 0;
  39. float M4_voltage_V =0;
  40. float M4_pH_mid = 1.500;
  41. float M4_pH_low = 2.030;
  42. float M4_pH_high =0.975;
  43. float M4_PH=0;
  44. uint8_t soil[8] ={0x01,0x03,0x00,0x00,0x00,0x03,0x05,0xCB};
  45. uint8_t data[11] ={0,0,0,0,0,0,0,0,0,0,0};
  46. float M8_Soil =0;
  47. int rh=0;
  48. int temp=0;
  49. int ec=0;
  50. uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};
  51. HAL_StatusTypeDef Status;
  52. #define ADDR_AT24C04_WRITE_FIRST_16_PAGES 0xEC
  53. #define ADDR_AT24C04_WRITE_FIRST_16_PAGES 0xEC
  54. #define ADDR_AT24C04_READ 0xED
  55. #define AT24C04_TIMEOUT 0xED
  56. #define AT24C04_PAGE_SIZE 16
  57. #define BUFFER_SIZE 1
  58. uint16_t dig_T1;
  59. uint16_t dig_T2;
  60. uint16_t dig_T3;
  61. uint16_t dig_P1;
  62. uint16_t dig_P2;
  63. uint16_t dig_P3;
  64. uint16_t dig_P4;
  65. uint16_t dig_P5;
  66. uint16_t dig_P6;
  67. uint16_t dig_P7;
  68. uint16_t dig_P8;
  69. uint16_t dig_P9;
  70. uint32_t adc_P=0;
  71. uint32_t adc_T=0;
  72. uint8_t WriteBuffer[BUFFER_SIZE]={0xb6};
  73. uint8_t WriteBuffer1[BUFFER_SIZE]={0xff};
  74. uint8_t WriteBuffer2[BUFFER_SIZE]={0x00};
  75. uint8_t ReadBuffer[BUFFER_SIZE];
  76. int RH;
  77. float M15_BMP280 =0;
  78. /* USER CODE BEGIN PV */
  79. typedef void (*pFunction)(void);
  80. /* USER CODE END PV */
  81. /* Private function prototypes -----------------------------------------------*/
  82. void SystemClock_Config(void);
  83. void MX_ADC1_Init1(char pin);
  84. void sensor(void);
  85. void user_pwm_setvalue(uint16_t value);
  86. void user_pwm_setvalue2(uint16_t value);
  87. HAL_StatusTypeDef AT24C04_Write(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData);
  88. HAL_StatusTypeDef AT24C04_Read(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData);
  89. long bmp280_T_MultipleReadThree(void);
  90. long bmp280_P_MultipleReadThree(void);
  91. short bmp280_MultipleReadTwo(uint16_t addr);
  92. /* USER CODE BEGIN PFP */
  93. /* USER CODE END PFP */
  94. /* Private user code ---------------------------------------------------------*/
  95. /* USER CODE BEGIN 0 */
  96. /* USER CODE END 0 */
  97. /**
  98. * @brief The application entry point.
  99. * @retval int
  100. */
  101. int main(void)
  102. {
  103. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  104. HAL_Init();
  105. SystemClock_Config();
  106. MX_GPIO_Init();
  107. MX_USART2_UART_Init();
  108. MX_UART4_Init();
  109. MX_USART1_UART_Init();
  110. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  111. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  112. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  113. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  114. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  115. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  116. if (zigbeestatu[18]== 0x01)
  117. {
  118. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  119. }
  120. else if(zigbeestatu[18]== 0x05 || zigbeestatu[18]== 0x00)
  121. {
  122. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  123. }
  124. MX_TIM2_Init();
  125. AT24C04_Write(&hi2c1,0xe0,WriteBuffer);
  126. AT24C04_Write(&hi2c1,0xf4,WriteBuffer1);
  127. AT24C04_Write(&hi2c1,0xf5,WriteBuffer2);
  128. dig_T1 = bmp280_MultipleReadTwo(0x88);
  129. dig_T2 = bmp280_MultipleReadTwo(0x8A);
  130. dig_T3 = bmp280_MultipleReadTwo(0x8C);
  131. dig_P1 = bmp280_MultipleReadTwo(0x8E);
  132. dig_P2 = bmp280_MultipleReadTwo(0x90);
  133. dig_P3 = bmp280_MultipleReadTwo(0x92);
  134. dig_P4 = bmp280_MultipleReadTwo(0x94);
  135. dig_P5 = bmp280_MultipleReadTwo(0x96);
  136. dig_P6 = bmp280_MultipleReadTwo(0x98);
  137. dig_P7 = bmp280_MultipleReadTwo(0x9A);
  138. dig_P8 = bmp280_MultipleReadTwo(0x9C);
  139. dig_P9 = bmp280_MultipleReadTwo(0x9E);
  140. while (1)
  141. {
  142. sensor();
  143. if(M4_PH<14 && M4_PH > 0 && M8_Soil > 0 && M15_BMP280 > 0)
  144. {
  145. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_RESET);
  146. HAL_Delay(10000);
  147. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_SET);
  148. user_pwm_setvalue2(0);
  149. user_pwm_setvalue(10);
  150. }
  151. }
  152. }
  153. void SystemClock_Config(void)
  154. {
  155. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  156. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  157. __HAL_RCC_PWR_CLK_ENABLE();
  158. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  159. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  160. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  161. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  162. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  163. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  164. RCC_OscInitStruct.PLL.PLLM = 8;
  165. RCC_OscInitStruct.PLL.PLLN = 72;
  166. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  167. RCC_OscInitStruct.PLL.PLLQ = 3;
  168. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  169. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  174. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  175. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  176. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  177. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  178. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  179. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  180. {
  181. Error_Handler();
  182. }
  183. }
  184. void user_pwm_setvalue(uint16_t value)
  185. {
  186. TIM_OC_InitTypeDef sConfigOC;
  187. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  188. sConfigOC.Pulse = value;
  189. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  190. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  191. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  192. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  193. }
  194. void user_pwm_setvalue2(uint16_t value)
  195. {
  196. TIM_OC_InitTypeDef sConfigOC;
  197. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  198. sConfigOC.Pulse = value;
  199. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  200. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  201. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  202. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  203. }
  204. void sensor(void)
  205. {
  206. adc_T=bmp280_T_MultipleReadThree();
  207. adc_P=bmp280_P_MultipleReadThree();
  208. double var1, var2,t_fine;
  209. var1 = (((double) adc_T) / 16384.0 - ((double) dig_T1) / 1024.0)* ((double) dig_T2);
  210. var2 = ((((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0)* (((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0))* ((double) dig_T3);
  211. t_fine = (int32_t) (var1 + var2);
  212. double temperature = (var1 + var2) / 5120.0;
  213. double pressure;
  214. var1 = (t_fine / 2.0) - 64000.0;
  215. var2 = var1 * var1 * ((double) dig_P6) / 32768.0;
  216. var2 = var2 + var1 * ((double) dig_P5) * 2.0;
  217. var2 = (var2 / 4.0) + (((double) dig_P4) * 65536.0);
  218. var1 = (((double) dig_P3) * var1 * var1 / 524288.0+ ((double) dig_P2) * var1) / 524288.0;
  219. var1 = (1.0 + var1 / 32768.0) * ((double) dig_P1);
  220. if (var1 == 0.0)
  221. {
  222. var1 = 0;
  223. }
  224. pressure = 1048576.0 - (double) adc_P;
  225. pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
  226. var1 = ((double) dig_P9) * pressure * pressure / 2147483648.0; var2 = pressure * ((double) dig_P8) / 32768.0;
  227. pressure = pressure + (var1 + var2 + ((double) dig_P7)) / 16.0;
  228. cmd[1]=pressure/10000;
  229. int pa =pressure;
  230. cmd[2]=(pa%10000)/100;
  231. cmd[3]=(pa%10000)%100;
  232. HAL_UART_Transmit(&huart4, (uint8_t *)cmd, sizeof(cmd),1);
  233. MX_ADC1_Init1(ADC_CHANNEL_0);
  234. HAL_ADC_Start(&hadc1);
  235. HAL_ADC_PollForConversion(&hadc1, 50);
  236. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  237. {
  238. M4_AD_Value = HAL_ADC_GetValue(&hadc1);
  239. M4_voltage_V = M4_AD_Value*3.3f/4096;
  240. if (M4_voltage_V > M4_pH_mid)
  241. {
  242. M4_PH = 7.0 - 3.0 / (M4_pH_low - M4_pH_mid) * (M4_voltage_V - M4_pH_mid);
  243. }
  244. else
  245. {
  246. M4_PH = 7.0 - 3.0 / (M4_pH_mid - M4_pH_high) * (M4_voltage_V - M4_pH_mid);
  247. }
  248. }
  249. HAL_UART_Transmit(&huart4,(unsigned char*)&M4_PH,4,10);
  250. HAL_UART_Transmit(&huart2, (uint8_t *)soil,sizeof(soil),50);
  251. HAL_UART_Receive_IT(&huart2, (uint8_t *)data,sizeof(data));
  252. adc_T=bmp280_T_MultipleReadThree();
  253. adc_P=bmp280_P_MultipleReadThree();
  254. double var1, var2,t_fine;
  255. var1 = (((double) adc_T) / 16384.0 - ((double) dig_T1) / 1024.0)* ((double) dig_T2);
  256. var2 = ((((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0)* (((double) adc_T) / 131072.0 - ((double) dig_T1) / 8192.0))* ((double) dig_T3);
  257. t_fine = (int32_t) (var1 + var2);
  258. double temperature = (var1 + var2) / 5120.0;
  259. double pressure;
  260. var1 = (t_fine / 2.0) - 64000.0;
  261. var2 = var1 * var1 * ((double) dig_P6) / 32768.0;
  262. var2 = var2 + var1 * ((double) dig_P5) * 2.0;
  263. var2 = (var2 / 4.0) + (((double) dig_P4) * 65536.0);
  264. var1 = (((double) dig_P3) * var1 * var1 / 524288.0+ ((double) dig_P2) * var1) / 524288.0;
  265. var1 = (1.0 + var1 / 32768.0) * ((double) dig_P1);
  266. if (var1 == 0.0)
  267. {
  268. var1 = 0;
  269. }
  270. pressure = 1048576.0 - (double) adc_P;
  271. pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
  272. var1 = ((double) dig_P9) * pressure * pressure / 2147483648.0; var2 = pressure * ((double) dig_P8) / 32768.0;
  273. pressure = pressure + (var1 + var2 + ((double) dig_P7)) / 16.0;
  274. cmd[1]=pressure/10000;
  275. int pa =pressure;
  276. cmd[2]=(pa%10000)/100;
  277. cmd[3]=(pa%10000)%100;
  278. HAL_UART_Transmit(&huart4, (uint8_t *)cmd, sizeof(cmd),1);
  279. HAL_Delay(10000);
  280. }
  281. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  282. {
  283. if (huart->Instance == USART2)
  284. {
  285. if (data[0]==0x01 && data[1]==0x03)
  286. {
  287. rh = (data[3] << 8) + data[4];
  288. temp = (data[5] << 8) + data[6];
  289. ec = (data[7] << 8) + data[8];
  290. }
  291. }
  292. }
  293. HAL_StatusTypeDef AT24C04_Write(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData)
  294. {
  295. Status = HAL_I2C_Mem_Write(&hi2c1, ADDR_AT24C04_WRITE_FIRST_16_PAGES, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, AT24C04_PAGE_SIZE, AT24C04_TIMEOUT);
  296. return Status;
  297. }
  298. HAL_StatusTypeDef AT24C04_Read(I2C_HandleTypeDef *hi2c,uint16_t MemAddress,uint8_t *pData)
  299. {
  300. Status = HAL_I2C_Mem_Read(&hi2c1, ADDR_AT24C04_READ, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, BUFFER_SIZE, AT24C04_TIMEOUT);
  301. return Status;
  302. }
  303. long bmp280_T_MultipleReadThree()
  304. {
  305. uint8_t msb, lsb, xlsb;
  306. int32_t temp = 0;
  307. AT24C04_Read(&hi2c1,0xfa,ReadBuffer);
  308. msb=ReadBuffer[0];
  309. AT24C04_Read(&hi2c1,0xfb,ReadBuffer);
  310. lsb=ReadBuffer[0];
  311. AT24C04_Read(&hi2c1,0xfc,ReadBuffer);
  312. xlsb=ReadBuffer[0];
  313. temp = ((msb << 12)|(lsb << 4)|(xlsb >> 4));
  314. return temp;
  315. }
  316. long bmp280_P_MultipleReadThree()
  317. {
  318. uint8_t msb, lsb, xlsb;
  319. int32_t pressure = 0;
  320. AT24C04_Read(&hi2c1,0xf7,ReadBuffer);
  321. msb=ReadBuffer[0];
  322. AT24C04_Read(&hi2c1,0xf8,ReadBuffer);
  323. lsb=ReadBuffer[0];
  324. AT24C04_Read(&hi2c1,0xf9,ReadBuffer);
  325. xlsb=ReadBuffer[0];
  326. pressure = ((msb << 12)|(lsb << 4)|(xlsb >> 4));
  327. return pressure;
  328. }
  329. short bmp280_MultipleReadTwo(uint16_t addr )
  330. {
  331. uint8_t msb, lsb;
  332. uint16_t temp = 0;
  333. AT24C04_Read(&hi2c1,addr,ReadBuffer);
  334. lsb = ReadBuffer[0];
  335. AT24C04_Read(&hi2c1,addr+1,ReadBuffer);
  336. msb = ReadBuffer[0];
  337. temp = msb << 8|lsb;
  338. return temp;
  339. }
  340. void MX_ADC1_Init1(char pin)
  341. {
  342. ADC_ChannelConfTypeDef sConfig = {0};
  343. hadc1.Instance = ADC1;
  344. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  345. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  346. hadc1.Init.ScanConvMode = DISABLE;
  347. hadc1.Init.ContinuousConvMode = DISABLE;
  348. hadc1.Init.DiscontinuousConvMode = DISABLE;
  349. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  350. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  351. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  352. hadc1.Init.NbrOfConversion = 1;
  353. hadc1.Init.DMAContinuousRequests = DISABLE;
  354. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  355. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  356. {
  357. Error_Handler();
  358. }
  359. sConfig.Channel = pin;
  360. sConfig.Rank = 1;
  361. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  362. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  363. {
  364. Error_Handler();
  365. }
  366. }
  367. void MX_GPIO_Input1(unsigned long pin)
  368. {
  369. GPIO_InitTypeDef GPIO_InitStruct = {0};
  370. __HAL_RCC_GPIOE_CLK_ENABLE();
  371. GPIO_InitStruct.Pin = pin;
  372. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  373. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  374. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  375. }
  376. void Error_Handler(void)
  377. {
  378. }