操作IO是学习MCU的一个开端,以下总结了一下GD32F20x的IO操作!如果您觉得不错,或对你有所帮助,请顶一下!呵呵!
不知道发了会不会成为热帖?哈哈!
设置IO口DOR寄存器法:输出寄存器法置位和清零bit #include <gd32f20x.h> #include <gd32f20x_gpio.h>
int main(void) { GPIO_TypeDef * GPIO_X; //定义IO口指针 GPIO_X = GPIOD; //指向PD口 RCC->APB2CCR |= (1<<5); //使能PD时钟 GPIO_X->CTLR2 &= ~(1<<14); //设置PD.11为输出,最大2M速率 GPIO_X->CTLR2 |= (1<<13); while(1) { GPIO_X->DOR |= (1<<11); //PD.11=1 GPIO_X->DOR &= ~(1<<11); //PD.11=0 } return 0; }
设置IO口BOR和BCR寄存器法: 1.BOR端口操作寄存器可对DOR输出寄存器进行1/0设置。 高16位:CRx=1,对应设置bit=0; 低16位:BORx=1, 对应设置bit=1; 如果传给BOR寄存器的“设置值”清零位和位置位都是1时,BORx设置有效! 2.BCR端口位清零寄存器可对DOR输出寄存器进行清零设置。 #include <gd32f20x.h> #include <gd32f20x_gpio.h>
int main(void) { GPIO_TypeDef * GPIO_X; //定义IO口指针 GPIO_X = GPIOD; //指向PD口 RCC->APB2CCR |= (1<<5); //使能PD时钟 GPIO_X->CTLR2 &= ~(1<<14); //设置PD.11为输出,最大2M速率 GPIO_X->CTLR2 |= (1<<13); while(1) { // GPIO_X->BOR |= (1<<11) + (1<<(11+16)); //PD.11=1 GPIO_X->BOR |= (1<<11); //PD.11=1 GPIO_X->BOR |= (1<<(11+16)); //PD.11=0 GPIO_X->BOR |= (1<<11); //PD.11=1 GPIO_X->BCR |= (1<<11); //PD.11=0 } return 0; }
设置IO口BOR寄存器法:多个位同时操作置位或清零 #include <gd32f20x.h> #include <gd32f20x_gpio.h>
int main(void) { GPIO_TypeDef * GPIO_X; //定义IO口指针 GPIO_X = GPIOD; //指向PD口 RCC->APB2CCR |= (1<<5); //使能PD时钟 GPIO_X->CTLR2 &= ~((1<<14) | (1<<18)); //设置PD.11和PD.12为输出 GPIO_X->CTLR2 |= (1<<13) | (1<<17); while(1) { GPIO_X->BOR |= (1<<11) | (1<<(12+16)); //PD.11=1, PD.12=0 GPIO_X->BOR |= (1<<(11+16)) | (1<<12); //PD.11=0, PD.12=1 } return 0; }
设置IO口置位和复位库函数法:可多个位同时操作 #include <gd32f20x.h>
#define LED1 GPIO_PIN_11 #define LED2 GPIO_PIN_12 #define LED3 GPIO_PIN_13 #define LED (LED1 | LED2 | LED3)
int main(void) { //初始化LED参数: 端口PD.11, PD.12, PD.13, 输出速率2M, 输出模式推挽输出 GPIO_InitPara LED_cf={LED,GPIO_SPEED_2MHZ, GPIO_MODE_OUT_PP}; RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE); //使能PD时钟 GPIO_Init(GPIOD, &LED_cf); //初始化PD端口 while(1) { GPIO_SetBits(GPIOD,LED); //PD.11=1, PD.12=1, PD.13=1 GPIO_ResetBits(GPIOD, LED); //PD.11=0, PD.12=0, PD.13=0 } return 0; }
设置IO口写位库函数法: 可多个位同时操作置位或清零 #include <gd32f20x.h>
#define LED1 GPIO_PIN_11 #define LED2 GPIO_PIN_12 #define LED3 GPIO_PIN_13 #define LED (LED1 | LED2 | LED3)
int main(void) { //初始化LED参数: 端口PD.11, PD.12, PD.13, 输出速率2M, 输出模式推挽输出 GPIO_InitPara LED_cf={LED,GPIO_SPEED_2MHZ, GPIO_MODE_OUT_PP}; RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE); //使能PD时钟 GPIO_Init(GPIOD, &LED_cf); //初始化PD端口 while(1) { GPIO_WriteBit(GPIOD, LED, Bit_SET); //PD.11=1, PD.12=1, PD.13=1 GPIO_WriteBit(GPIOD, LED, Bit_RESET); //PD.11=0, PD.12=0, PD.13=0 GPIO_WriteBit(GPIOD, LED1, 1); //PD.11=1, PD.12=0, PD.13=0 GPIO_WriteBit(GPIOD, LED1, 0); //PD.11=0, PD.12=0, PD.13=0 } return 0; }
设置IO口写端口库函数法: 可多个位同时操作置位和清零 #include <gd32f20x.h>
#define LED1 GPIO_PIN_11 #define LED2 GPIO_PIN_12 #define LED3 GPIO_PIN_13 #define LED (LED1 | LED2 | LED3)
int main(void) { //初始化LED参数: 端口PD.11, PD.12, PD.13, 输出速率2M, 输出模式推挽输出 GPIO_InitPara LED_cf={LED,GPIO_SPEED_2MHZ, GPIO_MODE_OUT_PP}; RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE); //使能PD时钟 GPIO_Init(GPIOD, &LED_cf); //初始化PD端口 while(1) { GPIO_WritePort(GPIOD, GPIOD->DOR | LED); //PD.11=1, PD.12=1, PD.13=1 GPIO_WritePort(GPIOD, GPIOD->DOR & (~LED)); //PD.11=0, PD.12=0, PD.13=0
GPIO_WritePort(GPIOD, (GPIOD->DOR | LED1) & (~LED2));//PD.11=1, PD.12=0, PD.13=0 GPIO_WritePort(GPIOD, (~LED1) & (GPIOD->DOR | LED2));//PD.11=0, PD.12=1, PD.13=0 } return 0; }
位绑定操作法: #include <gd32f20x.h>
#define LED1 GPIO_PIN_11 #define LED2 GPIO_PIN_12 #define LED3 GPIO_PIN_13 #define LED (LED1 | LED2 | LED3)
int main(void) { //初始化LED参数: 端口PD.11, PD.12, PD.13, 输出速率2M, 输出模式推挽输出 GPIO_InitPara LED_cf={LED,GPIO_SPEED_2MHZ, GPIO_MODE_OUT_PP}; RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOD,ENABLE); //使能PD时钟 GPIO_Init(GPIOD, &LED_cf); //初始化PD端口 while(1) { (*((u32*)0x422281AC))=1; //PD.11=1 (*((u32*)0x422281AC))=0; //PD.11=0 (*((u32*)0x422281B0))=1; //PD.12=1 (*((u32*)0x422281B0))=0; //PD.12=0 (*((u32*)0x422281B4))=1; //PD.13=1 (*((u32*)0x422281B4))=0; //PD.13=0 } return 0; }
|