#include "stm32l0xx_hal.h"
// SPI and GPIO initialization
SPI_HandleTypeDef hspi1;
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Function to initialize SPI
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_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
HAL_SPI_Init(&hspi1);
}
// Function to initialize GPIO pins for CS, DC, and RESET
void GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable clock for GPIOA
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4; // RESET, DC, CS pins
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
// Function to reset the display
void ST7735_Reset(void)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); // RESET low
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); // RESET high
HAL_Delay(100);
}
// Function to send command to the display
void ST7735_SendCommand(uint8_t cmd)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET); // DC low for command mode
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // CS low
HAL_SPI_Transmit(&hspi1, &cmd, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // CS high
}
// Function to send data to the display
void ST7735_SendData(uint8_t data)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET); // DC high for data mode
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // CS low
HAL_SPI_Transmit(&hspi1, &data, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // CS high
}
// ST7735 Initialization sequence
void ST7735_Init(void)
{
ST7735_Reset();
// Start initialization with some common ST7735 commands
ST7735_SendCommand(0x01); // Software Reset
HAL_Delay(150);
ST7735_SendCommand(0x11); // Sleep Out
HAL_Delay(500);
ST7735_SendCommand(0x29); // Display On
}
// Function to draw a pixel (simplified)
void ST7735_DrawPixel(uint16_t x, uint16_t y, uint16_t color)
{
// Set the column address
ST7735_SendCommand(0x2A); // Column address set
ST7735_SendData(x >> 8); // MSB of X
ST7735_SendData(x & 0xFF); // LSB of X
// Set the row address
ST7735_SendCommand(0x2B); // Row address set
ST7735_SendData(y >> 8); // MSB of Y
ST7735_SendData(y & 0xFF); // LSB of Y
ST7735_SendCommand(0x2C); // Memory Write
ST7735_SendData(color >> 8); // MSB of color
ST7735_SendData(color & 0xFF); // LSB of color
}
// Main function to initialize and test display
int main(void)
{
HAL_Init();
SystemClock_Config();
// Initialize SPI and GPIO
SPI1_Init();
GPIO_Init();
// Initialize the ST7735 display
ST7735_Init();
// Draw a pixel (example: red at position (10, 10))
ST7735_DrawPixel(10, 10, 0xF800); // Red color
while (1)
{
}
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6