This tutorial will be a bit different that the typical Arduino tutorials and tool chain that you may be familiar. It is with good reason as I'm teaching you how to be a production developer and engineer rather than a one-off project tinkerer. With this information, you will learn how to be efficient and cost effective in this arena.
Here is the basic circuit that show the power being distributed along the breadboard power rails and the PCB with the Arduino ARM Microcontroller in place. You can find out how to wire this in ___________ tutorial.
We'll be focusing on pins 33, 34, 35, 36, 37, 38, 39 and 40. We want to light up these pins on command,so we will need to add a resistor and an LED to each of host tie strips associated with those pins. Since the LEDs are larger than the spacing of the tie strips, you will want to bend the leads in such a way that the LED is staggered. You want to pay close attention to bending the leads so that all of the anodes are on one side and the cathodes are on the other side, but the LED portion staggers. Go ahead and insert the LEDs as shown in the image.
Now that all of the LED's are connected to the breadboard and are making connections to the pins 33-40 of the Arduino ARM Microcontroller, let's create a new project and code so we can see the output and test the LEDs.
Create a new project in CoIDE.
Select the STM32F030R8T8 Arduino ARM Microcontroller or the Arduino that you are using.
Now, click on New Project and name the project. I'm naming the project LCD Tutorial 1
There are a couple of changes on the library page, where you add or remove specific libraries for the project. You will see a library on the bottom of the page that is associated specifically to the Arduino Microcontroller that you are using called STM32F030x8_CUBELIB. The filename that you see may be different if you use a different Arduino than mine; however, it should still have the CUBELIB as the suffix. Add that library file and you will see that it will also automatically include the cmsis_core library as well as the library with the CUBELIB as the suffix.
Once you are finished adding the libraries, you can double-click main.c at the left pane of the development environment. This will open up the main.c file and show you the skeleton code in the programming code area which will simply be:
int main(void)
{
while(1)
{
}
}
The first code we need to write is to add the include file to the top of our program. This include will bring in all of the Arduino MCU register specifications, macros and define variables into the program so we will have access to these registers in the program. The registers are only simple bit level switches that we will be turning on and off to control various features in the Arduino ARM Microcontroller. This code goes at the top line of the program.
#include "stm32f0xx.f"
The next code to write code to test the LED to see if they turn on and off on command. there is a set of registers to control the pins for General Purpose Input and Output (GPIO). In this case, we will set these pins to output since we are wanting to output a high or low to the LEDs and we are not receiving information from the LEDs. The GPIO port needs to be enabled using the RCC (Reset Clock and Control register) under the AHB (Advanced High performance Bus), but don't worry about the complicated jargon, it's simple. The registers we need to change are the Mode, Type, Speed and PUPD (Pull-up or Pull-down) for each pin. Then we will output a high (3.3 volts) on each pin.
Starting with enabling the GPIO Port B and Port C:
RCC_>AHBENR |= RCC_AHBENR_GPIOBEN;
RCC_>AHBENR |= RCC_AHBENR_GPIOBEN;
Now we want to make the pin (whichever pin we want to control) set to output. The way to do this is to find the specific pin that we want to set as output. In this case, we are interested in pin 12 on port B.
Since we want bit 0 of pin 12 to be a 1 and bit 1 of pin 12 to be a 0, we will use the bitwise "or" or "|" to set the bit and use the "and not" or "& ~" bitwise operation to set .
GPIOB->MODER |= GPIO+MODER_MODER12_0;
GPIOB->MODER &= ~GPIO+MODER_MODER12_1;
Because we know which pins need to be set as 01, we can simply set all of the pins in one line using a standard binary number for that register:
GPIOB->MODER = 0b01010101000000000000000000000000;
If you wanted to use a hex number for that instruction instead of a binary number, you can use a binary to hex converter online and you will find that this number is 0x55000000;
Now let's take a look at the type, speed and PUPD registers. Since we need zero's in all of the type register's bits, the output type register will just be equal to zero. Be careful when assigning all of the bits like this. If you set bits for other features or operations, then those bits will be cleared as well. The output type we are setting here is output push-pull.
GPIOB->OTYPER = 0;
On to the speed register. If you look at the datasheet for the output speed register, you will notice that the reset value for that register is 0x0c000000. The C in that number is for the programming pins. Those need to be set and not changed (unless you don't care to re-program the chip) Note: if you do change this reset value on those pins, then you can do a special
clearing of the flash. The other registers for port A also has a special reset value for the programming pins.
The code for the output speed register for all high speed pins that we are concerned about is:
GPIOB->OSPEEDR = 0b11111111000000000000000000000000;
The PUPD (Pull Up Pull Down) register also should be all zeros according to the datasheet:
The code for the PUPD registers on the Port B is simply:
Port C needs to be set in the same way, but different pins. Let's take a look and see which pins we are using on Port C.
Since we know the bits to be set and we have the datasheet on hand to see where the bits need to be set for pins 6, 7, 8 and 9 on Port C, we can easily copy and paste the GPIOB and create a set of instructions for the GPIOC.
GPIOC->MODER = 0b00000000000001010101000000000000;
GPIOC->OTYPER = 0;
GPIOC->OSPEEDR = 0b00000000000011111111000000000000;
GPIOC->PUPD = 0;
Now we are ready to turn the LEDs on. To do this, we need to access the BSRR (Bit Ser/Reset Register) for Port B and Port C. The BS (Bit Set) bits will turn the pin on (bringing the pin high so the LED lights up). To turn the pin off, you would use the BR (Bit Reset) bit by setting that bit to a "1" which sets the pin to low.
GPIOB->BSRR = 0b00000000000000001111000000000000;
GPIOC->BSRR = 0b00000000000000000000001111000000;
Thus far, here is the code to configure the pins 33-40 as output and turn on the pins:
#include "stm32f0xx.f"
int main(void)
{
RCC_>AHBENR |= RCC_AHBENR_GPIOBEN;
RCC_>AHBENR |= RCC_AHBENR_GPIOBEN;
GPIOB->MODER = 0b01010101000000000000000000000000;
GPIOB->OTYPER = 0;
GPIOB->OSPEEDR = 0b11111111000000000000000000000000;
GPIOB->PUPD = 0;
GPIOC->MODER = 0b00000000000001010101000000000000;
GPIOC->OTYPER = 0;
GPIOC->OSPEEDR = 0b00000000000011111111000000000000;
GPIOC->PUPD = 0;
while(1)
{
}
}
Here is the result after we build the code (compile the code) and flash the STM32 Arduino Microcontroller: