你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 F0按键开启LED

[复制链接]
eefishing 发布时间:2019-3-1 22:41
步骤2:现在我们将使用STM32F030R8T6的2个GPIO。在本教程中,我们实现了按钮切换LED。

故事

本项目采用STM32 CubeMX软件制作,IDE是Keil,硬件方面我使用的是STM32F0 Nucleo板。在这个例子中,我们使用板载LED和按钮。
在这个项目中,我们实现了两个GPIOs。1为LED输出2。将按钮作为输入。在本例项目中,当用户按下按钮时,LED将打开。
bb0每一个GPIO引脚都可以通过软件配置为输出(推拉或开漏)、输入(带或不带上拉或下拉)或外设替代功能。

输入配置基础:

当I/O端口被编程为输入时:

  • 输出缓冲区被禁用
  • 激活Schmitt触发器输入
  • 根据GPIOx_PUPDR寄存器中的值激活上拉和下拉电阻
  • inputconfig_ref_CVoeFAB7AS.jpg
  • 在每个AHB时钟周期中,I/O引脚上的数据被采样到输入数据寄存器中
  • 对输入数据寄存器的读访问提供I/O状态






电路图:
pc13_buRbRQqiXN.PNG




代码:
按下按钮Main.c文件/ c++
如果代码无法工作,将此cose复制到main.c文件中。任何问题都要告诉我。
在USER code BEGIN 3下编写代码。

  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * @brief          : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  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. #include "stm32f0xx_hal.h"
  41. #include "gpio.h"

  42. /* USER CODE BEGIN Includes */

  43. /* USER CODE END Includes */

  44. /* Private variables ---------------------------------------------------------*/

  45. /* USER CODE BEGIN PV */
  46. /* Private variables ---------------------------------------------------------*/

  47. /* USER CODE END PV */

  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);

  50. /* USER CODE BEGIN PFP */
  51. /* Private function prototypes -----------------------------------------------*/

  52. /* USER CODE END PFP */

  53. /* USER CODE BEGIN 0 */

  54. /* USER CODE END 0 */

  55. /**
  56.   * @brief  The application entry point.
  57.   *
  58.   * @retval None
  59.   */
  60. int main(void)
  61. {
  62.   /* USER CODE BEGIN 1 */

  63.   /* USER CODE END 1 */

  64.   /* MCU Configuration----------------------------------------------------------*/

  65.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  66.   HAL_Init();

  67.   /* USER CODE BEGIN Init */

  68.   /* USER CODE END Init */

  69.   /* Configure the system clock */
  70.   SystemClock_Config();

  71.   /* USER CODE BEGIN SysInit */

  72.   /* USER CODE END SysInit */

  73.   /* Initialize all configured peripherals */
  74.   MX_GPIO_Init();
  75.   /* USER CODE BEGIN 2 */

  76.   /* USER CODE END 2 */

  77.   /* Infinite loop */
  78.   /* USER CODE BEGIN WHILE */
  79.   while (1)
  80.   {

  81.   /* USER CODE END WHILE */

  82.   /* USER CODE BEGIN 3 */
  83. if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_3)==GPIO_PIN_SET)
  84. {
  85.         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5, GPIO_PIN_SET);
  86. }
  87. else
  88. {
  89.         HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
  90.   }
  91.   /* USER CODE END 3 */

  92. }

  93. /**
  94.   * @brief System Clock Configuration
  95.   * @retval None
  96.   */
  97. void SystemClock_Config(void)
  98. {

  99.   RCC_OscInitTypeDef RCC_OscInitStruct;
  100.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  101.     /**Initializes the CPU, AHB and APB busses clocks
  102.     */
  103.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  104.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  105.   RCC_OscInitStruct.HSICalibrationValue = 16;
  106.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  107.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  108.   {
  109.     _Error_Handler(__FILE__, __LINE__);
  110.   }

  111.     /**Initializes the CPU, AHB and APB busses clocks
  112.     */
  113.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  114.                               |RCC_CLOCKTYPE_PCLK1;
  115.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  116.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  117.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  118.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  119.   {
  120.     _Error_Handler(__FILE__, __LINE__);
  121.   }

  122.     /**Configure the Systick interrupt time
  123.     */
  124.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  125.     /**Configure the Systick
  126.     */
  127.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  128.   /* SysTick_IRQn interrupt configuration */
  129.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  130. }

  131. /* USER CODE BEGIN 4 */

  132. /* USER CODE END 4 */

  133. /**
  134.   * @brief  This function is executed in case of error occurrence.
  135.   * @param  file: The file name as string.
  136.   * @param  line: The line in file as a number.
  137.   * @retval None
  138.   */
  139. void _Error_Handler(char *file, int line)
  140. {
  141.   /* USER CODE BEGIN Error_Handler_Debug */
  142.   /* User can add his own implementation to report the HAL error return state */
  143.   while(1)
  144.   {
  145.   }
  146.   /* USER CODE END Error_Handler_Debug */
  147. }

  148. #ifdef  USE_FULL_ASSERT
  149. /**
  150.   * @brief  Reports the name of the source file and the source line number
  151.   *         where the assert_param error has occurred.
  152.   * @param  file: pointer to the source file name
  153.   * @param  line: assert_param error line source number
  154.   * @retval None
  155.   */
  156. void assert_failed(uint8_t* file, uint32_t line)
  157. {
  158.   /* USER CODE BEGIN 6 */
  159.   /* User can add his own implementation to report the file name and line number,
  160.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  161.   /* USER CODE END 6 */
  162. }
  163. #endif /* USE_FULL_ASSERT */

  164. /**
  165.   * @}
  166.   */

  167. /**
  168.   * @}
  169.   */

  170. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码




原创:Ashish Maheshwari
出处:http://www.hackster.io/ashish205/stm32f0-push-button-to-switch-on-led-bc9958


收藏 评论0 发布时间:2019-3-1 22:41

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

相似分享

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版