Terms of service
Brokking
 
 
.net
Let's keep it simple
 

 
   Home
   Projects
 

   Articles
 

   Donation
         
 
 
 
 

 
Brokking.net - Video: Getting started with the STM32F103C8T6 microcontroller - STM32 via Arduino

Getting started with the STM32F103C8T6 microcontroller - STM32 via Arduino

This page contains the full script that I used for making this video.

Hello and welcome to another one of my videos. In this video we will have a look at this STM32F103 microcontroller board.

The goal of this video is to get you started as easy as possible so that you can upload your own programs by using the Arduino IDE.

And of course I will perform some speed tests and compare its performance with an Arduino Uno. After all this is a 32bit microcontroller that runs on 72MHz. So it should be really fast in comparison to the Arduino Uno…… And it is…. As long as you stay aware of the pitfalls.

So let's get started and see what is needed to get your code up and running.

First of course you need the STM32F103C8T6 microcontroller board and a FTDI programmer used for programming the Arduino Pro Mini. The price for both items is around 5 dollars and you can find links to these products in the description below.

Because we will use the Arduino IDE as the base for writing the code we need to download and unpack it first. So go to the Arduino website and download the Arduino IDE zip file for non admin install. As you can see I will be using version 1.8.3 for this video.

After downloading unzip the files to your hard drive and test if the FTDI programmer is correctly detected by your Windows hardware manager. If not you can find the drivers for the FTDI programmer in the Arduino folder that you just downloaded.

Open the Arduino IDE, go to tools, board and click boards manager.

Click on the 32-bit ARM Cortex-M3 and click install.

When the software is completely installed close the Arduino IDE and go to this Arduino_STM32 page on GitHub. I will also put the link in the description.

This is an Arduino add-on solution for the STM32 microcontrollers that is created and maintained by Roger Clark.

Click on download zip-file and wait for it to download.

Unpack the Arduino_STM32_master directory and place the complete directory in the hardware directory of the Arduino IDE.

And basically that's it. The Arduino IDE is now ready to program the STM32 microcontroller.

Next thing that you need to do is to connect the FTDI programmer to the STM32 microcontroller board.

Please note that the STM32 is a 3.3V microcontroller. Despite the fact that some pins are 5V tolerant, connecting it to 5V for power supply will definitely destroy it. So make sure that you connect the 5V from the FDI programmer to the 5V input of the STM32 board.

On the back side of the board you can see that there is a small 3.3 volt voltage regulator that will provide the 3.3V to the microcontroller.

Please note that this voltage regulator cannot provide a whole lot of current. So if you want to connect a lot of LED's the voltage regulator can easily be overloaded.

So, connect ground to ground, 5V to 5V

connect RX from the FTDI programmer to pin A9 on the STM32 board.

And finally connect the TX from the FTDI programmer to pin A10.

Insert the USB cable from the computer to the FTDI programmer and the LED on the board should start to flash. This of course depends on the test program that was installed by the manufacturer.

And now you are ready to upload your first program.

Open the Arduino IDE

Go to tools, boards and select the STM32F103C series

Go to variant and select the 64k flash version

Go to CPU speed and select 72MHz

Go to upload method and select serial

And finally select the com port of your FTDI programmer

Now go to files, examples and scroll down for the STM32F103C examples. Find the blink sketch and open it.

The LED on this board is connected to pin C13 as you can see on this pinout diagram. So you need to change the PB1 to PC13 to get the LED on this board to blink. And after this change the program is ready for upload.

Before each upload you need to switch the top jumper to the right and press the reset button. This way the microcontroller enters the program mode.

Now click upload in the Arduino IDE and the program should be uploaded to the STM32 microcontroller. After that the program is executed and the LED should start to flash.

To upload the program again you only need to press the reset button. The microcontroller enters the program mode and you can click upload again.

When you want to execute the program you need to switch the top jumper back to its original 0 position and press the reset button.

And that's it. We just uploaded the first program to the STM32 microcontroller.

Now that we have everything running it is time for some speed comparison. For this I will use the Arduino Uno as a competitor that will be wiped away by the STM32. After all the STM32 is a 32bit microcontroller that runs on 72MHz. The Arduino Uno is an 8 bit microcontroller that runs on 16MHz.

Judging by the clock speed alone the STM32 should be at least 4.5 times faster than the Arduino Uno.

So I made this program for the Arduino Uno that will execute 2000 float calculations with the use of this for loop. Every loop the f_a float variable is divided by 1.00014.

And at the end the result is printed via the serial output.

To measure the calculation time, output 13 is set high right before the start of the calculations. And directly after the calculations output 13 is set low again. This is done via direct port manipulation because this method is much faster than using digitalWrite.

This way it's possible to neglect the switching time of the digital output. I already explained this in another video that I will link in the description.

Why I'm switching over from the awesome Arduino IDE to Atmel Studio.

When I run this program we can see on the oscilloscope that the pulse length is approximately 61.2 milliseconds.

And here you can see the calculated result.

This is the same code for the STM32.

The only difference is the registers that is used for the direct port manipulation.

In the setup you can see that I will be using port PB5 for creating the pulse.

In the datasheet of the STM32F100 series we can see that the output can be changed with the port set and reset register. By setting the B-set-5 bit in the GPIOB_BSRR register, output B5 is set high.

That is done in this line of the code.

By setting the B-reset-5 bit in the GPIOB_BSRR register output B5 is set low.

And that is done in this line of the code.

When I upload this code I can measure the pulse time as I did with the Arduino Uno.

As you can see its approximately 25.6 milliseconds.

And here you can see the calculated result.

Now wait a minute, 25.6 milliseconds? When dividing 61.2 by 25.6 we get an approximated speed increase of only 2.4 and not 4.5 as we at least expected. So what is going on here?

Well if we have a look at the results you can see that there is a significant difference between the two.

So how do we determine which calculation is correct? Well, there is an amazing tool that I often use for simulating data flows. It's called Excel.

So let's simulate the calculations in Excel.

Here I have the same start number that I used in the programs. In the second column it will be divided by 1.00014.

In the next line the result is copied and the calculation is performed again.

And this is done 2000 times. And at the end we can see the result.

When we compare all the results you can see that the Arduino Uno is way off. So it must be wrong right.

Well, not exactly. Actually it's also a correct value. Let me explain.

When using floats all the values are stored as a 4 byte, or 32 bit, value. To keep it simple there will always be round-off errors due to the underlying mathematical method of floats.

Now you might ask yourself why the STM32 can calculate a more precise value. Assuming of course that Excel produced the correct result.

Well, that is because with the STM32 the data type double can be used. As a result the values are stored in 8 bytes instead of 4 byte. This hugely increases the precision and decreases the round-off errors.

This is not possible with the Arduino Uno because float and double are both 4 byte values when using the Arduino IDE.

Let's have a look at the code. In this line of the code the compiler decided that this calculation should be performed as a double. This is because I did not predefined the data type so the compiler assumed it to be a double.

Now look what happens when I define this value as a float and rerun the speed test again.

Yep, that looks much better. Approximately 6.5 milliseconds. That is over a 9 times speed increase.

Ok, in short, the STM32f103c8T6 can be really fast. But make sure that you know what you are doing otherwise the results may be disappointing.

And that is for this video. I hope you learned something and if you did please take the time to give this video a thumbs up.

Thank you for watching and see you next time.