{"id":68,"date":"2021-05-17T00:00:00","date_gmt":"2021-05-17T00:00:00","guid":{"rendered":"https:\/\/zhewana.cn\/?p=68"},"modified":"2021-05-17T00:00:00","modified_gmt":"2021-05-17T00:00:00","slug":"68","status":"publish","type":"post","link":"https:\/\/zhewana.cn\/?p=68","title":{"rendered":"HAL\u5e93\u57fa\u672c\u51fd\u6570\uff08\u4e0a\uff09"},"content":{"rendered":"<blockquote>\n<p>\u672c\u6587\u53ef\u80fd\u542b\u6709\u4e00\u4e9b\u8bfb\u8005\u672a\u66fe\u638c\u63e1\u7684\u77e5\u8bc6\u70b9\u5728\u5185\uff0c\u5176\u5b58\u5728\u53ea\u662f\u4e3a\u4e86\u8ba9\u7b14\u8005\u80fd\u591f\u6b63\u5e38\u8fdb\u884c\u8bb2\u8ff0\uff0c\u800c\u4e0d\u81f3\u4e8e\u4e00\u76f4\u7528\u7c7b\u4f3c\u4e8e\u201c\u4ec0\u4e48\u4ec0\u4e48\u4e4b\u7c7b\u7684\u201d\u8fd9\u79cd\u65e0\u7528\u7684\u5e9f\u8bdd\u6765\u6d6a\u8d39\u8bfb\u8005\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u6240\u4ee5\u5982\u679c\u6709\u7684\u540d\u8bcd\u8bfb\u8005\u770b\u4e0d\u61c2\uff0c\u522b\u62c5\u5fc3\uff0c\u968f\u4fbf\u770b\u770b\u4e5f\u597d\u561b<\/p>\n<\/blockquote>\n<h2>\u6211\u8be5\u53bb\u54ea\u91cc\u627e\u5230\u8fd9\u4e9b\u51fd\u6570\uff1f<\/h2>\n<p>\u9996\u5148\u6700\u76f4\u89c2\u7684\u5f53\u7136\u5c31\u662fST\u7684<a href=\"https:\/\/www.st.com\/resource\/en\/user_manual\/dm00105879-description-of-stm32f4-hal-and-ll-drivers-stmicroelectronics.pdf\">HAL\u5e93\u5b98\u65b9\u624b\u518c<\/a>\u5566\u3002<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u672c\u624b\u518c\u7684\u5185\u5bb9\u53ea\u662f<strong>\u51fd\u6570\u58f0\u660e\u7684\u7b80\u5355\u5806\u53e0<\/strong>\uff0c\u56e0\u6b64\u5efa\u8bae\u53bb\u770b\u770bHAL\u5e93\u7684\u51fd\u6570<strong>\u6e90\u7801\u4e2d\u7684\u6ce8\u91ca<\/strong>\u3002\u6211\u4eec\u6765\u4e3e\u4e2a\u7b80\u5355\u7684\u5c0f\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-C\">\/**\n  ******************************************************************************\n  * @file    stm32f4xx_hal_gpio.c\n  * @author  MCD Application Team\n  * @brief   GPIO HAL module driver.\n  *          This file provides firmware functions to manage the following \n  *          functionalities of the General Purpose Input\/Output (GPIO) peripheral:\n  *           + Initialization and de-initialization functions\n  *           + IO operation functions\n  *\n  @verbatim\n  ==============================================================================\n                    ##### GPIO Peripheral features #####\n  ==============================================================================\n  [..] \n  Subject to the specific hardware characteristics of each I\/O port listed in the datasheet, each port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software in several modes:\n  (+) Input mode \n  (+) Analog mode\n  (+) Output mode\n  (+) Alternate function mode\n  (+) External interrupt\/event lines\n\n  [..]  \n  During and just after reset, the alternate functions and external interrupt lines are not active and the I\/O ports are configured in input floating mode.\n  \n  [..]   \n  All GPIO pins have weak internal pull-up and pull-down resistors, which can be activated or not.\n\n  [..]\n  In Output or Alternate mode, each IO can be configured on open-drain or push-pull type and the IO speed can be selected depending on the VDD value.\n\n  [..]  \n  All ports have external interrupt\/event capability. To use external interrupt lines, the port must be configured in input mode. All available GPIO pins are connected to the 16 external interrupt\/event lines from EXTI0 to EXTI15.\n  \n  [..]\n  The external interrupt\/event controller consists of up to 23 edge detectors(16 lines are connected to GPIO) for generating event\/interrupt requests (each input line can be independently configured to select the type (interrupt or event)and the corresponding trigger event (rising or falling or both). Each line can also be masked independently. \n\n                     ##### How to use this driver #####\n  ==============================================================================  \n  [..]\n    (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE(). \n\n    (#) Configure the GPIO pin(s) using HAL_GPIO_Init().\n        (++) Configure the IO mode using &quot;Mode&quot; member from GPIO_InitTypeDef structure\n        (++) Activate Pull-up, Pull-down resistor using &quot;Pull&quot; member from GPIO_InitTypeDef structure.\n        (++) In case of Output or alternate function mode selection: the speed is configured through &quot;Speed&quot; member from GPIO_InitTypeDef structure.\n        (++) In alternate mode is selection, the alternate function connected to the IO is configured through &quot;Alternate&quot; member from GPIO_InitTypeDef structure.\n        (++) Analog mode is required when a pin is to be used as ADC channel or DAC output.\n        (++) In case of external interrupt\/event selection the &quot;Mode&quot; member from GPIO_InitTypeDef structure select the type (interrupt or event) and the corresponding trigger event (rising or falling or both).\n\n    (#) In case of external interrupt\/event mode selection, configure NVIC IRQ priority mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using HAL_NVIC_EnableIRQ().\n         \n    (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().\n            \n    (#) To set\/reset the level of a pin configured in output mode use HAL_GPIO_WritePin()\/HAL_GPIO_TogglePin().\n    \n    (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().\n\n                 \n    (#) During and just after reset, the alternate functions are not active and the GPIO pins are configured in input floating mode (except JTAG pins).\n  \n    (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has priority over the GPIO function.\n  \n    (#) The HSE oscillator pins OSC_IN\/OSC_OUT can be used as general purpose PH0 and PH1, respectively, when the HSE oscillator is off. The HSE has priority over the GPIO function.\n  \n  @endverbatim\n  ******************************************************************************\n  * @attention\n  *\n  * &lt;h2&gt;&lt;center&gt;&amp;copy; Copyright (c) 2017 STMicroelectronics.\n  * All rights reserved.&lt;\/center&gt;&lt;\/h2&gt;\n  *\n  * This software component is licensed by ST under BSD 3-Clause license,\n  * the &quot;License&quot;; You may not use this file except in compliance with the\n  * License. You may obtain a copy of the License at:\n  *                        opensource.org\/licenses\/BSD-3-Clause\n  *\n  ******************************************************************************\n  *\/ <\/code><\/pre>\n<p>\u8fd9\u5927\u6bb5\u7684\u6ce8\u91ca\u51fa\u73b0\u5728stm32f4xx_hal_gpio.c\u4e2d\uff0c\u6574\u4f53\u4e0a\u5148\u662f\u4ecb\u7ecd\u4e86GPIO\u7684\u5916\u8bbe\u7279\u70b9\uff08\u6a21\u5f0f\u3001\u5185\u90e8\u4e0a\u4e0b\u62c9\u3001\u4e2d\u65ad\u7b49\uff09\uff0c\u63a5\u7740\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528GPIO\u7684\u9a71\u52a8(\u9a71\u52a8GPIO\u7684\u6d41\u7a0b)\uff1a<\/p>\n<p>\u4f7f\u80fd\u603b\u7ebf\u65f6\u949f\u2192\u5f15\u811a\u521d\u59cb\u5316\u2192\uff08\u914d\u7f6e\u4e2d\u65ad\u2192\u8bbe\u7f6e\u4f18\u5148\u7ea7\u2192\uff09\u83b7\u53d6\/\u914d\u7f6e\u5f15\u811a\u72b6\u6001<\/p>\n<p>\u4ee5\u4e0a\u5185\u5bb9\u53ef\u4ee5\u8bf4\u662f\u76f8\u5f53\u7684\u8be6\u7ec6\u4e86\uff0c\u5728\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u5bf9\u6211\u4eec\u6709\u5f88\u5927\u7684\u5e2e\u52a9\u3002<\/p>\n<p>\u540c\u65f6\u4e0d\u53ea\u662f\u5bf9\u4e8e\u6574\u4e2a\u6a21\u5757\u6709\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u51fd\u6570\u4e5f\u90fd\u6709\u6ce8\u91ca\u505a\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u4f8b\u5982\u5bf9\u4e8eHAL_GPIO_WritePin\u51fd\u6570\u5c31\u6709\u5982\u4e0b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<pre><code class=\"lang-C\">\/**\n  * @brief  Sets or clears the selected data port bit.\n  *\n  * @note   This function uses GPIOx_BSRR register to allow atomic read\/modify\n  *         accesses. In this way, there is no risk of an IRQ occurring between\n  *         the read and the modify access.\n  *\n  * @param  GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or\n  *                      x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.\n  * @param  GPIO_Pin specifies the port bit to be written.\n  *          This parameter can be one of GPIO_PIN_x where x can be (0..15).\n  * @param  PinState specifies the value to be written to the selected bit.\n  *          This parameter can be one of the GPIO_PinState enum values:\n  *            @arg GPIO_PIN_RESET: to clear the port pin\n  *            @arg GPIO_PIN_SET: to set the port pin\n  * @retval None\n  *\/<\/code><\/pre>\n<blockquote>\n<p>\u5173\u4e8e\u6ce8\u91ca\u7684\u5185\u5bb9\uff1a<\/p>\n<p>\u8be6\u7ec6\u5185\u5bb9\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/blog.csdn.net\/wenrenhua08\/article\/details\/39591239\">\u8fd9\u7bc7\u6587\u7ae0<\/a>\uff0c\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e0b\uff1a<\/p>\n<p>brief\uff1a\u51fd\u6570\u529f\u80fd\u7b80\u4ecb<\/p>\n<p>note\uff1a\u51fd\u6570\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879<\/p>\n<p>param\uff1a\u5373parameter\uff0c\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u76f8\u5173\u8bf4\u660e<\/p>\n<p>retval\uff1a\u5373return value\uff0c\u51fd\u6570\u8fd4\u56de\u503c<\/p>\n<\/blockquote>\n<p>\u5927\u81f4\u770b\u4e86\u770b\u6ce8\u91ca\uff0c\u662f\u4e0d\u662f\u5bf9\u4e8e\u51fd\u6570\u6709\u66f4\u6df1\u7684\u4e86\u89e3\u4e86\u5462\uff1f\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb<del>\u5927\u89c4\u6a21\u590d\u5236\u7c98\u8d34<\/del>\u771f\u6b63\u7684\u4e3b\u9898\u4e86\u3002<\/p>\n<h2>GPIO\u51fd\u6570<\/h2>\n<p>GPIO\u5199\u51fd\u6570:\u914d\u7f6eGPIO\u5f15\u811a\u72b6\u6001<\/p>\n<pre><code class=\"lang-C\">void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)\n{\n  \/* Check the parameters *\/\n  assert_param(IS_GPIO_PIN(GPIO_Pin));\n  assert_param(IS_GPIO_PIN_ACTION(PinState));\/\/\u65ad\u8a00\u9a8c\u8bc1\u53c2\u6570\u6b63\u786e\u6027\n\n  if(PinState != GPIO_PIN_RESET)\n  {\n    GPIOx-&gt;BSRR = GPIO_Pin;\/\/\u5c06BSRR\u5bc4\u5b58\u5668\u7f6e\u4f4d\uff0c\u8fdb\u800c\u5b8c\u6210\u5f15\u811a\u7684\u914d\u7f6e\n  }\n  else\n  {\n    GPIOx-&gt;BSRR = (uint32_t)GPIO_Pin &lt;&lt; 16U;\n  }\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-C\">HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);\/\/\u5c06PC13\u7f6e\u9ad8\nHAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);\/\/\u5c06PC13\u7f6e\u4f4e<\/code><\/pre>\n<hr>\n<p>GPIO\u8bfb\u51fd\u6570\uff1a\u8bfb\u53d6GPIO\u5f15\u811a\u72b6\u6001\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u72b6\u6001\u503c<\/p>\n<pre><code class=\"lang-C\">GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\n{\n  GPIO_PinState bitstatus;\n  \/\/\u65ad\u8a00\u9a8c\u8bc1\u53c2\u6570\u6b63\u786e\u6027\n  assert_param(IS_GPIO_PIN(GPIO_Pin));\n    \n  \/\/\u8bfb\u53d6IDR\u5bc4\u5b58\u5668\u7684\u5bf9\u5e94\u5f15\u811a\u4f4d\n  if((GPIOx-&gt;IDR &amp; GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)\n  {\n    \/\/\u82e5\u4e3a\u9ad8\uff0c\u8fd4\u56deGPIO_PIN_SET\n    bitstatus = GPIO_PIN_SET;\n  }\n  else\n  {\n    \/\/\u5426\u5219\uff0c\u8fd4\u56deGPIO_PIN_RESET\n    bitstatus = GPIO_PIN_RESET;\n  }\n  return bitstatus;\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-C\">    \/\/\u5224\u65adPA0\u662f\u5426\u4e3a\u9ad8\u7535\u5e73\nif (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET) {\n    \/\/\u5982\u679c\u662f\uff0c\u5c06PC13\u7f6e\u9ad8\n    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);\n} else {\n    \/\/\u5982\u679c\u4e0d\u662f\uff0c\u5c06PC13\u7f6e\u4f4e\n    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);\n}<\/code><\/pre>\n<hr>\n<p>GPIO\u7ffb\u8f6c\u5f15\u811a\u72b6\u6001\uff1a\u53cd\u8f6cGPIO\u5f15\u811a\u7684\u72b6\u6001<\/p>\n<pre><code class=\"lang-C\">void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)\n{\n  uint32_t odr;\n\n  \/\/\u65ad\u8a00\u9a8c\u8bc1\u53c2\u6570\u6b63\u786e\u6027\n  assert_param(IS_GPIO_PIN(GPIO_Pin));\n\n  \/\/\u8bfb\u53d6ODR\u5bc4\u5b58\u5668\u7684\u503c\n  odr = GPIOx-&gt;ODR;\n\n  \/\/\u901a\u8fc7\u5199\u5165BSRR\u5bc4\u5b58\u5668\u66f4\u6539\u5f15\u811a\u72b6\u6001\n  GPIOx-&gt;BSRR = ((odr &amp; GPIO_Pin) &lt;&lt; GPIO_NUMBER) | (~odr &amp; GPIO_Pin);\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-C\">\/\/\u5224\u65adA0\u7684\u72b6\u6001\u662f\u5426\u4e3a\u9ad8\nif (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)\n    \/\/\u5982\u679c\u662f\uff0c\u53cd\u8f6cPC13\u7684\u5f15\u811a\u72b6\u6001\n    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);<\/code><\/pre>\n<hr>\n<p>GPIO\u4e2d\u65ad\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"lang-C\">__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)\n{\n  \/\/\u51fd\u6570\u5185\u5bb9\u9700\u8981\u5728\u7528\u6237\u6587\u4ef6\u4e2d\u91cd\u65b0\u5b9a\u4e49\n  UNUSED(GPIO_Pin);\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-C\">\/\/\u5728\u7528\u6237\u6587\u4ef6\u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a\nvoid HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){\n    \/\/\u5f53\u4e2d\u65ad\u89e6\u53d1\u7684\u65f6\u5019\u53cd\u8f6cPC13\u5f15\u811a\u7684\u72b6\u6001\n    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);\n}<\/code><\/pre>\n<hr>\n<p>\u7bc7\u5e45\u9650\u5236\uff0c\u53ea\u80fd\u8bb2\u8fd9\u4e48\u591a\u4e86\uff0c\u5269\u4e0b\u7684\u6211\u4eec\u4e0b\u4e00\u7bc7\u6587\u7ae0\u518d\u770b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u53ef\u80fd\u542b\u6709\u4e00\u4e9b\u8bfb\u8005\u672a\u66fe\u638c\u63e1\u7684\u77e5\u8bc6\u70b9\u5728\u5185\uff0c\u5176\u5b58\u5728\u53ea\u662f\u4e3a\u4e86\u8ba9\u7b14\u8005\u80fd\u591f\u6b63\u5e38\u8fdb\u884c\u8bb2\u8ff0\uff0c\u800c\u4e0d\u81f3\u4e8e\u4e00\u76f4\u7528\u7c7b\u4f3c\u4e8e\u201c\u4ec0\u4e48\u4ec0\u4e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[12],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-embedded","tag-12"],"_links":{"self":[{"href":"https:\/\/zhewana.cn\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhewana.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhewana.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhewana.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhewana.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":0,"href":"https:\/\/zhewana.cn\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhewana.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhewana.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhewana.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}