#include "stm32c0xx_hal.h"
#include "string.h"
#define TFT_DC GPIO_PIN_2
#define TFT_CS GPIO_PIN_3
#define TFT_RST GPIO_PIN_4
#define GPIO_PORT GPIOA // Assuming pins are on Port A (change as needed)
// Define SPI handle
SPI_HandleTypeDef hspi1;
// Function prototypes
void SystemClock_Config(void);
void GPIO_Init(void);
void SPI1_Init(void);
void ILI9341_Init(void);
void ILI9341_SendCommand(uint8_t cmd);
void ILI9341_SendData(uint8_t data);
void ILI9341_Reset(void);
void ILI9341_DisplayText(uint16_t x, uint16_t y, const char* text, uint16_t color, uint16_t size);
int main(void)
{
// Initialize the HAL Library
HAL_Init();
// Configure the system clock
SystemClock_Config();
// Initialize GPIO and SPI
GPIO_Init();
SPI1_Init();
// Initialize ILI9341 display
ILI9341_Init();
// Display some text on the screen
ILI9341_DisplayText(20, 120, "Hello STM32", 0xF800, 3); // Red text
ILI9341_DisplayText(24, 160, "I can do SPI :-)", 0x07E0, 2); // Green text
// Infinite loop (normally the loop function in Arduino)
while(1)
{
HAL_Delay(10);
}
}
// System Clock Configuration (adjust as needed)
void SystemClock_Config(void)
{
// Typically, STM32CubeMX or HAL library configures this.
// You can configure your system clock here if needed.
}
// GPIO Initialization (Configuring TFT control pins and SPI SCK, MISO, MOSI)
void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable GPIOA clock (or other port depending on pin assignments)
__HAL_RCC_GPIOA_CLK_ENABLE();
// Configure TFT control pins (DC, CS, RST)
GPIO_InitStruct.Pin = TFT_DC | TFT_CS | TFT_RST;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Push-pull output
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIO_PORT, &GPIO_InitStruct);
// Configure SPI pins (SCK, MOSI, MISO) – make sure they are set correctly in CubeMX or your code
// Typically these pins are on GPIOA or GPIOB depending on your configuration
}
// SPI Initialization (for SPI1)
void SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; // Adjust as needed
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
// Initialization Error
while(1);
}
}
// ILI9341 Initialization (sending commands to the display)
void ILI9341_Init(void)
{
ILI9341_Reset();
// Reset and setup ILI9341 display (initialize with some basic settings)
ILI9341_SendCommand(0x01); // Software Reset
HAL_Delay(150);
ILI9341_SendCommand(0x28); // Display off
ILI9341_SendCommand(0x3A); // Interface Pixel Format
ILI9341_SendData(0x55); // 16-bit color
ILI9341_SendCommand(0x36); // Memory Data Access Control
ILI9341_SendData(0x48); // Rotate screen
ILI9341_SendCommand(0x29); // Display on
}
// Send Command to ILI9341
void ILI9341_SendCommand(uint8_t cmd)
{
HAL_GPIO_WritePin(GPIO_PORT, TFT_DC, GPIO_PIN_RESET); // DC pin low for command
HAL_GPIO_WritePin(GPIO_PORT, TFT_CS, GPIO_PIN_RESET); // CS pin low (select)
HAL_SPI_Transmit(&hspi1, &cmd, 1, 1000);
HAL_GPIO_WritePin(GPIO_PORT, TFT_CS, GPIO_PIN_SET); // CS pin high (deselect)
}
// Send Data to ILI9341
void ILI9341_SendData(uint8_t data)
{
HAL_GPIO_WritePin(GPIO_PORT, TFT_DC, GPIO_PIN_SET); // DC pin high for data
HAL_GPIO_WritePin(GPIO_PORT, TFT_CS, GPIO_PIN_RESET); // CS pin low (select)
HAL_SPI_Transmit(&hspi1, &data, 1, 1000);
HAL_GPIO_WritePin(GPIO_PORT, TFT_CS, GPIO_PIN_SET); // CS pin high (deselect)
}
// Reset the ILI9341 display
void ILI9341_Reset(void)
{
HAL_GPIO_WritePin(GPIO_PORT, TFT_RST, GPIO_PIN_RESET); // Reset pin low
HAL_Delay(50);
HAL_GPIO_WritePin(GPIO_PORT, TFT_RST, GPIO_PIN_SET); // Reset pin high
HAL_Delay(150);
}
// Display Text on ILI9341
void ILI9341_DisplayText(uint16_t x, uint16_t y, const char* text, uint16_t color, uint16_t size)
{
// This is a simplified function to write text to the display
// Implement or use a library to send text pixel-by-pixel if you need full text rendering
// You can either create your own font or use an existing library
// Note: This example assumes a basic font or pixel-writing function is available
// You can implement more complex functions for better text rendering
}