3 Bit Binary Code Using Led and HAL Programming With STM 32
by tanushree4587be23 in Circuits > Microcontrollers
127 Views, 1 Favorites, 0 Comments
3 Bit Binary Code Using Led and HAL Programming With STM 32
This is a tutorial blog on how to blink 3 LEDs in 8 different combinations. Since 3 in decimal equates to 8 bits in binary I would make it like a counter. The STM32 Black Pill, paired with the STM32CubeIDE, offers an excellent platform for learning embedded systems development. In this tutorial, we'll explore how to implement a sequence where three LEDs blink in various combinations. These combinations will include patterns such as 100, 010, 001, 110, and others, totaling nine distinct combinations. We'll achieve this using HAL (Hardware Abstraction Layer) programming, which simplifies low-level hardware access and configuration on STM32 microcontrollers. By following this tutorial, you'll gain hands-on experience in configuring GPIO pins, controlling LEDs, and creating custom sequences, laying a solid foundation for more complex embedded systems projects. Let's dive in and start blinking those LEDs in exciting patterns!
Supplies
- STM32 Black Pill
- Bread Board
- Male-Male jumper connector cables
- LED (3 nos.)
- C Type cable
Software:
- STM32CubeIDE
- STM32CubeProgrammer
Making a New Project File in STM32CubeIDE
GOTO---->> File>New>STM32 Project
ADD Configurations to STM 32Board
Search For Component- STM32F401CCU6 click on "Next".
Open Main.c File
GOTO Project and find main.c file
Configuring Your Board
configure your stm32bord as shown
Configure Your Clock
Connect LEDs to the Breadboard
- Place the three LEDs on the breadboard.
- Connect a 220 Ω resistor to each LED's anode (the longer leg).
- Connect the cathode of each LED to a common ground line on the breadboard.
- Connect the anode of each LED (through the resistor) to the corresponding GPIO pin on the STM32 board:
Goto While(1) in Main() and Add Your Code
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,0);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_5,1);
HAL_Delay(500);
- Open your project in Project Explorer and follow this path; Core > Src > main.c.
- In main.c, inside int main() code block go to while(1) to write the code to turn on/ off the LEDs in 8 different combinations.
- Add delay so you are able to see the output.
- Save the file and press on debug.
Copying Path of .elf File From File Explorer
- Right click on the STMProject or use Alt + Shift + W shortcut and press "System Explorer"
- Open the project folder then "Debug" and copy path of .elf file.
Open STMCubeProgrammer and Connect to STM Board Via USB
- Once the file is saved in STM32CubeIDE connect the STM32 board after bootloader.
Check other instructables for more detailed steps.
- Press on "Open file" and select the .elf from the path copied earlier.
- Press on "Download" on the right.
- Go to the "Erasing & programming" from the menu in the left.
- Paste the File path copied earlier.
- Check the "Full chip erase" and "Download file" and press on "Start automatic mode".
Result
- Once the chip is erased, the code will be ready to upload on the chip.
- To upload, disconnect and reconnect the USB-C cable and your LEDs will start blinking.
Test Your Circuit
- Once the program is uploaded, the LEDs should blink in the 8 combinations with a 500 ms delay between each combination.
- If everything works as expected, you've successfully created a simple LED-blinking tutorial with an STM32 microcontroller.
Additional Tips:
- You can change the delay time to increase or decrease the speed of LED transitions.
- Experiment with different LED patterns to create unique blinking sequences.
- Explore other GPIO functionalities, like interrupts, for more complex projects.