Guest User

Untitled

a guest
Nov 28th, 2019
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.98 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   **************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   **************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   **************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "main.h"
  27. #include "stdio.h"
  28. #include "string.h"
  29. /* USER CODE END Includes */
  30.  
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33.  
  34. /* USER CODE END PTD */
  35.  
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */
  39.  
  40. /* Private macro -------------------------------------------------------------*/
  41. /* USER CODE BEGIN PM */
  42.  
  43. /* USER CODE END PM */
  44.  
  45. /* Private variables ---------------------------------------------------------*/
  46. UART_HandleTypeDef huart1;
  47.  
  48. /* USER CODE BEGIN PV */
  49. uint8_t Rh_byte1, Rh_byte2, Temp_byte1, Temp_byte2,sum;
  50. uint16_t  RH, TEMP, a;
  51. char str[50]="";
  52. /* USER CODE END PV */
  53.  
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. static void MX_GPIO_Init(void);
  57. static void MX_USART1_UART_Init(void);
  58. /* USER CODE BEGIN PFP */
  59.  
  60. /* USER CODE END PFP */
  61.  
  62. /* Private user code ---------------------------------------------------------*/
  63. /* USER CODE BEGIN 0 */
  64. void delay_ticks(uint32_t ticks)
  65.  { //SYSTICK TIMER 24 BIT
  66.      SysTick->LOAD = ticks;
  67.      SysTick->VAL = 0;
  68.      SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
  69.      SysTick->CTRL &= ~SYSTICK_CLKSOURCE_HCLK_DIV8;
  70.      SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
  71.      // COUNTFLAG is a bit that is set to 1 when counter reaches 0.
  72.      // It's automatically cleared when read.
  73.      while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0);
  74.      SysTick->CTRL = 0;
  75.  }
  76. //238 766
  77.  //delaylere fazla guvenme
  78.  void delay_us(uint32_t us)
  79.  {
  80.      delay_ticks(us * 8);
  81.  }
  82.  
  83.  static inline void delay_ms(uint32_t ms)
  84.   {
  85.       delay_ticks(ms * 8000);
  86.   }
  87.  
  88.  GPIO_InitTypeDef GPIO_InitStruct = {0};
  89.  void set_gpio_input (void)
  90.   {
  91.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  92.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  93.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  94.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  95.   }
  96.  
  97.   /*Configure GPIO pin : PA2 */
  98.   void set_gpio_output (void)
  99.   {
  100.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  101.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  102.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  103.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  104.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  105.   }
  106.  
  107.  
  108. void DHT22_start (void)
  109. {
  110.     set_gpio_output ();  // set the pin as output
  111.     HAL_GPIO_WritePin (GPIOA, GPIO_PIN_5, 0);   // pull the pin low
  112.     delay_us(500);   // wait for 500us
  113.     HAL_GPIO_WritePin (GPIOA, GPIO_PIN_5, 1);   // pull the pin high
  114.     delay_us(30);   // wait for 30us
  115.     set_gpio_input ();   // set as input
  116. }
  117.  
  118. void check_response (void)
  119. {
  120.      delay_us(40);
  121.     if (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_5)))
  122.     {
  123.         delay_us (80);
  124.     }
  125.     while ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_5)));   // wait for the pin to go low
  126. }
  127.  
  128. uint8_t read_data (void)
  129. {
  130.     uint8_t i,j;
  131.     for (j=0;j<8;j++)
  132.     {
  133.         while (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_5)));   // wait for the pin to go high
  134.         delay_us(40);   // wait for 40 us
  135.         if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_5)) == 0)   // if the pin is low
  136.         {
  137.             i&= ~(1<<(7-j));   // write 0
  138.         }
  139.         else i|= (1<<(7-j));  // if the pin is high, write 1
  140.  
  141.  
  142.         while (HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_5));
  143.  
  144.     }
  145.     return i;
  146. }
  147.  
  148.  
  149. /* USER CODE END 0 */
  150.  
  151. /**
  152.   * @brief  The application entry point.
  153.   * @retval int
  154.   */
  155. int main(void)
  156. {
  157.   /* USER CODE BEGIN 1 */
  158.  
  159.   /* USER CODE END 1 */
  160.  
  161.  
  162.   /* MCU Configuration--------------------------------------------------------*/
  163.  
  164.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  165.   HAL_Init();
  166.  
  167.   /* USER CODE BEGIN Init */
  168.  
  169.   /* USER CODE END Init */
  170.  
  171.   /* Configure the system clock */
  172.   SystemClock_Config();
  173.  
  174.   /* USER CODE BEGIN SysInit */
  175.  
  176.   /* USER CODE END SysInit */
  177.  
  178.   /* Initialize all configured peripherals */
  179.   MX_GPIO_Init();
  180.   MX_USART1_UART_Init();
  181.   /* USER CODE BEGIN 2 */
  182.  
  183.   /* USER CODE END 2 */
  184.  
  185.   /* Infinite loop */
  186.   /* USER CODE BEGIN WHILE */
  187.   while (1)
  188.   {
  189.     /* USER CODE END WHILE */
  190.  
  191.     /* USER CODE BEGIN 3 */
  192.             DHT22_start ();
  193.             check_response ();
  194.             Rh_byte1 = read_data ();
  195.             Rh_byte2 = read_data ();
  196.             Temp_byte1 = read_data ();
  197.             Temp_byte2 = read_data ();
  198.        
  199.             //sum = read_data();
  200.  
  201.             TEMP = ((Temp_byte1<<8)|Temp_byte2);
  202.             RH = ((Rh_byte1<<8)|Rh_byte2);
  203.  
  204.              //a=(uint8_t)(Rh_byte1+Rh_byte2+Temp_byte1+Temp_byte2);
  205.              //HAL_UART_Transmit(&huart1,(uint8_t*)"resul\r\n",sizeof("resul\r\n")-1,100);
  206. //    ////
  207. //              if ((sum) == a)
  208. //          {
  209. //              TEMP = ((Temp_byte1<<8)|Temp_byte2);
  210. //              RH = ((Rh_byte1<<8)|Rh_byte2);
  211. //
  212. //          }
  213.                 delay_ms(2000);
  214.               //  HAL_UART_Transmit(&huart1, (uint8_t *)str,strlen(str), 500);
  215.                 sprintf(str,"\f sicaklik = %d.%d  nem=%d.%d \n\r",TEMP/10,TEMP%10,RH/10,RH%10);
  216.  
  217.                                  HAL_UART_Transmit(&huart1, (uint8_t *)str,strlen(str), 500);
  218.                                              HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);
  219.   }
  220.   /* USER CODE END 3 */
  221. }
  222.  
  223. /**
  224.   * @brief System Clock Configuration
  225.   * @retval None
  226.   */
  227. void SystemClock_Config(void)
  228. {
  229.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  230.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  231.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  232.  
  233.   /** Initializes the CPU, AHB and APB busses clocks
  234.   */
  235.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  236.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  237.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  238.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  239.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  240.   {
  241.     Error_Handler();
  242.   }
  243.   /** Initializes the CPU, AHB and APB busses clocks
  244.   */
  245.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  246.                               |RCC_CLOCKTYPE_PCLK1;
  247.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  248.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  249.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  250.  
  251.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  252.   {
  253.     Error_Handler();
  254.   }
  255.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  256.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  257.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  258.   {
  259.     Error_Handler();
  260.   }
  261. }
  262.  
  263. /**
  264.   * @brief USART1 Initialization Function
  265.   * @param None
  266.   * @retval None
  267.   */
  268. static void MX_USART1_UART_Init(void)
  269. {
  270.  
  271.   /* USER CODE BEGIN USART1_Init 0 */
  272.  
  273.   /* USER CODE END USART1_Init 0 */
  274.  
  275.   /* USER CODE BEGIN USART1_Init 1 */
  276.  
  277.   /* USER CODE END USART1_Init 1 */
  278.   huart1.Instance = USART1;
  279.   huart1.Init.BaudRate = 38400;
  280.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  281.   huart1.Init.StopBits = UART_STOPBITS_1;
  282.   huart1.Init.Parity = UART_PARITY_NONE;
  283.   huart1.Init.Mode = UART_MODE_TX_RX;
  284.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  285.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  286.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  287.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  288.   if (HAL_UART_Init(&huart1) != HAL_OK)
  289.   {
  290.     Error_Handler();
  291.   }
  292.   /* USER CODE BEGIN USART1_Init 2 */
  293.  
  294.   /* USER CODE END USART1_Init 2 */
  295.  
  296. }
  297.  
  298. /**
  299.   * @brief GPIO Initialization Function
  300.   * @param None
  301.   * @retval None
  302.   */
  303. static void MX_GPIO_Init(void)
  304. {
  305.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  306.  
  307.   /* GPIO Ports Clock Enable */
  308.   __HAL_RCC_GPIOF_CLK_ENABLE();
  309.   __HAL_RCC_GPIOA_CLK_ENABLE();
  310.   __HAL_RCC_GPIOB_CLK_ENABLE();
  311.  
  312.   /*Configure GPIO pin Output Level */
  313.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_4, GPIO_PIN_RESET);
  314.  
  315.   /*Configure GPIO pin Output Level */
  316.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
  317.  
  318.   /*Configure GPIO pins : PA5 PA7 */
  319.   GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_4;
  320.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  321.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  322.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  323.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  324.  
  325.   /*Configure GPIO pin : PB1 */
  326.   GPIO_InitStruct.Pin = GPIO_PIN_1;
  327.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  328.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  329.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  330.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  331.  
  332. }
  333.  
  334. /* USER CODE BEGIN 4 */
  335.  
  336. /* USER CODE END 4 */
  337.  
  338. /**
  339.   * @brief  This function is executed in case of error occurrence.
  340.   * @retval None
  341.   */
  342. void Error_Handler(void)
  343. {
  344.   /* USER CODE BEGIN Error_Handler_Debug */
  345.   /* User can add his own implementation to report the HAL error return state */
  346.  
  347.   /* USER CODE END Error_Handler_Debug */
  348. }
  349.  
  350. #ifdef  USE_FULL_ASSERT
  351. /**
  352.   * @brief  Reports the name of the source file and the source line number
  353.   *         where the assert_param error has occurred.
  354.   * @param  file: pointer to the source file name
  355.   * @param  line: assert_param error line source number
  356.   * @retval None
  357.   */
  358. void assert_failed(char *file, uint32_t line)
  359. {
  360.   /* USER CODE BEGIN 6 */
  361.   /* User can add his own implementation to report the file name and line number,
  362.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  363.   /* USER CODE END 6 */
  364. }
  365. #endif /* USE_FULL_ASSERT */
  366.  
  367. /******** (C) COPYRIGHT STMicroelectronics **END OF FILE*/
Advertisement
Add Comment
Please, Sign In to add comment