📄 tslcd_elt240320tp_v1_31.c
字号:
#include "app_config.h"
// _____________________
// | _______________ |
// ||<-- origin in software
// || --------- |[] |
// || / |[] |
// || --------> |[] |
// ||_______________|[] |
// |<-- LCD's origin (0x0000)
//
//define TS_ORN_PORTRAIT when your LCD is installed in vertical
// ________________
// |<-- LCD's origin (0x0000) = origin in software
// ||--------------||
// || ||
// || ------ ||
// || / ||
// || / ||
// || / ||
// || ------> ||
// ||______________||
// | [] [] [] [] [] |
// |________________|
//
/*
#ifdef TS_ORN_PORTRAIT
#define TS_SIZE_X 240
#define TS_SIZE_Y 320
#define TS_VAL_ENTRY_MOD 0x0030
#define TS_INS_GRAM_ADX TS_INS_GRAM_HOR_AD
#define TS_INS_GRAM_ADY TS_INS_GRAM_VER_AD
#define TS_INS_START_ADX TS_INS_HOR_START_AD
#define TS_INS_END_ADX TS_INS_HOR_END_AD
#define TS_INS_START_ADY TS_INS_VER_START_AD
#define TS_INS_END_ADY TS_INS_VER_END_AD
#else
#define TS_SIZE_X 320
#define TS_SIZE_Y 240
#define TS_VAL_ENTRY_MOD 0x0028
#define TS_INS_GRAM_ADX TS_INS_GRAM_VER_AD
#define TS_INS_GRAM_ADY TS_INS_GRAM_HOR_AD
#define TS_INS_START_ADX TS_INS_VER_START_AD
#define TS_INS_END_ADX TS_INS_VER_END_AD
#define TS_INS_START_ADY TS_INS_HOR_START_AD
#define TS_INS_END_ADY TS_INS_HOR_END_AD
#endif
*/
extern void delay_1ms(unsigned int n);
void TSLCDCharDisp(char charactor,ts_pos_t sx,ts_pos_t sy,ts_mode_t mode); //low level function to print a character on LCD
const unsigned char *font;
unsigned char FONT_BIT_WIDTH = 9;
unsigned char FONT_BYTE_HEIGHT = 2;
int FONT_SIZE = 18;
unsigned char FONT_WIDTH = 9;
unsigned char FONT_HEIGHT = 16;
unsigned char char_gap = 2;
unsigned char char_buf[24][4];
void buf_store(unsigned char charactor);
unsigned short font_color;
unsigned short back_color;
ts_pos_t offsetx,offsety;
ts_pos_t ts_margin_xl = 0;
ts_pos_t ts_margin_xr = TS_SIZE_X - 1;
ts_pos_t ts_margin_yu = 0;
ts_pos_t ts_margin_yl = TS_SIZE_Y - 1;
unsigned char bold_on = 0;
unsigned char first_non_zero;
unsigned char last_non_zero;
unsigned char vary_width_on = 0;
#define TSLCDGetMarginXl() ts_margin_xl
#define TSLCDGetMarginXr() ts_margin_xr
#define TSLCDGetMarginYu() ts_margin_yu
#define TSLCDGetMarginYl() ts_margin_yl
void TSLCDSetFontColor(unsigned short color) //set text's color
{
font_color = color;
}
void TSLCDSetBackColor(unsigned short color) //set back color for TS_MODE_FULL
{
back_color = color;
}
void TSLCDOutDat(unsigned short dat) //write data to LCD
{
TS_XDAT = dat >> 8;
TS_XDAT = dat;
}
void TSLCDOutDat2(unsigned char dath,unsigned char datl) //write data to LCD
{
TS_XDAT = dath;
TS_XDAT = datl;
}
void TSLCDOutIns(unsigned short ins) //write instruction to LCD
{
TS_XINS = ins >> 8;
TS_XINS = ins;
}
unsigned short TSLCDInDat(void) //read data from LCD
{
unsigned short dat = TS_XDAT << 8;
dat |= TS_XDAT;
return (dat);
}
unsigned short TSLCDInIns(void) //read data from LCD
{
unsigned short ins = TS_XINS << 8;
ins |= TS_XINS;
return (ins);
}
void TSLCDRst(void) //pulse reset signal to LCD
{
Orb(LCD_RST_DPRT,LCD_RST_PIN);
Clrb(LCD_RST_PRTC,LCD_RST_PIN);
delay_1ms(50);
Setb(LCD_RST_PRTS,LCD_RST_PIN);
}
void TSLCDInit(void) //initial LCD
{
unsigned short driver_code;
delay_1ms(100);
TSLCDOutIns(TS_INS_START_OSC);
driver_code = TSLCDInDat();
if (driver_code == 0x9320) //ILI9320
{
TSLCDOutIns(0x00E5);
TSLCDOutDat(0x8000); //set the internal vcore voltage
TSLCDOutIns(TS_INS_START_OSC);
TSLCDOutDat(0x0001); //start oscillator
delay_1ms(50);
TSLCDOutIns(TS_INS_DRIV_OUT_CTRL);
TSLCDOutDat(0x0100); //set SS, SM
TSLCDOutIns(TS_INS_DRIV_WAV_CTRL);
TSLCDOutDat(0x0700); //set 1 line inversion
TSLCDOutIns(TS_INS_ENTRY_MOD);
TSLCDOutDat(TS_VAL_ENTRY_MOD); //set GRAM write direction, BGR=0
TSLCDOutIns(TS_INS_RESIZE_CTRL);
TSLCDOutDat(0x0000); //no resizing
TSLCDOutIns(TS_INS_DISP_CTRL2);
TSLCDOutDat(0x0202); //front & back porch periods = 2
TSLCDOutIns(TS_INS_DISP_CTRL3);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_DISP_CTRL4);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_RGB_DISP_IF_CTRL1);
TSLCDOutDat(0x0000); //select system interface
TSLCDOutIns(TS_INS_FRM_MARKER_POS);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_RGB_DISP_IF_CTRL2);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL1);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL2);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL3);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL4);
TSLCDOutDat(0x0000);
delay_1ms(200);
TSLCDOutIns(TS_INS_POW_CTRL1);
TSLCDOutDat(0x17B0);
TSLCDOutIns(TS_INS_POW_CTRL2);
TSLCDOutDat(0x0137);
delay_1ms(50);
TSLCDOutIns(TS_INS_POW_CTRL3);
TSLCDOutDat(0x013C);
delay_1ms(50);
TSLCDOutIns(TS_INS_POW_CTRL4);
TSLCDOutDat(0x1400);
TSLCDOutIns(TS_INS_POW_CTRL7);
TSLCDOutDat(0x0007);
delay_1ms(50);
TSLCDOutIns(TS_INS_GRAM_HOR_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_GRAM_VER_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_GAMMA_CTRL1);
TSLCDOutDat(0x0007);
TSLCDOutIns(TS_INS_GAMMA_CTRL2);
TSLCDOutDat(0x0504);
TSLCDOutIns(TS_INS_GAMMA_CTRL3);
TSLCDOutDat(0x0703);
TSLCDOutIns(TS_INS_GAMMA_CTRL4);
TSLCDOutDat(0x0002);
TSLCDOutIns(TS_INS_GAMMA_CTRL5);
TSLCDOutDat(0x0707);
TSLCDOutIns(TS_INS_GAMMA_CTRL6);
TSLCDOutDat(0x0406);
TSLCDOutIns(TS_INS_GAMMA_CTRL7);
TSLCDOutDat(0x0006);
TSLCDOutIns(TS_INS_GAMMA_CTRL8);
TSLCDOutDat(0x0404);
TSLCDOutIns(TS_INS_GAMMA_CTRL9);
TSLCDOutDat(0x0700);
TSLCDOutIns(TS_INS_GAMMA_CTRL10);
TSLCDOutDat(0x0A08);
TSLCDOutIns(TS_INS_HOR_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_HOR_END_AD);
TSLCDOutDat(0x00EF);
TSLCDOutIns(TS_INS_VER_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_VER_END_AD);
TSLCDOutDat(0x013F);
TSLCDOutIns(TS_INS_GATE_SCAN_CTRL1);
TSLCDOutDat(0x2700);
TSLCDOutIns(TS_INS_GATE_SCAN_CTRL2);
TSLCDOutDat(0x0001);
TSLCDOutIns(TS_INS_GATE_SCAN_CTRL3);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG1_DISP_POS);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG1_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG1_END_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG2_DISP_POS);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG2_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG2_END_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL1);
TSLCDOutDat(0x0010);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL2);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL3);
TSLCDOutDat(0x0003);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL4);
TSLCDOutDat(0x0110);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL5);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL6);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_DISP_CTRL1);
TSLCDOutDat(0x0173);
}
if (driver_code == 0x9325) //ILI9325
{
TSLCDOutIns(0x00E3);
TSLCDOutDat(0x3008); //set the internal timing
TSLCDOutIns(0x00E7);
TSLCDOutDat(0x0012); //set the internal timing
TSLCDOutIns(0x00EF);
TSLCDOutDat(0x1231); //set the internal timing
TSLCDOutIns(TS_INS_START_OSC);
TSLCDOutDat(0x0001); //start oscillator
delay_1ms(50);
TSLCDOutIns(TS_INS_DRIV_OUT_CTRL);
TSLCDOutDat(0x0100); //set SS, SM
TSLCDOutIns(TS_INS_DRIV_WAV_CTRL);
TSLCDOutDat(0x0700); //set 1 line inversion
TSLCDOutIns(TS_INS_ENTRY_MOD);
TSLCDOutDat(TS_VAL_ENTRY_MOD); //set GRAM write direction, BGR=0
TSLCDOutIns(TS_INS_RESIZE_CTRL);
TSLCDOutDat(0x0000); //no resizing
TSLCDOutIns(TS_INS_DISP_CTRL2);
TSLCDOutDat(0x0202); //front & back porch periods = 2
TSLCDOutIns(TS_INS_DISP_CTRL3);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_DISP_CTRL4);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_RGB_DISP_IF_CTRL1);
TSLCDOutDat(0x0000); //select system interface
TSLCDOutIns(TS_INS_FRM_MARKER_POS);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_RGB_DISP_IF_CTRL2);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL1);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL2);
TSLCDOutDat(0x0007);
TSLCDOutIns(TS_INS_POW_CTRL3);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_POW_CTRL4);
TSLCDOutDat(0x0000);
delay_1ms(200);
TSLCDOutIns(TS_INS_POW_CTRL1);
TSLCDOutDat(0x1690);
TSLCDOutIns(TS_INS_POW_CTRL2);
TSLCDOutDat(0x0227); //TSLCDOutDat(0x0137);
delay_1ms(50);
TSLCDOutIns(TS_INS_POW_CTRL3);
TSLCDOutDat(0x001A); //TSLCDOutDat(0x013C);
delay_1ms(50);
TSLCDOutIns(TS_INS_POW_CTRL4);
TSLCDOutDat(0x1800); //TSLCDOutDat(0x1400);
TSLCDOutIns(TS_INS_POW_CTRL7);
TSLCDOutDat(0x002A); //TSLCDOutDat(0x0007);
delay_1ms(50);
TSLCDOutIns(TS_INS_GRAM_HOR_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_GRAM_VER_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_GAMMA_CTRL1);
TSLCDOutDat(0x0007);
TSLCDOutIns(TS_INS_GAMMA_CTRL2);
TSLCDOutDat(0x0605);
TSLCDOutIns(TS_INS_GAMMA_CTRL3);
TSLCDOutDat(0x0106);
TSLCDOutIns(TS_INS_GAMMA_CTRL4);
TSLCDOutDat(0x0206);
TSLCDOutIns(TS_INS_GAMMA_CTRL5);
TSLCDOutDat(0x0808);
TSLCDOutIns(TS_INS_GAMMA_CTRL6);
TSLCDOutDat(0x0007);
TSLCDOutIns(TS_INS_GAMMA_CTRL7);
TSLCDOutDat(0x0201);
TSLCDOutIns(TS_INS_GAMMA_CTRL8);
TSLCDOutDat(0x0007);
TSLCDOutIns(TS_INS_GAMMA_CTRL9);
TSLCDOutDat(0x0602);
TSLCDOutIns(TS_INS_GAMMA_CTRL10);
TSLCDOutDat(0x0808);
TSLCDOutIns(TS_INS_HOR_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_HOR_END_AD);
TSLCDOutDat(0x00EF);
TSLCDOutIns(TS_INS_VER_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_VER_END_AD);
TSLCDOutDat(0x013F);
TSLCDOutIns(TS_INS_GATE_SCAN_CTRL1);
TSLCDOutDat(0xA700);
TSLCDOutIns(TS_INS_GATE_SCAN_CTRL2);
TSLCDOutDat(0x0001);
TSLCDOutIns(TS_INS_GATE_SCAN_CTRL3);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG1_DISP_POS);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG1_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG1_END_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG2_DISP_POS);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG2_START_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PART_IMG2_END_AD);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL1);
TSLCDOutDat(0x0010);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL2);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL3);
TSLCDOutDat(0x0003);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL4);
TSLCDOutDat(0x0110);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL5);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_PANEL_IF_CTRL6);
TSLCDOutDat(0x0000);
TSLCDOutIns(TS_INS_DISP_CTRL1);
TSLCDOutDat(0x0133);
}
}
//show picture from Bg
void TSLCDShowPicBack(ts_pos_t sx,ts_pos_t ex,ts_pos_t sy,ts_pos_t ey,const unsigned short *pic,unsigned int total_x,unsigned int total_y,ts_mode_t mode)
//show picture from code memory with specific size
{
unsigned long k,p;
unsigned short color;
unsigned int x,y;
unsigned int i,j;
p=sy*total_x+sx;
k=p;
if (sx < ts_margin_xl)
sx = ts_margin_xl;
if (ex > ts_margin_xr)
ex = ts_margin_xr;
if (sy < ts_margin_yu)
sy = ts_margin_yu;
if (ey > ts_margin_yl)
ey = ts_margin_yl;
TSLCDOutIns(TS_INS_START_ADX);
TSLCDOutDat(sx);
TSLCDOutIns(TS_INS_END_ADX);
TSLCDOutDat(ex);
TSLCDOutIns(TS_INS_GRAM_ADX);
TSLCDOutDat(sx);
x = ex - sx + 1;
#ifndef TS_ORN_PORTRAIT
sy = TS_SIZE_Y - 1 - sy; // mirror start y address
ey = TS_SIZE_Y - 1 - ey; // mirror end y address
TSLCDOutIns(TS_INS_START_ADY);
TSLCDOutDat(ey);
TSLCDOutIns(TS_INS_END_ADY);
TSLCDOutDat(sy);
TSLCDOutIns(TS_INS_GRAM_ADY);
TSLCDOutDat(sy);//fix from bug of v1_00
y = sy - ey + 1;
#else
TSLCDOutIns(TS_INS_START_ADY);
TSLCDOutDat(sy);
TSLCDOutIns(TS_INS_END_ADY);
TSLCDOutDat(ey);
TSLCDOutIns(TS_INS_GRAM_ADY);
TSLCDOutDat(sy);
y = ey - sy + 1;
#endif
TSLCDOutIns(TS_INS_RW_GRAM);
if (mode == TS_MODE_FULL)
{
for (j=sy; j<(sy+y); j++)
for (i=p; i<p+x; i++)
{
TSLCDOutDat(code(pic[k]));
k++;
}
}
else
if (mode == TS_MODE_NORMAL)
{
for (j=sy; j<(sy+y); j++)
{
for (i=p; i<(p+x); i++)
{
if (code(pic[k]) == TS_COL_WHITE)
{
color = TSLCDInDat(); // ignore invalid data
color = TSLCDInDat();
TSLCDOutDat(color);
}
else
{
TSLCDOutDat(code(pic[k]));
}
k++;
}
k+=total_x-x;
}
}
}
void TSLCDShowPic2(ts_pos_t sx,ts_pos_t ex,ts_pos_t sy,ts_pos_t ey,const unsigned short *pic,ts_mode_t mode)
//show picture from code memory with specific size
{
unsigned long k = 0;
unsigned short color;
unsigned int x,y;
unsigned int i,j;
if (sx < ts_margin_xl)
sx = ts_margin_xl;
if (ex > ts_margin_xr)
ex = ts_margin_xr;
if (sy < ts_margin_yu)
sy = ts_margin_yu;
if (ey > ts_margin_yl)
ey = ts_margin_yl;
TSLCDOutIns(TS_INS_START_ADX);
TSLCDOutDat(sx);
TSLCDOutIns(TS_INS_END_ADX);
TSLCDOutDat(ex);
TSLCDOutIns(TS_INS_GRAM_ADX);
TSLCDOutDat(sx);
x = ex - sx + 1;
#ifndef TS_ORN_PORTRAIT
sy = TS_SIZE_Y - 1 - sy; // mirror start y address
ey = TS_SIZE_Y - 1 - ey; // mirror end y address
TSLCDOutIns(TS_INS_START_ADY);
TSLCDOutDat(ey);
TSLCDOutIns(TS_INS_END_ADY);
TSLCDOutDat(sy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -