2930 views |4 replies
Last login 2024-11-16
Online Time 2983 hours
Prestige 11303 points
Points 675 points
MM32F031 Development Board Review 8: Simulating SPI to Drive 2.2-inch SPI Screen
[Copy link]
Some people will look down upon analog SPI, and some will not even look at it. I am very happy. Why? I am a person who sees results. That is to say, if I cannot develop SPI one day, I will not die in the spring like the song "Spring". I should persevere in using analog serial port to achieve it. I will not waste words. Here is the picture: The following is a clear red screen, it is so red.
So how to get it? It is very simple. You only need the information of this screen. The following is the information of this SPI screen:
2.2寸天马SPI串口模块资料9341_240X320_Rev2.0.zip
(15.33 MB, downloads: 35)
Put the driver and H file in the STMF103 routine in the program directory of lighting. Then add it and compile it. As for how to connect the wires, it is actually in the H file, and the order is as follows; B12-----CS A4----RES B1----DC B13----CLK B15-----DIN The following is the main function:
int main(void) { delay_init(); LED_Init(); LCD_GPIO_Init(); Lcd_Init(); Lcd_Clear(RED); while(1) //TT-· { LED1_TOGGLE(); LED2_TOGGLE(); LED3_TOGGLE(); LED4_TOGGLE(); delay_ms(1000); } } 复制代码 The following is the H file
#ifndef _LCD_DRIVER_H_ #define _LCD_DRIVER_H_ #include "HAL_conf.h" //////////////////////////////////////ó§///////////////////////////////////// //§3oáêúá ìù¨òD #define USE_HORIZONTAL 0 //¨òê·ê1óoáá 0,2ê1ó.1,ê1ó. //--------------------------------SPI SPI;0:USE Soft SPI //-------------------------áàíéè-----------------------------------------// #define LCD_X_SIZE 240 #define LCD_Y_SIZE 320 #if USE_HORIZONTAL//è1¨òáoáá #define X_MAX_PIXEL LCD_Y_SIZE #define Y_MAX_PIXEL LCD_X_SIZE #else #define X_MAX_PIXEL LCD_X_SIZE #define Y_MAX_PIXEL LCD_Y_SIZE #endif ///////////////////////////////////////////////////////////////////////////////////// #define RED 0xf800 #define GREEN 0x07e0 #define BLUE 0x001f #define WHITE 0xffff #define BLACK 0x0000 #define YELLOW 0xFFE0 #define GRAY0 0xEF7D //òé0 3165 00110 001011 00101 #define GRAY1 0x8410 //òé1 00000 000000 00000 #define GRAY2 0x4208 //òé2 1111111111011111 //±2aê3ìDòê1óμê£aSPIó úyˉ // é×óéüó úIO£ê1óèòa×éù4 IO′ éíê3é± òo§yˉê /********************************************************************************** ó ú¨òúLcd_Driver.hú¨ò£ùYóDT2¢DTàó|IO3êˉLCD_GPIO_Init() #define LCD_CTRL GPIOB //¨òTFTêyY ú #define LCD_LED GPIO_Pin_9 //PB9 áóáTFT -LED #define LCD_RS GPIO_Pin_10 //PB10áóáTFT --RS #define LCD_CS GPIO_Pin_11 //PB11 áóáTFT --CS #define LCD_RST GPIO_Pin_12 //PB12áóáTFT --RST #define LCD_SCL GPIO_Pin_13 //PB13áóáTFT -- CLK #define LCD_SDA GPIO_Pin_15 //PB15áóáTFT - SDI *************************** *************************************************** **/ #define LCD_CTRL GPIOB //¨òTFTêyY ú #define LCD_LED GPIO_Pin_9 //MCU_PB9--->>TFT --BL #define LCD_RS GPIO_Pin_10 //PB11--->>TFT --RS/DC #define LCD_CS GPIO_Pin_11 //MCU_PB11--->>TFT -- CS/CE #define LCD_RST GPIO_Pin_12 //PB10--->>TFT --RST #define LCD_SCL GPIO_Pin_13 //PB13--->>TFT --SCL/SCK #define LCD_SDA GPIO_Pin_15 //PB15 MOSI--->>TFT --SDA/DIN //#define LCD_CS_SET(x) LCD_CTRL->ODR= (LCD_CTRL->ODR&~LCD_CS)|(x ? LCD_CS:0) //òo§ ú12ù×÷óoê¨ò #define LCD_CS_SET LCD_CTRL->BSRR=LCD_CS #define LCD_RS_SET LCD_CTRL->BSRR=LCD_RS #define LCD_SDA_SET LCD_CTRL->BSRR=LCD_SDA #define LCD_SCL_SET LCD_CTRL->BSRR=LCD_SCL #define LCD_RST_SET LCD_CTRL->BSRR=LCD_RST #define LCD_LED_SET LCD_CTRL->BSRR=LCD_LED //òo§ ú02ù×÷óoê¨ò #define LCD_CS_CLR LCD_CTRL->BRR=LCD_CS #define LCD_RS_CLR LCD_CTRL->BRR=LCD_RS #define LCD_SDA_CLR LCD_CTRL->BRR=LCD_SDA #define LCD_SCL_CLR LCD_CTRL->BRR=LCD_SCL #define LCD_RST_CLR LCD_CTRL->BRR=LCD_RST #define LCD_LED_CLR LCD_CTRL->BRR=LCD_LED #define LCD_DATAOUT(x) LCD_DATA->ODR=x; //êyYê3 #define LCD_DATAIN LCD_DATA->IDR; //êyYêè #define LCD_WR_DATA(data ){\ LCD_RS_SET;\ LCD_CS_CLR;\ LCD_DATAOUT(data);\ LCD_WR_CLR;\ LCD_WR_SET;\ LCD_CS_SET;\ } void LCD_GPIO_Init(void); void Lcd_WriteIndex(u8 Index); void Lcd_WriteData(u8 Data); void Lcd_WriteReg(u8 Index,u8 Data); u16 Lcd_ReadReg(u8 LCD_Reg); void Lcd_Reset(void); void Lcd_Init(void); void Lcd_Clear(u16 Color); void Lcd_SetXY(u16 x,u16 y); void Gui_DrawPoint(u16 x,u16 y,u16 Data); unsigned int Lcd_ReadPoint(u16 x,u16 y); void Lcd_SetRegion(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd); void Lcd_WriteData_16Bit(u16 Data); #endif 复制代码 The following is the C file:
/***************************************** ************************************************** STM322 series: MiniSTM32 series MCU: STM32_F103_RBT6 12MHZ to 72MHZ ...12MHZ 12MHZ to 72MHZ 12MHZ to 72MHZ 12MHZ to 12MHZ 12MHZ to 72MHZ 12MHZ to 12MHZ 12MHZ to 72MHZ 12MHZ to 12MHZ 12MHZ to//------------------------------------------------ ------------------------------------- #define LCD_CTRL GPIOB //¨òTFTêyY ú #define LCD_LED GPIO_Pin_9 //PB9 áóáTFT -LED #define LCD_RS GPIO_Pin_10 //PB10áóáTFT --RS #define LCD_CS GPIO_Pin_11 //PB11 áóáTFT --CS #define LCD_RST GPIO_Pin_12 //PB12áóáTFT --RST #define LCD_SCL GPIO_Pin_13 //PB13áóáTFT -- CLK #define LCD_SDA GPIO_Pin_15 //PB15áóáTFT - SDI //VCC: éòó5Vò2 éòó3.3V //LED: éòó5Vò2 éòó3.3Vòê1óèòa DIO (μê1ü) //GND£oóμ′μ //μ÷£oèDèòa éüéùóIO£ éòLCD_CSóμ£LCD_LEDó3.3V£LCD_RSTóáμ¥ú′£ // éòêí·3 éóIO //ó ú¨òúLcd_Driver.hú¨ò£ //èDè±üIO󷨣ùYúμêμêóDTàó|IO3êˉLCD_GPIO_Init() //--------------- -------------------------------------------------- -------------------------- ày3ì1|üμ÷£o 1. òμ¥¢á2aê 2. ó¢ê2aêêày 3. Dê2aêêày 4. êy1ü×ìêê ay 5. íêêày 6. 2D°′ü2μ¥êày 7. ±ày3ì§3oáá/êúáD(aoêUSE_HORIZONTAL,êLcd_Driver.h) 8. ±ày3ì§3èít£aSPI/ó2tSPID( aoêUSE_HARDWARE_SPI,êLcd_Driver.h) ****** *************************************************** ***************************************/ //--------- ------------------------function------------------------- --------------------------// /******************** *************************************************** ****** * 3£ovoid LCD_GPIO_Init(void) * 1| ü£oSTM32_£aSPIùóμμGPIO3êˉ * è ú2êy£oT * 3 ú2êy£oT * μ ÷£o3êˉ£aSPIùóμGPIO ************************************* ***************************************/ void LCD_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd ( RCC_AHBPeriph_GPIOB ,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15; GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); } /************************************ ******************************************* * 3£ovoid SPIv_WriteData( u8 Data) * 1| ú2êy£oData * 3 ú2êy£oT * μ ÷£oSTM32_£aSPIáD′ò×úêyYμ×2oˉêy **************************** **********************************************/ void SPIv_WriteData(u8 Data) { unsigned char i=0; for(i=8;i>0;i--) { if(Data&0x80) LCD_SDA_SET; //ê3êyY else LCD_SDA_CLR; LCD_SCL_CLR; LCD_SCL_SET; Data<<=1; } } ///************************************************ ****************************** //* 3£ou8 SPI_WriteByte(SPI_TypeDef* SPIx,u8 Byte) //* 1 | ü£oSTM32_ó2tSPIáD′ò×úêyYμ×2oˉêy //* è ú2êy£oSPIx,Byte //* 3 ú2êy£o·μ×üêμμêyY //* μ ÷£oSTM32_ó2tSPIáD′ò×úêyYμ×2oˉêy //******************** *************************************************** ******/ //u8 SPI_WriteByte(SPI_TypeDef* SPIx,u8 Byte) //{ // while((SPIx->SR&SPI_I2S_FLAG_TXE)==RESET); //μè′y·¢í // SPIx->DR=Byte; //·¢íòbyte // while((SPIx->SR&SPI_I2S_FLAG_RXNE)==RESET);//μè′yóêíêòbyte // return SPIx->DR; / /·μêμμêyY //} ///****************************************** ************************************* //* 3£ovoid SPI_SetSpeed(SPI_TypeDef* SPIx, u8 SpeedSet) //* 1| ü£oéèSPIμùè //* è ú2êy£oSPIx,SpeedSet //* 3 ú2êy£oT //* μ ÷£oSpeedSet:1,ù;0,μíù; //**************************** *************************************************/ //void SPI_SetSpeed(SPI_TypeDef* SPIx,u8 SpeedSet) //{ // SPIx->CR1&=0XFFC7; // if(SpeedSet==1)//ù // { // SPIx->CR1|=SPI_BaudRatePrescaler_2; //Fsck=Fpclk/2 // } // else//μíù // { // SPIx->CR1|=SPI_BaudRatePrescaler_32; //Fsck=Fpclk/32 // } // SPIx->CR1|=1<< 6; //SPIéè±ê1ü //} ///*********************************************** ***************************** //* 3£oSPI2_Init(void) //* 1| ü£oSTM32_SPI2ó2t3êˉ / /* è ú2êy£oT //* 3 ú2êy£oT //* μ ÷£oSTM32_SPI2ó2t3êˉ //************************************* *******************************************/ //void SPI2_Init(void) / /{ // SPI_InitTypeDef SPI_InitStructure; // GPIO_InitTypeDef GPIO_InitStructure; // // //SPI21ü // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 3 | GPIO_Pin_15; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // GPIO_InitStructure.GPIO_S peed = GPIO_Speed_50MHz; // GPIO_Init(GPIOB, &GPIO_InitStructure ); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // GPIO_Init(GPIOB, &GPIO_InitStructure); // // //SPI2 // RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 ,ENABLE); // // SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // SPI_InitStructure.SPI_CRCPolynomial = 7; // SPI_Init(SPI2, &SPI_InitStructure); // //ê1üSPI2 // SPI_Cmd(SPI2,ENABLE); //} /******************************************* ********************************* * 3£oLcd_WriteIndex(u8 Index) * 1| ü£oòòo§áD ′ò8á * è ú2êy£oIndex ′÷μ· * 3 ú2êy£oT * μ ÷£oμ÷ó°Dèè D ÷£ú2 oˉêy ************************************************* ***************************/ void Lcd_WriteIndex(u8 Index) { LCD_CS_CLR; LCD_RS_CLR; SPIv_WriteData(Index); LCD_CS_SET; } /** *************************************************** ********************** * 3£oLcd_WriteData(u8 Data) * 1| ü£oòòo§áD′ò8êyY * è ú2êy£odat ′÷êyY * 3 ú2êy£oT * μ ÷£oò ÷¨μ·D′èêyY£ú2 oˉêy ************************************************* ***************************/ void Lcd_WriteData(u8 Data) { LCD_CS_CLR; LCD_RS_SET; SPIv_WriteData(Data); LCD_CS_SET; } /** *************************************************** ************************ * 3£ovoid LCD_WriteReg(u8 Index,u16 Data) * 1| ü£oD′′÷êyY * è ú2êy£oIndex,Data * 3 ú2êy£oT * μ ÷£o±oˉêya×éooˉêy£òIndexμ·μ′÷D′èDataμ ************************** *************************************************** / void LCD_WriteReg(u8 Index,u16 Data) { Lcd_WriteIndex(Index); Lcd_WriteData_16Bit(Data); } /************************************** **************************************** * 3£ovoid Lcd_WriteData_16Bit(u16 Data) * 1 | ü£oòòo§áD′ò16êyY * è ú2êy£oData * 3 ú2êy£oT * μ ÷£oò ÷¨μ·D′èò16êyY ********************************** ******************************************/ void Lcd_WriteData_16Bit(u16 Data) { Lcd_WriteData(Data>>8); Lcd_WriteData(Data); } /**************************************** ******************************************* * 3£ovoid Lcd_Reset(void) * 1| ü£oòo§ó2′oˉêy * è ú2êy£oT * 3 ú2êy£oT * μ ÷£oòo§3êˉ°Dè′DDò′′2ù×÷ ************************* *************************************************** ***/ void Lcd_Reset(void) { LCD_RST_CLR; delay_ms(100); LCD_RST_SET; delay_ms(50); } /******************************************* ********************************* * 3£ovoid Lcd_Init(void) * 1| ü£oòo§3ê ˉoˉêy * è ú2êy£oT * 3 ú2êy£oT * μ ÷£oòo§3êˉ_ILI9225_176X220 ************************************* ***************************************/ void Lcd_Init(void) { LCD_GPIO_Init(); //ê1ó£aSPI Lcd_Reset(); //Reset before LCD Init. //2.2inch TM2.2-G2.2 Init 20171020 Lcd_WriteIndex(0x11); Lcd_WriteData(0x00); Lcd_WriteIndex(0xCF); Lcd_WriteData(0X00); Lcd_WriteData(0XC1); Lcd_WriteData(0X30); Lcd_WriteIndex(0xED); Lcd_WriteData(0X6 4); Lcd_WriteData(0X03); Lcd_WriteData( 0X12); Lcd_WriteData(0X81); Lcd_WriteIndex(0xE8); Lcd_WriteData(0X85); Lcd_WriteData(0X11); Lcd_WriteData(0X78); Lcd_WriteIndex(0xF6); Lcd_WriteData(0X01); Lcd_WriteData(0X30); Lcd_WriteData(0X00); Lcd_WriteIndex(0xCB); Lcd_WriteData(0X39); Lcd_WriteData(0X00); Lcd_WriteData(0X34); Lcd_WriteData(0X05); Lcd_WriteIndex(0x F7); Lcd_WriteData(0X20); Lcd_WriteIndex(0xEA); Lcd_WriteData(0X00); Lcd_WriteData(0X00); Lcd_WriteIndex(0xC0); Lcd_WriteData(0X20); Lcd_WriteIndex(0xC1); Lcd_WriteData(0X11); Lcd_WriteData(0X31); Lcd_WriteData(0X3C); Lcd_WriteIndex(0xC7); Lcd_WriteData(0XA9); Lcd_WriteIndex(0x3A); Lcd_WriteData(0X55); Lcd_WriteIndex(0x36); #if USE_HORIZONTAL Lcd_WriteData(0xE8 );//oáá2êy #else Lcd_WriteData( 0x48);//êúá2êy #endif Lcd_WriteIndex(0xB1); Lcd_WriteData(0X00); Lcd_WriteData(0X18); Lcd_WriteIndex(0xB4); Lcd_WriteData(0X00); Lcd_WriteData(0X00); Lcd_WriteIndex(0xF2); Lcd_WriteData(0X00); Lcd_WriteIndex(0x26); Lcd_WriteData(0X01); Lcd_WriteIndex(0xE0); Lcd_WriteData(0X0F); Lcd_WriteData(0X17); Lcd_WriteData(0X14); 0X09); Lcd_WriteData(0X0C); Lcd_WriteData(0X06); Lcd_WriteData(0X43); Lcd_WriteData(0X75); Lcd_WriteData(0X36); Lcd_WriteData(0X08); Lcd_WriteData(0X13); Lcd_WriteData(0X05); Lcd_WriteData(0X0B); Lcd_WriteData(0X08); Lcd_WriteIndex(0xE1); Lcd_WriteData(0X00); Lcd_WriteData(0X1F); Lcd_WriteData(0X23); Lcd_WriteData(0X03); 04); Lcd_WriteData(0X39); Lcd_WriteData(0X25); Lcd_WriteData(0X4D); Lcd_WriteData(0X06); Lcd_WriteData(0X0D); Lcd_WriteData(0X0B); Lcd_WriteData(0X33); Lcd_WriteData(0X37); Lcd_WriteIndex(0x29); } /********************************************** ****** oˉêy£oLCD_Set_XY 1|ü£oéèlcdêeêμ è ú2êy£oxy×±ê ·μμ£oT ************************************* ************/ void Lcd_SetXY(u16 Xpos, u16 Ypos) { Lcd_WriteIndex(0x2A); Lcd_WriteData_16Bit(Xpos); Lcd_WriteIndex(0x2B); Lcd_WriteData_16Bit(Ypos); Lcd_WriteIndex(0x2c); } / ************************************************* oˉêy £oLCD_Set_Region 1|ü£oéèlcdêóò£ú′óòD′μêyY×ˉDD è ú2êy£oxyeμoíμ ·μμ£oT ***************************************** ********/ //éèê′° ú void Lcd_SetRegion(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd) { Lcd_WriteIndex(0x2A); Lcd_WriteData_16Bit(xStar); Lcd_WriteData_16Bit(xEnd); Lcd_WriteIndex(0x2B); Lcd_WriteData_16Bit(yStar); Lcd_WriteData_16Bit(yEnd); Lcd_WriteIndex(0x2c); /***** **************************************** oˉêy£oLCD_DrawPoint 1|ü£o- òμ è ú2êy£oxy×±êoíéêyY ·μμ£oT *********************************** ******************/ void Gui_DrawPoint(u16 x,u16 y,u16 Data) { Lcd_SetXY(x,y); Lcd_WriteData_16Bit(Data); } /******** ***************************************** oˉêy£oLcd_Clear 1|ü£oè ááoˉêy è ú2êy£oì3éCOLOR ·μμ£oT ********************************************* **********/ void Lcd_Clear(u16 Color) { unsigned int i; Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1); LCD_CS_CLR; LCD_RS_SET; for(i=0;i<x_max_pixel*y_max_pixel;i++) lcd_writedata_16bit(color);="" spiv_writedata(color="" {="">>8); SPIv_WriteData(Color); } LCD_CS_SET; } 复制代码 The next step is to challenge the hardware SPI. To know what happens next, please watch the next sharing.