微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 流水灯程序测试及程序说明

流水灯程序测试及程序说明

时间:10-02 整理:3721RD 点击:
本节测试NUCLEO-F412ZG的GPIO功能,通过下载固件库中的代码GPIO范例,可以看到流水灯闪烁,对于GPIO操作主要是先使能GPIO的时钟,再初始化GPIO,配置GPIO模式,最后设置GPIO输出高低还是读取IO的数据,下面给对应的程序做一下解释。
int main(void)
{
  /* This sample code shows how to use GPIO HAL API to toggle LED1 and LED3 IOs
    in an infinite loop. */

  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user
         can eventually implement his proper time base source (a general purpose
         timer for example or other time source), keeping in mind that Time base
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();//初始化HAL库

  /* Configure the system clock to 100 MHz */
  SystemClock_Config();//配置系统时钟为100MHz
  
  /* -1- Enable GPIO Clock (to be able to program the configuration registers) */
  LED1_GPIO_CLK_ENABLE();//使能控制LED1 GPIO时钟
  LED2_GPIO_CLK_ENABLE();//使能控制LED2 GPIO时钟
  LED3_GPIO_CLK_ENABLE();//使能控制LED2 GPIO时钟

  /* -2- Configure IO in output push-pull mode to drive external LEDs */
  GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;//配置IO推挽输出
  GPIO_InitStruct.Pull  = GPIO_NOPULL;//配置GPIO不上拉
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;//配置GPIO速度为高速

  GPIO_InitStruct.Pin = LED1_PIN;//选择GPIO引脚为LED1_PIN
  HAL_GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStruct);//初始化LED1_PIN
  GPIO_InitStruct.Pin = LED2_PIN;//选择GPIO引脚为LED2_PIN
  HAL_GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStruct);//初始化LED2_PIN
  GPIO_InitStruct.Pin = LED3_PIN;//选择GPIO引脚为LED3_PIN
  HAL_GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStruct);;//初始化LED3_PIN

  /* -3- Toggle IO in an infinite loop */
  while (1)
  {
    HAL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_PIN);//翻转LED1_PIN状态
    /* Insert delay 100 ms */
    HAL_Delay(100);//延时100ms
    HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);//翻转LED2_PIN状态
    /* Insert delay 100 ms */
    HAL_Delay(100);//延时100ms
    HAL_GPIO_TogglePin(LED3_GPIO_PORT, LED3_PIN);//翻转LED3_PIN状态
    /* Insert delay 100 ms */
    HAL_Delay(100);//延时100ms
  }
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top