[ Log In ]
Newbie Hack Shirt Back
The STM32 ultra basic kit showing all of the components

STM32 Ultra Basic Kit Special Introductory Price!

$34.95
Qty:
The ARM STM32F030 Advanced kit with the book ARM Microcontrollers Programming and Circuit Building Volume 1

ARM Advanced Kit with Book Vol 1

$158.00
Qty:
2x36 pin header IDC

2.54 mm (0.1") Pitch Male Connector 36 pin Header

$0.75
Qty:
ST Link v2 for STM32 and STM8 microcontrollers top view of all components

ST-Link v2 STM32 and STM8 Programmer

$9.95
Qty:
STM32F0 microcontroller and interface board top view

STM32 M0 MCU and Interface to Breadboard

$14.95
Qty:
Top view of the bluetooth module

Bluetooth Module

$17.50
Qty:
The ARM microcontroller beginners kit

ARM Microcontroller Beginners Kit (STM32F0)

$39.95
Qty:

ARM Microcontroller Intermediate Kit (STM32F0)

$89.95
Qty:

ARM Microcontroller Advanced Kit (STM32F0)

$119.95
Qty:

08. Arduino for Production!! How to Output to a Pin to Blink an LED on an ARM Microcontroller Part 3

This tutorial delves into the actual controlling of the PORT C Pin 6 on the ARM STM32 Microcontroller. BSRR and BRR registers are manipulated to set the pin to output a high and low signal. A wait (delay) function is developed so the human eye can discern the LED blinking. Some gotchas are also explained to help with any issues with flashing the ARM STM32 microcontroller

Before we get to turning the LED on and off, I want to go back and mention something about the mode register. This comes from a comment on YouTube and it notes that the register needs to have a 0 and a 1 in it, but we’re not really ensuring there is a 0 in the first bit place, and we want to make sure we do that because somewhere else (maybe in a program) we may have set the register to an analog or alternate function mode where it has a 1 in the first position. To ensure that we have a 0 in the first place, we need to do an “and” and a “not” on the first bit. Let’s take a look at the defined values they give us. By going to the declaration we see there are 3. We have one without a 0 or a 1, one with the 0, and one with the 1. Let’s go ahead and note this in the program for reference. Now we know which modes to use with 11, 01, and 10. If we do an “and not” with the 10, it will turn the first position to a 0, ensuring the first position is a 0. We change the MODER to &= ~ on the mode 1 to do this.

// Moder
GPIOC ->MODER |= GPIO_MODER_MODER6_0;
GPIOC &= ~(GPIO_MODER_MODE6_1);

We confirm the Otyper is only a single bit, so we do not need to worry about it. The speed is going to be two bits per pin. In this case it is at the 11 so we don’t need to worry about this. The pull up pull down is also 11 so it can be left as is.

Now let’s talk about turning the LED on and off. Let’s turn on the LED using the BSRR register. We’re going to use the same GPIOC structure. There is a BRR and a BSRR, and we’re going to use the BSRR first. Using the “or” bitwise operation |=, we will see where port C is located. The set is at 6 and the reset is at 22.



There are a few ways of doing this. We can actually just type in GPIO like we did before, and because we’re using BSRR, generally you would use the member from the GPIOC as the next statement after the underscore. We’ll try the BSRR here and go to the number 6. You can also do things like doing a shift, 6 positions to the left, but we don’t really need to do that since it is already stated for you (hex number 40).

// Turn on the LED (BSRR)
GPIOC ->BSRR |= GPIO_BSRR_BS_6;

Now we will turn off the LED. We will use the BRR, the bit reset register. We’re going to use the “or” bitwise operation and the bit reset. The BRR should only have the bit reset which is number 6.

// Turn off the LED (BRR)
GPIOC ->BRR |= GPIO_BRR_BR_6;

Now we need to create the delays. We will make the first function by entering “void” and then “WaitForAMoment” we will then use an integer here. We need a for loop for a time delay. We will initiate the variables beforehand by using i = 0. We will increase the j. Next we will make sure it is a volatile integer. As long as i is less than the moment, the variable that we pass in, it will keep doing it. If it is not doing it, it will escape the for loop and allow the program to go to the next instruction.

// Create function to wait for a specified amount of time
void WaitForAMoment(int Moment)
{
volatile int i, j;

for (i=0; i < Moment; i++) {
j ++;
}
}

We’ll include “WaitForAMoment” with a number of 200,000. The while loop ensures it will be a never-ending loop.

// Wait
WaitForAMoment(200000);

Here is the code up to this point:

include "stm32f0xx.h"

// Create function to wait for a specified amount of time
void WaitForAMoment(int Moment)
{
volatile int i, j;

for (i=0; i < Moment; i++) {
j ++;
}
}

int main(void)
{
// Enable the GPIO Clock for Port C using the AHB and RCC
RCC ->AHBENR |= RCC_AHBENR_GPIOCEN;
// Set any Control Registers for PortC Pin 6
// Moder
GPIOC ->MODER |= GPIO_MODER_MODER6_0;
// OTyper
GPIOC ->OTYPER &= ~(GPIO_OTYPER_OT_6);
//OSpeedr
GPIO ->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR6;
// PUPDr
GPIOC ->PUPDR &= ~(GPIO_PUPDR_PUPDR6);

while(1)
{

// Turn on the LED (BSRR)
GPIOC ->BSRR |= GPIO_BSRR_BS_6;
// Wait
WaitForAMoment(200000);
// Turn off the LED (BRR)
GPIOC ->BRR |= GPIO_BRR_BR_6;
// Wait
WaitForAMoment(200000);

}

}

Now we are ready to compile (build) our program. We’re noticing a few errors so let’s first correct those. We go under the boot folder and locate the file and move it to the proper folder. The other error does not concern us at this time so we’re ready to flash the microcontroller. We plug in the ST link and we see that the LED is blinking.

This is when it is on and off, that’s the 200,000 iterations for the for loop. Let’s go ahead and change that and make one of them a lesser value. We want to try and make it on longer than it it is off. To do this we go back into the program and change the value from 200,000 to 100,000. Do the change and build the program. Now we can see the changes represented by the duration of the blinking on the LED.

There are a couple of things to point out before we end this video. If you have issues flashing the microcontroller, this will help you figure out what may be going wrong. The first issue is where you have the resistors from the SWCLK and the SWDIO and remove those and where they connect to the plus and minus rail. I had to do this when I had Windows 7 but when I upgraded to Windows 10 this became a problem although it could have been a number of other issues. I understand that these are actually pulled up internally so you really don’t need those resistors there. The other thing is you can put a 510 ohm resistor going to ground on the boot 0 pin and the boot 0 pin is pin number 60. Another thing I did was take the ground and positive from the ST Link interface as close to the PCB as possible because when you’re working with breadboards, they can cause interference. These are some fixes you can look into if you’re having any trouble.

Now we’ll talk about what to do if you’ve accidentally bricked the microcontroller. Maybe you’ve had an error in the program where you’ve caused the program to doing something unintended to the microcontroller. You can also pull the boot 0 (which is pin 60) high, using a 10k resistor going to the positive rail. While the pin is pulled high you do a full memory erase. You use a program called the ST-link utility. Search for it and download and install it. It is very straight-forward. You can also use this program to program the microcontroller by loading the file and selecting it from the debug and the binary file. Double click that and you will have the program. It will also show you the device memory when you have it connected so you can click on connect to target and it will show you what is on the microcontroller. The settings can be accessed using the icon shown on the video. The mode should be in normal and SWD on connection settings. To do a full chip erase use the icon shown in the video. Thank you for watching.


01. Arduino for Production!! Introduction to ARM Microcontrollers
02. Arduino for Production!! How to Instal and Set up the Arduino IDE (Integrated Development Environment) for the ARM Microcontroller
03. Arduino for Production!! How to Connect the ST-Link v2 ARM Programmer to your Computer
04. Arduino for Production!! How to Use the CoIDE (Adruino IDE) for ARM Microcontroller Development
05. Arduino for Production!! How to Connect the ST-Link v2 to the ARM STM32 Microcontroller
06. Arduino for Production!! How to Output to a Pin to Blink an LED on the ARM Microcontroller Part 1
07. Arduino for Production!! How to Output to a Pin to Blink an LED on the ARM Microcontroller Part 2
08. Arduino for Production!! How to Output to a Pin to Blink an LED on an ARM Microcontroller Part 3
09. Arduino for Production!! Can Not Connect to Target! How to Establish a Connection Again.
10. Arduino for Production!! How to Receive Input from a Pin for Push Button Input (GPIO) on the ARM Microcontroller
11. Arduino for Production!! How to Receive Push Button Input on the ARM Microcontroller Part 2
12. Arduino for Production!! How to Receive Stable GPIO Push Button Input on the ARM Microcontroller - Software Debouncing Part 1
13. Arduino for Production!! How to Receive Stable GPIO PUSH Button Input onthe ARM Microcontroller - Software Debouncing Part 2
14. Arduino for Production - How to Establish Software Debouncing on the ARM Microcontroller Exclusive
15. Arduino for Production!! How to Interface an LCD on the ARM Microcontroller Part 1
16. Arduino for Production!! How to Interface an LCD on the ARM Microcontroller Part 2
17. Arduino for Production!! How to Interface an LCD to an ARM Microcontroller Part 3
18. Arduino for Production!! How to Interface an LCD to the ARM Microcontroller Part 4