#include "mygpio.h" uint32_t GetGPIO_RCC(MyPinDef pin){ return RCC_APB2Periph_GPIOA<<(pin/16); } GPIO_TypeDef* GetGPIO_Port(MyPinDef pin){ return ((GPIO_TypeDef *) (GPIOA_BASE+0x0400*(pin/16))); } uint16_t GetGPIO_PIN(MyPinDef pin){ return 0x0001<<(pin%16); } void GPIO_SetMode(MyPinDef pin,GPIOMode_TypeDef mode){ GPIO_InitTypeDef GPIO_InitStructure; //声明结构体 RCC_APB2PeriphClockCmd(GetGPIO_RCC(pin), ENABLE); //**All notes can be deleted and modified**// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GetGPIO_Port(pin), &GPIO_InitStructure); } void Pin_Out(MyPinDef pin, uint8_t bit){ if(pin<80) BIT_ADDR(GPIOA_ODR_Addr+0x400*(pin/16),pin%16) = bit; else BIT_ADDR(GPIOF_ODR_Addr+0x400*((pin-80)/16),pin%16) = bit; } uint8_t Pin_Read(MyPinDef pin){ if(pin<80) return BIT_ADDR(GPIOA_IDR_Addr+0x400*(pin/16),pin%16); else return BIT_ADDR(GPIOF_IDR_Addr+0x400*((pin-80)/16),pin%16); } void SetPin(MyPinDef pin){ Pin_Out(pin, 1); } void ResetPin(MyPinDef pin){ Pin_Out(pin, 0); }
时间: 2024-01-24 20:17:12 浏览: 145
这段代码是用来控制 STM32 的 GPIO 端口的,主要包含了以下几个函数:
1. `GetGPIO_RCC`:根据引脚号返回该引脚所在的 GPIO 端口的时钟 RCC_APB2Periph_GPIOx,其中 x 表示端口号,如 A、B、C 等。
2. `GetGPIO_Port`:根据引脚号返回该引脚所在的 GPIO 端口,其中 GPIOA_BASE 表示 A 端口的基地址,0x0400 表示一个端口的地址偏移量,因此 `(GPIOA_BASE+0x0400*(pin/16))` 就是引脚所在的端口的地址。
3. `GetGPIO_PIN`:根据引脚号返回该引脚的编号,例如引脚号为 2,返回值就是 0x0004。
4. `GPIO_SetMode`:配置 GPIO 端口的工作模式,包括输入、输出、复用功能等。
5. `Pin_Out`:将引脚设置为输出模式,并输出指定的数值(0 或 1)。
6. `Pin_Read`:读取引脚的输入值。
7. `SetPin`:将引脚设置为高电平状态。
8. `ResetPin`:将引脚设置为低电平状态。
需要注意的是,代码中的 `BIT_ADDR` 是一个宏定义,用于读写位地址,其定义如下:
```
#define ADDR_FLASH_PAGE_31 ((uint32_t)0x08007C00) /* Base @ of Page 31, 2 Kbytes */
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x02000000+((addr & 0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOA_ODR_Addr (GPIOA_BASE+0x14)
#define GPIOB_ODR_Addr (GPIOB_BASE+0x14)
#define GPIOC_ODR_Addr (GPIOC_BASE+0x14)
#define GPIOD_ODR_Addr (GPIOD_BASE+0x14)
#define GPIOE_ODR_Addr (GPIOE_BASE+0x14)
#define GPIOF_ODR_Addr (GPIOF_BASE+0x14)
#define GPIOG_ODR_Addr (GPIOG_BASE+0x14)
#define GPIOA_IDR_Addr (GPIOA_BASE+0x10)
#define GPIOB_IDR_Addr (GPIOB_BASE+0x10)
#define GPIOC_IDR_Addr (GPIOC_BASE+0x10)
#define GPIOD_IDR_Addr (GPIOD_BASE+0x10)
#define GPIOE_IDR_Addr (GPIOE_BASE+0x10)
#define GPIOF_IDR_Addr (GPIOF_BASE+0x10)
#define GPIOG_IDR_Addr (GPIOG_BASE+0x10)
```
这里使用了位带操作,将每个位单独映射到一个地址上,以实现对单个位的读写操作。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)