[text] contador binario stm32f7

Viewer

copydownloadembedprintName: contador binario stm32f7
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * Copyright (c) 2024 STMicroelectronics.
  10.   * All rights reserved.
  11.   *
  12.   * This software is licensed under terms that can be found in the LICENSE file
  13.   * in the root directory of this software component.
  14.   * If no LICENSE file comes with this software, it is provided AS-IS.
  15.   *
  16.   ******************************************************************************
  17.   */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "string.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25.  
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35.  
  36. /* USER CODE END PD */
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40.  
  41. /* USER CODE END PM */
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. #if defined ( _ICCARM_ ) /*!< IAR Compiler */
  45. #pragma location=0x2004c000
  46. ETH_DMADescTypeDef  DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  47. #pragma location=0x2004c0a0
  48. ETH_DMADescTypeDef  DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  49.  
  50. #elif defined ( __CC_ARM )  /* MDK ARM Compiler */
  51.  
  52. _attribute_((at(0x2004c000))) ETH_DMADescTypeDef  DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  53. _attribute_((at(0x2004c0a0))) ETH_DMADescTypeDef  DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  54.  
  55. #elif defined ( _GNUC_ ) /* GNU Compiler */
  56. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] _attribute_((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
  57. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] _attribute_((section(".TxDecripSection")));   /* Ethernet Tx DMA Descriptors */
  58.  
  59. #endif
  60.  
  61. ETH_TxPacketConfig TxConfig;
  62.  
  63. ETH_HandleTypeDef heth;
  64.  
  65. UART_HandleTypeDef huart3;
  66.  
  67. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  68.  
  69. /* USER CODE BEGIN PV */
  70.  
  71. /* USER CODE END PV */
  72.  
  73. /* Private function prototypes -----------------------------------------------*/
  74. void SystemClock_Config(void);
  75. static void MX_GPIO_Init(void);
  76. static void MX_ETH_Init(void);
  77. static void MX_USART3_UART_Init(void);
  78. static void MX_USB_OTG_FS_PCD_Init(void);
  79. /* USER CODE BEGIN PFP */
  80.  
  81. /* USER CODE END PFP */
  82.  
  83. /* Private user code ---------------------------------------------------------*/
  84. /* USER CODE BEGIN 0 */
  85.  
  86. /* USER CODE END 0 */
  87.  
  88. /**
  89.   * @brief  The application entry point.
  90.   * @retval int
  91.   */
  92. int main(void)
  93. {
  94.   /* USER CODE BEGIN 1 */
  95.  
  96.   /* USER CODE END 1 */
  97.  
  98.   /* MCU Configuration--------------------------------------------------------*/
  99.  
  100.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  101.   HAL_Init();
  102.  
  103.   /* USER CODE BEGIN Init */
  104.  
  105.   /* USER CODE END Init */
  106.  
  107.   /* Configure the system clock */
  108.   SystemClock_Config();
  109.  
  110.   /* USER CODE BEGIN SysInit */
  111.  
  112.   /* USER CODE END SysInit */
  113.  
  114.   /* Initialize all configured peripherals */
  115.   MX_GPIO_Init();
  116.   MX_ETH_Init();
  117.   MX_USART3_UART_Init();
  118.   MX_USB_OTG_FS_PCD_Init();
  119.   /* USER CODE BEGIN 2 */
  120.  
  121.   /* USER CODE END 2 */
  122.   int numled[6]={Leda_Pin, Ledb_Pin, Ledc_Pin, Ledd_Pin, Lede_Pin, Ledf_Pin};
  123.   int opc;
  124.    /* Infinite loop */
  125.    /* USER CODE BEGIN WHILE */
  126.  while (1)
  127.   {
  128.  /* USER CODE END WHILE */
  129.    while(HAL_GPIO_ReadPin(switch3_GPIO_Port, switch3_Pin)==GPIO_PIN_SET)
  130.         {
  131.         if (HAL_GPIO_ReadPin(switch1_GPIO_Port, switch1_Pin)==GPIO_PIN_SET){
  132.           opc=1;
  133.               }
  134.         else if(HAL_GPIO_ReadPin(switch1_GPIO_Port, switch1_Pin)==GPIO_PIN_SET && HAL_GPIO_ReadPin(switch2_GPIO_Port, switch2_Pin)==GPIO_PIN_SET){
  135.           opc=2;
  136.            }
  137.         switch(opc)
  138.            {
  139.            case 1:
  140.                while(opc==1){
  141.                 for(int x=0; x<=63; ++x)
  142.                 {
  143.                      for(int i=0; i<6;++i)
  144.                      {
  145.                              HAL_GPIO_WritePin(GPIOF, numled[i],x >> i & 1);
  146.                              HAL_Delay(150);
  147.                      }
  148.                 }
  149.                 opc=0;
  150.                }
  151.           break;
  152.           case 2:
  153.                   for(int x=63; x=0; --x)
  154.                   {
  155.                           for(int i=0; i<6;++i)
  156.                           {
  157.                                   HAL_GPIO_WritePin(GPIOF, numled[i],x >> i & 1);
  158.                                   HAL_Delay(150);
  159.                           }
  160.                           opc=0;
  161.               }
  162.  
  163.           break;
  164.  
  165.           default:
  166.             HAL_GPIO_TogglePin(GPIOF, Leda_Pin);
  167.             HAL_GPIO_TogglePin(GPIOF, Ledb_Pin);
  168.             HAL_GPIO_TogglePin(GPIOF, Ledc_Pin);
  169.             HAL_GPIO_TogglePin(GPIOF, Ledd_Pin);
  170.             HAL_GPIO_TogglePin(GPIOF, Lede_Pin);
  171.             HAL_GPIO_TogglePin(GPIOF, Ledf_Pin);
  172.             HAL_Delay(50);
  173.            }
  174.         }
  175. }
  176. }
  177.  
  178. /**
  179.   * @brief System Clock Configuration
  180.   * @retval None
  181.   */
  182. void SystemClock_Config(void)
  183. {
  184.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  185.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  186.  
  187.   /** Configure LSE Drive Capability
  188.   */
  189.   HAL_PWR_EnableBkUpAccess();
  190.  
  191.   /** Configure the main internal regulator output voltage
  192.   */
  193.   __HAL_RCC_PWR_CLK_ENABLE();
  194.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  195.  
  196.   /** Initializes the RCC Oscillators according to the specified parameters
  197.   * in the RCC_OscInitTypeDef structure.
  198.   */
  199.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  200.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  201.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  202.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  203.   RCC_OscInitStruct.PLL.PLLM = 4;
  204.   RCC_OscInitStruct.PLL.PLLN = 72;
  205.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  206.   RCC_OscInitStruct.PLL.PLLQ = 3;
  207.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  208.   {
  209.     Error_Handler();
  210.   }
  211.  
  212.   /** Initializes the CPU, AHB and APB buses clocks
  213.   */
  214.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  215.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  216.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  217.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  218.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  219.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  220.  
  221.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  222.   {
  223.     Error_Handler();
  224.   }
  225. }
  226.  
  227. /**
  228.   * @brief ETH Initialization Function
  229.   * @param None
  230.   * @retval None
  231.   */
  232. static void MX_ETH_Init(void)
  233. {
  234.  
  235.   /* USER CODE BEGIN ETH_Init 0 */
  236.  
  237.   /* USER CODE END ETH_Init 0 */
  238.  
  239.    static uint8_t MACAddr[6];
  240.  
  241.   /* USER CODE BEGIN ETH_Init 1 */
  242.  
  243.   /* USER CODE END ETH_Init 1 */
  244.   heth.Instance = ETH;
  245.   MACAddr[0] = 0x00;
  246.   MACAddr[1] = 0x80;
  247.   MACAddr[2] = 0xE1;
  248.   MACAddr[3] = 0x00;
  249.   MACAddr[4] = 0x00;
  250.   MACAddr[5] = 0x00;
  251.   heth.Init.MACAddr = &MACAddr[0];
  252.   heth.Init.MediaInterface = HAL_ETH_RMII_MODE;
  253.   heth.Init.TxDesc = DMATxDscrTab;
  254.   heth.Init.RxDesc = DMARxDscrTab;
  255.   heth.Init.RxBuffLen = 1524;
  256.  
  257.   /* USER CODE BEGIN MACADDRESS */
  258.  
  259.   /* USER CODE END MACADDRESS */
  260.  
  261.   if (HAL_ETH_Init(&heth) != HAL_OK)
  262.   {
  263.     Error_Handler();
  264.   }
  265.  
  266.   memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
  267.   TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
  268.   TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
  269.   TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
  270.   /* USER CODE BEGIN ETH_Init 2 */
  271.  
  272.   /* USER CODE END ETH_Init 2 */
  273.  
  274. }
  275.  
  276. /**
  277.   * @brief USART3 Initialization Function
  278.   * @param None
  279.   * @retval None
  280.   */
  281. static void MX_USART3_UART_Init(void)
  282. {
  283.  
  284.   /* USER CODE BEGIN USART3_Init 0 */
  285.  
  286.   /* USER CODE END USART3_Init 0 */
  287.  
  288.   /* USER CODE BEGIN USART3_Init 1 */
  289.  
  290.   /* USER CODE END USART3_Init 1 */
  291.   huart3.Instance = USART3;
  292.   huart3.Init.BaudRate = 115200;
  293.   huart3.Init.WordLength = UART_WORDLENGTH_8B;
  294.   huart3.Init.StopBits = UART_STOPBITS_1;
  295.   huart3.Init.Parity = UART_PARITY_NONE;
  296.   huart3.Init.Mode = UART_MODE_TX_RX;
  297.   huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  298.   huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  299.   huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  300.   huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  301.   if (HAL_UART_Init(&huart3) != HAL_OK)
  302.   {
  303.     Error_Handler();
  304.   }
  305.   /* USER CODE BEGIN USART3_Init 2 */
  306.  
  307.   /* USER CODE END USART3_Init 2 */
  308.  
  309. }
  310.  
  311. /**
  312.   * @brief USB_OTG_FS Initialization Function
  313.   * @param None
  314.   * @retval None
  315.   */
  316. static void MX_USB_OTG_FS_PCD_Init(void)
  317. {
  318.  
  319.   /* USER CODE BEGIN USB_OTG_FS_Init 0 */
  320.  
  321.   /* USER CODE END USB_OTG_FS_Init 0 */
  322.  
  323.   /* USER CODE BEGIN USB_OTG_FS_Init 1 */
  324.  
  325.   /* USER CODE END USB_OTG_FS_Init 1 */
  326.   hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  327.   hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  328.   hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  329.   hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  330.   hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  331.   hpcd_USB_OTG_FS.Init.Sof_enable = ENABLE;
  332.   hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  333.   hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  334.   hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;
  335.   hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  336.   if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  337.   {
  338.     Error_Handler();
  339.   }
  340.   /* USER CODE BEGIN USB_OTG_FS_Init 2 */
  341.  
  342.   /* USER CODE END USB_OTG_FS_Init 2 */
  343.  
  344. }
  345.  
  346. /**
  347.   * @brief GPIO Initialization Function
  348.   * @param None
  349.   * @retval None
  350.   */
  351. static void MX_GPIO_Init(void)
  352. {
  353.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  354. /* USER CODE BEGIN MX_GPIO_Init_1 */
  355. /* USER CODE END MX_GPIO_Init_1 */
  356.  
  357.   /* GPIO Ports Clock Enable */
  358.   __HAL_RCC_GPIOC_CLK_ENABLE();
  359.   __HAL_RCC_GPIOF_CLK_ENABLE();
  360.   __HAL_RCC_GPIOH_CLK_ENABLE();
  361.   __HAL_RCC_GPIOA_CLK_ENABLE();
  362.   __HAL_RCC_GPIOB_CLK_ENABLE();
  363.   __HAL_RCC_GPIOG_CLK_ENABLE();
  364.   __HAL_RCC_GPIOD_CLK_ENABLE();
  365.  
  366.   /*Configure GPIO pin Output Level */
  367.   HAL_GPIO_WritePin(GPIOF, Ledf_Pin|Lede_Pin|Ledd_Pin|Leda_Pin
  368.                           |Ledb_Pin|Ledc_Pin, GPIO_PIN_RESET);
  369.  
  370.   /*Configure GPIO pin Output Level */
  371.   HAL_GPIO_WritePin(GPIOB, LD1_Pin|LD3_Pin|LD2_Pin|switch1_Pin
  372.                           |switch2_Pin, GPIO_PIN_RESET);
  373.  
  374.   /*Configure GPIO pin Output Level */
  375.   HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);
  376.  
  377.   /*Configure GPIO pin : USER_Btn_Pin */
  378.   GPIO_InitStruct.Pin = USER_Btn_Pin;
  379.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  380.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  381.   HAL_GPIO_Init(USER_Btn_GPIO_Port, &GPIO_InitStruct);
  382.  
  383.   /*Configure GPIO pins : Ledf_Pin Lede_Pin Ledd_Pin Leda_Pin
  384.                            Ledb_Pin Ledc_Pin */
  385.   GPIO_InitStruct.Pin = Ledf_Pin|Lede_Pin|Ledd_Pin|Leda_Pin
  386.                           |Ledb_Pin|Ledc_Pin;
  387.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  388.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  389.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  390.   HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  391.  
  392.   /*Configure GPIO pins : LD1_Pin LD3_Pin LD2_Pin switch1_Pin
  393.                            switch2_Pin */
  394.   GPIO_InitStruct.Pin = LD1_Pin|LD3_Pin|LD2_Pin|switch1_Pin
  395.                           |switch2_Pin;
  396.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  397.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  398.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  399.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  400.  
  401.   /*Configure GPIO pins : switch3_Pin USB_OverCurrent_Pin */
  402.   GPIO_InitStruct.Pin = switch3_Pin|USB_OverCurrent_Pin;
  403.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  404.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  405.   HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  406.  
  407.   /*Configure GPIO pin : USB_PowerSwitchOn_Pin */
  408.   GPIO_InitStruct.Pin = USB_PowerSwitchOn_Pin;
  409.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  410.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  411.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  412.   HAL_GPIO_Init(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
  413.  
  414. /* USER CODE BEGIN MX_GPIO_Init_2 */
  415. /* USER CODE END MX_GPIO_Init_2 */
  416. }
  417.  
  418. /* USER CODE BEGIN 4 */
  419.  
  420. /* USER CODE END 4 */
  421.  
  422. /**
  423.   * @brief  This function is executed in case of error occurrence.
  424.   * @retval None
  425.   */
  426. void Error_Handler(void)
  427. {
  428.   /* USER CODE BEGIN Error_Handler_Debug */
  429.   /* User can add his own implementation to report the HAL error return state */
  430.   __disable_irq();
  431.   while (1)
  432.   {
  433.   }
  434.   /* USER CODE END Error_Handler_Debug */
  435. }
  436.  
  437. #ifdef  USE_FULL_ASSERT
  438. /**
  439.   * @brief  Reports the name of the source file and the source line number
  440.   *         where the assert_param error has occurred.
  441.   * @param  file: pointer to the source file name
  442.   * @param  line: assert_param error line source number
  443.   * @retval None
  444.   */
  445. void assert_failed(uint8_t *file, uint32_t line)
  446. {
  447.   /* USER CODE BEGIN 6 */
  448.   /* User can add his own implementation to report the file name and line number,
  449.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  450.   /* USER CODE END 6 */
  451. }
  452. #endif /* USE_FULL_ASSERT */

Editor

You can edit this paste and save as new:


File Description
  • contador binario stm32f7
  • Paste Code
  • 23 Apr-2024
  • 12.88 Kb
You can Share it: