f4-discoveryLED点不亮,的奇葩原因
板子:STM32F4-DISCOVERYLED部分原理图:
函数调用:
============main.c===============
#include "stm32f4xx.h"
#include "bsp_led.h"
int main(void)
{
LED_GPIO_Config();
//GPIO_ResetBits(LED_R_PORT,LED_R_PIN);
GPIO_SetBits(LED_R_PORT,LED_R_PIN);
while(1)
{
};
}
============bsp_led.h===============
#ifndef __BSP_LED_H__
#define __BSP_LED_H__
//#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#define LED_R_PIN GPIO_Pin_13
#define LED_R_PORT GPIOG
#define LED_R_CLK RCC_AHB1Periph_GPIOG
void LED_GPIO_Config(void);
#endif
=============bsp_led.c==============
#include "bsp_led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef* GPIO_Init_Pin;
//时钟
RCC_AHB1PeriphClockCmd(LED_R_CLK,ENABLE);
GPIO_Init_Pin->GPIO_Pin = LED_R_PIN;
GPIO_Init_Pin->GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init_Pin->GPIO_Speed = GPIO_Fast_Speed;
GPIO_Init_Pin->GPIO_OType = GPIO_OType_PP;
GPIO_Init_Pin->GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(LED_R_PORT,GPIO_Init_Pin);
}
运行keil没编译出错的提示,但是烧到板子就是点不亮PG13对应的LED灯。
该问题的出现是因为指针并没有在使用前初始化,必须要
GPIO_InitTypeDef *GPIO_Init_Pin;
GPIO_InitTypeDef SomeOne;
GPIO_Init_Pin = &SomeOne;
这样子才能用正常使用。
=============bsp_led.c==============
#include "bsp_led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef* GPIO_Init_Pin;
//时钟
RCC_AHB1PeriphClockCmd(LED_R_CLK,ENABLE);
GPIO_Init_Pin->GPIO_Pin = LED_R_PIN;
GPIO_Init_Pin->GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init_Pin->GPIO_Speed = GPIO_Fast_Speed;
GPIO_Init_Pin->GPIO_OType = GPIO_OType_PP;
GPIO_Init_Pin->GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(LED_R_PORT,GPIO_Init_Pin);
}
定义了指针而没有分配对象,当然就不会起作用了
下面 的直接定义实例,系统自动分配空间,所以运行就正确了
这是C语言 的基本要求 gpio_mode 这个寄存器没法修改是怎么回事?按照上面的例程,应该会修改才对哇
哪位大神知道原因啊? 先用万用表响档量一下 我直接mbed
DigitalOut myled(LED1);
myled=1;
就亮了 手动改也不行吗 你的系统时钟起来了嘛? 高二毛 发表于 2016-10-21 08:26
你的系统时钟起来了嘛?
时钟起来了。跟正常能点亮的程序对比过。结果发现这个问题。 ynwscfsfi 发表于 2016-10-20 22:34
手动改也不行吗
没有试过呢。 anywill 发表于 2016-10-20 22:04
我直接mbed
DigitalOut myled(LED1);
myled=1;
刚刚入门,还没接触到这个的介绍。。。 楼主把工程传上来看看