以下是一个使用C语言编写的驱动ST7735 LCD的示例代码:
- #include <stdio.h>
- #include <stdint.h>
- #include "stm32f4xx.h"
-
- // 定义SPI接口的引脚和端口
- #define SPI_PORT GPIOC
- #define SPI_CLK_PIN GPIO_Pin_10
- #define SPI_MOSI_PIN GPIO_Pin_12
-
- // 定义D/C引脚所在的端口和引脚
- #define DC_PIN GPIO_Pin_15
- #define DC_PORT GPIOA
-
- // 定义RST引脚所在的端口和引脚
- #define RST_PIN GPIO_Pin_5
- #define RST_PORT GPIOB
-
- // 定义SPI控制器
- #define SPIx SPI3
-
- void SPI_Config(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- // 使能SPI和GPIO时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOA, ENABLE);
-
- // 配置SPI引脚
- GPIO_InitStructure.GPIO_Pin = SPI_CLK_PIN | SPI_MOSI_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(SPI_PORT, &GPIO_InitStructure);
-
- // 配置D/C引脚为输出
- GPIO_InitStructure.GPIO_Pin = DC_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(DC_PORT, &GPIO_InitStructure);
-
- // 配置RST引脚为输出
- GPIO_InitStructure.GPIO_Pin = RST_PIN;
- GPIO_Init(RST_PORT, &GPIO_InitStructure);
-
- // 将引脚连到SPI功能
- GPIO_PinAFConfig(SPI_PORT, GPIO_PinSource10, GPIO_AF_SPI3);
- GPIO_PinAFConfig(SPI_PORT, GPIO_PinSource12, GPIO_AF_SPI3);
-
- // 配置SPI控制器
- SPI_I2S_DeInit(SPIx);
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPIx, &SPI_InitStructure);
-
- SPI_Cmd(SPIx, ENABLE);
- }
-
- void SPI_SendCommand(uint8_t cmd)
- {
- // 设置D/C为低电平,表示发送命令
- GPIO_ResetBits(DC_PORT, DC_PIN);
-
- // 等待SPI发送缓冲区空
- while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
-
- // 发送命令
- SPI_SendData8(SPIx, cmd);
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET);
- }
-
- void SPI_SendData(uint8_t data)
- {
- // 设置D/C引脚为高电平,表示发送数据
- GPIO_SetBits(DC_PORT, DC_PIN);
-
- // 等待SPI发送缓冲区空
- while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
-
- // 发送数据
- SPI_SendData8(SPIx, data);
- while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET);
- }
-
- void LCD_Reset(void)
- {
- // 复位LCD
- GPIO_ResetBits(RST_PORT, RST_PIN);
- delay_ms(100);
- GPIO_SetBits(RST_PORT, RST_PIN);
- delay_ms(100);
- }
-
- void LCD_Init(void)
- {
- LCD_Reset();
- SPI_SendCommand(0x11); // Sleep Out
- delay_ms(100);
- SPI_SendCommand(0xB1); // Frame Rate Control
- SPI_SendData(0x01);
- SPI_SendData(0x2C);
- SPI_SendData(0x2D);
- SPI_SendCommand(0xB2); // Frame Rate Control
- SPI_SendData(0x01);
- SPI_SendData(0x2C);
- SPI_SendData(0x2D);
- SPI_SendCommand(0xB3); // Frame Rate Control
- SPI_SendData(0x01);
- SPI_SendData(0x2C);
- SPI_SendData(0x2D);
- SPI_SendData(0x01);
- SPI_SendData(0x2C);
- SPI_SendData(0x2D);
- SPI_SendCommand(0xB4); // Display Inversion Control
- SPI_SendData(0x07);
- SPI_SendCommand(0xC0); // Power Control 1
- SPI_SendData(0xA2);
- SPI_SendData(0x02);
- SPI_SendData(0x84);
- SPI_SendCommand(0xC1); // Power Control 2
- SPI_SendData(0xC5);
- SPI_SendCommand(0xC2); // Power Control 3
- SPI_SendData(0x0A);
- SPI_SendData(0x00);
- SPI_SendCommand(0xC3); // Power Control 4
- SPI_SendData(0x8A);
- SPI_SendData(0x2A);
- SPI_SendCommand(0xC4); // Power Control 5
- SPI_SendData(0x8A);
- SPI_SendData(0xEE);
- SPI_SendCommand(0xC5); // VCOM Control 1
- SPI_SendData(0x0E);
- SPI_SendCommand(0x36); // Memory Access Control
- SPI_SendData(0xC8);
- SPI_SendCommand(0x3A); // COLMOD Pixel Format Set
- SPI_SendData(0x05);
- SPI_SendCommand(0x2A); // Column Address Set
- SPI_SendData(0x00);
- SPI_SendData(0x02);
- SPI_SendData(0x00);
- SPI_SendData(0x7F);
- SPI_SendCommand(0x2B); // Page Address Set
- SPI_SendData(0x00);
- SPI_SendData(0x01);
- SPI_SendData(0x00);
- SPI_SendData(0x9F);
- SPI_SendCommand(0x29); // Display ON
- }
-
- void LCD_DrawPixel(uint16_t x, uint16_t y, uint16_t color)
- {
- SPI_SendCommand(0x2A); // Column Address Set
- SPI_SendData((x >> 8) & 0xFF);
- SPI_SendData(x & 0xFF);
- SPI_SendData(((x + 1) >> 8) & 0xFF);
- SPI_SendData((x + 1) & 0xFF);
- SPI_SendCommand(0x2B); // Page Address Set
- SPI_SendData((y >> 8) & 0xFF);
- SPI_SendData(y & 0xFF);
- SPI_SendData(((y + 1) >> 8) & 0xFF);
- SPI_SendData((y + 1) & 0xFF);
- SPI_SendCommand(0x2C); // Memory Write
- SPI_SendData((color >> 8) & 0xFF);
- SPI_SendData(color & 0xFF);
- }
-
- void LCD_FillRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t color)
- {
- uint16_t i, j;
- SPI_SendCommand(0x2A); // Column Address Set
- SPI_SendData((x >> 8) & 0xFF);
- SPI_SendData(x & 0xFF);
- SPI_SendData(((x + width - 1) >> 8) & 0xFF);
- SPI_SendData((x + width - 1) & 0xFF);
- SPI_SendCommand(0x2B); // Page Address Set
- SPI_SendData((y >> 8) & 0xFF);
- SPI_SendData(y & 0xFF);
- SPI_SendData(((y + height - 1) >> 8) & 0xFF);
- SPI_SendData((y + height - 1) & 0xFF);
- SPI_SendCommand(0x2C); // Memory Write
- for (i = 0; i < width; i++) {
- for (j = 0; j < height; j++) {
- SPI_SendData((color >> 8) & 0xFF);
- SPI_SendData(color & 0xFF);
- }
- }
- }
-
- void delay_ms(uint32_t ms)
- {
- volatile uint32_t nCount;
-
- RCC_ClocksTypeDef RCC_Clocks;
- RCC_GetClocksFreq(&RCC_Clocks);
-
- nCount = (RCC_Clocks.HCLK_Frequency / 10000) * ms;
- for (; nCount != 0; nCount--);
- }
-
- int main(void)
- {
- SPI_Config();
- LCD_Init();
-
- // 示例:绘制一个红色的矩形框
- LCD_FillRect(10, 10, 100, 50, 0xF800);
-
- while (1) {
-
- }
- }
|