【国民技术N32G430】LCD-ST7735
[复制链接]
本帖最后由 lugl4313820 于 2022-9-16 21:59 编辑
【目的】SPI驱动ST7735LCD屏
1、定义ST7735驱动IO:
/*
----------------------------------------------------------------
GND 电源地
VCC 3.3v电源
SCL PA5(SCLK)
SDA PA7(MOSI)
RES PA3
DC PA2
CS PA4
BLK PA
----------------------------------------------------------------
*/
3、宏定义相关IO,及电平转换等:
#define SPI_MASTER SPI1
#define SPI_MASTER_PERIPH RCC_APB2_PERIPH_SPI1
#define SPI_MASTER_PERIPH_GPIO RCC_AHB_PERIPH_GPIOA
#define SPI_MASTER_GPIO GPIOA
#define SPI_MASTER_GPIO_ALTERNATE GPIO_AF1_SPI1
#define SPI_MASTER_MISO_PIN GPIO_PIN_6
#define SPI_MASTER_MOSI_PIN GPIO_PIN_7
#define SPI_MASTER_CLK_PIN GPIO_PIN_5
#define SPI_MASTER_NSS_PIN GPIO_PIN_4
#define LCD_RES_PIN GPIO_PIN_3
#define LCD_DC_PIN GPIO_PIN_2
#define LCD_BLK_PIN GPIO_PIN_1
#define LCD_SCLK_Clr() GPIO_Pins_Reset(GPIOA,SPI_MASTER_CLK_PIN)
#define LCD_SCLK_Set() GPIO_Pins_Set(GPIOA,SPI_MASTER_CLK_PIN)
#define LCD_MOSI_Clr() GPIO_Pins_Reset(GPIOA,SPI_MASTER_MOSI_PIN)
#define LCD_MOSI_Set() GPIO_Pins_Set(GPIOA,SPI_MASTER_MOSI_PIN)
#define LCD_CS_Clr() GPIO_Pins_Reset(GPIOA,SPI_MASTER_NSS_PIN)
#define LCD_CS_Set() GPIO_Pins_Set(GPIOA,SPI_MASTER_NSS_PIN)
#define LCD_RES_Clr() GPIO_Pins_Reset(GPIOA,LCD_RES_PIN)
#define LCD_RES_Set() GPIO_Pins_Set(GPIOA,LCD_RES_PIN)
#define LCD_DC_Clr() GPIO_Pins_Reset(GPIOA,LCD_DC_PIN)
#define LCD_DC_Set() GPIO_Pins_Set(GPIOA,LCD_DC_PIN)
#define LCD_BLK_LOW() GPIO_Pins_Reset(GPIOA,LCD_BLK_PIN)
#define LCD_BLK_Set() GPIO_Pins_Set(GPIOA,LCD_BLK_PIN)
#define USE_HORIZONTAL 2 //设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
#if USE_HORIZONTAL==0||USE_HORIZONTAL==1
#define LCD_W 128 //0x80
#define LCD_H 160 //0xA0
#else
#define LCD_W 160
#define LCD_H 128
#endif
4、时钟初始化:
void RCC_Configuration(void)
{
/* PCLK2 = HCLK/2 */
RCC_Pclk2_Config(RCC_HCLK_DIV2);
/* Enable peripheral clocks --------------------------------------------------*/
/* spi clock enable */
RCC_APB2_Peripheral_Clock_Enable(SPI_MASTER_PERIPH );
/* GPIO Periph clock enable */
RCC_AHB_Peripheral_Clock_Enable(SPI_MASTER_PERIPH_GPIO );
}
5、IO、SPI初始化:
void LCD_GPIO_Init(void)
{
GPIO_InitType GPIO_InitStructure;
SPI_InitType SPI_InitStructure;
GPIO_Structure_Initialize(&GPIO_InitStructure);
GPIO_InitStructure.Pin = SPI_MASTER_NSS_PIN | LCD_RES_PIN | LCD_DC_PIN | LCD_BLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
/* Set the pin drive current to 4MA*/
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
/* Initialize GPIO */
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
#if SOFTWARE_SPI_ENABLE
/* Configure master pins: NSS, SCK, MISO and MOSI */
/* Confugure SPI pins as Alternate Function Push Pull */
GPIO_InitStructure.Pin = SPI_MASTER_MOSI_PIN | SPI_MASTER_CLK_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Alternate = SPI_MASTER_GPIO_ALTERNATE;
GPIO_Peripheral_Initialize(SPI_MASTER_GPIO, &GPIO_InitStructure);
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_TX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_16BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
SPI_InitStructure.NSS = SPI_NSS_SOFT;
/* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_8;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI_MASTER, &SPI_InitStructure);
SPI_NSS_Config(SPI_MASTER, SPI_NSS_SOFT);
SPI_SS_Output_Enable(SPI1);
/* Enable SPI_MASTER */
SPI_ON(SPI_MASTER);
#else
GPIO_InitStructure.Pin = SPI_MASTER_MOSI_PIN | SPI_MASTER_CLK_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
/* Set the pin drive current to 4MA*/
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
/* Initialize GPIO */
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
#endif
}
6、写一个字节函数:
/******************************************************************************
函数说明:LCD串行数据写入函数
入口数据:dat 要写入的串行数据
返回值: 无
******************************************************************************/
void LCD_Writ_Bus(uint8_t dat)
{
#if SOFTWARE_SPI_ENABLE
LCD_CS_Clr();
while((SPI1->STS & SPI_I2S_FLAG_TE) == RESET);//等待发送区空
SPI_I2S_Data_Transmit(SPI1, dat);
SysTick_Delay_Us(10);
LCD_CS_Set();
#else
uint8_t i;
LCD_CS_Clr();
for(i=0;i<8;i++)
{
LCD_SCLK_Clr();
if(dat&0x80)
{
LCD_MOSI_Set();
}
else
{
LCD_MOSI_Clr();
}
LCD_SCLK_Set();
dat<<=1;
}
LCD_CS_Set();
#endif
}
到这里移植的主要驱动写好了,其他的见附件。
7、在main.c中初始化LCD,并显示出来:
|