embeded/Cortex-M3 STM

stm32 port 전체 한번에 출력 변경하기

구차니 2026. 6. 22. 12:21

AVR 때는 자주 쓴거 같은데

stm32 되면서 함수를 통해 1개 핀에 대해서만 바꾸다 보니 불편해서 찾아보는데

머.. 결국은 레지스터 건드릴 수 밖에 없는 듯

 

ODR 값 읽고 마스킹 후에 한번에 갈아 치우는게 나을 듯

If you want the whole port (all 16 pins) get the value, then you simply write it to the ODR register:
GPIOB->ODR = 0xFFFF; //writing 1/0 in a bit position, 
sets/resets that bit. ALL BITS ARE AFFECTED no matter the value your write in.

If you want to set some pins, but not altering the other pins, with one instruction:
GPIOB->BSRR = 0x00F; //writing 1 in a bit position, 
sets that bit leaves the others as they are

If you want to reset some pins, but not altering the other pins, with one instruction:
GPIOB->BRR = 0x00F; //writing 1 in a bit position, 
clears that bit and leaves the others as they are

If you want a "portion" of a GPIO port to get an specific value, this method is 2 instructions instead of "read-change-write":
GPIOB->BRR = 0x00F; //clear the first 4 bits
GPIOB->BSRR = 0x00A; //set the desired bits in that 4-bit field

 

 ODR 정도는 low level 함수로 존재하나 보다.

using LL API:

LL_GPIO_WriteOutputPort(GPIOA,x);

[링크 : https://electronics.stackexchange.com/questions/683401/can-i-write-a-8-bit-or-16-bit-data-to-the-outport-of-the-stm32-directly]

 

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;
}

[링크 : https://stackoverflow.com/questions/48365156/stm32f4-write-read-16bits-into-two-8-bit-gpio-port]