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

16. Arduino for Production!! How to Interface an LCD on the ARM Microcontroller Part 2

Interfacing the LCD to the STM32 Microcontroller Tutorial Part 2



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.

The LEDs are plugged into the breadboard connected to the Arduino ARM Microcontroller pins 33-40.

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.

Create a project in CoIDE

Select the STM32F030R8T8 Arduino ARM Microcontroller or the Arduino that you are using.

Select the STM32F030R8T8 Arduino ARM Microcontroller
Now, click on New Project and name the project. I'm naming the project LCD Tutorial 1

Click on new project for the Arduino ARM Microcontroller selected

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.

Set the mode register (MODER) for output for that pin

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.

Set the output speed register (Ospeedr) for high speed for that pin


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:

Set the PUPD register for no pull up or pull down for those pins


The code for the PUPD registers on the Port B is simply:

GPIOB->PUPD = 0;


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.

The pins on the Arduino STM32 Microcontroller that will be used for the LCD data pins

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.

The bit set/reset register (BSRR) for the STM32 Arduino chip.


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:

Results of flashing the Arduino and lighting up the LEDs



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