STM32 Code : Controlling 7 Segment 4 Digit LCD - part 3
Title: STM32 Code: Controlling 7 Segment 4 Digit LCD - Part 3
Now, the much-anticipated connection.
We are using a program called STM32 Cube IDE. There are many tutorials and blog posts about how to use it, and covering its usage here would make this post too long, so I will not go into that.
Anyway, STM32 Cube IDE is very convenient. You can easily set many pins in the ioc file, and the program automatically generates the code for those pins. After entering SPI in the ioc file, set it to "Transmit Only Master" because we will only send data and not receive it. The IDE will then automatically configure the pins.
Next, go to GPIO SPI and change the name.
Set PA6 to GPIO OUTPUT and rename it to PA6_RCLK.
Now, if you look at the main code, you can see the initialization parts for SPI1 and GPIO. This is the advantage of setting it in the ioc file, as it generates the code for you.
This completes the pinMode settings that were initially done in the Arduino code.
Now, to control the display from 1 to 9999, instead of creating multiple functions, let's create a source file and modularize it. Then, we can simply include lcd_display in main.c.
Also, create a header file.
First, we created the files. Now, let's go back to the code.
The first array in this code can be used.
Since functions like shiftOut and digitalWrite are Arduino-specific, we use HAL drivers in STM32.
shiftOut is part of SPI communication, so we use the HAL_SPI_Transmit function.
//shiftOut(DS, SH_CP, LSBFIRST, data ); HAL_SPI_Transmit(hspi, pData, Size, Timeout);
Looking at the HAL_SPI_Transmit() function, it requires a handler, data pointer, size, and timeout.
The handler is automatically generated in main.c when SPI is set in the ioc file, so we receive the handler in main.c, pass the data pointer as &data, set the size to 1 since it's 16bit, and set the timeout to around 100.
To receive the handler, create an init function:
void lcd_init(SPI_HandleTypeDef * hspi){
m_hspi = hspi;
}
Then, in main.c, pass the handler and execute it.
Next, replace //digitalWrite(ST_CP, LOW); with
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, PinState);
Replace it with this code:
HAL_GPIO_WritePin(PA6_RCLK_GPIO_Port, PA6_RCLK_Pin, 0);
This sets the port, pin, and pin state (0 - LOW, 1 - HIGH).
So, we can replace it like this.
Include this in main.c,
And in the while loop, execute:
for (int i = 0; i < 9999; i++) {
displayNumber(i);
}
When you run this code~
Beautifully, we have some errors now.
In this post, we tried converting Arduino code to STM32 code. We will stop here for now and explain the process of debugging the errors and getting it to work correctly in the next post.