Illustration of a laptop connected to an Arduino board

Learn Microcontroller Programming Professionally Leave a comment

Importance of Programming for Electronics Engineers

Nowadays most of the electronic circuits use micro-controllers and processors that need good programming level. But you as an electronic engineer can’t learn it in the university. Maybe you say it’s computer engineers’ duties, but they aren’t familiar with hardware limitations; imagine if a computer engineer wants to write a program that can use only 512 bit of RAM. So the best solution is the electronic engineers that have a good programming skills and know how to code professionally.

Here we want to challenge your programming skills, Are you ready?  

Challenge

Nowadays that using ARM micro-controllers spread out rapidly, cause a big change in hardware design process, i.e. a 32-bits processor that has enough RAM & FLASH memory and presents high speed beside low consumer power. You might did not use Z80 or 8086 with hardness and complexity that Assembly calculation with 32-bits number made it like a nightmare, or even number calculation with whether fixed or floating point; today, technology changes this nightmare into a sweet dream, But technology still doesn’t solve the programming challenge.

In this article we want you to write a program (for 32-bits ARM micro) that counts how many one a 32-bit variable has E.g.

Writing this program is simple but in different ways, that we want to discuss. A Routine Program that Everybody Writes:

First of all, lets explain the program function:

There is a loop for every variable’s bit and check if it is one or not and increase sum as one detected. so why this program has a poor performance? First and the most important reason is loop (it seems to be necessary), second the calculation such as compare, bit-shift, ADD and the logical operation AND that needs to be calculated in every loop. We recommend you to read article” It’s not micro-controller fault, it’s about programming” to know how to improve the program performance.

After A Little Bit of Thinking You Write:

As you see we deleted shift and compare operation, in fact they are in the loop; now we have better performance but not the best. Do you have any idea?

A Program that A Hardware Engineer with Micro-controller’s Knowledge Write

Still there is loop but we deleted the shift and logical AND that means a faster program! What happen and How do hardware help us?
Cortex-m micro-controller series has an option named Bit-Banding; it means micro. allowed to access the SRAM memory bit by bit – both read and write. But how is it possible? The following picture can help you.

 

Diagram of STM32 memory aliasing showing 32MB alias region mapping
 Bit-banding: 32MB alias region allows direct bit manipulation of the 1MB SRAM block (0x20000000–0x200FFFFF) via alias addresses.

As you see, every bit of SRAM memory mapped with another address, that you can access every bit by reading or writing its address.

The Perfection One

We have been check some simple solution so far, using programming and hardware knowledge to perform the result, but can we perform it more? Of course, look at the following:

So we deleted the FOR loop and use the table that speed up the result a lot, in fact BitsSetTable256 table consists of a number’s bits from 0 to 255, means a bite:

This is a challenge for you how we made such a table with 4 line #define. Every 32 bits consist of 4 bites, if we calculate summation of every bite’s bits the result will be variable’s bits. This is the rest of code:

Final Challenge

So we can use this simple program! But how does it work? This isn’t a method that everyone can use; beside of writing such a program, understanding this program is not anyone’s job. Can anyone say how does it work?

FAQ – Learn Microcontroller Programming Professionally

Why is programming essential for electronics engineers?

Modern electronic systems rely on microcontrollers and processors. Efficient programming allows engineers to work within strict hardware limits such as low RAM, limited flash, and power consumption.

What is the goal of the bit-counting challenge?

The challenge is to count the number of set bits (1s) in a 32-bit variable using the fastest and most efficient method possible on a 32-bit ARM microcontroller.

Why is the basic loop-based solution slow?

It requires looping through all 32 bits and performing shift, AND, compare, and add operations in each iteration, which increases execution time on low-frequency microcontrollers.

How does shifting the data inside the loop improve performance?

By shifting the variable itself, explicit bit indexing and comparison operations are removed, reducing the total number of instructions per loop iteration.

What is Bit-Banding and how does it help?

Bit-banding is a Cortex-M feature that maps each SRAM bit to a unique address, enabling direct bit access without masking or shifting, which improves execution speed.

Why is the lookup table method faster?

It removes loops entirely by using a precomputed table that stores the number of set bits for every possible byte value (0–255).

How does the final bitwise algorithm work?

It uses parallel bit counting (SWAR technique) by grouping bits with masks, shifts, and arithmetic operations to compute the total number of set bits in minimal steps. 

Leave a Reply