歌者長門 2023-10-16 18:14 采纳率: 0%
浏览 6

使用结构体指针对结构体中的变量赋值再运行代码,结果单片机无反应无应答的问题

使用结构体指针对结构体中的变量赋值再运行代码,结果单片机无反应的问题。

关键点:
(1)结构体定义如下:

img

(2)断点调试的结果如下:分为正常结构体变量赋值和使用结构体指针赋值,二者是一样的赋值,没有区别:
结构体变量:

img

结构体指针:

img


(3)完整代码:

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void)
{

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//    被注释的这块使用的是结构体指针,结果无法使得单片机产生应答
//    GPIO_InitTypeDef* GPIO_InitStructure;
//    GPIO_InitStructure->GPIO_Mode = GPIO_Mode_Out_PP;
//    GPIO_InitStructure->GPIO_Pin = GPIO_Pin_0;
//    GPIO_InitStructure->GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(GPIOA, GPIO_InitStructure);
           
    while(1)
    {
        GPIO_ResetBits(GPIOA, GPIO_Pin_0);
        Delay_ms(500);
        GPIO_SetBits(GPIOA, GPIO_Pin_0);
        Delay_ms(500);    
            
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET);
        Delay_ms(500);
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET);
        Delay_ms(500);
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)0);
        Delay_ms(500);
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)1);
        Delay_ms(500);
    }
}



(4)上机测试结果:结构体变量单片机有响应,结构体指针没有。
结构体变量:

img

结构体指针:

img

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2023-10-16 20:44
    关注

    指针需要指向一块内存空间:GPIO_InitStructure = (GPIO_InitTypeDef*)malloc(sizeof(GPIO_InitTypeDef));

    评论

报告相同问题?

问题事件

  • 创建了问题 10月16日