本帖最后由 gaoyang9992006 于 2024-9-11 19:58 编辑
本次实验使用的是液晶屏型号为ST7735S,分辨率为160*80的IPS屏幕,色彩顺序为BGR。
1、创建工程,创建MCC工程,并添加ST7735库文件。
2、配置IO,添加SPI Host模块,添加delay支持。
为方便插拔屏幕模块我们选取这一组接口
配置如下图所示
实物图如下所示,方便直接将排针插在板子上。
3、接下来配置SPI
4、将IO的操作映射到ST7735库函数
- void ST7735_SPI_RST_SetLow(void)
- {
- RST_SetLow();
- }
- void ST7735_SPI_RST_SetHigh(void)
- {
- RST_SetHigh();
- }
- void ST7735_SPI_CS_SetLow(void)
- {
- CS_SetLow();
- }
- void ST7735_SPI_CS_SetHigh(void)
- {
- CS_SetHigh();
- }
- void ST7735_SPI_DC_SetLow(void)
- {
- DC_SetLow();
- }
- void ST7735_SPI_DC_SetHigh(void)
- {
- DC_SetHigh();
- }
5、重要的SPI数据发送函数实现
MCC生成的SPI库函数中有
- while (!PIR5bits.SSP1IF)
- {
- // Wait for flag to get set
- }
- PIR5bits.SSP1IF = 0;
只需要将该函数映射到ST7735的字节发送函数即可。
- void ST7735_SPI_SendByte(uint8_t byte)
- {
- SPI1_ByteWrite(byte);
- }
6、编写测试函数
- #include "mcc_generated_files/system/system.h"
- #include"ST7735S/st7735.h"
- /*
- Main application
- */
- int main(void)
- {
- SYSTEM_Initialize();
- // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
- // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
- // Use the following macros to:
- // Enable the Global Interrupts
- //INTERRUPT_GlobalInterruptEnable();
- // Disable the Global Interrupts
- //INTERRUPT_GlobalInterruptDisable();
- // Enable the Peripheral Interrupts
- //INTERRUPT_PeripheralInterruptEnable();
- // Disable the Peripheral Interrupts
- //INTERRUPT_PeripheralInterruptDisable();
- SPI1_Open(HOST_CONFIG);
- ST7735_Init();
- ST7735_FillScreen(ST7735_BLACK);
- ST7735_DrawRectangle(0,24,160,80,ST7735_YELLOW);
- ST7735_DrawRectangle(20,20+24,120,20,ST7735_BLUE);
- ST7735_DrawRectangle(40,30+24,80,20,ST7735_RED);
- while(1)
- {
- LED0_SetLow();
- DELAY_milliseconds(500);
- LED0_SetHigh();
- DELAY_milliseconds(500);
- ST7735_Init();
- ST7735_FillScreen(ST7735_BLACK);
- ST7735_DrawRectangle(0,0+24,10,5,ST7735_YELLOW);
- ST7735_DrawRectangle(15,5+24,10,20,ST7735_BLUE);
- ST7735_DrawRectangle(30,25+24,10,10,ST7735_RED);
- }
- }
最后看运行效果
|