[应用相关] [stm32] GPIO及最小框架

[复制链接]
864|9
 楼主| deadtime 发表于 2020-1-1 16:54 | 显示全部楼层 |阅读模式
1、GPIO硬件结构图:
112212428649912.jpg

 楼主| deadtime 发表于 2020-1-1 16:54 | 显示全部楼层
2、GPIO程序结构:

656855e0c5e39afae1.png
 楼主| deadtime 发表于 2020-1-1 16:56 | 显示全部楼层
3、框架介绍:

847565e0c5ead14bf3.png
 楼主| deadtime 发表于 2020-1-1 16:56 | 显示全部楼层
4、代码片段解析:

4.1 引入函数

#include "stm32f10x.h"
这个是用户文件中唯一要包含和修改的库函数!除此之外我们还要把文件stm32f10x_conf.h做相应修改:(如第二行注释所示就是使能你FWlib中引入的文件,这个非常重要,一定不要少了)
  1. /* Includes ------------------------------------------------------------------*/
  2. /* Uncomment the line below to enable peripheral header file inclusion */
  3. /* #include "stm32f10x_adc.h" */
  4. /* #include "stm32f10x_bkp.h" */
  5. /* #include "stm32f10x_can.h" */
  6. /* #include "stm32f10x_crc.h" */
  7. /* #include "stm32f10x_dac.h" */
  8. /* #include "stm32f10x_dbgmcu.h" */
  9. /* #include "stm32f10x_dma.h" */
  10. /* #include "stm32f10x_exti.h" */
  11. /* #include "stm32f10x_flash.h" */
  12. /* #include "stm32f10x_fsmc.h" */
  13. #include "stm32f10x_gpio.h"
  14. /* #include "stm32f10x_i2c.h" */
  15. /* #include "stm32f10x_iwdg.h" */
  16. /* #include "stm32f10x_pwr.h" */
  17. #include "stm32f10x_rcc.h"
  18. /* #include "stm32f10x_rtc.h" */
  19. /* #include "stm32f10x_sdio.h" */
  20. /* #include "stm32f10x_spi.h" */
  21. /* #include "stm32f10x_tim.h" */
  22. /* #include "stm32f10x_usart.h" */
  23. /* #include "stm32f10x_wwdg.h" */
  24. /* #include "misc.h" */  /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
 楼主| deadtime 发表于 2020-1-1 16:57 | 显示全部楼层
4.2 端口宏定义

  1. #define LED1_ON GPIO_SetBits(GPIOB, GPIO_Pin_8);  
  2. #define LED1_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_8);

  3. #define LED2_ON GPIO_SetBits(GPIOD, GPIO_Pin_6);  
  4. #define LED2_OFF GPIO_ResetBits(GPIOD, GPIO_Pin_6);

  5. #define LED3_ON GPIO_SetBits(GPIOD, GPIO_Pin_3);  
  6. #define LED3_OFF GPIO_ResetBits(GPIOD, GPIO_Pin_3);


这里就是宏定义PB8、PD6、PD3三个端口输出高低电平,这样在这3个端口接上LED就能通过给高低电平控制灯的亮灭。
 楼主| deadtime 发表于 2020-1-1 16:57 | 显示全部楼层
4.3 系统时钟使能函数

void RCC_Configuration(void)
{   
   SystemInit();
}

这里函数是RCC初始化,这里只调用库函数初始化了系统时钟72Mhz
 楼主| deadtime 发表于 2020-1-1 16:58 | 显示全部楼层
4.4 GPIO初始化函数

  1. void LED_Config(void){
  2.   GPIO_InitTypeDef GPIO_InitStructure;

  3.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD , ENABLE);   
  4.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                     //LED1  V6       //将V6,V7,V8 配置为通用推挽输出  
  5.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  6.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;             //口线翻转速度为50MHz
  7.   GPIO_Init(GPIOB, &GPIO_InitStructure);                     

  8.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_3;         //LED2, LED3     V7 V8
  9.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  10. }


这里是GPIO的初始化函数,第二行是定义一个GPIO初始化结构体,第四行是使能GPIOB和GPIOD的时钟,第5-7行是对GPIO初始化结构体信息的填充,要根据所需GPIO的不同属性进行设置,第8行是调用库函数GPIO_Init()对GPIOB8进行初始化,采用结构体的信息,同样的道理,来初始化GPIOD6和D3.
 楼主| deadtime 发表于 2020-1-1 16:58 | 显示全部楼层
4.5 简单延时函数

void Delay(__IO uint32_t nCount)
{
    for(; nCount != 0; nCount--);
}
 楼主| deadtime 发表于 2020-1-1 16:59 | 显示全部楼层
4.6 主程序

  1. int main(void)
  2. {
  3.   RCC_Configuration();                   //系统时钟配置
  4.   LED_Config();                            //LED控制配置
  5.   while (1)
  6.   {
  7.     LED1_ON; LED2_OFF; LED3_OFF;        //LED1亮  LED2,LED3灭(LED2,LED3 仅V3,V2,V2.1板有)
  8.     Delay(0xAFFFF);
  9.     LED1_OFF; LED2_ON; LED3_OFF;        //LED2亮  LED1,LED3灭(LED2,LED3 仅V3,V2,V2.1板有)
  10.     Delay(0xAFFFF);
  11.     LED1_OFF; LED2_OFF; LED3_ON;        //LED3亮  LED1,LED2灭(LED2,LED3 仅V3,V2,V2.1板有)
  12.     Delay(0xAFFFF);   
  13.   }
  14. }
 楼主| deadtime 发表于 2020-1-1 16:59 | 显示全部楼层
代码链接(stm32f103VE):http://pan.baidu.com/s/1jGolNO6
您需要登录后才可以回帖 登录 | 注册

本版积分规则

44

主题

470

帖子

2

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