[ 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:
Arm Microcontrollers programming and circuit building volume 1 by Patrick hood-Daniel front cover
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:

07. Arduino for Production!! How to Output to a Pin to Blink an LED on the ARM Microcontroller Part 2

We get our feet wet with actual ARM microcontroller coding in this tutorial. In this tutorial, you will learn how to control the registers associated with the GPIO and enable the AHBENR register to allow the PORC GPIO to function.

Before we get back in the programming, I want to share with you another register that we need to consider to actually enable the GPIO. This register is called RCC which is Reset, Clock, and Control. This register is going to be controlling a few buses that are located on the microcontroller. The bus that we’re interested in is the AHB bus (Advanced High Performance). This bus can access certain peripherals on the microcontroller, one of which is the GPIO. To make this work we need to enable the AHB bus using the AHB enable register (AHBENR) which is under the RCC. Under this register we can enable the clock for the port we need, which is going to be the GPIOC, the C being the port we’re going to be using.


Back in the program, let’s put in some pseudo-code for the RCC. Enable the GPIO Clock for Port C using the AHB and RCC.

// Create function to wait for a specified amount of time
int main(void)
{
// Enable the GPIO Clock for Port C using the AHB and RCC
// Set any Control Registers for PortC Pin 6
// Moder
// OTyper
// OSpeedr
// PUPDr
while(1)
{
// Turn on the LED (BSRR)
// Wait
// Turn off the LED (BRR)
// Wait
}
}

To access all these registers we need to put in a header file for the microcontroller, so let’s go ahead and do that. We’re going to use the include statement.

#include “stm32f0xx.h”

This header file will have all the defines, specifications and types and so forth.

Now we’ll start with the registers we need to specify. We’ll start off by using the RCC type. This is actually just a type in C++ that is a structure or a struct. A struct is just a type that you define yourself. You’re creating your own type that has properties and members within that type that may have their own type. We’re going to be accessing those members using the symbol “->”, called member access. Member access will show you all the members in that structure.


We’re looking for the AHBENR register, we will select that and then use our “or” operator because we don’t want to overwrite any other bits in this register.

Hovering over it, we can see that it is a 32 bit register and more information.


If you right click and select “open declaration” you can actually see the member within the RCC type definition. You can also see that we’re in the f030 header file which this header file probably refers to.

Let’s go ahead and find the specific bit that we need to adjust in this specific member. We’re going to use RCC and find the port we need in the GPIO which is the GPIOC. We’re now going to specify the 1 in the 32 bit binary number which is 8 in Hexadecimal.

This is bit number 19.


So at this point, the program looks like this:

// Enable the GPIO Clock for Port C using the AHB and RCC
RCC ->AHBENR |= RCC_AHBENR_GPIOCEN;

We can also use the bitwise operation of shift like we did in the AVR series to specify the port number. For example, to put a 1 in the 19th bit position, the solution would be (1 << 19) using the bitwise shift (<<) operator. So the previous statement without using members would be:

RCC ->AHBENR |= (1 << 19);

There are different ways to do things, but don’t be afraid of the code if the language seems foreign to you. There is really no magic going on here. You’re just affecting one bit that is within the AHBENR register.

Let’s take a look at the GPIOCEN register in the reference manual. You can find the reference manual here: STM32F030 reference manual. We can go straight to it by going to the RCC control from the table of contents. We’ll go straight to the actual register where you have the 32 bits and what is controlled under each bit. You’ll see that the IOPCEN is on 19, which is what we’re using. You can see that the C here is on 19, the B is on 18, the A is on 17, and the F is on 22.


Depending on the processor that you have, you may have a lot more features that you can enable. You’ll also notice under the AHB you can enable other functions. Remember that 19th bit? There it is in the manual, so that is good confirmation that the number corresponding in the code is matching the bit specified in the reference manual.

Let’s go to the Mode register and take a look at the reference manual for the Moder. Look under the GPIO registers and the mode register is the first one.


You’ll notice here that they give you what has to be inserted into the two bit part of the 32 bit number for enabling the mode that you need. You’ll see the Input mode, General purpose output mode, Alternate mode, and Analog mode. We talked about the General purpose output mode, and that’s 01. We’re going to put that 01 for the pin that we need to use. These are the pin numbers and you’ll notice that since there are 2 bits for each pin, there are only 16 pins so it begins at pin 0 and ends at pin 15. We’re looking for pin 6 and we need to put the 0 and 1 in this pin.

To access the GPIO Port C, we type in GPIOC and then the symbol -> for member access to view the members. We select Moder and use the “or” operator (|=) so we don’t trouble any other bits in that register as we’re only concerned with pin 6. Next we type GPIO_Mode and we select 6_0.

// Moder
GPIOC ->MODER |= GPIO_MODER_MODER6_0;

We can look at the hex number to make sure the 1 is on the 12th location. We verify this on the datasheet.

Now we look at the type register in the reference manual. We can see there are 16 pins and only one bit for each pin. Since we’re not using open-drain but push-pull, we can actually just make sure it as set at zero.


Type in GPIOC and access OTYPER and we’ll use the “&= ~” bitwise operator with brackets. Since we’re using the not operator we will not interfere with any other bits on the register. We use the “not” operator to make sure it will keep the zero in pin 6.

// OTyper
GPIOC ->OTYPER &= ~(GPIO_OTYPER_OT_6);

Next we will look at the speed. We will investigate the datasheet once again. In this case we have low, medium, and high. We’re going to be doing high speed which is 11.


We will make sure the 3 is in the number since this is 11 in binary.

//OSpeedr
GPIO ->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR6;

Next we will use the pull-up pull-down register.


We want the 00 here for No pull-up, pull-down. To make both of these 0, we need to do an and not (&= ~), once again we select the proper pin on the GPIO. We see there 3 here, and we know it will now put in a zeroes because we have the and not operator in place. We are stopping here for now. In the next video we’ll look at getting the delay function created and sending the signal on and off.

// PUPDr
GPIOC ->PUPDR &= ~(GPIO_PUPDR_PUPDR6);

So, now we have the following from the modifications we made:

#include "stm32f0xx.h"
// Create function to wait for a specified amount of time
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)
// Wait
// Turn off the LED (BRR)
// Wait
}
}


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
STM32 Microcontroller Experiments Livestream 5