新手求教关于STM32中用结构体指针的问题!
时间:10-02
整理:3721RD
点击:
- void LED_Init(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure; //定义一个初始化GPIO的一个结构体GPIO_InitStructure
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE); //使能PA,PD端口时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0-->PA.8 端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOA.8
- GPIO_SetBits(GPIOA,GPIO_Pin_8); //PA.8 输出高
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1-->PD.2 端口配置, 推挽输出
- GPIO_Init(GPIOD, &GPIO_InitStructure); //推挽输出 ,IO口速度为50MHz
- GPIO_SetBits(GPIOD,GPIO_Pin_2); //PD.2 输出高
-
- //一下为自己修改的结构体指针操作,有问题
- /*GPIO_InitTypeDef *GPIO_InitStructure; //定义一个初始化GPIO的一个结构体指针 *GPIO_InitStructure
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE); //使能PA,PD端口时钟
-
- GPIO_InitStructure->GPIO_Pin = GPIO_Pin_8; //LED0-->PA.8 端口配置
- GPIO_InitStructure->GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure->GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOA, GPIO_InitStructure); //根据设定参数初始化GPIOA.8
- GPIO_SetBits(GPIOA,GPIO_Pin_8); //PA.8 输出高
-
- GPIO_InitStructure->GPIO_Pin = GPIO_Pin_2; //LED1-->PD.2 端口配置, 推挽输出
- GPIO_Init(GPIOD, GPIO_InitStructure); //推挽输出 ,IO口速度为50MHz
- GPIO_SetBits(GPIOD,GPIO_Pin_2); //PD.2 输出高
- */
- }
关于结构体指针的问题,我用了下面被注释代码,编译能通过,就是烧到板子上没啥效果,实在找不出代码那里有问题,大家帮帮忙,谢了
配置问题,GPIO_InitStructure->GPIO_Pin 。你定义的结构体里有没有这个变量。
新手就不要乱用用指针了,按照上面直接用结构体就行了。
指针使用之前是必须要初始化的,也就是要说明你要使用的指针是指向哪个内存块的,
比如:
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef *pGPIO_InitStructure = &GPIO_InitStructure;
这里定义了一个叫pGPIO_InitStructure的GPIO_InitTypeDef指针,该指针指向的是新开辟的名叫GPIO_InitStructure的内存,该内存存放的是一个GPIO_InitTypeDef结构体。
如果只是一句
GPIO_InitTypeDef *GPIO_InitStructure;
请问 *GPIO_InitStructure指向哪里?它随机指向一块内存,然后不管原来这块内存是用来干什么的,把它当做GPIO_InitTypeDef结构体读取或写入数据这样肯定是会出错的
你先查一下->和. 的区别。
哦哦~谢谢了!
