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)
	{
		
	}
	
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值