找回密码
 立即注册

QQ登录

只需一步,快速开始

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

自己写的一个ILI9325 TFT彩屏单片机驱动程序

[复制链接]
跳转到指定楼层
楼主


单片机源程序如下:


  1. /*********************
  2. TFT液晶显示测试                                                                                           
  3. ***********************/


  4. #include"reg52.h"

  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=P0^4;                //片选
  23. sbit RES=P0^3;                //复位
  24. sbit RS=P0^7;                //数据/命令选择
  25. sbit RW=P0^6;



  26. //====================================================//

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

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

  47. void  main(void)
  48. {   
  49.         CS=1;
  50.         delayms(25);
  51.         RES=0;
  52.         delayms(150);
  53.         RES=1;
  54.         delayms(25);
  55.         ILI9325_Initial();
  56.         while(1)
  57.         {
  58.        


  59. ClearScreen(0x0);

  60. //show_colour_bar();       


  61. LCD_PutString(2,40,"http://www.baidu.com/",Red,Blue);                  ///前景颜色和背景颜色,可以直接预定义,也可以直接填入16进制数字
  62. LCD_PutString(40,60,"I LOVE MY JOB ",Blue2,Yellow);
  63. LCD_PutString(40,80,"123电子工作室",Magenta,Green);
  64. LCD_PutString(40,100,"专业技术支持论坛",0x07e0,0xf800);
  65. LCD_PutString(40,120,"专业开发板工控板",0xF800,Yellow);
  66. LCD_PutString(40,140,"1234567890",0xF800,Yellow);
  67. LCD_PutString(20,160,"abcdefghijklmnopqistuvwxyz",0xF800,Yellow);
  68. LCD_PutString(20,180,"`,./<>';:[]{}\|?)(-=+*&^%$",0xF800,Yellow);
  69. PutGB3232(20,200,"我",Blue,Yellow);        //写入32x32汉字
  70. PutGB3232(60,200,"爱",Blue,Red);
  71. PutGB3232(100,200,"单",Blue,Magenta);
  72. PutGB3232(140,200,"片",Blue,Green);
  73. PutGB3232(180,200,"机",Blue,Cyan);       
  74. PutGB3232(20,240,"3",Blue,0X00);       
  75. PutGB3232(60,240,"2",Blue,0X00);       
  76. PutGB3232(100,240,"6",Blue,0X00);       
  77. PutGB3232(140,240,"电",Blue,0X00);       
  78. PutGB3232(180,240,"子",Blue,0X00);
  79. PutGB3232(20,280,"",Blue,0X00);               
  80. PutGB3232(60,280,"欢",Blue,0X00);       
  81. PutGB3232(100,280,"迎",Blue,0X00);       
  82. PutGB3232(140,280,"你",Blue,0X00);       
  83. PutGB3232(180,280,"!",Blue,0X00);       
  84.        

  85. //        show_photo();

  86.                 while(1);       


  87.         }
  88. }


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

  98.         }
  99. }

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


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

  120.                 LCD_PutChar8x16( x, y, c, fColor, bColor );
  121.         }


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

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

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

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

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

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

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

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

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



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

  189.         }


  190. }

  191. //====================== 显示彩条 ======================//
  192. void show_colour_bar (void)

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

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

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

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

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

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

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

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

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

  236. }

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

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

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

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

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

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

  265.         }

  266. //=======================================================//



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

  274. }


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

  281. }
  282. //====================== 写命令 ==========================//

  283. void Write_Cmd(unsigned char DH,unsigned char DL)
  284. {
  285.   
  286.        

  287.         CS=0;
  288.         RS=0;

  289.         P2=DH;
  290.         RW=0;
  291.         RW=1;

  292.         P2=DL;
  293.        
  294.         RW=0;
  295.         RW=1;
  296.         CS=1;
  297. }

  298. //===================== 写数据 ===========================//

  299. void Write_Data(unsigned char DH,unsigned char DL)
  300. {

  301.   
  302.        
  303.         CS=0;
  304.        
  305.         RS=1;
  306.         P2=DH;
  307.         RW=0;
  308.     RW=1;

  309.         P2=DL;       
  310.         RW=0;
  311.         RW=1;
  312.         CS=1;
  313. }





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



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



  324. void ILI9325_Initial(void)
  325. {

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

  369. ……………………

  370. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
TFT彩屏.zip (97.24 KB, 下载次数: 39)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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