Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.14 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. #include "adc.h"
  24. #include "dma.h"
  25. #include "tim.h"
  26. #include "gpio.h"
  27.  
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30.  
  31. /* USER CODE END Includes */
  32.  
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */
  35.  
  36. /* USER CODE END PTD */
  37.  
  38. /* Private define ------------------------------------------------------------*/
  39. /* USER CODE BEGIN PD */
  40. /* USER CODE END PD */
  41.  
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44.  
  45. /* USER CODE END PM */
  46.  
  47. /* Private variables ---------------------------------------------------------*/
  48. uint8_t Received;
  49. /* USER CODE BEGIN PV */
  50. //ADC_HandleTypeDef hadc1;
  51. volatile uint16_t num = 5;
  52. volatile uint16_t disp = 0;
  53. volatile uint16_t command = 0;
  54. volatile uint8_t pause = 0;
  55. volatile uint8_t start = 0;
  56. /* USER CODE END PV */
  57.  
  58. /* Private function prototypes -----------------------------------------------*/
  59. void SystemClock_Config(void);
  60. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
  61. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);
  62. void newNum(uint16_t number, uint16_t display);
  63. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
  64. /* USER CODE BEGIN PFP */
  65.  
  66. /* USER CODE END PFP */
  67.  
  68. /* Private user code ---------------------------------------------------------*/
  69. /* USER CODE BEGIN 0 */
  70.  
  71. /* USER CODE END 0 */
  72.  
  73. /**
  74.   * @brief  The application entry point.
  75.   * @retval int
  76.   */
  77. int main(void)
  78. {
  79.   /* USER CODE BEGIN 1 */
  80.  
  81.   /* USER CODE END 1 */
  82.  
  83.  
  84.   /* MCU Configuration--------------------------------------------------------*/
  85.  
  86.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  87.   HAL_Init();
  88.  
  89.   /* USER CODE BEGIN Init */
  90.  
  91.   /* USER CODE END Init */
  92.  
  93.   /* Configure the system clock */
  94.   SystemClock_Config();
  95.  
  96.   /* USER CODE BEGIN SysInit */
  97.  
  98.   /* USER CODE END SysInit */
  99.  
  100.   /* Initialize all configured peripherals */
  101.   MX_GPIO_Init();
  102.   MX_DMA_Init();
  103. MX_TIM2_Init();
  104.   MX_TIM4_Init();
  105.   /* USER CODE BEGIN 2 */
  106.   HAL_TIM_Base_Start_IT(&htim2);
  107.     HAL_TIM_Base_Start_IT(&htim4);
  108.   HAL_UART_Received_IT(&huart1, &Received, 1);
  109.   /* USER CODE END 2 */
  110.  
  111.   /* Infinite loop */
  112.   /* USER CODE BEGIN WHILE */
  113.   while (1)
  114.   {
  115.          newNum(PomiarADC, disp);
  116.     /* USER CODE END WHILE */
  117.  
  118.     /* USER CODE BEGIN 3 */
  119.   }
  120.   /* USER CODE END 3 */
  121. }
  122.  
  123. /**
  124.   * @brief System Clock Configuration
  125.   * @retval None
  126.   */
  127. void SystemClock_Config(void)
  128. {
  129.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  130.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  131.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  132.  
  133.   /** Initializes the CPU, AHB and APB busses clocks
  134.   */
  135.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  136.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  137.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV4;
  138.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  139.   RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
  140.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  141.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  142.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL5;
  143.   RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
  144.   RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
  145.   RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
  146.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  147.   {
  148.     Error_Handler();
  149.   }
  150.   /** Initializes the CPU, AHB and APB busses clocks
  151.   */
  152.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  153.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  154.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  155.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  156.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  157.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  158.  
  159.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  160.   {
  161.     Error_Handler();
  162.   }
  163.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  164.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  165.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  166.   {
  167.     Error_Handler();
  168.   }
  169.   /** Configure the Systick interrupt time
  170.   */
  171.   __HAL_RCC_PLLI2S_ENABLE();
  172. }
  173.  
  174. /* USER CODE BEGIN 4 */
  175. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
  176.     if(GPIO_Pin == Pause_Pin && pause == 0){
  177.         pause = 1;
  178.  
  179.     }
  180.     else{
  181.         pause = 0;
  182.  
  183.     }
  184.     if(GPIO_Pin == Start_Pin && start == 0){
  185.         start = 1;
  186.  
  187.     }
  188.     else{
  189.         start = 0;
  190.     }
  191. }
  192. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
  193.   if(htim->Instance == TIM2 && pause != 1){
  194.         num == 999 ? num = 0 : num++;
  195.     }
  196.     if(htim->Instance == TIM4){
  197.             HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
  198.         //HAL_GPIO_TogglePin(green_GPIO_Port,green_Pin);
  199.         if(disp == 0){
  200.             disp = 1;
  201.                   //GPIOC->ODR = 0xF7;
  202.                             HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_RESET);  
  203.                                     HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_SET);  
  204.                                     HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_SET);  
  205.                                     HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_SET);
  206.         }
  207.         else if(disp == 1){
  208.             disp = 2;
  209.                   //GPIOC->ODR = 0xFB;
  210.                             HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_SET);  
  211.                                     HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_RESET);  
  212.                                     HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_SET);  
  213.                                     HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_SET);
  214.         }
  215.                 else if(disp == 2){
  216.             disp = 3;
  217.                   //GPIOC->ODR = 0xDF;
  218.                                     HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_SET);  
  219.                                     HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_SET);  
  220.                                     HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_RESET);  
  221.                                     HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_SET);
  222.                         HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_SET);                  
  223.         }
  224.                 else if(disp == 3){
  225.             disp = 0;
  226.                         //GPIOC->ODR = 0x7F;
  227.                         HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_SET);  
  228.                                                 HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_SET);  
  229.                                     HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_SET);  
  230.                                     HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_RESET);
  231. HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_RESET);                                     
  232.         }
  233.                
  234.     }
  235. }
  236. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  237.  
  238.  uint8_t Data[50];
  239.  uint16_t size = 0;
  240.  
  241.  switch (atoi(&Received)) {
  242.  
  243.  case 's':
  244.  size = sprintf(Data, "START/STOP\n\r");
  245.  if(command == 0 || command == 2){command = 1;}
  246.  else{command = 0;}
  247.  break;
  248.  
  249.  case 'p':
  250.  if(command == 1 || command = 0){command = 2;}
  251.  else{command = 2;)}
  252.  size = sprintf(Data, "PAUSE\n\r");
  253.  break;
  254.  
  255.  default:
  256.  size = sprintf(Data, "Odebrano nieznany znak: %c\n\r", Received);
  257.  break;
  258.  }
  259.  
  260.  HAL_UART_Transmit_IT(&huart1, Data, size);
  261.  HAL_UART_Receive_IT(&huart1, &Received, 1);
  262. }
  263. void newNum(uint16_t number, uint16_t display)
  264. {  
  265.     if(HAL_GPIO_ReadPin(DIG4_GPIO_Port,DIG4_Pin)==GPIO_PIN_RESET){
  266.         number = number%10;
  267.     }
  268.         else if(HAL_GPIO_ReadPin(DIG3_GPIO_Port,DIG3_Pin)==GPIO_PIN_RESET){
  269.         number = (number/10)%10;
  270.  
  271.     }
  272.     else if(HAL_GPIO_ReadPin(DIG2_GPIO_Port,DIG2_Pin)==GPIO_PIN_RESET){
  273.                 number = (number/100)%10;
  274.     }
  275.         else if(HAL_GPIO_ReadPin(DIG1_GPIO_Port,DIG1_Pin)==GPIO_PIN_RESET){
  276.         number = (number/1000)%10;
  277.     }
  278.  
  279.     switch(number){
  280.     case 0: GPIOE->ODR = 0xC0<<8; break;
  281.     case 1: GPIOE->ODR = 0xF9<<8; break;
  282.     case 2: GPIOE->ODR = 0xA4<<8; break;
  283.     case 3: GPIOE->ODR = 0xB0<<8; break;
  284.     case 4: GPIOE->ODR = 0x99<<8; break;
  285.     case 5: GPIOE->ODR = 0x92<<8; break;
  286.     case 6: GPIOE->ODR = 0x82<<8; break;
  287.     case 7: GPIOE->ODR = 0xF8<<8; break;
  288.     case 8: GPIOE->ODR = 0x80<<8; break;
  289.     case 9: GPIOE->ODR = 0x90<<8; break;
  290.     }
  291.         if(HAL_GPIO_ReadPin(DIG2_GPIO_Port,DIG2_Pin)==GPIO_PIN_RESET){
  292.                 HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_RESET);
  293.     }
  294.         else{
  295.             HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_SET);
  296.         }
  297. }
  298. /* USER CODE END 4 */
  299.  
  300. /**
  301.   * @brief  This function is executed in case of error occurrence.
  302.   * @retval None
  303.   */
  304. void Error_Handler(void)
  305. {
  306.   /* USER CODE BEGIN Error_Handler_Debug */
  307.   /* User can add his own implementation to report the HAL error return state */
  308.  
  309.   /* USER CODE END Error_Handler_Debug */
  310. }
  311.  
  312. #ifdef  USE_FULL_ASSERT
  313. /**
  314.   * @brief  Reports the name of the source file and the source line number
  315.   *         where the assert_param error has occurred.
  316.   * @param  file: pointer to the source file name
  317.   * @param  line: assert_param error line source number
  318.   * @retval None
  319.   */
  320. void assert_failed(uint8_t *file, uint32_t line)
  321. {
  322.   /* USER CODE BEGIN 6 */
  323.   /* User can add his own implementation to report the file name and line number,
  324.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  325.   /* USER CODE END 6 */
  326. }
  327. #endif /* USE_FULL_ASSERT */
  328.  
  329. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement