找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1471|回复: 2
收起左侧

c51单片机测温320x240TFT液晶显示

[复制链接]
ID:359496 发表于 2018-6-26 16:45 | 显示全部楼层 |阅读模式
温度测量液晶显示

单片机源程序如下:
  1. /*===================================================================================================  
  2. 文件功能描述:320x240TFT驱动程序,控制TFT实现汉字,字符显示,画点功能。
  3. ====================================================================================================*/
  4. //******************包含头文件***************************
  5. #include"NBCTFT.h"
  6. #include"reg52.h"

  7. //**************控制端口定义********************
  8. #define DataPort P0     //数据口使用DataPort
  9. sbit RS    =P2^5;                //数据/命令选择
  10. sbit RW    =P2^4;       //写数据/命令
  11. sbit nRD   =P2^3;       //读控制
  12. sbit CS    =P2^2;                //片选
  13. sbit RES   =P2^1;                //复位

  14. sbit LE    =P2^0;       //74HC573锁存控制
  15. //**************声明外部函数和变量**************

  16. extern unsigned int Device_code;

  17. //================================================================================================
  18. //        实现功能:        延时
  19. //        输入参数:        count 设置延时时间
  20. //================================================================================================
  21. void delayms(unsigned int count)
  22. {
  23.     int i,j;                                                                                
  24.     for(i=0;i<count;i++)                                                                    
  25.        {
  26.              for(j=0;j<255;j++);
  27.        }                                                                                    
  28. }

  29. //================================================================================================
  30. //        实现功能:        写命令
  31. //        输入参数:  DH 需要输入16bits命令的高8位
  32. //              DL 需要输入16bits命令的低8位
  33. //================================================================================================
  34. void Write_Cmd(unsigned char DH,unsigned char DL)
  35. {
  36.         CS=0;
  37.         RS=0;
  38.     nRD=1;
  39.     RW=0;

  40.     //注意:当使用8位数据口驱动16位数据模式时,使用74HC573作为IO扩展,程序如下
  41.     DataPort=DL;            //送低8位命令给573待锁存
  42.     LE=1;                   //锁存位
  43.     LE=0;                   //断开锁存,位选573的Q7~Q0仍保持

  44.         DataPort=DH;            //送高8位命令给TFT

  45.         /*
  46.     //如果使用16位数据口驱动16位数据模式,则无需IO扩展,直接将数据送到数据口即可
  47.     DataPort_L=DL;
  48.     DataPort_H=DH;
  49.         */

  50.         RW=1;
  51.         CS=1;
  52. }

  53. //================================================================================================
  54. //        实现功能:        写数据(2*8bits)
  55. //        输入参数:  DH 需要输入16bits数据的高8位
  56. //              DL 需要输入16bits数据的低8位
  57. //================================================================================================
  58. void Write_Data(unsigned char DH,unsigned char DL)
  59. {
  60.        
  61.         CS=0;
  62.         RS=1;

  63.     //注意:当使用8位数据口驱动16位数据模式时,使用74HC573作为IO扩展,程序如下
  64.         DataPort=DL;            //送低8位数据给573待锁存
  65.     LE=1;                   //锁存位
  66.     LE=0;                   //断开锁存,位选573的Q7~Q0仍保持

  67.         DataPort=DH;            //送高8位数据给TFT

  68.         /*
  69.     //如果使用16位数据口驱动16位数据模式,则无需IO扩展,直接将数据送到数据口即可
  70.     DataPort_L=DL;
  71.     DataPort_H=DH;
  72.         */

  73.         RW=0;
  74.         RW=1;
  75.         CS=1;
  76. }

  77. //================================================================================================
  78. //        实现功能:        写数据(16位)
  79. //        输入参数:  y 需要输入16bits数据
  80. //================================================================================================
  81. void  Write_Data_U16(unsigned int y)
  82. {
  83.         unsigned char m,n;
  84.         m=y>>8;
  85.         n=y;
  86.         Write_Data(m,n);
  87. }

  88. //================================================================================================
  89. //        实现功能:        向x寄存器写入y数据
  90. //        输入参数:  x 需要输入的命令 16位
  91. //              y 需要输入的数据 16位
  92. //================================================================================================
  93. void  Write_Cmd_Data (unsigned char x,unsigned int y)
  94. {
  95.         unsigned char m,n;
  96.         m=y>>8;
  97.         n=y;
  98.         Write_Cmd(0x00,x);
  99.         Write_Data(m,n);
  100. }

  101. //================================================================================================
  102. //        实现功能:        TFT清屏
  103. //        输入参数:        bColor 清屏所使用的背景色
  104. //================================================================================================
  105. void CLR_Screen(unsigned int bColor)
  106. {
  107. unsigned int i,j;
  108. LCD_SetPos(0,240,0,320);//320x240
  109. for (i=0;i<320;i++)
  110.         {
  111.            for (j=0;j<240;j++)
  112.                Write_Data_U16(bColor);
  113.         }
  114. }

  115. //================================================================================================
  116. //        实现功能:        显示Ascii字符
  117. //  输入参数:  x 横坐标
  118. //              y 纵坐标
  119. //                        c 需要显示的字符
  120. //                        fColor 字符颜色
  121. //                        bColor 字符背景颜色
  122. //================================================================================================
  123. #include "Ascii_8x16.h"
  124. void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor)
  125. {
  126. unsigned int i,j;
  127. LCD_SetPos(x,x+8-1,y,y+16-1);                    //设置字符显示位置
  128. for(i=0; i<16;i++) {                             //循环写入16字节,一个字符为16字节
  129.                 unsigned char m=Font8x16[(c-0x20)*16+i];  //提取c字符的第i个字节以,c减去0x20是由于Ascii码库中的0~1f被去掉
  130.                 for(j=0;j<8;j++) {                        //循环写入8位,一个字节为8位
  131.                         if((m&0x80)==0x80) {                  //判断最高位是否为1
  132.                                 Write_Data_U16(fColor);           //最高位为1,写入字符颜色
  133.                                 }
  134.                         else {
  135.                                 Write_Data_U16(bColor);           //最高位为0,写入背景颜色
  136.                                 }
  137.                         m<<=1;                                //左移1位,准备写下一位
  138.                         }
  139.                 }
  140. }

  141. //================================================================================================
  142. //        实现功能:        显示16x16汉字
  143. //  输入参数:  x 横坐标
  144. //              y 纵坐标
  145. //                        g 需要显示的字符编码
  146. //                        fColor 字符颜色
  147. //                        bColor 字符背景颜色
  148. //================================================================================================
  149. #include "chinese.h"        //包含16*16汉字字模

  150. void Put16x16(unsigned short x, unsigned short  y, unsigned char g[2], unsigned int fColor,unsigned int bColor)
  151. {
  152.         unsigned int i,j,k;

  153.         LCD_SetPos(x,  x+16-1,y, y+16-1);                       //设置汉字显示位置

  154.         for (k=0;k<64;k++)                                      //循环64次,查询汉字字模位置
  155.         {
  156.           if ((ch16[k].GBK[0]==g[0])&&(ch16[k].GBK[1]==g[1]))   //判断第k个汉字的编码是否与输入汉字g[2]相等
  157.           {
  158.             for(i=0;i<32;i++)                                   //如果相等,既已找到待显示字模位置,循环写入32字节
  159.                 {
  160.                   unsigned short m=ch16[k].hz16[i];                 //读取32字节中的第i字节
  161.                   for(j=0;j<8;j++)                                  //循环写入8位数据
  162.                    {                                                
  163.                          if((m&0x80)==0x80) Write_Data_U16(fColor);     //判断最高位是否为1,最高位为1,写入字符颜色
  164.                          else              Write_Data_U16(bColor);      //最高位为0,写入背景颜色
  165.                      m<<=1;                                         //左移1位,准备写下一位
  166.                }
  167.                 }
  168.           }  
  169.     }       
  170. }

  171. //================================================================================================
  172. //        实现功能:        显示中英文字符串
  173. //  输入参数:  x 横坐标
  174. //              y 纵坐标
  175. //                        *s 待显示的字符串,例如LCD_PutString(24,16,"123蓝芯",White,Blue);即把"123蓝芯"的第一个字符地址赋给指针变量s.
  176. //                        bColor 字符背景颜色
  177. //================================================================================================
  178. void LCD_PutString(unsigned short x, unsigned short y, unsigned char *s, unsigned int fColor, unsigned int bColor)
  179. {
  180.          unsigned char l=0;                            //显示屏位置增量
  181.      while(*s)
  182.          {
  183.                 if( *s < 0x80)                             //判断s指向的字符串中的某字符的编码值是否小于128,如果小于,即为ASCII字符
  184.                     {
  185.                          LCD_PutChar(x+l*8,y,*s,fColor,bColor);//显示该字符
  186.                      s++;l++;                              //指针加1,位置加1
  187.                         }
  188.                 else
  189.                     {
  190.                          Put16x16(x+l*8,y,(unsigned char*)s,fColor,bColor);//显示该汉字
  191.                      s+=2;l+=2;                                        //因为汉字为编码为2字节,指针加2,显示16x16所以位置加2
  192.                         }
  193.          }
  194. }

  195. //================================================================================================
  196. //        实现功能:        指定位置显示RGB颜色
  197. //  输入参数:  x0,y0 起始坐标
  198. //              x1,y1 结束坐标
  199. //                        Color  背景颜色
  200. //================================================================================================

  201. void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
  202. {
  203.         unsigned int i,j;
  204.         LCD_SetPos(x0,x1,y0,y1);      //设置显示位置
  205.         for (i=y0;i<=y1;i++)
  206.         {
  207.            for (j=x0;j<=x1;j++)
  208.                Write_Data_U16(Color);
  209.         }
  210. }

  211. //================================================================================================
  212. //        实现功能:        TFT初始化
  213. //================================================================================================
  214. void TFT_Initial(void)
  215. {  

  216.     RES = 1;   
  217.         delayms(1);                    // Delay 1ms
  218.         RES = 0;  
  219.         delayms(10);                   // Delay 10ms            
  220.         RES = 1;  
  221.         delayms(50);                   // Delay 50 ms  
  222.   
  223.         if(Device_code==0x9320)
  224.      {

  225.     //************* Start Initial Sequence **********//
  226.         Write_Cmd_Data(0x00,0x0001);   //Set the OSC bit as ‘1’ to start the internal oscillator
  227.     Write_Cmd_Data(0x01,0x0100);   // set SS and SM bit
  228.         Write_Cmd_Data(0x02,0x0700);   // set 1 line inversion
  229.         Write_Cmd_Data(0x03,0x1030);   //set GRAM Write direction and BGR=1
  230.     Write_Cmd_Data(0x04,0x0000);   // Resize register
  231.         Write_Cmd_Data(0x08,0x0202);   // set the back porch and front porch
  232.     Write_Cmd_Data(0x09,0x0000);   // set non-display area refresh cycle ISC[3:0]
  233.     Write_Cmd_Data(0x0A,0x0000);   // FMARK function
  234.     Write_Cmd_Data(0x0C,0x0000);   // RGB interface setting
  235.         Write_Cmd_Data(0x0D,0x0000);   // Frame marker Position
  236.     Write_Cmd_Data(0x0F,0x0000);   // RGB interface polarity
  237.     delayms(30);
  238.         //*************Power On sequence ****************//
  239.         Write_Cmd_Data(0x10, 0x16b0);   // SAP, BT[3:0], AP, DSTB, SLP, STB
  240.         delayms(30);
  241.         Write_Cmd_Data(0x11, 0x0007);   //Write final user’s setting values to VC bit
  242.         Write_Cmd_Data(0x12, 0x013a);   // set Internal reference voltage
  243.         Write_Cmd_Data(0x13, 0x1a00);   // VDV[4:0] for VCOM amplitude
  244.     delayms(30);
  245.     Write_Cmd_Data(0x29, 0x000c);   // Set VCM[5:0] for VCOMH
  246.         delayms(30); // Delay 50ms

  247.         // ----------- Adjust the Gamma Curve ----------//
  248.         Write_Cmd_Data(0x0030, 0x0000);
  249.         Write_Cmd_Data(0x0031, 0x0505);
  250.         Write_Cmd_Data(0x0032, 0x0304);
  251.         Write_Cmd_Data(0x0035, 0x0006);
  252.         Write_Cmd_Data(0x0036, 0x0707);
  253.         Write_Cmd_Data(0x0037, 0x0105);
  254.         Write_Cmd_Data(0x0038, 0x0002);
  255.         Write_Cmd_Data(0x0039, 0x0707);
  256.         Write_Cmd_Data(0x003C, 0x0704);
  257.         Write_Cmd_Data(0x003D, 0x0807);

  258.         //------------------ Set GRAM area ---------------//
  259.         Write_Cmd_Data(0x0050, 0x0000); // Horizontal GRAM Start Address
  260.         Write_Cmd_Data(0x0051, 0x00EF); // Horizontal GRAM End Address
  261.         Write_Cmd_Data(0x0052, 0x0000); // Vertical GRAM Start Address
  262.         Write_Cmd_Data(0x0053, 0x013F); // Vertical GRAM Start Address
  263.         Write_Cmd_Data(0x0060, 0x2700); // Gate Scan Line
  264.         Write_Cmd_Data(0x0061, 0x0001); // NDL,VLE, REV
  265.         Write_Cmd_Data(0x006A, 0x0000); // set scrolling line
  266.     Write_Cmd_Data(0x20, 0x0000);   // GRAM horizontal Address
  267.         Write_Cmd_Data(0x21, 0x0000);   // GRAM Vertical Address

  268.         //-------------- Partial Display Control ---------//
  269.         Write_Cmd_Data(0x0080, 0x0000);
  270.         Write_Cmd_Data(0x0081, 0x0000);
  271.         Write_Cmd_Data(0x0082, 0x0000);
  272.         Write_Cmd_Data(0x0083, 0x0000);
  273.         Write_Cmd_Data(0x0084, 0x0000);
  274.         Write_Cmd_Data(0x0085, 0x0000);

  275.         //-------------- Panel Control ---------//
  276.     Write_Cmd_Data(0x90,0x0010);   //Frame Cycle Contral
  277.         Write_Cmd_Data(0x92,0x0000);   //Panel Interface Contral
  278.         Write_Cmd_Data(0x93,0x0003);   //Panel Interface Contral 3.
  279.         Write_Cmd_Data(0x95,0x0110);   //Frame Cycle Contral
  280.         Write_Cmd_Data(0x97,0x0000);   //
  281.         Write_Cmd_Data(0x98,0x0000);   //Frame Cycle Contral.     

  282.         //-------------- Display on ---------//
  283.     Write_Cmd_Data(0x07,0x0173);

  284.         }

  285.         else if(Device_code==0x1505 )
  286.      {

  287.     //************* Start Initial Sequence **********//
  288.         Write_Cmd_Data(0x00,0x0001);   //Set the OSC bit as ‘1’ to start the internal oscillator
  289.     Write_Cmd_Data(0x01,0x0100);   // set SS and SM bit
  290.         Write_Cmd_Data(0x02,0x0700);   // set 1 line inversion
  291.         Write_Cmd_Data(0x03,0x1030);   //set GRAM Write direction and BGR=1
  292.     Write_Cmd_Data(0x04,0x0000);   // Resize register
  293.         Write_Cmd_Data(0x08,0x0202);   // set the back porch and front porch
  294.     Write_Cmd_Data(0x09,0x0000);   // set non-display area refresh cycle ISC[3:0]
  295.     Write_Cmd_Data(0x0A,0x0000);   // FMARK function
  296.     Write_Cmd_Data(0x0C,0x0000);   // RGB interface setting
  297.         Write_Cmd_Data(0x0D,0x0000);   // Frame marker Position
  298.     Write_Cmd_Data(0x0F,0x0000);   // RGB interface polarity
  299.     delayms(30);
  300.         //*************Power On sequence ****************//
  301.         Write_Cmd_Data(0x10, 0x16b0);   // SAP, BT[3:0], AP, DSTB, SLP, STB
  302.         delayms(30);
  303.         Write_Cmd_Data(0x11, 0x0007);   //Write final user’s setting values to VC bit
  304.         Write_Cmd_Data(0x12, 0x013a);   // set Internal reference voltage
  305.         Write_Cmd_Data(0x13, 0x1a00);   // VDV[4:0] for VCOM amplitude
  306.     delayms(30);
  307.     Write_Cmd_Data(0x29, 0x000c);   // Set VCM[5:0] for VCOMH
  308.         delayms(30); // Delay 50ms

  309.         // ----------- Adjust the Gamma Curve ----------//
  310.         Write_Cmd_Data(0x0030, 0x0000);
  311.         Write_Cmd_Data(0x0031, 0x0505);
  312.         Write_Cmd_Data(0x0032, 0x0304);
  313.         Write_Cmd_Data(0x0035, 0x0006);
  314.         Write_Cmd_Data(0x0036, 0x0707);
  315.         Write_Cmd_Data(0x0037, 0x0105);
  316.         Write_Cmd_Data(0x0038, 0x0002);
  317.         Write_Cmd_Data(0x0039, 0x0707);
  318.         Write_Cmd_Data(0x003C, 0x0704);
  319.         Write_Cmd_Data(0x003D, 0x0807);

  320.         //------------------ Set GRAM area ---------------//
  321.         Write_Cmd_Data(0x0050, 0x0000); // Horizontal GRAM Start Address
  322.         Write_Cmd_Data(0x0051, 0x00EF); // Horizontal GRAM End Address
  323.         Write_Cmd_Data(0x0052, 0x0000); // Vertical GRAM Start Address
  324.         Write_Cmd_Data(0x0053, 0x013F); // Vertical GRAM Start Address
  325.         Write_Cmd_Data(0x0060, 0x2700); // Gate Scan Line
  326.         Write_Cmd_Data(0x0061, 0x0001); // NDL,VLE, REV
  327.         Write_Cmd_Data(0x006A, 0x2700); // set scrolling line
  328.     Write_Cmd_Data(0x20, 0x0000);   // GRAM horizontal Address
  329.         Write_Cmd_Data(0x21, 0x0000);   // GRAM Vertical Address

  330.         //-------------- Partial Display Control ---------//
  331.         Write_Cmd_Data(0x0080, 0x0000);
  332.         Write_Cmd_Data(0x0081, 0x0000);
  333.         Write_Cmd_Data(0x0082, 0x0000);
  334.         Write_Cmd_Data(0x0083, 0x0000);
  335.         Write_Cmd_Data(0x0084, 0x0000);
  336.         Write_Cmd_Data(0x0085, 0x0000);

  337.         //-------------- Panel Control ---------//
  338.     Write_Cmd_Data(0x90,0x0010);   //Frame Cycle Contral
  339.         Write_Cmd_Data(0x92,0x0000);   //Panel Interface Contral
  340.         Write_Cmd_Data(0x93,0x0003);   //Panel Interface Contral 3.
  341.         Write_Cmd_Data(0x95,0x0110);   //Frame Cycle Contral
  342.         Write_Cmd_Data(0x97,0x0000);   //
  343.         Write_Cmd_Data(0x98,0x0000);   //Frame Cycle Contral.     

  344.         //-------------- Display on ---------//
  345.     Write_Cmd_Data(0x07,0x0173);

  346.         }

  347.     else if(Device_code==0x9328)
  348.      {

  349.     //************* Start Initial Sequence **********//
  350.          Write_Cmd_Data(0x0001,0x0100);   //set SS and SM bit //设置扫描方向
  351.     Write_Cmd_Data(0x0002,0x0700);   //EOR=1 and B/C=1 to set the line inversion  //设置行反转
  352.     Write_Cmd_Data(0x0003,0x1030);   //set Entry Mode  //设置进入模式   
  353.     Write_Cmd_Data(0x0004,0x0000);   //
  354.     Write_Cmd_Data(0x00A4,0x0001);
  355.     Write_Cmd_Data(0x0008,0x0202); // set the back porch and front porch
  356.     Write_Cmd_Data(0x0009,0x0000); // set non-display area refresh cycle ISC[3:0]
  357.     Write_Cmd_Data(0x000A,0x0000); // FMARK function
  358.     Write_Cmd_Data(0x000C,0x0000); // RGB interface setting
  359.     Write_Cmd_Data(0x000D, 0x0000); // Frame marker Position
  360.     Write_Cmd_Data(0x000F, 0x0000); // RGB interface polarity



  361. //*************Power On sequence ****************//
  362.     Write_Cmd_Data(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB
  363.     Write_Cmd_Data(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
  364.     Write_Cmd_Data(0x0012, 0x0000); // VREG1OUT voltage
  365.     Write_Cmd_Data(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
  366.     delayms(30);
  367.     Write_Cmd_Data(0x0010, 0x1690); // SAP, BT[3:0], AP, DSTB, SLP, STB
  368.     Write_Cmd_Data(0x0011, 0x0227); // R11h=0x0221 at VCI=3.3V, DC1[2:0], DC0[2:0], VC[2:0]
  369.     delayms(30);
  370.     Write_Cmd_Data(0x0012, 0x001C); // External reference voltage= Vci;
  371.     delayms(30);
  372.     Write_Cmd_Data(0x0013, 0x1800); // R13=1200 when R12=009D;VDV[4:0] for VCOM amplitude
  373.     Write_Cmd_Data(0x0029, 0x001C); // R29=000C when R12=009D;VCM[5:0] for VCOMH
  374.     Write_Cmd_Data(0x002B, 0x000D); // Frame Rate = 91Hz
  375.     delayms(30);   
  376.     Write_Cmd_Data(0x0020, 0x0000); // GRAM horizontal Address
  377.     Write_Cmd_Data(0x0021, 0x0000); // GRAM Vertical Address
  378. // ----------- Adjust the Gamma Curve ----------//                  
  379.         Write_Cmd_Data(0x0030, 0x0007);
  380.         Write_Cmd_Data(0x0031, 0x0302);
  381.     Write_Cmd_Data(0x0032, 0x0105);
  382.         Write_Cmd_Data(0x0035, 0x0206);
  383.     Write_Cmd_Data(0x0036, 0x0808);                  
  384.     Write_Cmd_Data(0x0037, 0x0206);
  385.     Write_Cmd_Data(0x0038, 0x0504);
  386.     Write_Cmd_Data(0x0039, 0x0007);
  387.     Write_Cmd_Data(0x003C, 0x0105);
  388.     Write_Cmd_Data(0x003D, 0x0808);
  389. //------------------ Set GRAM area ---------------//
  390.     Write_Cmd_Data(0x0050, 0x0000); // Horizontal GRAM Start Address
  391.     Write_Cmd_Data(0x0051, 0x00EF); // Horizontal GRAM End Address
  392.     Write_Cmd_Data(0x0052, 0x0000); // Vertical GRAM Start Address
  393.         delayms(30);
  394.     Write_Cmd_Data(0x0053, 0x013F); // Vertical GRAM Start Address
  395.         delayms(30);
  396.     Write_Cmd_Data(0x0060, 0xA700); // Gate Scan Line
  397. ……………………

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

所有资料51hei提供下载:
温度测量液晶显示1.rar (64.38 KB, 下载次数: 29)
回复

使用道具 举报

ID:199188 发表于 2018-7-1 11:50 | 显示全部楼层
非常好!
回复

使用道具 举报

ID:393193 发表于 2019-9-15 21:30 | 显示全部楼层
感谢分享了,拿去做参考
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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