嵌入式单片机设计吧 关注:23贴子:95
  • 0回复贴,共1

STM32F103c8t6 SPI屏幕驱动

只看楼主收藏回复

#include "lcd.h"
#include "lcd_init.h"
#include "lcd_init.h"
#include "perf_counter.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_conf.h"
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define SYS_CLOCK_MHZ(36)
#define SPI_ST7789_SPISPI1
#define SPI_ST7789_SPI_APBxClock_FUNRCC_APB2PeriphClockCmd
#define SPI_ST7789_SPI_CLKRCC_APB2Periph_SPI1
#define SPI_ST7789_GPIO_APBxClock_FUNRCC_APB2PeriphClockCmd
#define SPI_ST7789_GPIO_CLKRCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD
//BLK
#define SPI_ST7789_BLK_PINGPIO_Pin_5
#define SPI_ST7789_BLK_LOW()GPIO_ResetBits(GPIOB, SPI_ST7789_BLK_PIN)
#define SPI_ST7789_BLK_HIGH()GPIO_SetBits(GPIOB, SPI_ST7789_BLK_PIN)
//RST
#define SPI_ST7789_RST_PINGPIO_Pin_6
#define SPI_ST7789_RST_LOW()GPIO_ResetBits(GPIOB, SPI_ST7789_RST_PIN)
#define SPI_ST7789_RST_HIGH()GPIO_SetBits(GPIOB, SPI_ST7789_RST_PIN)
//DC
#define SPI_ST7789_DC_PINGPIO_Pin_3
#define SPI_ST7789_DC_LOW()GPIO_ResetBits(GPIOB, SPI_ST7789_DC_PIN)
#define SPI_ST7789_DC_HIGH()GPIO_SetBits(GPIOB, SPI_ST7789_DC_PIN)
#defineSPI_ST7789_MODE_CMD() SPI_ST7789_DC_LOW()
#defineSPI_ST7789_MODE_DAT() SPI_ST7789_DC_HIGH()
//CS(NSS)
#define SPI_ST7789_CS_PINGPIO_Pin_4
#define SPI_ST7789_CS_LOW()GPIO_ResetBits(GPIOA, SPI_ST7789_CS_PIN)
#define SPI_ST7789_CS_HIGH()GPIO_SetBits(GPIOA, SPI_ST7789_CS_PIN)
//SCLK
#define SPI_ST7789_SCLK_PINGPIO_Pin_5
#define SPI_ST7789_SCLK_LOW()GPIO_ResetBits(GPIOA, SPI_ST7789_SCLK_PIN)
#define SPI_ST7789_SCLK_HIGH()GPIO_SetBits(GPIOA, SPI_ST7789_SCLK_PIN)
//MOSI
#define SPI_ST7789_MOSI_PINGPIO_Pin_7
#define SPI_ST7789_MOSI_LOW()GPIO_ResetBits(GPIOA, SPI_ST7789_MOSI_PIN)
#define SPI_ST7789_MOSI_HIGH()GPIO_SetBits(GPIOA, SPI_ST7789_MOSI_PIN)
//#define SPI_ST7789_BLK_LOW();
static void ST7789_DelayUs(uint32_t uiTimes)
{
uint16_t uiLoops;
if(uiTimes > 0)
{
while(--uiTimes)
{
uiLoops = SYS_CLOCK_MHZ;
while(--uiLoops)
{
//__nop();
}
}
}
}
void ST7789_InitializeGPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_ST7789_GPIO_APBxClock_FUN(SPI_ST7789_GPIO_CLK|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //Disable jtag for use PB3/4
GPIO_InitStructure.GPIO_Pin = SPI_ST7789_DC_PIN|SPI_ST7789_RST_PIN|SPI_ST7789_BLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, SPI_ST7789_DC_PIN|SPI_ST7789_BLK_PIN);
GPIO_InitStructure.GPIO_Pin = SPI_ST7789_CS_PIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, SPI_ST7789_CS_PIN);
GPIO_InitStructure.GPIO_Pin =SPI_ST7789_SCLK_PIN|SPI_ST7789_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void ST7789_InitializeSPI(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_ST7789_SPI_APBxClock_FUN(SPI_ST7789_SPI_CLK, ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; // Transmit only
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //Master mode
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // 8-Bit data.
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // High-clock-signal when free.
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // Software NSS signal
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // Most Significant Bit
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI_ST7789_SPI, &SPI_InitStructure); // Initialize SPI
SPI_Cmd(SPI_ST7789_SPI, ENABLE); // Enable SPI control
}
void ST7789_WriteByte(uint8_t uiByte)
{
SPI_ST7789_CS_LOW();
while(SPI_I2S_GetFlagStatus(SPI_ST7789_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_ST7789_SPI, uiByte);
while(SPI_I2S_GetFlagStatus(SPI_ST7789_SPI, SPI_I2S_FLAG_TXE) == RESET);
ST7789_DelayUs(1);
SPI_ST7789_CS_HIGH();
}
void ST7789_WriteWord(uint16_t uiWord)
{
ST7789_WriteByte(uiWord>>8);
ST7789_WriteByte(uiWord);
}
void ST7789_WriteCommand(uint8_t uiCmd)
{
SPI_ST7789_MODE_CMD();
ST7789_WriteByte(uiCmd);
SPI_ST7789_MODE_DAT();
}
void ST7789_BkgLightOn22(bool bBlkOn)
{
if(bBlkOn)
{
SPI_ST7789_BLK_LOW();
}
else
{
SPI_ST7789_BLK_HIGH();
//SPI_ST7789_BLK_LOW();
}
}
void delay_ms_ss2(int time){
volatile int i = 0, j = 0;
for(i=0; i < time;i++){
for(j=0;j<1000;j++){}
}
}
void ST7789_Initialize2(void)
{
ST7789_InitializeSPI();
ST7789_InitializeGPIO();
ST7789_BkgLightOn22(false);
SPI_ST7789_MODE_DAT();
SPI_ST7789_CS_HIGH();
SPI_ST7789_RST_LOW();
delay_ms_ss2(100);
SPI_ST7789_RST_HIGH();
delay_ms_ss2(100);
ST7789_WriteCommand(0x11);
delay_ms_ss2(120);
ST7789_WriteCommand(0x36);
ST7789_WriteByte(0x70);
ST7789_WriteCommand(0x3A);
ST7789_WriteByte(0x05);
ST7789_WriteCommand(0xB2);
ST7789_WriteByte(0x0C);
ST7789_WriteByte(0x0C);
ST7789_WriteByte(0x00);
ST7789_WriteByte(0x33);
ST7789_WriteByte(0x33);
ST7789_WriteCommand(0xB7);
ST7789_WriteByte(0x35);
ST7789_WriteCommand(0xBB);
ST7789_WriteByte(0x19);
ST7789_WriteCommand(0xC0);
ST7789_WriteByte(0x2C);
ST7789_WriteCommand(0xC2);
ST7789_WriteByte(0x01);
ST7789_WriteCommand(0xC3);
ST7789_WriteByte(0x12);
ST7789_WriteCommand(0xC4);
ST7789_WriteByte(0x20);
ST7789_WriteCommand(0xC6);
ST7789_WriteByte(0x0F);
ST7789_WriteCommand(0xD0);
ST7789_WriteByte(0xA4);
ST7789_WriteByte(0xA1);
ST7789_WriteCommand(0xE0);
ST7789_WriteByte(0xD0);
ST7789_WriteByte(0x04);
ST7789_WriteByte(0x0D);
ST7789_WriteByte(0x11);
ST7789_WriteByte(0x13);
ST7789_WriteByte(0x2B);
ST7789_WriteByte(0x3F);
ST7789_WriteByte(0x54);
ST7789_WriteByte(0x4C);
ST7789_WriteByte(0x18);
ST7789_WriteByte(0x0D);
ST7789_WriteByte(0x0B);
ST7789_WriteByte(0x1F);
ST7789_WriteByte(0x23);
ST7789_WriteCommand(0xE1);
ST7789_WriteByte(0xD0);
ST7789_WriteByte(0x04);
ST7789_WriteByte(0x0C);
ST7789_WriteByte(0x11);
ST7789_WriteByte(0x13);
ST7789_WriteByte(0x2C);
ST7789_WriteByte(0x3F);
ST7789_WriteByte(0x44);
ST7789_WriteByte(0x51);
ST7789_WriteByte(0x2F);
ST7789_WriteByte(0x1F);
ST7789_WriteByte(0x1F);
ST7789_WriteByte(0x20);
ST7789_WriteByte(0x23);
ST7789_WriteCommand(0x21);
ST7789_WriteCommand(0x29);
}
#define LCD_1IN3_SendCommand ST7789_WriteCommand
#define LCD_1IN3_SendData_8Bit ST7789_WriteByte
void ST7789_Initialize(void)
{
ST7789_InitializeSPI();
ST7789_InitializeGPIO();
ST7789_BkgLightOn22(false);
SPI_ST7789_MODE_DAT();
SPI_ST7789_CS_HIGH();
SPI_ST7789_RST_LOW();
delay_ms_ss2(100);
SPI_ST7789_RST_HIGH();
delay_ms_ss2(100);
ST7789_WriteCommand(0x11);
delay_ms_ss2(120);
ST7789_WriteCommand(0x36);
ST7789_WriteByte(0x70);
LCD_1IN3_SendCommand(0x3A);
LCD_1IN3_SendData_8Bit(0x05);
LCD_1IN3_SendCommand(0xB2);
LCD_1IN3_SendData_8Bit(0x0C);
LCD_1IN3_SendData_8Bit(0x0C);
LCD_1IN3_SendData_8Bit(0x00);
LCD_1IN3_SendData_8Bit(0x33);
LCD_1IN3_SendData_8Bit(0x33);
LCD_1IN3_SendCommand(0xB7); //Gate Control
LCD_1IN3_SendData_8Bit(0x35);
LCD_1IN3_SendCommand(0xBB); //VCOM Setting
LCD_1IN3_SendData_8Bit(0x19);
LCD_1IN3_SendCommand(0xC0); //LCM Control
LCD_1IN3_SendData_8Bit(0x2C);
LCD_1IN3_SendCommand(0xC2); //VDV and VRH Command Enable
LCD_1IN3_SendData_8Bit(0x01);
LCD_1IN3_SendCommand(0xC3); //VRH Set
LCD_1IN3_SendData_8Bit(0x12);
LCD_1IN3_SendCommand(0xC4); //VDV Set
LCD_1IN3_SendData_8Bit(0x20);
LCD_1IN3_SendCommand(0xC6); //Frame Rate Control in Normal Mode
LCD_1IN3_SendData_8Bit(0x0F);
LCD_1IN3_SendCommand(0xB0);
LCD_1IN3_SendData_8Bit(0x00);
LCD_1IN3_SendData_8Bit(0xC8);
LCD_1IN3_SendCommand(0xD0); // Power Control 1
LCD_1IN3_SendData_8Bit(0xA4);
LCD_1IN3_SendData_8Bit(0xA1);
LCD_1IN3_SendCommand(0xE0); //Positive Voltage Gamma Control
LCD_1IN3_SendData_8Bit(0xD0);
LCD_1IN3_SendData_8Bit(0x04);
LCD_1IN3_SendData_8Bit(0x0D);
LCD_1IN3_SendData_8Bit(0x11);
LCD_1IN3_SendData_8Bit(0x13);
LCD_1IN3_SendData_8Bit(0x2B);
LCD_1IN3_SendData_8Bit(0x3F);
LCD_1IN3_SendData_8Bit(0x54);
LCD_1IN3_SendData_8Bit(0x4C);
LCD_1IN3_SendData_8Bit(0x18);
LCD_1IN3_SendData_8Bit(0x0D);
LCD_1IN3_SendData_8Bit(0x0B);
LCD_1IN3_SendData_8Bit(0x1F);
LCD_1IN3_SendData_8Bit(0x23);
LCD_1IN3_SendCommand(0xE1); //Negative Voltage Gamma Control
LCD_1IN3_SendData_8Bit(0xD0);
LCD_1IN3_SendData_8Bit(0x04);
LCD_1IN3_SendData_8Bit(0x0C);
LCD_1IN3_SendData_8Bit(0x11);
LCD_1IN3_SendData_8Bit(0x13);
LCD_1IN3_SendData_8Bit(0x2C);
LCD_1IN3_SendData_8Bit(0x3F);
LCD_1IN3_SendData_8Bit(0x44);
LCD_1IN3_SendData_8Bit(0x51);
LCD_1IN3_SendData_8Bit(0x2F);
LCD_1IN3_SendData_8Bit(0x1F);
LCD_1IN3_SendData_8Bit(0x1F);
LCD_1IN3_SendData_8Bit(0x20);
LCD_1IN3_SendData_8Bit(0x23);
LCD_1IN3_SendCommand(0x21); //Display Inversion On
LCD_1IN3_SendCommand(0x11); //Sleep Out
LCD_1IN3_SendCommand(0x29); //Display On
}
void LCD_SPI_init(){
ST7789_InitializeGPIO();
ST7789_InitializeSPI();
ST7789_Initialize();
}
#define ST7789_POS_OFFECT_X(40)
#define ST7789_POS_OFFECT_Y(53)
#define ST7789_SetArea(X1,Y1,X2,Y2)ST7789_WriteCommand(0x2A);\
ST7789_WriteWord(X1+ST7789_POS_OFFECT_X);\
ST7789_WriteWord(X2+ST7789_POS_OFFECT_X);\
ST7789_WriteCommand(0x2B);\
ST7789_WriteWord(Y1+ST7789_POS_OFFECT_Y);\
ST7789_WriteWord(Y2+ST7789_POS_OFFECT_Y);\
ST7789_WriteCommand(0x2C);
typedef void(*fn_get_res_data)(uint32_t,uint8_t*,uint32_t);
typedef struct _t_meminfo_t
{
uint32_tuiAddress;
uint32_tuiLength;
fn_get_res_datafnGetResData;
}MEM_INFO;
typedef struct _t_bmpres_t
{
uint16_tuiWidth;
uint16_tuiHeight;
MEM_INFOstRes;
}BMP_RES;
#define LCD_BMP_BUFFER_SIZE(64)
#define LCD_COLOR_BITS(16)
#define LCD_COLOR_BYTES(((LCD_COLOR_BITS-1)/8)+1)
static uint8_t G_BMP_BUFFER[LCD_BMP_BUFFER_SIZE] = {0x00};
void SCREEN_DrawBitmap(int16_t iPosX, int16_t iPosY, const BMP_RES* pcstBitmapRes)
{
/*----------------------------------*/
/* Variable Declaration*/
/*----------------------------------*/
size_tsBmpDataLength = pcstBitmapRes->uiWidth*pcstBitmapRes->uiHeight*LCD_COLOR_BYTES;
size_tsRemainingData = sBmpDataLength;
size_tsReadDataLeng;
size_tsWriteIndex;
uint32_tuiStartAddress = pcstBitmapRes->stRes.uiAddress;
/*----------------------------------*/
/* Process*/
/*----------------------------------*/
/* Setup paint area. */
ST7789_SetArea(iPosX, iPosY, iPosX+pcstBitmapRes->uiWidth-1, iPosY+pcstBitmapRes->uiHeight-1);
/* Read data and write to screen. */
while(sRemainingData > 0)
{
/* The length of the data to be read this time */
sReadDataLeng = (sRemainingData > LCD_BMP_BUFFER_SIZE)?LCD_BMP_BUFFER_SIZE:sRemainingData;
sRemainingData = sRemainingData - sReadDataLeng;
/* Read data form flash resource. */
pcstBitmapRes->stRes.fnGetResData(uiStartAddress, G_BMP_BUFFER, sReadDataLeng);
sWriteIndex = 0;
while(sWriteIndex < sReadDataLeng)
{
ST7789_WriteByte(G_BMP_BUFFER[sWriteIndex]);
sWriteIndex++;
}
uiStartAddress += sReadDataLeng;
}
}
void LCD_ShowPicture_Spi(int16_t iPosX, int16_t iPosY, u16 uiWidth,u16 uiHeight,const u8 pic[])
{
/*----------------------------------*/
/* Variable Declaration*/
/*----------------------------------*/
u16 i,j;
u32 k=0;
//uint32_tuiStartAddress = pcstBitmapRes->stRes.uiAddress;
/*----------------------------------*/
/* Process*/
/*----------------------------------*/
/* Setup paint area. */
ST7789_SetArea(iPosX, iPosY, iPosX+uiWidth-1, iPosY+uiHeight-1);
/* Read data and write to screen. */
for(i=0;i<uiWidth;i++)
{
for(j=0;j<uiHeight;j++)
{
ST7789_WriteByte(pic[k*2]);
ST7789_WriteByte(pic[k*2+1]);
k++;
}
}//uiStartAddress += sReadDataLeng;
}
void LCD_ShowPicture_Spi2(u16 x,u16 y,u16 length,u16 width,const u8 pic[])
{
u16 i,j;
u32 k=0;
LCD_Address_Set(x,y,x+length-1,y+width-1);
for(i=0;i<length;i++)
{
for(j=0;j<width;j++)
{
ST7789_WriteByte(pic[k*2]);
ST7789_WriteByte(pic[k*2+1]);
k++;
}
}
}
void SCREEN_DrawHorizontalLine(int16_t iStartX, int16_t iEndX, int16_t iY, uint16_t uiColour)
{
/*----------------------------------*/
/* Variable Declaration*/
/*----------------------------------*/
int16_t iPointX;
/*----------------------------------*/
/* Process*/
/*----------------------------------*/
ST7789_SetArea(iStartX, iY, iEndX, iY);
for(iPointX=iStartX; iPointX<=iEndX; iPointX++)
{
ST7789_WriteWord(uiColour);
}
}
#define LCD_SIZE_WIDTH(240)
#define LCD_SIZE_HEIGHT(135)
#define X_POS_MAX(LCD_SIZE_WIDTH-1)
#define Y_POS_MAX(LCD_SIZE_HEIGHT-1)
#defineDEFAULT_BKG_COLOR(0x0000)
int16_t SCREEN_Initialize(void)
{
/*----------------------------------*/
/* Process*/
/*----------------------------------*/
ST7789_Initialize();
return 0;
}
void SCREEN_ClearDisplay(void)
{
/*----------------------------------*/
/* Variable Declaration*/
/*----------------------------------*/
int16_tiPosX;
int16_tiPosY;
/*----------------------------------*/
/* Process*/
/*----------------------------------*/
ST7789_SetArea(0, 0, X_POS_MAX, Y_POS_MAX);
for(iPosY=0; iPosY<LCD_SIZE_HEIGHT; iPosY++)
{
for(iPosX=0; iPosX<LCD_SIZE_WIDTH; iPosX++)
{
ST7789_WriteWord(DEFAULT_BKG_COLOR);
}
}
}
void test_init(){
// Initialize screen device.
SCREEN_Initialize();
// Cleanup sceen display.
SCREEN_ClearDisplay();
}
void SCREEN_BackgroundLightEnable(bool bBlkOn)
{
ST7789_BkgLightOn22(bBlkOn);
}
void test_bak_led(){
// Enable screen background light.
SCREEN_BackgroundLightEnable(true);
SPI_ST7789_BLK_HIGH();
}


IP属地:河北1楼2022-10-05 17:32回复