STM32点灯-关于GPIO_Init问题

#include "stm32f10x.h"                  // Device header
//#include "Delay.h"

int main(void)
{
	//1.Use RCC to start the GPIO's CLK
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	//2.Use GPIO_init to initial the GPIO
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	//	GPIO_InitTypeDef *temp;
	//	temp->GPIO_Mode = GPIO_Mode_Out_PP;
	//	temp->GPIO_Pin = GPIO_Pin_0;
	//	temp->GPIO_Speed = GPIO_Speed_50MHz;
	//	GPIO_Init(GPIOA, temp);
	
	//3.use input/output function to contrl the GPIO
	GPIO_ResetBits(GPIOA, GPIO_Pin_0);
	while(1)
	{
		
	}
	
}

如上图所示,为什么我用注释内的代码,灯就不亮呢。。他们本质不是一样的吗,传入一个结构体的指针。

懂了,因为第二种写法只是定义了一个指向结构体的指针,但是它还没指向任何一个结构体。。

尝试改成如下代码,验证这个想法成功。

#include "stm32f10x.h"                  // Device header
//#include "Delay.h"

int main(void)
{
	//1.Use RCC to start the GPIO's CLK
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	//2.Use GPIO_init to initial the GPIO
	GPIO_InitTypeDef GPIO_InitStruct;
//	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
//	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	GPIO_InitTypeDef *temp = &GPIO_InitStruct;
	temp->GPIO_Mode = GPIO_Mode_Out_PP;
	temp->GPIO_Pin = GPIO_Pin_0;
	temp->GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, temp);
	
	//3.use input/output function to contrl the GPIO
	GPIO_ResetBits(GPIOA, GPIO_Pin_0);
	while(1)
	{
		
	}
	
}

你可能感兴趣的:(stm32,嵌入式硬件,单片机)