IAR for STM8L标准库驱动ST7735 1.8’'LCD显示
✨STM8驱动ST7735 1.8’'LCD屏幕的话,自己移植的话,可以参考stm32标准库驱动来移植,GPIO的操作方式和STM32标准库函数名都一致,移植起来改动量很少,这仅针对软件驱动方式。
🌿本移植代码是基于中景园的屏幕驱动资料参考移植过来的。厂家所给的驱动资料中并不带STM8系列的驱动示例代码。 🌻驱动显示效果 👉🏻驱动屏幕资料可以前往《1.8寸TFT LCD128X160 ST7735S SPI串口屏驱动示例》 🔖本代码是基于STM8L052R8单片机驱动的,对于STM8L系列基本通用,其他系列需要做修改,并不能通用。
📓LCD核心驱动代码
# include "lcd_init.h"
# include "delay.h"
static void LCD_GPIO_Init ( void )
{
GPIO_Init ( GPIOD, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Slow ) ;
GPIO_Init ( GPIOD, GPIO_Pin_1 , GPIO_Mode_Out_PP_High_Slow ) ;
GPIO_Init ( GPIOD, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Slow ) ;
GPIO_Init ( GPIOD, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Slow ) ;
GPIO_Init ( GPIOD, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Slow ) ;
GPIO_Init ( GPIOE, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Slow ) ;
GPIO_Init ( GPIOD, GPIO_Pin_3, GPIO_Mode_Out_PP_High_Slow ) ;
}
void LCD_Writ_Bus ( u8 dat )
{
u8 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 ( ) ;
}
void LCD_WR_DATA8 ( u8 dat )
{
LCD_Writ_Bus ( dat ) ;
}
void LCD_WR_DATA ( u16 dat )
{
LCD_Writ_Bus ( dat >> 8 ) ;
LCD_Writ_Bus ( dat ) ;
}
void LCD_WR_REG ( u8 dat )
{
LCD_DC_Clr ( ) ;
LCD_Writ_Bus ( dat ) ;
LCD_DC_Set ( ) ;
}
void LCD_Address_Set ( u16 x1, u16 y1, u16 x2, u16 y2 )
{
if ( USE_HORIZONTAL == 0 )
{
LCD_WR_REG ( 0x2a ) ;
LCD_WR_DATA ( x1 ) ;
LCD_WR_DATA ( x2 ) ;
LCD_WR_REG ( 0x2b ) ;
LCD_WR_DATA ( y1 ) ;
LCD_WR_DATA ( y2 ) ;
LCD_WR_REG ( 0x2c ) ;
}
else if ( USE_HORIZONTAL == 1 )
{
LCD_WR_REG ( 0x2a ) ;
LCD_WR_DATA ( x1 ) ;
LCD_WR_DATA ( x2 ) ;
LCD_WR_REG ( 0x2b ) ;
LCD_WR_DATA ( y1 ) ;
LCD_WR_DATA ( y2 ) ;
LCD_WR_REG ( 0x2c ) ;
}
else if ( USE_HORIZONTAL == 2 )
{
LCD_WR_REG ( 0x2a ) ;
LCD_WR_DATA ( x1 ) ;
LCD_WR_DATA ( x2 ) ;
LCD_WR_REG ( 0x2b ) ;
LCD_WR_DATA ( y1 ) ;
LCD_WR_DATA ( y2 ) ;
LCD_WR_REG ( 0x2c ) ;
}
else
{
LCD_WR_REG ( 0x2a ) ;
LCD_WR_DATA ( x1 ) ;
LCD_WR_DATA ( x2 ) ;
LCD_WR_REG ( 0x2b ) ;
LCD_WR_DATA ( y1 ) ;
LCD_WR_DATA ( y2 ) ;
LCD_WR_REG ( 0x2c ) ;
}
}
void LCD_ST7735_Init ( void )
{
LCD_GPIO_Init ( ) ;
LCD_RES_Clr ( ) ;
delay_ms ( 100 ) ;
LCD_RES_Set ( ) ;
delay_ms ( 100 ) ;
LCD_BLK_Set ( ) ;
delay_ms ( 100 ) ;
LCD_WR_REG ( 0x11 ) ;
delay_ms ( 120 ) ;
LCD_WR_REG ( 0xB1 ) ;
LCD_WR_DATA8 ( 0x05 ) ;
LCD_WR_DATA8 ( 0x3C ) ;
LCD_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CD_WR_DATA8 ( 0x2E ) ;
LCD_WR_DATA8 ( 0x30 ) ;
LCD_WR_DATA8 ( 0x25 ) ;
LCD_WR_DATA8 ( 0x2A ) ;
LCD_WR_DATA8 ( 0x28 ) ;
LCD_WR_DATA8 ( 0x26 ) ;
LCD_WR_DATA8 ( 0x2E ) ;
LCD_WR_DATA8 ( 0x3A ) ;
LCD_WR_DATA8 ( 0x00 ) ;
LCD_WR_DATA8 ( 0x01 ) ;
LCD_WR_DATA8 ( 0x03 ) ;
LCD_WR_DATA8 ( 0x13 ) ;
LCD_WR_REG ( 0xE1 ) ;
LCD_WR_DATA8 ( 0x04 ) ;
LCD_WR_DATA8 ( 0x16 ) ;
LCD_WR_DATA8 ( 0x06 ) ;
LCD_WR_DATA8 ( 0x0D ) ;
LCD_WR_DATA8 ( 0x2D ) ;
LCD_WR_DATA8 ( 0x26 ) ;
LCD_WR_DATA8 ( 0x23 ) ;
LCD_WR_DATA8 ( 0x27 ) ;
LCD_WR_DATA8 ( 0x27 ) ;
LCD_WR_DATA8 ( 0x25 ) ;
LCD_WR_DATA8 ( 0x2D ) ;
LCD_WR_DATA8 ( 0x3B ) ;
LCD_WR_DATA8 ( 0x00 ) ;
LCD_WR_DATA8 ( 0x01 ) ;
LCD_WR_DATA8 ( 0x04 ) ;
LCD_WR_DATA8 ( 0x13 ) ;
LCD_WR_REG ( 0x3A ) ;
LCD_WR_DATA8 ( 0x05 ) ;
LCD_WR_REG ( 0x29 ) ;
}
# ifndef __LCD_INIT_H
# define __LCD_INIT_H
# include "stm8l15x.h"
# define USE_HORIZONTAL 1
# if USE_HORIZONTAL== 0 || USE_HORIZONTAL== 1
# define LCD_W 128
# define LCD_H 160
# else
# define LCD_W 160
# define LCD_H 128
# endif
# define LCD_SCLK_Clr ( ) GPIO_ResetBits ( GPIOD, GPIO_Pin_2)
# define LCD_SCLK_Set ( ) GPIO_SetBits ( GPIOD, GPIO_Pin_2)
# define LCD_MOSI_Clr ( ) GPIO_ResetBits ( GPIOD, GPIO_Pin_5)
# define LCD_MOSI_Set ( ) GPIO_SetBits ( GPIOD, GPIO_Pin_5)
# define LCD_RES_Clr ( ) GPIO_ResetBits ( GPIOD, GPIO_Pin_4)
# define LCD_RES_Set ( ) GPIO_SetBits ( GPIOD, GPIO_Pin_4)
# define LCD_DC_Clr ( ) GPIO_ResetBits ( GPIOD, GPIO_Pin_6)
# define LCD_DC_Set ( ) GPIO_SetBits ( GPIOD, GPIO_Pin_6)
# define LCD_CS_Clr ( ) GPIO_ResetBits ( GPIOD, GPIO_Pin_1)
# define LCD_CS_Set ( ) GPIO_SetBits ( GPIOD, GPIO_Pin_1)
# define LCD_BLK_Clr ( ) GPIO_ResetBits ( GPIOE, GPIO_Pin_2)
# define LCD_BLK_Set ( ) GPIO_SetBits ( GPIOE, GPIO_Pin_2)
void LCD_Writ_Bus ( u8 dat) ;
void LCD_WR_DATA8 ( u8 dat) ;
void LCD_WR_DATA ( u16 dat) ;
void LCD_WR_REG ( u8 dat) ;
void LCD_Address_Set ( u16 x1, u16 y1, u16 x2, u16 y2) ;
void LCD_ST7735_Init ( void ) ;
# endif
int main ( void )
{
float t = 0 ;
MCU_Config ( ) ;
LCD_ST7735_Init ( ) ;
LCD_Fill ( 0 , 0 , LCD_W, LCD_H, WHITE ) ;
uint8_t i;
uint16_t frames = 0 ;
uint16_t fps = 0 ;
uint16_t loops = 0 ;
uint16_t loops_overal = 0 ;
char fps_c[ 15 ] ;
for ( ; ; )
{
if ( ! timer_1s )
{
timer_1s = 1000 ;
fps = frames;
frames = 0 ;
loops_overal = loops;
loops = 0 ;
}
LCD_ShowChinese ( 0 , 0 , "中景园电子" , RED, WHITE, 24 , 0 ) ;
LCD_ShowString ( 24 , 30 , "LCD_W:" , RED, WHITE, 16 , 0 ) ;
LCD_ShowIntNum ( 72 , 30 , LCD_W, 3 , RED, WHITE, 16 ) ;
LCD_ShowString ( 24 , 50 , "LCD_H:" , RED, WHITE, 16 , 0 ) ;
LCD_ShowIntNum ( 72 , 50 , LCD_H, 3 , RED, WHITE, 16 ) ;
LCD_ShowFloatNum1 ( 24 , 70 , t, 4 , RED, WHITE, 16 ) ;
LCD_ShowPicture ( 70 , 80 , 40 , 40 , gImage_1 ) ;
sprintf ( fps_c, " FPS: %02d" , fps ) ;
LCD_ShowString ( 0 , 122 , ( u8* ) fps_c, RED, WHITE, 16 , 0 ) ;
sprintf ( fps_c, " LOOP: %04d %03d" , loops_overal, i ) ;
LCD_ShowString ( 0 , 142 , ( u8* ) fps_c, RED, WHITE, 16 , 0 ) ;
frames++ ;
loops++ ;
i++ ;
t += 0.11 ;
}
}
📚驱动工程代码
链接:https:
提取码:z072