[ 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:
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:
USB 2.0 Cable 10 Foot Type A Male to Type B Male

USB 2.0 Cable Type A Male to Type B Male - 10 FT

$4.80
Qty:

11. Arduino for Production!! How to Receive Push Button Input on the ARM Microcontroller Part 2

In this video we’re going to look at the GPIO input registers. The registers we use for the input will be similar to the registers used for the output. Some of them are even the same. We’ll look at the mode register, the O-type register, speed, and pull-up, pull-down register. We’ll also look at a new one: the IDR register.


The switch is going to be connected to the input pin. We need to figure out how to get the information from the input pin. We’re going to need to know if there is a 1 or 0 on that pin. Since we need the microcontroller to act when the pin is 1, we need to figure out how to do it. One way to do this is to pull it, which will test the pin every time we go through the loop. The other way would be using an interrupt. Using an interrupt, the microcontroller will tell us automatically going into another part of the program.

In this video we’re going to be using the pulling method. First let’s pull up the CoIDE. We’ll go to New Project. Locate the STM32F030R8T6 and click on new project. We’ll name the project GPIOSimpleInput. Click finish and go to the next page to add the boot library. Now we can see the project. Now we need to build the code. First we need to relocate the STM file into the correct file. Find the conf file from the boot folder and copy it and paste it into the new folder we have just created.

Now we will start creating the pseudo-code. First we will include the library. Since we have skeleton code it doesn’t know how to control the chip so we include the library for this. Within the main section we need to set up the control registers for the LED output. We also need to set up the control registers for the push button input. Then we will add code to turn on the LED when the button is pressed.
In the loop we will test if the button is pressed, so we will add code to turn on the LED if the button is pressed and turn off the LED if the button is not pressed.

Next we will set the mode registers for output, speed, and Pull up pull down. For the input we will set up a mode register and the pull up pull down. We use the BSRR for turning on the LED and the BRR for turning off. The button register (input data) will be IDR.

Next, we will put in the reset clock and control, which enables the GPIO we need for Port C pin 6 and Port B pin 1. Now we need to include the library so it understands what we’re referencing. Next we will use the AHBENR high-performance bus register. We will also do this for Port B. Remember that pseudo-code is very useful because it creates annotations so you know what the code does.
We will now move on to control registers. The mode register will be under GPIOC.

The datasheet shows we need the general purpose output mode which is a 01 on the 6th pin. We put the 1 in the 0 spot and a 0 in the 1 spot, so we will use the and-not to ensure the zero in the spot. Next we will put the push pull on the output type register, and the high speed registers.

Next we set the pull up pull down register. We use the and-not here to ensure we have both zeroes.
Now on the input side we will use the GPIOB for port B. The mode R register is now used. We will use the and-not once again. The pull up pull down will be the same as we did for the output.

To review, we are setting up our mode for output for LED, output type as push-pull, speed for high speed, and pull-up pull-down as none. For input we’re setting the mode for input and setting no to pull-up pull-down.

In the infinite loop we’ll be using the pulling method. We’re going to say “if the button is pressed” we need it to do something, in this case, turning on the LED. We’ll use the “else” for the button being off. Now we need to use bitwise arithmetic. The IDR register has 16 bits. Each bit represents a single pin. If there is a high digital level on one of the pins, there will be a 1 in one of the bit positions. We have ours on Port B Pin 1. If we press the button and we put a high signal on that pin, there will be a 1 here. If we have no other switches on the other bits, then all of them will be 0.

Now we need to test whether the pin is going to be 1 or 0. We’re going to use the and bitwise operation. We start with the IDR register which is 16 bits. Next we’ll use a mask which is GPIO_IDR_IDR1 register. Since we have a 1 there, we’ll have a 1 in the 1 bit place. In the non-pushed button state this will have a 0, when we depress the button we will have a 1. This would be a false statement using bitwise arithmetic. By using the mask we are making it a 1 on both 1 bit places. By doing this we can use code to test this particular pin and whether the push-button is pressed or not.

The actual button register that will be listening for the pins to be 0 or 1 will be GPIOB->IDR. Then we add the mask which is GPIO_IDR_1. The mask has a 1 in that location. Now we use the BSRR to turn on the LED. We use the or bitwise operation with GPIO_BSRR_BS_6. Next we need to turn off the LED if this is a false statement. We’ll use the BSRR and put a 1 in that place for the bit reset for number 6.

In this simple implementation of code we’re not going to be taking advantage of any debouncing. This is a very simple implementation of using the button. We’ll take a look at more complex situations later. Now we will test the code to see if it compiles. Now that the code is compiled correctly we’ll hook up the programmer and program the microcontroller. First we’ll hook up the SWDIO, the 2nd pin on the programmer. SWCLK is the first pin which is the orange. We can see ground and VCC and hook them up as well.

Now that the programmer is hooked up we have programmed the microcontroller. When we push the button, the LED lights up. To review, we set up the LED for output, the Port B pin 1 for input, installed the push button switch, and created code to make the Port B pin 1 be read by software. In the next video we’ll start exploring the debouncing of the push button.


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