0
\$\begingroup\$

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?

\$\endgroup\$
  • 2
    \$\begingroup\$ What are you struggling with? Hardware or software? What have you done so far? \$\endgroup\$ – Colin Oct 3 '18 at 10:12
  • \$\begingroup\$ 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 \$\endgroup\$ – cath001 Oct 3 '18 at 10:16
  • \$\begingroup\$ 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. \$\endgroup\$ – Colin Oct 3 '18 at 10:28
  • \$\begingroup\$ 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. \$\endgroup\$ – Lundin Oct 4 '18 at 8:49
  • \$\begingroup\$ All number are binary for the hardware including chars and strings \$\endgroup\$ – P__J__ Oct 7 '18 at 14:46
1
\$\begingroup\$
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.

\$\endgroup\$
0
\$\begingroup\$

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);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.