1、初始化代码

  1. #include "bsp_gpio.h"
  2. /*
  3. GPIO_Mode_AIN = 0x0, 模拟输入
  4. GPIO_Mode_IN_FLOATING = 0x04, 浮空输入
  5. GPIO_Mode_IPD = 0x28, 下拉输入
  6. GPIO_Mode_IPU = 0x48, 上拉输入
  7. GPIO_Mode_Out_OD = 0x14, 开漏输出
  8. GPIO_Mode_Out_PP = 0x10, 推挽输出
  9. GPIO_Mode_AF_OD = 0x1C, 复用开漏输出
  10. GPIO_Mode_AF_PP = 0x18 复用推挽输出
  11. */
  12. /*
  13. GPIO_Speed_10MHz = 1,
  14. GPIO_Speed_2MHz,
  15. GPIO_Speed_50MHz
  16. */
  17. void GPIO_OUT_INIT()
  18. {
  19. //打开时钟
  20. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  21. //初始化IO口
  22. {
  23. GPIO_InitTypeDef GPIO_InitStruct;
  24. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
  25. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  26. GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
  27. GPIO_Init(GPIOC,&GPIO_InitStruct);
  28. }
  29. }
  30. void GPIO_IN_INIT()
  31. {
  32. //打开时钟
  33. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  34. //初始化IO口
  35. {
  36. GPIO_InitTypeDef GPIO_InitStruct;
  37. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
  38. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  39. // GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
  40. GPIO_Init(GPIOB,&GPIO_InitStruct);
  41. }
  42. }

2、GPIO函数使用

  1. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  2. 例:i = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2);
  3. 读取PB2的数据
  4. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
  5. 例:i = GPIO_ReadInputData(GPIOB);
  6. 读取PB的数据
  7. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  8. 例:GPIO_SetBits(GPIOC,GPIO_Pin_13);
  9. PC13设置为高电平
  10. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  11. 例:GPIO_ResetBits(GPIOC,GPIO_Pin_13);
  12. PC13设置为低电平
  13. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
  14. 例:GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_SET);
  15. PC13设置为高电平
  16. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
  17. 例:GPIO_Write(GPIOC, 0X0055);
  18. PC口输出设为0x0055;