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.