STM32F103 GPIO 端口 [英] STM32F103 GPIO Ports
问题描述
我有一个 STM32F103C8 MCU,我想在没有 Cube MX 的情况下控制 GPIO 寄存器.MCU 有一个嵌入式 LED,我想控制它.我目前正在使用 CubeMX 和 IAR 软件,并使用以下代码将引脚设为输出(在 CubeMX 中):
I have a STM32F103C8 MCU, and I want to control GPIO registers without Cube MX. The MCU has an embedded LED and I want control it. I'm currently using CubeMX and IAR Software, and I make the pin an output (in CubeMX) with this code:
HAL_GPIO_TogglePin(Ld2_GPIO_Port,Ld2_Pin);
HAL_Delay(1000);
这可行,但我想在没有 Cube 和 HAL 库的情况下做到这一点;我想直接编辑寄存器文件.
This works, but I want to do it without Cube and HAL library; I want to edit the register files directly.
推荐答案
通过寄存器使用 GPIO 非常容易.您不必编写自己的启动程序(就像@old_timer 的答案一样).只需要两步
Using GPIO using registers is very easy. You fo not have to write your own startup (as ion the @old_timer answer). Only 2 steps are needed
您将需要 STM 提供的带有数据类型声明和人类可读的 #defines 的 CMSIS 标头以及 参考手册>
you will need the STM provided CMSIS headers with datatypes declarations and human readable #defines and the reference manual
- 启用 GPIO 端口时钟.ecample:
RCC ->APB2ENR |= RCC_APB2ENR_IOPAEN;
- 使用 CRL/CRH GPIO 寄存器配置引脚
#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;
- 操作输出
/* to toggle */
GPIOA -> ODR ^= (1 << pinNummer);
/* to set */
GPIOA -> BSRR = (1 << pinNummer);
/* to reset */
GPIOA -> BRR = (1 << pinNummer);
//or
GPIOA -> BSRR = (1 << (pinNummer + 16));
这篇关于STM32F103 GPIO 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!