找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 920|回复: 0
打印 上一主题 下一主题
收起左侧

STM32F1—HAL库空气监测程序

[复制链接]
跳转到指定楼层
楼主
ID:404476 发表于 2019-1-21 11:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   ** This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * COPYRIGHT(c) 2019 STMicroelectronics
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39. /* USER CODE END Header */

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

  42. /* Private includes ----------------------------------------------------------*/
  43. /* USER CODE BEGIN Includes */
  44. #include "oled.h"

  45. /* USER CODE END Includes */

  46. /* Private typedef -----------------------------------------------------------*/
  47. /* USER CODE BEGIN PTD */
  48. uint8_t tx_buf[7]={0x42,0x4d,0xab,0x00,0x00,0x01,0x3a};

  49. uint8_t aRxBuffer[50];
  50. volatile uint8_t CNT=0;

  51. uint16_t  PM_VAL;
  52. uint16_t  TVOC_VAL_H ;
  53. uint16_t  TVOC_VAL_L ;
  54. uint16_t  HCHO_VAL_H ;
  55. uint16_t         HCHO_VAL_L;
  56. uint16_t         CO2_VAL;
  57. uint16_t         TEM_VAL_H;
  58. uint16_t  TEM_VAL_L;
  59. uint16_t         HIM_VAL_H;
  60. uint16_t         HIM_VAL_L;
  61. /* USER CODE END PTD */

  62. /* Private define ------------------------------------------------------------*/
  63. /* USER CODE BEGIN PD */

  64. /* USER CODE END PD */

  65. /* Private macro -------------------------------------------------------------*/
  66. /* USER CODE BEGIN PM */

  67. /* USER CODE END PM */

  68. /* Private variables ---------------------------------------------------------*/
  69. TIM_HandleTypeDef htim2;
  70. TIM_HandleTypeDef htim3;

  71. UART_HandleTypeDef huart1;

  72. /* USER CODE BEGIN PV */

  73. /* USER CODE END PV */

  74. /* Private function prototypes -----------------------------------------------*/
  75. void SystemClock_Config(void);
  76. static void MX_GPIO_Init(void);
  77. static void MX_USART1_UART_Init(void);
  78. static void MX_TIM2_Init(void);
  79. static void MX_TIM3_Init(void);
  80. /* USER CODE BEGIN PFP */

  81. /* USER CODE END PFP */

  82. /* Private user code ---------------------------------------------------------*/
  83. /* USER CODE BEGIN 0 */

  84. /* USER CODE END 0 */

  85. /**
  86.   * @brief  The application entry point.
  87.   * @retval int
  88.   */
  89. int main(void)
  90. {
  91.   /* USER CODE BEGIN 1 */
  92.         

  93.   /* USER CODE END 1 */

  94.   /* MCU Configuration--------------------------------------------------------*/

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

  97.   /* USER CODE BEGIN Init */

  98.   /* USER CODE END Init */

  99.   /* Configure the system clock */
  100.   SystemClock_Config();

  101.   /* USER CODE BEGIN SysInit */

  102.   /* USER CODE END SysInit */

  103.   /* Initialize all configured peripherals */
  104.   MX_GPIO_Init();
  105.   MX_USART1_UART_Init();
  106.   MX_TIM2_Init();
  107.   MX_TIM3_Init();
  108.   /* USER CODE BEGIN 2 */
  109.         HAL_TIM_Base_Start_IT(&htim2);
  110.         OLED_Init();
  111.         OLED_Clear();

  112.   /* USER CODE END 2 */

  113.   /* Infinite loop */
  114.   /* USER CODE BEGIN WHILE */
  115.   while (1)
  116.   {
  117.     /* USER CODE END WHILE */

  118.     /* USER CODE BEGIN 3 */
  119.                 HAL_UART_Receive_IT(&huart1,(uint8_t*)aRxBuffer,24);
  120.                         OLED_ShowString(25,0,"AIR-SENSER");
  121.                         OLED_ShowString(0,2,"CO2:");
  122.                         OLED_ShowString(65,2,"HH:");
  123.                         OLED_ShowString(105,2,".");
  124.                         OLED_ShowString(0,4,"HIM:");
  125.                         OLED_ShowString(49,4,".");
  126.                         OLED_ShowString(65,4,"TEM");
  127.                         OLED_ShowString(113,4,".");
  128.                         OLED_ShowString(0,6,"TVOC");
  129.                         OLED_ShowString(41,6,".");
  130.                         OLED_ShowString(65,6,"PM");
  131.                         
  132.                  OLED_ShowNum(97,6,PM_VAL,3,12);
  133.                  OLED_ShowNum(33,2,CO2_VAL,4,12);
  134.                
  135.                  OLED_ShowNum(97,4,TEM_VAL_H,2,12);
  136.                  OLED_ShowNum(121,4,TEM_VAL_L,1,12);
  137.                  OLED_ShowNum(33,4, HIM_VAL_H,2,12);
  138.                  OLED_ShowNum(57,4, HIM_VAL_L,1,12);
  139.                  OLED_ShowNum(97,2, HCHO_VAL_H,1,12);
  140.                  
  141.                  if(HCHO_VAL_L<10)
  142.                  {
  143.                  OLED_ShowNum(113,2, 0,1,12);
  144.                  OLED_ShowNum(121,2, HCHO_VAL_L,1,12);
  145.                  }else OLED_ShowNum(113,2, HCHO_VAL_L,2,12);
  146.                
  147.                  OLED_ShowNum(33,6,TVOC_VAL_H,1,12);
  148.                  if(HCHO_VAL_L<10)
  149.                  {
  150.                  OLED_ShowNum(49,6,0,1,12);
  151.                  OLED_ShowNum(57,6,TVOC_VAL_L,1,12);
  152.                  }else OLED_ShowNum(49,6, TVOC_VAL_L,2,12);
  153.         }
  154.   /* USER CODE END 3 */
  155. }

  156. /**
  157.   * @brief System Clock Configuration
  158.   * @retval None
  159.   */
  160. void SystemClock_Config(void)
  161. {
  162.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  163.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  164.   /**Initializes the CPU, AHB and APB busses clocks
  165.   */
  166.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  167.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  168.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  169.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  170.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  171.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  172.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  173.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  174.   {
  175.     Error_Handler();
  176.   }
  177.   /**Initializes the CPU, AHB and APB busses clocks
  178.   */
  179.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  180.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  181.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  182.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  183.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  184.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  185.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  186.   {
  187.     Error_Handler();
  188.   }
  189. }

  190. /**
  191.   * @brief TIM2 Initialization Function
  192.   * @param None
  193.   * @retval None
  194.   */
  195. static void MX_TIM2_Init(void)
  196. {

  197.   /* USER CODE BEGIN TIM2_Init 0 */

  198.   /* USER CODE END TIM2_Init 0 */

  199.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  200.   TIM_MasterConfigTypeDef sMasterConfig = {0};

  201.   /* USER CODE BEGIN TIM2_Init 1 */

  202.   /* USER CODE END TIM2_Init 1 */
  203.   htim2.Instance = TIM2;
  204.   htim2.Init.Prescaler = 49999;
  205.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  206.   htim2.Init.Period = 71999;
  207.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  208.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  209.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  210.   {
  211.     Error_Handler();
  212.   }
  213.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  214.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  215.   {
  216.     Error_Handler();
  217.   }
  218.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  219.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  220.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  221.   {
  222.     Error_Handler();
  223.   }
  224.   /* USER CODE BEGIN TIM2_Init 2 */

  225.   /* USER CODE END TIM2_Init 2 */

  226. }

  227. /**
  228.   * @brief TIM3 Initialization Function
  229.   * @param None
  230.   * @retval None
  231.   */
  232. static void MX_TIM3_Init(void)
  233. {

  234.   /* USER CODE BEGIN TIM3_Init 0 */

  235.   /* USER CODE END TIM3_Init 0 */

  236.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  237.   TIM_MasterConfigTypeDef sMasterConfig = {0};

  238.   /* USER CODE BEGIN TIM3_Init 1 */

  239.   /* USER CODE END TIM3_Init 1 */
  240.   htim3.Instance = TIM3;
  241.   htim3.Init.Prescaler = 9999;
  242.   htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  243.   htim3.Init.Period = 71999;
  244.   htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  245.   htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  246.   if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  247.   {
  248.     Error_Handler();
  249.   }
  250.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  251.   if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  252.   {
  253.     Error_Handler();
  254.   }
  255.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  256.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  257.   if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  258.   {
  259.     Error_Handler();
  260.   }
  261.   /* USER CODE BEGIN TIM3_Init 2 */

  262.   /* USER CODE END TIM3_Init 2 */

  263. }

  264. /**
  265.   * @brief USART1 Initialization Function
  266.   * @param None
  267.   * @retval None
  268.   */
  269. static void MX_USART1_UART_Init(void)
  270. {

  271.   /* USER CODE BEGIN USART1_Init 0 */

  272.   /* USER CODE END USART1_Init 0 */

  273.   /* USER CODE BEGIN USART1_Init 1 */

  274.   /* USER CODE END USART1_Init 1 */
  275.   huart1.Instance = USART1;
  276.   huart1.Init.BaudRate = 9600;
  277.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  278.   huart1.Init.StopBits = UART_STOPBITS_1;
  279.   huart1.Init.Parity = UART_PARITY_NONE;
  280.   huart1.Init.Mode = UART_MODE_TX_RX;
  281.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  282.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  283.   if (HAL_UART_Init(&huart1) != HAL_OK)
  284.   {
  285.     Error_Handler();
  286.   }
  287.   /* USER CODE BEGIN USART1_Init 2 */

  288.   /* USER CODE END USART1_Init 2 */

  289. }

  290. /**
  291.   * @brief GPIO Initialization Function
  292.   * @param None
  293.   * @retval None
  294.   */
  295. static void MX_GPIO_Init(void)
  296. {
  297.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  298.   /* GPIO Ports Clock Enable */
  299.   __HAL_RCC_GPIOD_CLK_ENABLE();
  300.   __HAL_RCC_GPIOA_CLK_ENABLE();
  301.   __HAL_RCC_GPIOB_CLK_ENABLE();

  302.   /*Configure GPIO pin Output Level */
  303.   HAL_GPIO_WritePin(GPIOA, CS_Pin|RES_Pin|DC_Pin|D0_Pin
  304.                           |D1_Pin, GPIO_PIN_SET);

  305.   /*Configure GPIO pins : CS_Pin RES_Pin DC_Pin D0_Pin
  306.                            D1_Pin */
  307.   GPIO_InitStruct.Pin = CS_Pin|RES_Pin|DC_Pin|D0_Pin
  308.                           |D1_Pin;
  309.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  310.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  311.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  312.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  313. }

  314. /* USER CODE BEGIN 4 */

  315. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  316. {
  317.   if(htim==(&htim2)){
  318.         HAL_UART_Transmit(&huart1,tx_buf,7,10);
  319.         }
  320.         /*
  321.          if(htim==(&htim3)){
  322.            OLED_ShowNum(105,6,PM_VAL,3,12);
  323.                  OLED_ShowNum(25,2,CO2_VAL,3,12);
  324.                  OLED_ShowNum(89,2,TEM_VAL_H,2,12);
  325.                  OLED_ShowNum(113,2,TEM_VAL_L,2,12);
  326.                  OLED_ShowNum(25,4, HIM_VAL_H,2,12);
  327.                  OLED_ShowNum(49,4, HIM_VAL_L,2,12);
  328.                  OLED_ShowNum(97,4, HCHO_VAL_H,2,12);
  329.                  OLED_ShowNum(113,4, HCHO_VAL_L,2,12);
  330.                  OLED_ShowNum(33,6,TVOC_VAL_H,2,12);
  331.                  OLED_ShowNum(57,6,TVOC_VAL_L,2,12);
  332.         }
  333.         */
  334. }



  335. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  336. {
  337.         UNUSED(huart);
  338.         uint16_t t;
  339.         uint16_t  WAIT_C=0;
  340.         uint16_t  CH;
  341.         if(aRxBuffer[0]==0x42&&aRxBuffer[1]==0x4d)
  342.         {
  343.            for(t=0;t<22;t++)
  344.      {
  345.                   WAIT_C+=aRxBuffer[t];
  346.                  }        
  347.      CH=(aRxBuffer[22]<<8)+aRxBuffer[23];         
  348.                       if(WAIT_C==CH)
  349.                             {
  350.                                          PM_VAL=((aRxBuffer[4])<<8)+aRxBuffer[5];
  351.                                          TVOC_VAL_H=((aRxBuffer[6]<<8)+aRxBuffer[7])/100;
  352.                                          TVOC_VAL_L=((aRxBuffer[6]<<8)+aRxBuffer[7])%100;
  353.                                          HCHO_VAL_H=((aRxBuffer[9]<<8)+aRxBuffer[10])/100;
  354.                                          HCHO_VAL_L=((aRxBuffer[9]<<8)+aRxBuffer[10])%100;
  355.                                          CO2_VAL=(aRxBuffer[12]<<8)+aRxBuffer[13];
  356.                                          TEM_VAL_H=((aRxBuffer[14]<<8)+aRxBuffer[15])/10;
  357.                                          TEM_VAL_L=((aRxBuffer[14]<<8)+aRxBuffer[15])%10;
  358.                                          HIM_VAL_H=((aRxBuffer[16]<<8)+aRxBuffer[17])/10;
  359.                                          HIM_VAL_L=((aRxBuffer[16]<<8)+aRxBuffer[17])%10;
  360.                                          CNT=0;
  361.                        }        
  362.                                         else CNT=0;
  363.         }
  364.         else CNT=0;
  365. }

  366. /* USER CODE END 4 */

  367. /**
  368.   * @brief  This function is executed in case of error occurrence.
  369.   * @retval None
  370.   */
  371. void Error_Handler(void)
  372. {
  373.   /* USER CODE BEGIN Error_Handler_Debug */
  374.   /* User can add his own implementation to report the HAL error return state */

  375.   /* USER CODE END Error_Handler_Debug */
  376. }

  377. #ifdef  USE_FULL_ASSERT
  378. /**
  379.   * @brief  Reports the name of the source file and the source line number
  380.   *         where the assert_param error has occurred.
  381.   * @param  file: pointer to the source file name
  382.   * @param  line: assert_param error line source number
  383.   * @retval None
  384.   */
  385. void assert_failed(uint8_t *file, uint32_t line)
  386. {
  387.   /* USER CODE BEGIN 6 */
  388.   /* User can add his own implementation to report the file name and line number,
  389.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  390.   /* USER CODE END 6 */
  391. }
  392. #endif /* USE_FULL_ASSERT */

  393. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表