gpt4 book ai didi

c - stm32f103板不闪烁

转载 作者:行者123 更新时间:2023-11-30 14:59:49 25 4
gpt4 key购买 nike

我无法让我的新 stm32f103c8t6 板闪烁简单的 LED。我已经尝试了一切。我已经将裸机直接写入寄存器,并且还使用了 GPIO 库,但它仍然无法工作。我用的是keil。我的 LED 通过 1k 电阻连接在面包板上。我还测试了输出引脚上的电压,但它微不足道。请问可能出了什么问题?下面的代码...

#include "stm32f10x.h"

GPIO_InitTypeDef GPIO_InitStructure;

void delay(int a)
{
for (int i = 0; i < a; i++)
{

}

}
int main(void)
{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

/* Configure PD0 and PD2 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

/* To achieve GPIO toggling maximum frequency, the following sequence is mandatory.
You can monitor PD0 or PD2 on the scope to measure the output signal.
If you need to fine tune this frequency, you can add more GPIO set/reset
cycles to minimize more the infinite loop timing.
This code needs to be compiled with high speed optimization option. */
while (1)
{
/* Set PD0 and PD2 */
GPIOA->BSRR = 0x00000005;
delay(1000000);
/* Reset PD0 and PD2 */
GPIOA->BRR = 0x00000005;
delay(1000000);

}
}

最佳答案

几个选项:

错误的延迟实现和编译器优化代码:

void delay(volatile int a) {
//Added volatile in a and in i
for (volatile int i = 0; i < a; i++);
}

如您的情况,初始化错误。您初始化了 GPIOD 但使用了 GPIOA

关于c - stm32f103板不闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42509783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com