代码如下
#include "stm32f10x.h" // Device header
void Led_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //LED端口配置
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
int main(void)
{
Led_Init();
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
while(1);
}
烧写程序后LED没有任何反应,
系统的时钟可能没做配置哦~单片机可能都没跑起来
不知道你这个问题是否已经解决, 如果还没有解决的话:根据参考资料中的段落0,了解GPIO的原理,需要设置GPIO的模式和状态。
根据参考资料中的段落1、2,使能GPIO所需的时钟。
根据参考资料中的段落3,使用GPIO_Init函数进行GPIO的配置,使用结构体GPIO_InitTypeDef定义GPIO的模式和状态,并传入GPIO_Init函数中进行初始化。
针对LED无法点亮的问题,需要检查是否正确连接LED以及LED正极是否连接到输出端口的引脚,同时也需要检查GPIO配置是否正确。
下面给出参考代码:
#include "stm32f10x.h"
int main(void) {
// 使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 配置PB0引脚为推挽输出
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 点亮LED
GPIO_SetBits(GPIOB, GPIO_Pin_0);
while(1) {
// 在此处添加自己的代码
}
}
需要注意的是,在使用GPIO_Init函数配置GPIO引脚的时候,使用的GPIO_Pin参数需要根据实际连接的引脚进行设置。此处以PB0为例,如果使用的是其他引脚,GPIO_Pin需要按照STM32手册中对应的值进行设置。