# STM32 USART Receive Binary Number

I'm new to embedded systems so apologies if this is a stupid question.

I am trying to implement a binary counter in the form of 8 LEDs. I want to send an 8-bit binary number through USART and have the 8 LEDs display the number (led off if bit is 0, led on when bit is 1).

What would be the best method to do this?

• What are you struggling with? Hardware or software? What have you done so far? – Colin Oct 3 '18 at 10:12
• I'm struggling with the software. So far ive initialised and configured the GPIO's for the LED's and USART. I have also written a function to turn on/off the LED's based on whether there is a 1 or 0 present. I am just struglging with how to actually receive the data from the PC using USART – cath001 Oct 3 '18 at 10:16
• Read the USART section of the reference manual, essentially you need to wait for the rxne bit of the USART status register to be high, then you can read the received byte from the USART data register. – Colin Oct 3 '18 at 10:28
• Not entirely sure what you mean, send data from PC over UART? Otherwise, SPI would be quite suitable, since you can connect it directly to a shift register, which in turn can drive the LEDs. – Lundin Oct 4 '18 at 8:49
• All number are binary for the hardware including chars and strings – P__J__ Oct 7 '18 at 14:46

I want to send an 8-bit binary number through USART and have the 8 LED's display the number (led off if bit is 0, led on when bit is 1). What would be the best method to do this?

The built-in UART already decodes the serial signal into bytes. Put another way, the hardware delivers you the byte you want directly. Your question then comes down to how to drive 8 LEDs from the 8 separate bits of a byte somewhere in the processor.

The easiest solution is to use 8 output pins. It would be particularly easy if these are all driven by the same output port register internally. In that case, you simply write the byte to the port register.

If the output lines are scattered among different port registers, then in the worst case you set each bit individually. Test the LSB of the input byte, then set or clear the port bit for that LED. Then test bit 1 and set or clear the port bit for the next LED, etc.

The simplest electrically is to drive each LED directly from its output pin:

If the LED drops 2.1 V (typical for common green), then it will have about 5 mA thru it when the digital signal is high. That will be plenty bright enough with a modern LED to easily see in a typical office environment.

It's not clear to me if you want the STM32 to be the receiver or sender, but I assume the receiver.

First find out first how to receive the value you want to receive. There are plenty of examples since UART is about the most used peripheral.

Assume you have the byte stored from UART in a byte in SRAM of your STM32, you can use a simple loop to iterate over the bits.

for (int bit = 7; bit >= 0; bit--)
{
// Set GPIO 'bit' on or off
}


To set a GPIO on or off find out how to do this, e.g. in HAL you should use HAL_GPIO_WritePin.

You can attach the LEDs with a resistor to eight pins and set the LED accordingly.

To set the GPIO bit on or off you can create an array to save the port/pin numbers, totalling 8 items, where each item is the port/pin for that bit, e.g.

GPIO_TypeDef gpio_ports
{
GPIOA, GPIOA, GPIOA, GPIOA, GPIOB, GPIOB, GPIOC, GPIOB
};

uint16_t gpio_pins
{
GPIO_PIN_0, GPIO_PIN_2, GPIO_PIN_0, GPIO_PIN_2,
GPIO_PIN_5, GPIO_PIN_1, GPIO_PIN_6, GPIO_PIN_4,
}


Bit 0 will be mapped to GPIO pin A0, bit 7 to B4.

To set the bits use

for (int bit = 7; bit >= 0; bit--)
{
SetBit(bit, value & (1 << bit));
}

SetBit(int bit, int status)
{
HAL_GPIO_WritePin(gpio_ports[bit], gpio_pins[bit],
status > 0 ? GPIO_PIN_SET : GPIO_PIN_RESET);
}