0

I create function for manipulating ports for drive 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);
    }

But in main() when use this function second time, program collapsed, and in debug mode all resisters change to "target not available". I use union for accessing bites of input character value. Is it needed to clear or destroy union after use or it will destroy by end of scope?why second time of send_8_bit_command function cause program's hang?

send_8_bit_command(0x20);
send_8_bit_command(0xE);//This line program collapsed.
  • The problem is in something that is not included in your question, maybe the definitions of the constants (D1, D2 etc) that you didn't include or the function Delay_us. – Tom V Nov 28 '21 at 16:27

1 Answers1

0

I fixed it by using a delay between two functions:

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