[应用相关] STM32F401GPIO翻转

[复制链接]
1433|10
 楼主| 643757107 发表于 2017-12-24 12:31 | 显示全部楼层 |阅读模式
本帖最后由 643757107 于 2017-12-24 13:50 编辑

  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    GPIO/GPIO_IOToggle/Src/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.2.5
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    06-May-2016
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   This example describes how to configure and use GPIOs through
  8.   *          the STM32F4xx HAL API.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */

  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"

  40. /** @addtogroup STM32F4xx_HAL_Examples
  41.   * @{
  42.   */

  43. /** @addtogroup GPIO_IOToggle
  44.   * @{
  45.   */

  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. static GPIO_InitTypeDef  GPIO_InitStruct;

  51. /* Private function prototypes -----------------------------------------------*/
  52. static void SystemClock_Config(void);
  53. static void Error_Handler(void);

  54. /* Private functions ---------------------------------------------------------*/

  55. /**
  56.   * @brief  Main program
  57.   * @param  None
  58.   * @retval None
  59.   */
  60. int main(void)
  61. {
  62. /* This sample code shows how to use STM32F4xx GPIO HAL API to toggle PA05 IOs
  63.     connected to LED2 on STM32F4xx-Nucleo board  
  64.     in an infinite loop.
  65.     To proceed, 3 steps are required: */

  66.   /* STM32F4xx HAL library initialization:
  67.        - Configure the Flash prefetch, instruction and Data caches
  68.        - Configure the Systick to generate an interrupt each 1 msec
  69.        - Set NVIC Group Priority to 4
  70.        - Global MSP (MCU Support Package) initialization
  71.      */
  72.   HAL_Init();
  73.   
  74.   /* Configure the system clock to 84 MHz */
  75.   SystemClock_Config();
  76.   
  77.   /* -1- Enable GPIOA Clock (to be able to program the configuration registers) */
  78.   __HAL_RCC_GPIOA_CLK_ENABLE();
  79.   
  80.   /* -2- Configure PA05 IO in output push-pull mode to
  81.          drive external LED */
  82.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  83.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  84.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  85.   GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  86.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  87.   /* -3- Toggle PA05 IO in an infinite loop */  
  88.   while (1)
  89.   {
  90.     HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  91.    
  92.     /* Insert delay 100 ms */
  93.     HAL_Delay(100);
  94.   }
  95. }

  96. /**
  97.   * @brief  System Clock Configuration
  98.   *         The system Clock is configured as follow :
  99.   *            System Clock source            = PLL (HSI)
  100.   *            SYSCLK(Hz)                     = 84000000
  101.   *            HCLK(Hz)                       = 84000000
  102.   *            AHB Prescaler                  = 1
  103.   *            APB1 Prescaler                 = 2
  104.   *            APB2 Prescaler                 = 1
  105.   *            HSI Frequency(Hz)              = 16000000
  106.   *            PLL_M                          = 16
  107.   *            PLL_N                          = 336
  108.   *            PLL_P                          = 4
  109.   *            PLL_Q                          = 7
  110.   *            VDD(V)                         = 3.3
  111.   *            Main regulator output voltage  = Scale2 mode
  112.   *            Flash Latency(WS)              = 2
  113.   * @param  None
  114.   * @retval None
  115.   */
  116. static void SystemClock_Config(void)
  117. {
  118.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  119.   RCC_OscInitTypeDef RCC_OscInitStruct;

  120.   /* Enable Power Control clock */
  121.   __HAL_RCC_PWR_CLK_ENABLE();
  122.   
  123.   /* The voltage scaling allows optimizing the power consumption when the device is
  124.      clocked below the maximum system frequency, to update the voltage scaling value
  125.      regarding system frequency refer to product datasheet.  */
  126.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  127.   
  128.   /* Enable HSI Oscillator and activate PLL with HSI as source */
  129.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  130.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  131.   RCC_OscInitStruct.HSICalibrationValue = 0x10;
  132.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  133.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  134.   RCC_OscInitStruct.PLL.PLLM = 16;
  135.   RCC_OscInitStruct.PLL.PLLN = 336;
  136.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  137.   RCC_OscInitStruct.PLL.PLLQ = 7;
  138.   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  139.   {
  140.     Error_Handler();
  141.   }
  142.   
  143.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  144.      clocks dividers */
  145.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  146.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  147.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  148.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;  
  149.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
  150.   if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  151.   {
  152.     Error_Handler();
  153.   }
  154. }

  155. /**
  156.   * @brief  This function is executed in case of error occurrence.
  157.   * @param  None
  158.   * @retval None
  159.   */
  160. static void Error_Handler(void)
  161. {
  162.   while(1)
  163.   {
  164.   }
  165. }

  166. #ifdef  USE_FULL_ASSERT
  167. /**
  168.   * @brief  Reports the name of the source file and the source line number
  169.   *         where the assert_param error has occurred.
  170.   * @param  file: pointer to the source file name
  171.   * @param  line: assert_param error line source number
  172.   * @retval None
  173.   */
  174. void assert_failed(uint8_t* file, uint32_t line)
  175. {
  176.   /* User can add his own implementation to report the file name and line number,
  177.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  178.   /* Infinite loop */
  179.   while (1)
  180.   {
  181.   }
  182. }
  183. #endif

  184. /**
  185.   * @}
  186.   */

  187. /**
  188.   * @}
  189.   */

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

472715174931162.jpg
472715184964987.jpg
xuanhuanzi 发表于 2017-12-24 13:51 | 显示全部楼层
虽然就是一个简单的翻转,但是前面的初始化配置时钟什么的,这就是比51难的地方。
antusheng 发表于 2017-12-24 14:34 | 显示全部楼层
的确,系统时钟才是根本,这个搞懂了,其他的就容易了。
dongliushui 发表于 2017-12-24 15:05 | 显示全部楼层
这个HAL划分的细致,所以看着这样,用到的东西多了,就觉得好用了。
xixi2017 发表于 2017-12-24 15:18 | 显示全部楼层
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
直接用翻转函数搞,免得自己再思考怎么位计算。
xinpian101 发表于 2017-12-24 15:29 | 显示全部楼层
PWR那个是怎么玩的
antusheng 发表于 2017-12-24 15:35 | 显示全部楼层
时钟部分好多是直接用例子里就行了,再配合cubeF开发工具图像配置。
xinxianshi 发表于 2017-12-24 21:21 | 显示全部楼层
在相同的标准下的库函数内操作,你就是换别的芯片也是这个代码,这就是兼容性。
huangcunxiake 发表于 2017-12-24 22:26 | 显示全部楼层
看懂库函数后面的玄机,才叫学问。
yiyigirl2014 发表于 2017-12-25 10:48 | 显示全部楼层
这用库函数实现翻转,是不是要比使用寄存器直接操作慢?
zhuotuzi 发表于 2017-12-25 14:04 | 显示全部楼层
有没有人测试过这种翻转速度。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3972

帖子

11

粉丝
快速回复 在线客服 返回列表 返回顶部