【问题标题】:second use of function cause to stop program第二次使用函数导致程序停止
【发布时间】:2022-01-05 17:07:14
【问题描述】:

我创建了用于操作驱动 1602 LCD 端口的函数。

  #include "delay.h"
    #include <stdint.h>
    #include "stm32f0xx.h"
    
    #ifndef LCD1602A_H_
    #define LCD1602A_H_
    #define DATA_PORT GPIOA
    #define CONTROL_PORT GPIOA
    //Set pins
    #define RS  6
    #define RW  7
    #define E   9
    #define D4  10
    #define D5  0
    #define D6  1
    #define D7  2    
    void send_8_bit_command(unsigned char command){     
        
            union DATA{
                struct {
                    unsigned char Bit0:1;
                    unsigned char Bit1:1;
                    unsigned char Bit2:1;
     

            unsigned char Bit3:1;
                unsigned char Bit4:1;
                unsigned char Bit5:1;
                unsigned char Bit6:1;
                unsigned char Bit7:1;
            }Bits;
            unsigned char input;
        }data;
        data.input=command;
        GPIOA->BSRR|=1<<(16+RS);
        GPIOA->BSRR|=1<<(16+RW);
    
        GPIOA->BSRR|=1<<(16+D7);
        GPIOA->BSRR|=1<<(16+D6);
        GPIOA->BSRR|=1<<(16+D5);
        GPIOA->BSRR|=1<<(16+D4);
        GPIOA->BSRR|=1<<(E);
        Delay_us(1);
        if(data.Bits.Bit7==1){GPIOA->BSRR|=1<<(D7);}else{GPIOA->BSRR|=1<<(16+D7);}
        if(data.Bits.Bit6==1){GPIOA->BSRR|=1<<(D6);}else{GPIOA->BSRR|=1<<(16+D6);}
        if(data.Bits.Bit5==1){GPIOA->BSRR|=1<<(D5);}else{GPIOA->BSRR|=1<<(16+D5);}
        if(data.Bits.Bit4==1){GPIOA->BSRR|=1<<(D4);}else{GPIOA->BSRR|=1<<(16+D4);}
        GPIOA->BSRR|=1<<(16+E);
        Delay_us(1);
    
        GPIOA->BSRR|=1<<(16+D7);
        GPIOA->BSRR|=1<<(16+D6);
        GPIOA->BSRR|=1<<(16+D5);
        GPIOA->BSRR|=1<<(16+D4);
        GPIOA->BSRR|=1<<(E);
        Delay_us(1);
        if(data.Bits.Bit3==1){GPIOA->BSRR|=1<<(D7);}else{GPIOA->BSRR|=1<<(16+D7);}
        if(data.Bits.Bit2==1){GPIOA->BSRR|=1<<(D6);}else{GPIOA->BSRR|=1<<(16+D6);}
        if(data.Bits.Bit1==1){GPIOA->BSRR|=1<<(D5);}else{GPIOA->BSRR|=1<<(16+D5);}
        if(data.Bits.Bit0==1){GPIOA->BSRR|=1<<(D4);}else{GPIOA->BSRR|=1<<(16+D4);}
        GPIOA->BSRR|=1<<(16+E);
        Delay_us(1);
    }

但是在 main() 中,当第二次使用这个函数时,程序崩溃了,并且在调试模式下,所有的电阻都变为“目标不可用”。 我使用联合来访问输入字符值的片段。使用后是否需要清除或销毁union,否则会在作用域结束时销毁?为什么第二次send_8_bit_command函数会导致程序挂起?

send_8_bit_command(0x20);
send_8_bit_command(0xE);//This line program collapsed.

【问题讨论】:

  • 问题出在您的问题中未包含的内容中,可能是您未包含的常量(D1、D2 等)的定义或函数 Delay_us。

标签: stm32 bare-metal stm32f0


【解决方案1】:

我通过在两个函数之间使用延迟来修复它:

send_8_bit_command(0x20);
delay_us(10);
send_8_bit_command(0xE);//Program live again!

【讨论】:

  • 您似乎刚刚隐藏了一些未知问题(延迟功能?)。它可能会在其他地方弹出,或者在不同的条件下弹出。
  • 是的,当然,它弹出了!我尝试基本修复。
猜你喜欢
  • 2019-08-15
  • 2020-09-30
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多