Search code examples
stm32gpio

stm32f4 write/read 16bits into two 8-bit gpio port


I try to write 16 bit data into two selected 8-bits gpio ports . I must split data for LSB and MSB :

void LCD_write_command(uint16_t cmd) {
    GPIOD->ODR = cmd & 0x00ff; //lsb
    GPIOA->ODR = (GPIOA->ODR & 0x00ff) | (cmd >> 8); //msb
}

and read data :

uint16_t LCD_read_data(void) {
    (here is instruct gpio as input)
    volatile uint16_t data = 0;
    data  = (uint16_t)GPIOD->IDR & 0x00ff; //lsb
    data |= (uint16_t)GPIOA->IDR << 8 ; // msb
    (here is instruct gpio as output)
    return data;
}

When i use one 16bit gpio to write and read everything is fine:

 void LCD_write_command(uint16_t cmd) {
    GPIOD->ODR = cmd & 0xffff; 
}
uint16_t LCD_read_data(void) {
    volatile uint16_t data = 0;
    data  = (uint16_t)GPIOD->IDR & 0xffff; 
    return data;
}

I relay dont know what im missing.


Solution

  • wtite_bits(uint16_t cmd)
    {
        uint32_t data = GPIOA -> ODR;
    
        data &= ~(0x1fff);
        data |= cmd & 0x1fff;
        GPIOA -> ODR = data;
    
        data = GPIOB -> ODR;
        data &= ~(0x0007);
        data |= (cmd & 0x8fff) >> 13;
        GPIOB -> ODR = data;
    }
    

    preserve other bits in the register