Loushangyu2021 2023-03-29 14:01 采纳率: 100%
浏览 18
已结题

蓝桥杯按键结构体数组初始化位置引起的错误

在STM32定义结构体数组的过程中,我发现将结构体数组的位置定义在不一样的位置会有不一样的结果。结构梯定义的是按键的功能,当短按下按键后实现LCD屏幕界面切换。目前的问题是定义的key[0]按键的使用不正常,然而同样操作的key[1]、]key[2]、key[3]
功能正常。下图是问题所在:

img

下面是有问题的代码定义

__IO uint32_t uwadc;
__IO uint32_t uwuart;
__IO uint32_t uwled,uwled2,uwled3;
unsigned char led_time2,led_time3;
char lcd_string[30];
float V;
float H;
int H1=10,H2=20,H3=30;
unsigned char menu=0;
unsigned char level,level_old;

typedef struct
{
    unsigned char judge_sta;
    unsigned char key_sta;
    unsigned char single_flag;
}keys;
keys key[4];
unsigned char uart_len;
unsigned char uart_word;
char uart_string[30];
unsigned char Threshold=1;

后来我发现将keys key[4];放置在其他位置则key[0]的功能正常。

img

img

 typedef          struct
              char  unsicned                 judgesta;
               har  unsigned                  keysta;
   unsigned char single flag
 L)keys;                  正常
 keys      key[4];
   IO   uint32t             uwadc
   IOuint32t                uwuart
   IOuint32t                uwled,uwled2,uwled3;
 unsicned char                ledtime2,ledtime3
 char lcd string[30】
 floatV
 float H
 intH1=10,H2=20,H3=30;
 unsicned char menu=0
 unsigned char level,level old;
 unsigned char uart len
 unsigned char uart word:
 charuart string[30];
 unsigned char Threshold=l

这是可以正常运行的代码;

__IO uint32_t uwadc;
__IO uint32_t uwuart;
__IO uint32_t uwled,uwled2,uwled3;
unsigned char led_time2,led_time3;
char lcd_string[30];
float V;
float H;
int H1=10,H2=20,H3=30;
unsigned char menu=0;
unsigned char level,level_old;

typedef struct
{
    unsigned char judge_sta;
    unsigned char key_sta;
    unsigned char single_flag;
}keys;
unsigned char uart_len;
unsigned char uart_word;
char uart_string[30];
unsigned char Threshold=1;
keys key[4];

请问我的问题出在哪里呢?是软件问题还是硬件问题呢?


#include "main.h"
#include "stdio.h"
#include "i2c.h"
#include "string.h"

ADC_HandleTypeDef hadc2;
TIM_HandleTypeDef htim4;
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC2_Init(void);
static void MX_TIM4_Init(void);
static void MX_USART1_UART_Init(void);

//用户代码区
__IO uint32_t uwadc;
__IO uint32_t uwuart;
__IO uint32_t uwled,uwled2,uwled3;
unsigned char led_time2,led_time3;
char lcd_string[30];
float V;
float H;
int H1=10,H2=20,H3=30;
unsigned char menu=0;
unsigned char level,level_old;

typedef struct
{
    unsigned char judge_sta;
    unsigned char key_sta;
    unsigned char single_flag;
}keys;
unsigned char uart_len;
unsigned char uart_word;
char uart_string[30];
unsigned char Threshold=1;
keys key[4];

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance==TIM4)
    {
        key[0].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
        key[1].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
        key[2].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
        key[3].key_sta=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
        for(int i=0;i<4;i++)
        {
            switch(key[i].judge_sta)
            {
                case 0:
                {
                    if(key[i].key_sta==0)
                    {
                        key[i].judge_sta=1;
                    }
                    else
                    {
                        key[i].judge_sta=0;
                    }
                }
                break;
                case 1:
                {
                    if(key[i].key_sta==0)
                    {
                        key[i].judge_sta=2;
                        key[i].single_flag=1;
                    }
                    else
                    {
                        key[i].judge_sta=0;
                    }
                }
                break;
                case 2:
                {
                    if(key[i].key_sta==1)
                    {
                        key[i].judge_sta=0;
                    }
                }
                break;
                
            }
        }
        
    }
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart==&huart1)
    {
        uart_string[uart_len]=uart_word;
        uart_len++;
        HAL_UART_Receive_IT(&huart1,&uart_word,1);
    }
}
unsigned char i2c_read(unsigned char address)
{
    I2CStart();
    I2CSendByte(0XA0);
    I2CWaitAck();
    
    I2CSendByte(address);
    I2CWaitAck();
    
    I2CStart();
    I2CSendByte(0XA1);
    I2CWaitAck();
    unsigned char val=I2CReceiveByte();
    I2CWaitAck();
    I2CStop();
    return val;
}
void i2c_write(unsigned char address,unsigned char info)
{
    I2CStart();
    I2CSendByte(0XA0);
    I2CWaitAck();
    
    I2CSendByte(address);
    I2CWaitAck();
    
    I2CSendByte(info);
    I2CWaitAck();
    I2CStop();
}
void adc_proc()
{
    if(uwTick-uwadc>=1000)
    {
        uwadc=uwTick;
        HAL_ADC_Start(&hadc2);
        V=HAL_ADC_GetValue(&hadc2)*3.3/4096;
        H=V*100/3.3;
        if(H<=H1)level=0;
        if(H<=H2&&H>H1)level=1;
        if(H>H2&&H<=H3)level=2;
        if(H>H3)level=3;
    }
    
}
void lcd_proc()
{
    if(menu==0)
    {
        sprintf(lcd_string,"   Liquid Level     ");
        LCD_DisplayStringLine(Line2,(unsigned char*)lcd_string);
        sprintf(lcd_string,"   Height:%.0fcm           ",H);
        LCD_DisplayStringLine(Line4,(unsigned char*)lcd_string);
        sprintf(lcd_string,"   ADC:%.2fV            ",V);
        LCD_DisplayStringLine(Line5,(unsigned char*)lcd_string);
        sprintf(lcd_string,"   Level:%d            ",level);
        LCD_DisplayStringLine(Line6,(unsigned char*)lcd_string);
    }
    if(menu==1)
    {
        if(Threshold==1)
        {
            sprintf(lcd_string,"   Parameter Setup     ");
            LCD_DisplayStringLine(Line2,(unsigned char*)lcd_string);
            LCD_SetTextColor(Green);
            sprintf(lcd_string,"   Threshold 1:%dcm           ",H1);
            LCD_DisplayStringLine(Line4,(unsigned char*)lcd_string);
            LCD_SetTextColor(White);
            sprintf(lcd_string,"   Threshold 2:%dcm           ",H2);
            LCD_DisplayStringLine(Line5,(unsigned char*)lcd_string);
            sprintf(lcd_string,"   Threshold 3:%dcm           ",H3);
            LCD_DisplayStringLine(Line6,(unsigned char*)lcd_string);
        }
        else if(Threshold==2)
        {
            sprintf(lcd_string,"   Parameter Setup     ");
            LCD_DisplayStringLine(Line2,(unsigned char*)lcd_string);
            sprintf(lcd_string,"   Threshold 1:%dcm           ",H1);
            LCD_DisplayStringLine(Line4,(unsigned char*)lcd_string);
            LCD_SetTextColor(Green);
            sprintf(lcd_string,"   Threshold 2:%dcm           ",H2);
            LCD_DisplayStringLine(Line5,(unsigned char*)lcd_string);
            LCD_SetTextColor(White);
            sprintf(lcd_string,"   Threshold 3:%dcm           ",H3);
            LCD_DisplayStringLine(Line6,(unsigned char*)lcd_string);
        }
        else if(Threshold==3)
        {
            sprintf(lcd_string,"   Parameter Setup     ");
            LCD_DisplayStringLine(Line2,(unsigned char*)lcd_string);
            sprintf(lcd_string,"   Threshold 1:%dcm           ",H1);
            LCD_DisplayStringLine(Line4,(unsigned char*)lcd_string);
            sprintf(lcd_string,"   Threshold 2:%dcm           ",H2);
            LCD_DisplayStringLine(Line5,(unsigned char*)lcd_string);
            LCD_SetTextColor(Green);
            sprintf(lcd_string,"   Threshold 3:%dcm           ",H3);
            LCD_DisplayStringLine(Line6,(unsigned char*)lcd_string);
            LCD_SetTextColor(White);
        }
    }
}
unsigned char t;
void key_proc()
{
    if(key[0].single_flag==1)
    {
        if(menu==0)
        {
            menu=1;
        }
        else
        {
            menu=0;
            Threshold=1;
            i2c_write(0,H1);
            HAL_Delay(10);
            i2c_write(1,H2);
            HAL_Delay(10);
            i2c_write(2,H3);
            HAL_Delay(10);
        }
        key[0].single_flag=0;
    }
    
    if(key[1].single_flag==1&&menu==1)
    {
        Threshold++;
        if(Threshold>3)
        {
            Threshold=1;
        }
        key[1].single_flag=0;
    }
    if(key[2].single_flag==1&&menu==1)
    {
        if(Threshold==1)
        {
            H1=H1+5;
            if(H1>95)
            {
                H1=95;
            }
        }
        
            if(Threshold==2)
        {
            H2=H2+5;
            if(H2>95)
            {
                H2=95;
            }
        }
            if(Threshold==3)
        {
            H3=H3+5;
            if(H3>95)
            {
                H3=95;
            }
        }
        key[2].single_flag=0;
    }
    if(key[3].single_flag==1&&menu==1)
    {
        if(Threshold==1)
        {
            H1=H1-5;
            if(H1<=0)
            {
                H1=0;
            }
        }
        
            if(Threshold==2)
        {
            H2=H2-5;
            if(H2<=0)
            {
                H2=0;
            }
        }
        
            if(Threshold==3)
        {
            H3=H3-5;
            if(H3<=0)
            {
                H3=0;
            }
        }
        key[3].single_flag=0;
    }
}
unsigned char judge_uart()
{
    if(uart_len==1)
    {
        if(uart_string[0]=='C')return 1;
        else if(uart_string[0]=='S')return 2;
        else HAL_UART_Transmit(&huart1,"ERROR\n",6,1000);
    }
    else
    {
        return 0;
    }
}
void uart_proc()
{
    if(uwTick-uwuart<=100)return;
    uwuart=uwTick;
    if(judge_uart()==1)
    {
        char text[30];
        sprintf(text,"C:H%0.0f+L%d\r\n",H,level);
        HAL_UART_Transmit(&huart1,text,strlen(text),1000);
        led_time3=1;
    }
    if(judge_uart()==2)
    {
        char text[30];
        sprintf(text,"S:TL%d+TM%d+TH%d\r\n",H1,H2,H3);
        HAL_UART_Transmit(&huart1,text,strlen(text),1000);
        led_time3=1;
    }
    uart_len=0;
}
void uart_output()
{
    if(level<level_old)
    {
        led_time2=1;
        level_old=level;
        char text[30];
        sprintf(text,"A:H%0.0f+L%d+D\r\n",H,level);
        HAL_UART_Transmit(&huart1,text,strlen(text),1000);
        
    }
    else if(level>level_old)
    {
        led_time2=1;
        level_old=level;
        char text[30];
        sprintf(text,"A:H%0.0f+L%d+U\r\n",H,level);
        HAL_UART_Transmit(&huart1,text,strlen(text),1000);
    }
}
void led_proc()
{
    if(uwTick-uwled>=1000)
    {
        HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_8);
        HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
        HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);
        uwled=uwTick;
    }
    if(uwTick-uwled2>=200&&led_time2>0)
    {
        uwled2=uwTick;
        HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_9);
        HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
        HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);
        led_time2++;
        if(led_time2>10)
        {
            HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,1);
            HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
            HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);
            led_time2=0;
        }
    }
    if(uwTick-uwled3>=200&&led_time3>0)
    {
        uwled3=uwTick;
        HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_10);
        HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
        HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);
        led_time3++;
        if(led_time3>10)
        {
            HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,1);
            HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
            HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);
            led_time3=0;
        }
    }
}

int main(void)
{
  
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_ADC2_Init();
  MX_TIM4_Init();
  MX_USART1_UART_Init();
    
    LCD_Init();
    LCD_Clear(Black);
    LCD_SetBackColor(Black);
    LCD_SetTextColor(White);
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,1);
    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);
    level=level_old;
//    i2c_write(0,30);
//    HAL_Delay(10);
//    
//    i2c_write(1,50);
//    HAL_Delay(10);
//    
//    i2c_write(2,70);
//    HAL_Delay(10);
    
    H1=i2c_read(0);
    HAL_Delay(10);
    
    H2=i2c_read(1);
    HAL_Delay(10);
    
    H3=i2c_read(2);
    HAL_Delay(10);
    HAL_UART_Receive_IT(&huart1,&uart_word,1);
    HAL_TIM_Base_Start_IT(&htim4);
  while (1)
  {
        adc_proc();
        lcd_proc();
        key_proc();
        uart_proc();
        uart_output();
        led_proc();
  }
}

  • 写回答

2条回答 默认 最新

  • 赵4老师 2023-03-29 14:18
    关注

    你这是典型的越界访问踩内存问题。
    比如
    int a;
    int b[3];
    int c;
    如果你代码中修改了b[-1]或b[3],a或者c的值就会被修改。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月29日

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。