找回密码
 立即注册
查看: 137|回复: 1

ST7789_240x240TFT自制例程

[复制链接]
已绑定手机

35

主题

91

回帖

251

积分

中级会员

积分
251
发表于 2026-10-5 16:37:05 | 显示全部楼层 |阅读模式
仅供原理参考,欢迎讨论
#include <STC8051U.H>
#define TFT_SCL P32
#define TFT_SDA P34
#define TFT_RST P33
#define TFT_DC P36
#define TFT_CS P35
#define TFT_BL P37
unsigned char code Chinese24x24[][72]={
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x02,0x06,0x0c,0x08,0x10, /*'小', 0 */
    0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x08,0x08,0x08,0x08,
    0x09,0x88,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x98,0x78,0x30,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x38,0x18,0x0c,0x0c,
    0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x0f,0x0c,0x0c,0x0c,0x0f,0x0c,0x0c,0x0c,0x0c,0x0d,0x08,0x08,0x08,0x0f, /*'屏', 1 */
    0x08,0x10,0x10,0x10,0x20,0x21,0x42,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0xff,0x00,
    0x20,0x30,0x11,0xff,0x30,0x30,0x30,0xff,0x20,0x20,0x20,0x40,0xc0,0x00,0x00,0x00,
    0x00,0x00,0xf0,0x18,0x18,0x18,0xf8,0x90,0xc0,0x80,0x10,0xf8,0x80,0x80,0x88,0xfc,
    0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
    0x00,0x00,0x00,0x3f,0x00,0x03,0x02,0x02,0x03,0x02,0x03,0x02,0x00,0x7f,0x01,0x03, /*'幕', 2 */
    0x0f,0x33,0x43,0x03,0x03,0x02,0x00,0x00,0x00,0x42,0x42,0xff,0x42,0xff,0x00,0x00,
    0xff,0x00,0xff,0x20,0x60,0xff,0x91,0x18,0xff,0x10,0x10,0x10,0x13,0x19,0x18,0x10,
    0x00,0x00,0x08,0xfc,0x00,0xc0,0x40,0x40,0xc0,0x40,0xc0,0x40,0x00,0xfc,0x00,0xc0,
    0xf8,0xdc,0xc0,0xc0,0xc0,0x80,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0c, /*',', 3 */
    0x1c,0x1c,0x0c,0x08,0x08,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x08, /*'不', 4 */
    0x30,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x0c,0x18,0x10,0x30,
    0x38,0x78,0xd9,0x98,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x10,0x00,
    0x00,0x00,0x08,0xfc,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x30,0x18,0x0c,0x0c,
    0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x10,0x0c,0x0c,0x04,0x01,0x00,0x00,0x7e,0x04,0x04,0x04,0x04,0x04,0x04, /*'过\xFD', 5 */
    0x04,0x04,0x0e,0x11,0x60,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,
    0x40,0x20,0x10,0x18,0x08,0x00,0x00,0x00,0x00,0x07,0x01,0x80,0xff,0x0f,0x00,0x00,
    0x00,0x80,0xc0,0xc0,0xc0,0xc0,0xfc,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,
    0xc0,0xc0,0xc0,0x80,0xfe,0xf8,0x00,0x00,
    0x00,0x02,0x02,0x02,0x02,0x06,0x7f,0x04,0x04,0x04,0x0c,0x08,0x08,0x08,0x08,0x1c, /*'如', 6 */
    0x03,0x00,0x01,0x03,0x04,0x18,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf3,0x22,
    0x22,0x22,0x22,0x22,0x62,0x42,0x42,0x42,0xc2,0xe2,0x33,0x12,0x02,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0xfc,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,
    0x08,0x08,0xf8,0x0c,0x0c,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08, /*'此', 7 */
    0x08,0x08,0x09,0x1e,0x78,0x20,0x00,0x00,0x00,0x02,0xc3,0x82,0x82,0x82,0x82,0x82,
    0x82,0xfe,0x82,0x83,0x82,0x82,0x82,0x82,0x82,0xba,0xc2,0x02,0x02,0x03,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x18,0x60,0x80,0x00,0x00,0x00,0x00,0x00,
    0x04,0x04,0x04,0x04,0x06,0xfc,0x00,0x00,
    0x00,0x38,0x44,0x43,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*'~', 8 */
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0xc0,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
void TFT_delay(unsigned int d){//@40.000MHz
        unsigned long edata i;
        _nop_();
        _nop_();
        i = d*10000UL-2UL;
        while (i) i--;
}
void TFT_SPIInit(){
        P3M1&=~0xfc;P3M0|=0xfc;
        P3SR&=~0xfc;P3DR&=~0xfc;
        TFT_SCL=0;TFT_SDA=0;TFT_RST=1;
        TFT_DC=0;TFT_CS=1;TFT_BL=0;
        P_SW1|=0x0c;
        SPCTL = 0xd0;
  SPSTAT = 0xc0;
}
void TFT_SPI(unsigned char byte){
        TFT_CS=0;
        SPDAT=byte;
  while (!(SPSTAT&0x80));
  SPSTAT=0xc0;
        TFT_CS=1;
}
void TFT_SetWindow(unsigned int xstart,unsigned int xend,
        unsigned int ystart,unsigned int yend){
        TFT_DC=0;
        TFT_SPI(0x2a);
        TFT_DC=1;
        TFT_SPI((unsigned char)(xstart>>8));
        TFT_SPI(xstart&0xff);
        TFT_SPI((unsigned char)(xend>>8));
        TFT_SPI(xend&0xff);
        TFT_DC=0;
        TFT_SPI(0x2b);
        TFT_DC=1;
        TFT_SPI((unsigned char)(ystart>>8));
        TFT_SPI(ystart&0xff);
        TFT_SPI((unsigned char)(yend>>8));
        TFT_SPI(yend&0xff);
        TFT_DC=0;
        TFT_SPI(0x2c);
}
void TFT_Init(){
        unsigned int i;
        TFT_SPIInit();
        TFT_RST=0;
        TFT_RST=1;
        TFT_DC=0;TFT_SPI(0x11);
        TFT_SPI(0x36);
        TFT_DC=1;TFT_SPI(0x00);
        TFT_DC=0;TFT_SPI(0x3a);
        TFT_DC=1;TFT_SPI(0x05);
        TFT_DC=0;TFT_SPI(0x21);
        TFT_SPI(0x29);
        TFT_SetWindow(0,239,0,239);
        TFT_DC=1;
        for(i=0;i<57600;i++){
                TFT_SPI(0x00);
                TFT_SPI(0x00);
        }
        TFT_BL=1;
}
void TFT_ShowChinese(unsigned int x,unsigned int y,
        unsigned char w,unsigned int c1,unsigned int c2){
        unsigned char i,j;
        TFT_SetWindow(x,x+7,y,y+23);
        TFT_DC=1;
        for(i=0;i<24;i++){
                for(j=0;j<8;j++){
                        if(Chinese24x24[w][i]&(0x80>>(j%8))){
                                TFT_SPI((unsigned char)(c1>>8));
                                TFT_SPI((unsigned char)(c1&0xff));
                        }else{
                                TFT_SPI((unsigned char)(c2>>8));
                                TFT_SPI((unsigned char)(c2&0xff));
                        }
                }
        }
        TFT_SetWindow(x+8,x+15,y,y+23);
        TFT_DC=1;
        for(i=0;i<24;i++){
                for(j=0;j<8;j++){
                        if(Chinese24x24[w][i+24]&(0x80>>(j%8))){
                                TFT_SPI((unsigned char)(c1>>8));
                                TFT_SPI((unsigned char)(c1&0xff));
                        }else{
                                TFT_SPI((unsigned char)(c2>>8));
                                TFT_SPI((unsigned char)(c2&0xff));
                        }
                }
        }
        TFT_SetWindow(x+16,x+23,y,y+23);
        TFT_DC=1;
        for(i=0;i<24;i++){
                for(j=0;j<8;j++){
                        if(Chinese24x24[w][i+48]&(0x80>>(j%8))){
                                TFT_SPI((unsigned char)(c1>>8));
                                TFT_SPI((unsigned char)(c1&0xff));
                        }else{
                                TFT_SPI((unsigned char)(c2>>8));
                                TFT_SPI((unsigned char)(c2&0xff));
                        }
                }
        }
}
void main(){
        unsigned int i;
        EAXFR=1;WTST=0x00;CKCON=0x00;
        TFT_Init();
        TFT_SetWindow(0,239,0,239);
        TFT_DC=1;
        for(i=0;i<57600;i++){
                TFT_SPI(0x7B);TFT_SPI(0xEF);
        }
        TFT_ShowChinese(0,96,0,0xF800,0x7BEF);
        TFT_ShowChinese(24,96,0,0xFFE0,0x7BEF);
        TFT_ShowChinese(48,96,1,0x07E0,0x7BEF);
        TFT_ShowChinese(72,96,2,0x07FF,0x7BEF);
        TFT_ShowChinese(96,96,3,0x001F,0x7BEF);
        TFT_ShowChinese(120,120,4,0xF81F,0x7BEF);
        TFT_ShowChinese(144,120,5,0xFFFF,0x7BEF);
        TFT_ShowChinese(168,120,6,0xFBEF,0x7BEF);
        TFT_ShowChinese(192,120,7,0xFBE0,0x7BEF);
        TFT_ShowChinese(216,120,8,0x7BFF,0x7BEF);
        while(1);
}

回复

使用道具 举报 送花

已绑定手机

35

主题

91

回帖

251

积分

中级会员

积分
251
 楼主| 发表于 2026-10-5 19:22:11 来自手机 | 显示全部楼层
效果展示
1791199280931.jpg
回复

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2026-11-8 03:07 , Processed in 0.068940 second(s), 46 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表