Advertisement
pabblo097

main.c_V2

Dec 9th, 2020 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.39 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) 2020 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. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "string.h"
  26. #include "stdlib.h"
  27. #include "stdio.h"
  28. #include "math.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.  
  47. /* USER CODE BEGIN PV */
  48. struct Napoj
  49. {
  50.     int id;
  51.     float kwota;
  52.     int ilosc;
  53. };
  54. /* USER CODE END PV */
  55.  
  56. /* Private function prototypes -----------------------------------------------*/
  57. void SystemClock_Config(void);
  58. static void MX_GPIO_Init(void);
  59.  
  60. int concat(int a, int b, int i)
  61. {
  62.  
  63.         if(i == 1)
  64.         {
  65.             char s1[20];
  66.             char s2[20];
  67.        
  68.             // Convert both the integers to string
  69.             sprintf(s1, "%d", a);
  70.             sprintf(s2, "%d", b);
  71.        
  72.             // Concatenate both strings
  73.             strcat(s1, s2);
  74.        
  75.             // Convert the concatenated string
  76.             // to integer
  77.             int c = atoi(s1);
  78.        
  79.             // return the formed integer
  80.             return c;
  81.             }
  82.             else if(i == 0)
  83.         {
  84.             return b;
  85.         }
  86. }
  87.  
  88. float float_rand( float min, float max )
  89. {
  90.     float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
  91.     return min + scale * ( max - min );      /* [min, max] */
  92. }
  93.  
  94. int random_int(int min, int max)
  95. {
  96.    return min + rand() % (max+1 - min);
  97. }
  98.  
  99. float myRound(float wartosc, int dokladnosc)
  100. {
  101.     float tmp = wartosc * pow(10, dokladnosc);
  102.     tmp = floor(tmp);
  103.     tmp /= pow(10, dokladnosc);
  104.     return tmp;
  105. }
  106.  
  107. void wczytajDane(struct Napoj napoje[], int tabSize)
  108. {
  109.     for(int i=0; i<tabSize; i++)
  110.     {
  111.         napoje[i].id = i;
  112.         napoje[i].kwota = myRound(float_rand(2.0, 4.0), 1);
  113.         napoje[i].ilosc = 5;
  114.     }
  115. }
  116.  
  117. /* USER CODE BEGIN PFP */
  118.  
  119. /* USER CODE END PFP */
  120.  
  121. /* Private user code ---------------------------------------------------------*/
  122. /* USER CODE BEGIN 0 */
  123.  
  124. /* USER CODE END 0 */
  125.  
  126. /**
  127.   * @brief  The application entry point.
  128.   * @retval int
  129.   */
  130. int main(void)
  131. {
  132.   /* USER CODE BEGIN 1 */
  133.  
  134.   /* USER CODE END 1 */
  135.  
  136.   /* MCU Configuration--------------------------------------------------------*/
  137.  
  138.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  139.   HAL_Init();
  140.  
  141.   /* USER CODE BEGIN Init */
  142.  
  143.   /* USER CODE END Init */
  144.  
  145.   /* Configure the system clock */
  146.   SystemClock_Config();
  147.  
  148.   /* USER CODE BEGIN SysInit */
  149.  
  150.   /* USER CODE END SysInit */
  151.  
  152.   /* Initialize all configured peripherals */
  153.   MX_GPIO_Init();
  154.   /* USER CODE BEGIN 2 */
  155.  
  156.   /* USER CODE END 2 */
  157.  
  158.   /* Infinite loop */
  159.   /* USER CODE BEGIN WHILE */
  160.     int nrNapoju = 0;
  161.     int i = 0;
  162.     float kwota = 0.0;
  163.     int tabSize = 20;
  164.     struct Napoj napoje[tabSize];
  165.     wczytajDane(napoje, tabSize);
  166.  
  167.   while (1)
  168.   {
  169.         //zablokowanie mozlkiwosci wrzucania monet
  170.         //moneta po wrzuceniu nie zostanie pobrana
  171.         HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET);
  172.        
  173.         //wprowadzenie nrNapoju
  174.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_0) == GPIO_PIN_SET)
  175.         {
  176.             nrNapoju = concat(nrNapoju, 0, i);
  177.             i++;
  178.             HAL_Delay(1000);
  179.         }
  180.        
  181.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_1) == GPIO_PIN_SET)
  182.         {
  183.             nrNapoju = concat(nrNapoju, 1, i);
  184.             i++;
  185.             HAL_Delay(1000);
  186.         }
  187.        
  188.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_2) == GPIO_PIN_SET)
  189.         {
  190.             nrNapoju = concat(nrNapoju, 2, i);
  191.             i++;
  192.             HAL_Delay(1000);
  193.         }
  194.        
  195.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_3) == GPIO_PIN_SET)
  196.         {
  197.             nrNapoju = concat(nrNapoju, 3, i);
  198.             i++;
  199.             HAL_Delay(1000);
  200.         }
  201.        
  202.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_4) == GPIO_PIN_SET)
  203.         {
  204.             nrNapoju = concat(nrNapoju, 4, i);
  205.             i++;
  206.             HAL_Delay(1000);
  207.         }
  208.        
  209.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_5) == GPIO_PIN_SET)
  210.         {
  211.             nrNapoju = concat(nrNapoju, 5, i);
  212.             i++;
  213.             HAL_Delay(1000);
  214.         }
  215.        
  216.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_6) == GPIO_PIN_SET)
  217.         {
  218.             nrNapoju = concat(nrNapoju, 6, i);
  219.             i++;
  220.             HAL_Delay(1000);
  221.         }
  222.        
  223.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_7) == GPIO_PIN_SET)
  224.         {
  225.             nrNapoju = concat(nrNapoju, 7, i);
  226.             i++;
  227.             HAL_Delay(1000);
  228.         }
  229.        
  230.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_8) == GPIO_PIN_SET)
  231.         {
  232.             nrNapoju = concat(nrNapoju, 8, i);
  233.             i++;
  234.             HAL_Delay(1000);
  235.         }
  236.        
  237.         if (HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_9) == GPIO_PIN_SET)
  238.         {
  239.             nrNapoju = concat(nrNapoju, 9, i);
  240.             i++;
  241.             HAL_Delay(1000);
  242.         }
  243.        
  244.         //czy wprowadzono wartosc 2 cyfrowa
  245.         if(i == 2)
  246.         {
  247.             //czy wybrany napoj jest dostepny i nr napoju < rozmiaru tablicy
  248.             if(napoje[nrNapoju-1].ilosc > 0 && (nrNapoju-1) < tabSize)
  249.             {
  250.                 printf("id= %u\n", napoje[nrNapoju-1].id);
  251.                 printf("kwota= %f\n", napoje[nrNapoju-1].kwota);
  252.                 printf("ilosc= %u\n", napoje[nrNapoju-1].ilosc);
  253.                 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET);
  254.             }
  255.             else
  256.             {
  257.                 nrNapoju = 0;
  258.                 i = 0;
  259.             }
  260.         }
  261.        
  262.  
  263.   }
  264.   /* USER CODE END 3 */
  265. }
  266.  
  267. /**
  268.   * @brief System Clock Configuration
  269.   * @retval None
  270.   */
  271. void SystemClock_Config(void)
  272. {
  273.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  274.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  275.  
  276.   /** Initializes the RCC Oscillators according to the specified parameters
  277.   * in the RCC_OscInitTypeDef structure.
  278.   */
  279.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  280.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  281.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  282.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  283.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  284.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  285.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  286.   {
  287.     Error_Handler();
  288.   }
  289.   /** Initializes the CPU, AHB and APB buses clocks
  290.   */
  291.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  292.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  293.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  294.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  295.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  296.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  297.  
  298.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  299.   {
  300.     Error_Handler();
  301.   }
  302. }
  303.  
  304. /**
  305.   * @brief GPIO Initialization Function
  306.   * @param None
  307.   * @retval None
  308.   */
  309. static void MX_GPIO_Init(void)
  310. {
  311.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  312.  
  313.   /* GPIO Ports Clock Enable */
  314.   __HAL_RCC_GPIOF_CLK_ENABLE();
  315.   __HAL_RCC_GPIOC_CLK_ENABLE();
  316.  
  317.   /*Configure GPIO pin Output Level */
  318.   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET);
  319.  
  320.   /*Configure GPIO pins : PF0 PF1 PF2 PF3
  321.                            PF4 PF5 PF6 PF7
  322.                            PF8 PF9 PF10 */
  323.   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  324.                           |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  325.                           |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  326.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  327.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  328.   HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  329.  
  330.   /*Configure GPIO pins : PC1 PC2 PC3 */
  331.   GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
  332.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  333.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  334.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  335.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  336.  
  337. }
  338.  
  339. /* USER CODE BEGIN 4 */
  340.  
  341. /* USER CODE END 4 */
  342.  
  343. /**
  344.   * @brief  This function is executed in case of error occurrence.
  345.   * @retval None
  346.   */
  347. void Error_Handler(void)
  348. {
  349.   /* USER CODE BEGIN Error_Handler_Debug */
  350.   /* User can add his own implementation to report the HAL error return state */
  351.   __disable_irq();
  352.   while (1)
  353.   {
  354.   }
  355.   /* USER CODE END Error_Handler_Debug */
  356. }
  357.  
  358. #ifdef  USE_FULL_ASSERT
  359. /**
  360.   * @brief  Reports the name of the source file and the source line number
  361.   *         where the assert_param error has occurred.
  362.   * @param  file: pointer to the source file name
  363.   * @param  line: assert_param error line source number
  364.   * @retval None
  365.   */
  366. void assert_failed(uint8_t *file, uint32_t line)
  367. {
  368.   /* USER CODE BEGIN 6 */
  369.   /* User can add his own implementation to report the file name and line number,
  370.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  371.   /* USER CODE END 6 */
  372. }
  373. #endif /* USE_FULL_ASSERT */
  374.  
  375. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  376.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement