[ 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:

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

I explain the registers that control the GPIO (General Purpose Input/Output) pins

The purpose of this video is to simply show the output of a signal on a pin to an LED, turning the LED on and off. So we’re going to be putting a high signal to the pin and a low signal to the pin.

Outputting a signal on a pin is the general subject of GPIO (general purpose input and output). We’re only concerned with the output at this point. The features, modes, and control for just the output on a pin are quite vast.

There are two main modes when outputting on a pin on a microcontroller: open drain and push pull. There are three states for each mode: pull up, pull down, and none. This is all controlled with registers. You do not actually do this externally, it is happening internally in the microcontroller.

In a pull up scenario using open drain, there is resistance connecting to the positive power source. With an open drain scenario you have the input coming from a pin from the microcontroller, then you have a transistor (also called an open collector). You have the signal coming in from the pin and then you have the base input which is essentially turning on or off the transistor. In the output, you have a pull up resistor on the line and you also have the ability to pull down. Depending on the signal that you have on the base input which will be coming from the microcontroller, this will determine the output signal whether you have a pull up or pull down resistor on that line.


It is also important to note that if you’re going to have multiple signals on a single output - for instance: you have multiple microcontrollers or multiple pins from a microcontroller going on the same line to be able to communicate - it would be good to use the open drain or open collector scenario.

When working with open drain it is pretty important to use a pull up or pull down resistor, but with push pull we’re not going to use a pull up or pull down, we’re going to use the none state.


For output pins and the push pull mode, the pull up and pull down resistor really isn’t as important as if it was an input pin. The input pin would need to have a pull down or pull up resistor so the pin wouldn’t be floating and getting interference. To put the PortC pin 6 into the right state for output, we’ll need to control four different registers to set it up. The first one is the mode register (ModeR), the second is the output type (OtypeR), the third is the output speed (OspeedR), and the last is the pull up pull down (PUPDR).

The mode register (ModeR) controls whether you’re going to be in output mode, input mode, using the alternate function, or an input output analog type. We’re going to be using the GP (general purpose). The GP has a value of 01. Since there are four types of modes you need two bits to select four different modes. We’ll go over the three modes in future videos. The output type (OtypeR) register has one bit which can be either a 0 or a 1. That will determine whether it is going to be open drain (OD) or push pull (PP).

In this scenario we’re going to select the PP. The type will be equal to 0.

The output speed (OspeedR) register has three different speeds: low, medium, and high speed. We’ll be concerned with the high speed this time, but it really doesn’t matter because we’re just turning on and off an LED in a slow way so we can perceive it as a human. The high will be 11. The medium is 01 and the low is 10.

Now we come to the final control register which is pull up pull down (PUPDR). This is a two bit register so you have four options, but really you only have three options which are pull up, pull down and none. The last option is reserved. None or no will be 00. PU will be 01. PD will be 10. We’re going to be using none (00).

We’ve only discussed the control registers to set this pin up, we haven’t really talked about how to output to the actual pin. We’re going to be outputting 01010101…. We’re blinking an LED so we’re just going to be outputting this type of stream with delays in between. There are a couple of ways to do this. The way we’re going to use in this video is setting bits to the BSRR register. BSRR stands for Bit Set Reset Register.


This register has 32 bits in it; as you know, the ports only have 16 pins so why would the BSRR have 32 bits? The reason is because the set and reset are separated so you can set a pin and that has 16 bits in that, and there are 16 bits in the reset. So you’re not actually assigning a 0 to a pin to turn it off, you’re actually assigning a 1 to the reset portion of the 32 bit number. Let’s go ahead and look at a 32 bit number. There are 16 bits on one line and 16 bits on another bit. The top left 0 will be the 32nd bit and the bottom right 0 will be the 0 bit. So on the table number the pins from 0 to 15 as shown in the video. So, if we wanted to turn on pin number 6, I would affect the pin by putting a 1 in it. If I wanted to reset the pin or set the pin outputting nothing, I would put the 1 on the top column. Setting a 1 on the reset portion of this 32 bit number isn’t very straight forward, and we’d have to use some shifting and things like that, so we’ll leave that for a later video. For now, instead let’s use the BRR which is the Bit Reset Register. It acts just like the 0 – 15 portion of the set and it is a 16 bit register, so if you set bit number 6 on a zero based index on the BRR, you’d be resetting as if you’d done the other method.


I think we know enough about controlling the registers for controlling the output GPIO pins and we know enough about how to control the actual pin itself so let’s jump into programming.

We’re going to start the CoIDE and start a new project.


From the welcome screen, click on the Create a New Project link.


We’re using the STM32F030R8 microcontroller. Click on this microsontroller link and an information and option bubble will appear.


Click on “new project” and name the file.


We are naming it LEDBlinkPortC6.


Once you've named the project, you will now need to add the library that will be used for this GPIO project. Since we are only doing simple output to a pin, the stm32f030x8_boot will need to be added, which will also automatically add the cmsis library.


These are the only libraries we’ll need now.


Let’s jump into our main.c. We have our skeleton code.

int main (void)
{
while(1)
{
}
}

We’ll now put in pseudo-code so we can specify the general outline of the program without having to put in real code. Within the main routine we’ll do a lot of control after the code block for the int main, and before the whil never ending loop. In the “while” we’ll turn on the LED, wait, turn off the LED and wait agiain. The wait function can be created outside of the main since that code is being repeated. The specified amount of time can actually be something brought into this function. As you know from the AVR videos, we can create the function at the top and not have to put in a prototype function, or we can put in the function at the end having the main part at the top but then putting the prototypes up here.

// Create function to wait for a specified amount of time
int main(void)
{
// Set any Control Registers for PortC Pin 6
while(1)
{
// Turn on the LED
// Wait
// Turn off the LED
// Wait
}
}

Now we’ll put in the registers we’ll be using. The four registers we’re using are Moder, OtypeR, Ospeedr, PUPDr. Under the “while” we’ll be using the BSRR to turn on and the BRR to turn off. In the next video we’ll start to populate the pseudo-code with real code.

// Create function to wait for a specified amount of time
int main(void)
{
// 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
}
}


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