找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2305|回复: 0
打印 上一主题 下一主题
收起左侧

TFT2.4汉字2样例程序 51单片机开发板程序

[复制链接]
跳转到指定楼层
楼主
单片机TFT2.4彩屏显示程序.

完整源码下载:
TFT2.4汉字2样例程序.zip (111.47 KB, 下载次数: 23)


预览:

  1. #include  "reg52.h"
  2. #include "string.h"
  3. #include "stdio.h"
  4.   char tempbuffer[10];
  5. #define WINDOW_XADDR_START        0x0050 // Horizontal Start Address Set
  6. #define WINDOW_XADDR_END        0x0051 // Horizontal End Address Set
  7. #define WINDOW_YADDR_START        0x0052 // Vertical Start Address Set
  8. #define WINDOW_YADDR_END        0x0053 // Vertical End Address Set
  9. #define GRAM_XADDR                    0x0020 // GRAM Horizontal Address Set
  10. #define GRAM_YADDR                    0x0021 // GRAM Vertical Address Set
  11. #define GRAMWR                             0x0022 // memory write

  12. /* LCD color */
  13. #define White          0xFFFF
  14. #define Black          0x0000
  15. #define Blue           0x001F
  16. #define Blue2          0x051F
  17. #define Red            0xF800
  18. #define Magenta        0xF81F
  19. #define Green          0x07E0
  20. #define Cyan           0x7FFF
  21. #define Yellow         0xFFE0

  22. sbit CS=P2^7;                //片选
  23. sbit RES=P2^5;                //复位
  24. sbit RS=P2^4;                //数据/命令选择
  25. sbit RW=P2^3;
  26.                     //数据口使用P1
  27. //====================================================//

  28. void main (void);
  29. void ILI9325_Initial(void);
  30. void show_colour_bar (void);
  31. void Init_data(unsigned char x, unsigned int y);
  32. void Write_Cmd(unsigned char DH,unsigned char DL);
  33. void Write_Data(unsigned char DH,unsigned char DL);
  34. void delayms(unsigned int tt);
  35. void show_photo(void);
  36. void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color);
  37. unsigned char code pic[];
  38. void  Write_Data_U16(unsigned int y);
  39. static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1);
  40. void ClearScreen(unsigned int bColor);
  41. void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
  42. void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
  43. void LCD_PutString(unsigned short x, unsigned short y, char *s, unsigned int fColor, unsigned int bColor);

  44. void LCD_PutChar8x8(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
  45. void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor);
  46. void PutGB3232(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor);
  47. //================================================//

  48. void  main(void)
  49. {   
  50. float j=80.12;
  51.         CS=1;
  52.         delayms(25);
  53.         RES=0;
  54.         delayms(150);
  55.         RES=1;
  56.         delayms(25);
  57.         ILI9325_Initial();
  58.         while(1)
  59.         {
  60.        
  61.                    sprintf(tempbuffer,"%4.5f",j);

  62. ClearScreen(0x0);

  63. //show_colour_bar();       


  64. //LCD_PutString(2,40,"     周期",Red,Blue);                  ///前景颜色和背景颜色,可以直接预定义,也可以直接填入16进制数字
  65. LCD_PutString(40,60,tempbuffer,Blue2,Yellow);
  66. //LCD_PutString(40,80,"频率",Magenta,Green);
  67. LCD_PutString(40,100,"周期",0x07e0,0xf800);
  68. //LCD_PutString(40,120,"专业开发板工控板",0xF800,Yellow);
  69. LCD_PutString(40,140,"频率",0xF800,Yellow);
  70. //LCD_PutString(20,160,"abcdefghijklmnopqistuvwxyz",0xF800,Yellow);
  71. LCD_PutString(20,180,"幅度值",0xF800,Yellow);
  72. PutGB3232(20,200,"我",Blue,Yellow);        //写入32x32汉字
  73. PutGB3232(60,200,"爱",Blue,Red);
  74. PutGB3232(100,200,"单",Blue,Magenta);
  75. PutGB3232(140,200,"片",Blue,Green);
  76. PutGB3232(180,200,"机",Blue,Cyan);       
  77. //PutGB3232(20,240,"硕",Blue,0X00);       
  78. //PutGB3232(60,240,"耀",Blue,0X00);       
  79. //PutGB3232(100,240,"电",Blue,0X00);       
  80. //PutGB3232(140,240,"子",Blue,0X00);       
  81. //PutGB3232(180,240,"科",Blue,0X00);
  82. //PutGB3232(20,280,"技",Blue,0X00);               
  83. //PutGB3232(60,280,"欢",Blue,0X00);       
  84. //PutGB3232(100,280,"迎",Blue,0X00);       
  85. //PutGB3232(140,280,"你",Blue,0X00);       
  86. //PutGB3232(180,280,"!",Blue,0X00);       

  87. //        show_photo();

  88.                 while(1);       


  89.         }
  90. }


  91. void ClearScreen(unsigned int bColor)
  92. {
  93. unsigned int i,j;
  94. LCD_SetPos(0,240,0,320);//320x240
  95. for (i=0;i<325;i++)
  96.         {
  97.        
  98.            for (j=0;j<240;j++)
  99.                Write_Data_U16(bColor);

  100.         }
  101. }

  102. /////////////////////////////////////////////////////////////
  103. #include "8X16.h"
  104. void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor)
  105. {
  106. unsigned int i,j;
  107. LCD_SetPos(x,x+8-1,y,y+16-1);
  108. for(i=0; i<16;i++) {
  109.                 unsigned char m=Font8x16[c*16+i];
  110.                 for(j=0;j<8;j++) {
  111.                         if((m&0x80)==0x80) {
  112.                                 Write_Data_U16(fColor);
  113.                                 }
  114.                         else {
  115.                                 Write_Data_U16(bColor);
  116.                                 }
  117.                         m<<=1;
  118.                         }
  119.                 }
  120. }


  121. void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor) {

  122.                 LCD_PutChar8x16( x, y, c, fColor, bColor );
  123.         }


  124. #include "GB1616.h"        //16*16汉字字模

  125. void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor){
  126.         unsigned int i,j,k;

  127.         LCD_SetPos(x,  x+16-1,y, y+16-1);

  128.         for (k=0;k<64;k++) { //64标示自建汉字库中的个数,循环查询内码
  129.           if ((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])){
  130.             for(i=0;i<32;i++) {
  131.                   unsigned short m=codeGB_16[k].Msk[i];
  132.                   for(j=0;j<8;j++) {
  133.                         if((m&0x80)==0x80) {
  134.                                 Write_Data_U16(fColor);
  135.                                 }
  136.                         else {
  137.                                 Write_Data_U16(bColor);
  138.                                 }
  139.                         m<<=1;
  140.                         }
  141.                   }
  142.                 }  
  143.           }       
  144.         }

  145.         #include "GB3232.h"        //32*32汉字字模

  146. void PutGB3232(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor){
  147.         unsigned int i,j,k;

  148.         LCD_SetPos(x,  x+32-1,y, y+32-1);

  149.         for (k=0;k<15;k++) { //15标示自建汉字库中的个数,循环查询内码
  150.           if ((codeGB_32[k].Index[0]==c[0])&&(codeGB_32[k].Index[1]==c[1])){
  151.             for(i=0;i<128;i++) {
  152.                   unsigned short m=codeGB_32[k].Msk[i];
  153.                   for(j=0;j<8;j++) {
  154.                         if((m&0x80)==0x80) {
  155.                                 Write_Data_U16(fColor);
  156.                                 }
  157.                         else {
  158.                                 Write_Data_U16(bColor);
  159.                                 }
  160.                         m<<=1;
  161.                         }
  162.                   }
  163.                 }  
  164.           }       
  165.         }

  166. void LCD_PutString(unsigned short x, unsigned short y, unsigned char *s, unsigned int fColor, unsigned int bColor) {
  167.          unsigned char l=0;
  168.         while(*s) {
  169.                 if( *s < 0x80)
  170.                     {
  171.                         LCD_PutChar(x+l*8,y,*s,fColor,bColor);
  172.                         s++;l++;
  173.                         }
  174.                 else
  175.                     {
  176.                         PutGB1616(x+l*8,y,(unsigned char*)s,fColor,bColor);
  177.                         s+=2;l+=2;
  178.                         }
  179.                 }
  180.         }
  181. //==================== 显示R G B 顔色 ====================//



  182. void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
  183. {
  184.         unsigned int i,j;
  185.         //address_set();
  186.         LCD_SetPos(x0,x1,y0,y1);
  187.         for (i=y0;i<=y1;i++)
  188.         {
  189.            for (j=x0;j<=x1;j++)
  190.                Write_Data_U16(Color);

  191.         }


  192. }

  193. //====================== 显示彩条 ======================//
  194. void show_colour_bar (void)

  195. {
  196.         int V,H;
  197.         LCD_SetPos(0,240,0,320);//320x240

  198.         for(H=0;H<240;H++)
  199.         {
  200.                 for(V=0;V<40;V++)
  201.                 Write_Data(0xf8,0x00);
  202.         }

  203.         for(H=0;H<240;H++)
  204.         {
  205.                 for(V=40;V<80;V++)
  206.                 Write_Data(0x07,0xe0);
  207.         }

  208.         for(H=0;H<240;H++)
  209.         {
  210.                 for(V=80;V<120;V++)
  211.                 Write_Data(0x00,0x1f);
  212.         }

  213.         for(H=0;H<240;H++)
  214.         {
  215.                 for(V=120;V<160;V++)
  216.                 Write_Data(0xff,0xe0);
  217.         }

  218.         for(H=0;H<240;H++)
  219.         {
  220.                 for(V=160;V<200;V++)
  221.                 Write_Data(0xf8,0x1f);
  222.         }

  223.         for(H=0;H<240;H++)
  224.         {
  225.                 for(V=200;V<240;V++)
  226.                 Write_Data(0x07,0xff);
  227.         }

  228.         for(H=0;H<240;H++)
  229.         {
  230.                 for(V=240;V<280;V++)
  231.                 Write_Data(0xff,0xff);
  232.         }

  233.         for(H=0;H<240;H++)
  234.         {
  235.                 for(V=280;V<320;V++)
  236.                 Write_Data(0x00,0x00);
  237.         }

  238. }

  239. void show_photo(void)
  240. {
  241.         unsigned char j;
  242.         unsigned int i;
  243.         unsigned long s=0;

  244.         LCD_SetPos(0,240,0,320);//320x240

  245.         for (i=0;i<75;i++)
  246.         {
  247.         for (j=0;j<240;j++)
  248.         Write_Data(0xff,0xff);
  249.                
  250.         }

  251.         for (i=0;i<170;i++)
  252.         {
  253.         for (j=0;j<55;j++)
  254.                 Write_Data(0xff,0xff);

  255.                 for (j=0;j<130;j++)
  256.                 Write_Data(pic[s++],pic[s++]);
  257.                
  258.                 for (j=0;j<55;j++)
  259.                 Write_Data(0xff,0xff);
  260.         }

  261.     for (i=0;i<75;i++)
  262.         {
  263.         for (j=0;j<240;j++)
  264.         Write_Data(0xff,0xff);
  265.                
  266.         }

  267.         }

  268. //=======================================================//



  269. void  Init_data (unsigned char x,unsigned int y)
  270. {
  271.         unsigned char m,n;
  272.         m=y>>8;
  273.         n=y;
  274.         Write_Cmd(0x00,x);
  275.         Write_Data(m,n);

  276. }


  277. void  Write_Data_U16(unsigned int y)
  278. {
  279.         unsigned char m,n;
  280.         m=y>>8;
  281.         n=y;
  282.         Write_Data(m,n);

  283. }
  284. //====================== 写命令 ==========================//

  285. void Write_Cmd(unsigned char DH,unsigned char DL)
  286. {
  287.   
  288.        

  289.         CS=0;
  290.         RS=0;

  291.         P1=DH;
  292.         RW=0;
  293.         RW=1;

  294.         P1=DL;
  295.        
  296.         RW=0;
  297.         RW=1;
  298.         CS=1;
  299. }

  300. //===================== 写数据 ===========================//

  301. void Write_Data(unsigned char DH,unsigned char DL)
  302. {

  303.   
  304.        
  305.         CS=0;
  306.        
  307.         RS=1;
  308.         P1=DH;
  309.         RW=0;
  310.     RW=1;

  311.         P1=DL;       
  312.         RW=0;
  313.         RW=1;
  314.         CS=1;
  315. }





  316. //=======================================================
  317. void delayms(unsigned int count)
  318. {
  319.     int i,j;                                                                                
  320.     for(i=0;i<count;i++)                                                                    
  321.        {
  322.              for(j=0;j<260;j++);
  323.        }                                                                                    
  324. }



  325. //===================== 初始化代码 =======================//



  326. void ILI9325_Initial(void)
  327. {

  328.   //////////////////////////////////////////////////////////
  329. delayms(150);                     //根据不同晶振速度可以调整延时,保障稳定显示
  330. Init_data(0x00E5, 0x8000); // Set the Vcore voltage and this setting is must.
  331. Init_data(0x0000, 0x0001); // Start internal OSC.
  332. Init_data(0x0001, 0x0100); // set SS and SM bit
  333. Init_data(0x0002, 0x0700); // set 1 line inversion
  334. Init_data(0x0003, 0x1090); // set GRAM write direction and BGR=0.
  335. Init_data(0x0004, 0x0000); // Resize register
  336. Init_data(0x0008, 0x0202); // set the back porch and front porch
  337. Init_data(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0]
  338. Init_data(0x000A, 0x0000); // FMARK function
  339. Init_data(0x000C, 0x0000); // RGB interface setting
  340. Init_data(0x000D, 0x0000); // Frame marker Position
  341. Init_data(0x000F, 0x0000); // RGB interface polarity
  342. //*************Power On sequence ****************//
  343. Init_data(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB
  344. Init_data(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
  345. Init_data(0x0012, 0x0000); // VREG1OUT voltage
  346. Init_data(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
  347. delayms(200); // Dis-charge capacitor power voltage
  348. Init_data(0x0010, 0x17B0); // SAP, BT[3:0], AP, DSTB, SLP, STB
  349. Init_data(0x0011, 0x0037); // DC1[2:0], DC0[2:0], VC[2:0]
  350. delayms(50); // Delay 50ms
  351. Init_data(0x0012, 0x013E); // VREG1OUT voltage
  352. delayms(50); // Delay 50ms
  353. Init_data(0x0013, 0x1F00); // VDV[4:0] for VCOM amplitude
  354. Init_data(0x0029, 0x0013); // VCM[4:0] for VCOMH
  355. delayms(50);
  356. Init_data(0x0020, 0x0000); // GRAM horizontal Address
  357. Init_data(0x0021, 0x0000); // GRAM Vertical Address
  358. // ----------- Adjust the Gamma Curve ----------//
  359. Init_data(0x0030, 0x0000);
  360. Init_data(0x0031, 0x0404);
  361. Init_data(0x0032, 0x0404);
  362. Init_data(0x0035, 0x0004);
  363. Init_data(0x0036, 0x0404);
  364. Init_data(0x0037, 0x0404);
  365. Init_data(0x0038, 0x0404);
  366. Init_data(0x0039, 0x0707);
  367. Init_data(0x003C, 0x0500);
  368. Init_data(0x003D, 0x0607);
  369. //------------------ Set GRAM area ---------------//
  370. Init_data(0x0050, 0x0000); // Horizontal GRAM Start Address

  371. Init_data(0x0051, 0x00EF); // Horizontal GRAM End Address
  372. Init_data(0x0052, 0x0000); // Vertical GRAM Start Address
  373. Init_data(0x0053, 0x013F); // Vertical GRAM Start Address
  374. Init_data(0x0060, 0x2700); // Gate Scan Line
  375. Init_data(0x0061, 0x0001); // NDL,VLE, REV
  376. Init_data(0x006A, 0x0000); // set scrolling line
  377. //-------------- Partial Display Control ---------//
  378. Init_data(0x0080, 0x0000);
  379. Init_data(0x0081, 0x0000);
  380. Init_data(0x0082, 0x0000);
  381. Init_data(0x0083, 0x0000);
  382. Init_data(0x0084, 0x0000);
  383. Init_data(0x0085, 0x0000);
  384. //-------------- Panel Control -------------------//
  385. Init_data(0x0090, 0x0010);
  386. Init_data(0x0092, 0x0000);
  387. Init_data(0x0093, 0x0003);
  388. Init_data(0x0095, 0x0110);
  389. Init_data(0x0097, 0x0000);
  390. Init_data(0x0098, 0x0000);
  391. Init_data(0x0007, 0x0173); // 262K color and display ON

  392.        
  393. //        Write_Cmd_Data(0x0022);//               

  394. }







  395. /*===========================================================*/

  396. /*************************************************************
  397. 函数名称:LCD_DefineDispWindow
  398. 功    能:定义显示窗体
  399. 参    数:x0:  窗体中X坐标中较小者
  400.          x1:  窗体中X坐标中较大者
  401.          y0:  窗体中Y坐标中较小者
  402.          y1:  窗体中Y坐标中较大者
  403. 返 回 值:无
  404. *************************************************************/
  405. static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
  406. {
  407.   Init_data(WINDOW_XADDR_START,x0);
  408.   Init_data(WINDOW_XADDR_END,x1);
  409.   Init_data(WINDOW_YADDR_START,y0);
  410.   Init_data(WINDOW_YADDR_END,y1);
  411.   Init_data(GRAM_XADDR,x0);
  412.   Init_data(GRAM_YADDR,y0);
  413.   Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
  414. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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