ST32微控制器GPIO功能描述

每个GPI/O端口有

两个32位配置寄存器(GPIOx_CRL,GPIOx_CRH),

两个32位数据寄存器(GPIOx_IDR和GPIOx_ODR),

一个32位置位/复位寄存器(GPIOx_BSRR),

一个16位复位寄存器(GPIOx_BRR)

一个32位锁定寄存器(GPIOx_LCKR)。

每个I/O端口位可以自由编程,然而必须按照32位字访问I/O端口寄存器(不允许半字或字节访
问)。GPIOx_BSRR和GPIOx_BRR寄存器允许对任何GPIO寄存器进行读/更改的独立访问;这
样,在读和更改访问之间产生IRQ时不会发生危险。

几种配置GPIO的分析

RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; //开启 GPIO端口 C 的时钟

GPIOC->CRH=0x33333333; //将 8-15号 GPIO配置成:输出模式,速度50MHz

GPIOC->ODR ^= (1 << 13) // 13号 GPIO输出 高电平


// I/O port C clock enable
RCC->APB2ENR = RCC_APB2ENR_IOPCEN;
// Set PC_12 to output
GPIOC->CRH &= ~(GPIO_CRH_MODE12 | GPIO_CRH_CNF12);
GPIOC->CRH |= GPIO_CRH_MODE12;

while(1)
{
GPIOC->BSRR = (1<<12);
delay();
GPIOC->BRR = (1<<12);
delay();
}


// Enable GPIO port clock
RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN;
// Configure the pins using CRL/CRH GPIO registers
#define GPIO_OUTPUT_2MHz (0b10)
#define GPIO_OUTPUT_PUSH_PULL (0 << 2)
GPIOA -> CRL &= ~(GPIO_CRL_MODE0 | GPIO_CRL_CNF0);
GPIOA -> CRL |= GPIO_OUTPUT_2MHz | GPIO_OUTPUT_PUSH_PULL;

// Manipulate the output

/* to toggle */
GPIOA -> ODR ^= (1 << pinNummer);

/* to set */
GPIOA -> BSRR = (1 << pinNummer);

/* to reset */
GPIOA -> BRR = (1 << pinNummer);
//or
GPIOA -> BSRR = (1 << (pinNummer + 16));


RCC->APB2ENR |= 0x10; //enable GPIOC clock
GPIOC->CRH = (GPIOC->CRH & 0xFF0FFFF) | 0x00100000; //PC13 output pull-push, 10MHz speed

GPIOC-> BSRR = 0x00002000; //pc13=1, leave others unchanged
GPIOC->BSRR = 0x20000000; //pc13=0, 其他不变
GPIOC->BSRR = 0x20002000; //pc13=1, 其他不变,设置时,忽略清除

GPIOC->BRR = 0x2000; //pc13=0, 其他不变
GPIOC->ODR = 0x00002000; //pc13=1,并且其他所有都设置为0
GPIOC->ODR |= 0x00002000; //pc13=1, 其他不变
GPIOC->ODR = 0x00000000; //pc13=0, 其他位也强制为0
GPIOC->ODR &= ~0x00002000; //pc12=0, 其他位不变


RCC->APB2ENR |= RCC_APB2ENR_IOPCEN //enable APB2_clock GPIOC
GPIOC->CRH |= GPIO_CRH_MODE13_1 //PC13 output
GPIOC->CRH &= ~GPIO_CRH_CNF13 //PC13 push pull


RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;

GPIOC->CRH &= ~GPIO_CRH_CNF13;
GPIOC->CRH |= GPIO_CRH_MODE13_0;

GPIOC->BSRR = GPIO_BSRR_BR13;
sleep(1);
GPIOC->BSRR = GPIO_BSRR_BS13;


RCC->APB2ENR |=
GPIOC->CRH &= ~GPIO_CRH_CNF13;

GPIOC->CRH |= GPIO_CRH_MODE13_0; //set C13 as a general purporse push-pull output

GPIOC->BSRR |= GPIO_BSRR_BR13; // set C13 0

soft_delay(5000000);

GPIOC->BSRR |= GPIO_BSRR_BS13;


// I/O port C clock enable
RCC->APB2ENR = RCC_APB2ENR_IOPCEN;

// Set PC_13 to output
GPIOC->CRH &= ~GPIO_CRH_CNF13;
GPIOC->CRH |= GPIO_CRH_MODE13_0;

while(1)
{
GPIOC->BSRR = GPIO_BSRR_BS13; // LED off
delay(500);

GPIOC->BRR = GPIO_BRR_BR13; // LED on
delay(500);
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注