找回密码
 立即注册

QQ登录

只需一步,快速开始

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

TFT液晶SSD2119的stm32单片机驱动

[复制链接]
跳转到指定楼层
楼主
SSD2119 ID:9919;基于stm32f1的


单片机源程序如下:
  1. //-------------------------------------------------------------------------*
  2. //文件名:  LCD.c (LCD操作函数文件)                                         *
  3. //-------------------------------------------------------------------------*
  4. #include "LCD.h"
  5. #include "font.h"
  6. //内部调用的函数声明
  7. static void WriteRegister(uint16 index,uint16 data);//写指定地址寄存器的值
  8. static void WriteIndex(uint16 index);//写 SSD2119 控制器寄存器地址
  9. static void WriteData(uint16 data);//写 SSD2119 寄存器数据
  10. static uint16 ReadRegister(uint16 index);//读取指定地址寄存器的值
  11. static uint16 ReadData(void);//读取控制器数据
  12. static void SetCursor(uint16 x,uint16 y);//设置屏幕坐标
  13. static void Reset();//复位 SSD2119 控制器
  14. static void LCDDrawChar(uint16 x, uint16 y, uint8 *c);//在屏幕上画一个字符
  15. static void LCDDrawChinese(uint16 x, uint16 y, uint16 *c);//在屏幕上画一个汉字
  16. //-------------------------------------------------------------------------*
  17. //函数名: LCDInit                                                          *
  18. //功  能: 液晶初始化程序                                                   *
  19. //参  数: 无                                                               *
  20. //返  回: 无                                                               *
  21. //内部调用:WriteRegister                                                   *
  22. //说  明: 初始化液晶的显示方式                                             *
  23. //-------------------------------------------------------------------------*
  24. void LCDInit(void)
  25. {
  26.         uint8 tmp;
  27.         uint16 i;
  28.     static uint16 DeviceCode;
  29.         PIN_FUNCTION();//配置引脚功能为GPIO
  30.         CONTROL_OUTPUT();//配置控制引脚为输出  
  31.         //设置GPIO为输出
  32.         DATA_OUTPUT();
  33.         WriteData(0xffff);
  34.         Set_RST;
  35.         Set_Cs;
  36.         Set_Rs;
  37.         Set_nWr;
  38.         Set_nRd;
  39.         Set_RST;
  40.         Reset();
  41.         Delay(200);                           
  42.         WriteRegister(0x0000,0x0001);
  43.         Delay(200);   
  44.         for(i=50000;i>0;i--);
  45.         for(i=50000;i>0;i--);
  46.         //DeviceCode = ReadRegister(0x0000);
  47.         DeviceCode=0x9919;
  48.         if(DeviceCode == 0x9919)
  49.         {
  50.                 //*********POWER ON &RESET DISPLAY OFF
  51.                 WriteRegister(0x28,0x0006);   
  52.                 WriteRegister(0x00,0x0001);   
  53.                 WriteRegister(0x10,0x0000);   
  54.                 WriteRegister(0x01,0x72ef);   
  55.                 WriteRegister(0x02,0x0600);   
  56.                 WriteRegister(0x03,0x6a38);   
  57.                 //WriteRegister(0x11,0x6874);//70//从左到右(横屏显示)
  58.                 WriteRegister(0x11,0x685C);  //70//从上到下(竖屏显示)  
  59.                 //  RAM WRITE DATA MASK
  60.                 WriteRegister(0x0f,0x0000);
  61.                 //  RAM WRITE DATA MASK
  62.                 WriteRegister(0x0b,0x5308);   
  63.                 WriteRegister(0x0c,0x0003);   
  64.                 WriteRegister(0x0d,0x000a);   
  65.                 WriteRegister(0x0e,0x2e00);  //0030   
  66.                 WriteRegister(0x1e,0x00be);   
  67.                 WriteRegister(0x25,0x8000);   
  68.                 WriteRegister(0x26,0x7800);   
  69.                 WriteRegister(0x27,0x0078);   
  70.                 WriteRegister(0x4e,0x0000);   
  71.                 WriteRegister(0x4f,0x0000);   
  72.                 WriteRegister(0x12,0x08d9);   
  73.                 // -----------------Adjust the Gamma Curve----//
  74.                 WriteRegister(0x30,0x0000);         //0007   
  75.                 WriteRegister(0x31,0x0104);         //0203   
  76.                 WriteRegister(0x32,0x0100);  //0001   
  77.                 WriteRegister(0x33,0x0305);         //0007   
  78.                 WriteRegister(0x34,0x0505);         //0007   
  79.                 WriteRegister(0x35,0x0305);  //0407   
  80.                 WriteRegister(0x36,0x0707);         //0407   
  81.                 WriteRegister(0x37,0x0300);  //0607   
  82.                 WriteRegister(0x3a,0x1200);  //0106   
  83.                 WriteRegister(0x3b,0x0800);               
  84.                 WriteRegister(0x07,0x0033);
  85.         }
  86.         for(i=50000;i>0;i--);
  87.         //LCDClear(0xff00);
  88. }

  89. //-------------------------------------------------------------------------*
  90. //函数名: LCDClear                                                         *
  91. //功  能: 将屏幕填充成指定的颜色,如清屏,则填充 0xffff                    *
  92. //参  数: ClearColor:填充颜色                                              *
  93. //返  回: 无                                                               *
  94. //内部调用:SetCursor,WriteIndex,WriteData                                  *
  95. //说  明: 将屏幕填充成指定的颜色                                           *
  96. //-------------------------------------------------------------------------*
  97. void LCDClear(uint16 ClearColor)
  98. {
  99.         uint32 i;
  100.         SetCursor(0x0000, 0x0000);  
  101.         Clr_Cs;
  102.         WriteIndex(0x0022);
  103.         Set_Rs;   
  104.         for(i=0;i<76800;i++)
  105.         {
  106.                 WriteData(ClearColor);
  107.                 Clr_nWr;
  108.                 Set_nWr;
  109.         }
  110.         Set_Cs;
  111. }
  112. //-------------------------------------------------------------------------*
  113. //函数名: LCDTest                                                          *
  114. //功  能: 测试液晶屏                                                       *
  115. //参  数: 无                                                               *
  116. //返  回: 无                                                               *
  117. //内部调用:WriteIndex,WriteRegister,WriteData                              *
  118. //说  明:                                                                  *
  119. //-------------------------------------------------------------------------*
  120. void LCDTest(void)
  121. {
  122.         uint16 i,j;
  123.         WriteRegister(0x4e, 0);
  124.         WriteRegister(0x4f, 0);
  125.         Clr_Cs;
  126.         WriteIndex(0x22);  
  127.         for(i=0;i<320;i++)
  128.                 for(j=0;j<240;j++)
  129.                 {
  130.                           if(i>279)WriteData(0xffff);
  131.                           else if(i>239)WriteData(0x001f);
  132.                           else if(i>199)WriteData(0x07e0);
  133.                           else if(i>159)WriteData(0x07ff);
  134.                           else if(i>119)WriteData(0xf800);
  135.                           else if(i>79)WriteData(0xf81f);
  136.                           else if(i>39)WriteData(0xffe0);
  137.                           else WriteData(0xffff);
  138.             }
  139.     Set_Cs;
  140. }
  141. //-------------------------------------------------------------------------*
  142. //函数名: LCDSetPixel                                                      *
  143. //功  能: 在指定座标画点                                                   *
  144. //参  数: x      行座标                                                    *
  145. //        y      列座标                                                    *
  146. //        PixcelColor  像素的颜色                                          *
  147. //返  回: 无                                                               *
  148. //内部调用:SetCursor,WriteIndex,WriteData                                  *
  149. //说  明:                                                                  *
  150. //-------------------------------------------------------------------------*
  151. void LCDSetPixel(uint16 x,uint16 y,uint16 PixcelColor)
  152. {
  153.         if ( (x>320)||(y>240) ) return;
  154.         SetCursor(x,y);
  155.         Clr_Cs;
  156.         WriteIndex(0x0022);
  157.         WriteData(PixcelColor);
  158.         Set_Cs;
  159. }
  160. //-------------------------------------------------------------------------*
  161. //函数名: LCDShowChar                                                      *
  162. //功  能: 显示一个字符(8*16点阵)                                           *
  163. //参  数: ln:字符要显示的行                                                *
  164. //       col:字符要显示的列                                                *
  165. //         c:要显示的字符                                                  *
  166. //返  回: 无                                                               *
  167. //内部调用:LCDDrawChar                                                     *
  168. //说  明:                                                                  *
  169. //-------------------------------------------------------------------------*
  170. void LCDShowChar(uint16 ln, uint16 col, uint8  c)
  171. {
  172.         c -= 32;
  173.     LCDDrawChar(ln, col, &ASCII_Character[c*16]);
  174. }
  175. //-------------------------------------------------------------------------*
  176. //函数名: LCDShowStringLn                                                  *
  177. //功  能: 在指定的行显示字符串                                             *
  178. //参  数: ln:要显示的行                                                    *
  179. //       *s: 指向字符串的指针                                              *
  180. //返  回: 无                                                               *
  181. //内部调用:LCDDrawChar                                                     *
  182. //说  明:                                                                  *
  183. //-------------------------------------------------------------------------*
  184. void LCDShowStringLn(uint16 ln, uint8 *s)
  185. {
  186.         uint16 i = 1;
  187.         uint16 Count=0;
  188.         //uint16  refcolumn = (WIDTH/*-1*/)-16;
  189.         uint16 refcolumn = 240;
  190.         uint8 *p;
  191.         p=s;
  192.         //统计字符个数
  193.         while(*p!= 0)
  194.         {
  195.                 Count++;
  196.                 p++;        
  197.         }
  198.         UartSend1(0,Count);
  199.     while ((*s != 0) && (i <= Count))  //在屏幕上依次写每个字符
  200.         {
  201.                 LCDShowChar(ln, refcolumn, *s);                        
  202.                    refcolumn -= 8; //列减去一个字符
  203.                 s++;                                             
  204.                 if((i%30)==0)  
  205.                 {
  206.                         refcolumn=240;
  207.                         ln+=16;//换行
  208.                 }            
  209.                 i++;        //统计字符的个数            
  210.         }
  211. }
  212. //-------------------------------------------------------------------------*
  213. //函数名: LCDShowChinese                                                   *
  214. //功  能: 在指定的行、列显示一个汉字                                       *
  215. //参  数: ln:汉字要显示的行                                                *
  216. //        col:汉字要显示的列                                               *
  217. //        start:要显示的汉字串的开始下标                                   *
  218. //返  回: 无                                                               *
  219. //内部调用:LCDDrawChinese                                                  *
  220. //说  明:                                                                  *
  221. //-------------------------------------------------------------------------*
  222. void LCDShowChinese(uint16 ln, uint16 col, uint8 start)
  223. {
  224.         LCDDrawChinese(ln, col, &ASCII_Chinese[16*start]);
  225. }
  226. //-------------------------------------------------------------------------*
  227. //函数名: LCDShowChineseLine                                               *
  228. //功  能: 在指定的行显示一串汉字                                           *
  229. //参  数: ln:汉字要显示的行                                                *
  230. //        start:要显示的汉字串的开始下标                                   *
  231. //        count:一行中要显示的汉字个数                                     *
  232. //返  回: 无                                                               *
  233. //内部调用:LCDDrawChinese                                                  *
  234. //说  明:                                                                  *
  235. //-------------------------------------------------------------------------*
  236. void LCDShowChineseLine(uint16 ln, uint8 start,uint8 count)
  237. {
  238.         uint8 i,temp=0;
  239.         uint16 column = 240;
  240.         for(i=start;i<count+start;i++)
  241.         {
  242.                 LCDDrawChinese(ln, column, &ASCII_Chinese[16*i]);
  243.                 column-=16;
  244.                 temp++;
  245.                 if(temp%15==0)
  246.                 {
  247.                         ln+=16;
  248.                         column=240;
  249.                 }
  250.                
  251.         }
  252. }
  253. //-------------------------------------------------------------------------*
  254. //函数名: LCDShowBitmap                                                    *
  255. //功  能: 显示一幅图片,x为水平坐标,y为垂直坐标,每个像素用16位二进制表示    *
  256. //        5:6:5格式                                                        *
  257. //参  数: x: 水平起始位置                                                  *
  258. //        y: 垂直起始位置                                                  *
  259. //        w: 图片的宽度                                                    *
  260. //        h: 图片的高度                                                    *
  261. //        bitmap: 指向图片数组的指针                                       *
  262. //返  回: 无                                                               *
  263. //内部调用:                                                                *
  264. //说  明:                                                                  *
  265. //-------------------------------------------------------------------------*
  266. void LCDShowBitmap(uint16 x, uint16 y, uint16 w, uint16 h, uint8 *bitmap)
  267. {
  268.         uint16   i,j;
  269.         uint16  len = w*h;
  270.         uint16 *bitmap_ptr = (uint16 *)bitmap;
  271.         for (j = 0; j< h; j++)
  272.         {
  273.                 SetCursor(x,y);
  274.                 Clr_Cs;
  275.         WriteIndex(0x22);
  276.         Set_Rs;
  277.             for(i = 0; i<w; i++)
  278.             {
  279.                         WriteData(*bitmap_ptr++);
  280.             }
  281.             x++;//换行
  282.             Set_Cs;   
  283.           }
  284. }



  285. //内部调用的函数
  286. //-------------------------------------------------------------------------*
  287. //函数名: WriteRegister                                                    *
  288. //功  能: 写指定地址寄存器的值                                             *
  289. //参  数: index    寄存器地址                                              *
  290. //        data      寄存器值                                               *
  291. //返  回: 无                                                               *
  292. //内部调用:WriteIndex,WriteData                                            *
  293. //说  明:  内部函数                                                        *
  294. //-------------------------------------------------------------------------*
  295. static void WriteRegister(uint16 index,uint16 data)
  296. {
  297.         /************************************************************************
  298.         **                                                                     **
  299.         ** nCS       ----\__________________________________________/-------   **
  300.         ** RS        ------\____________/-----------------------------------   **
  301.         ** nRD       -------------------------------------------------------   **
  302.         ** nWR       --------\_______/--------\_____/-----------------------   **
  303.         ** DB[0:15]  ---------[index]----------[data]-----------------------   **
  304.         **                                                                     **
  305.         ************************************************************************/
  306.         Clr_Cs;
  307.         WriteIndex(index);
  308.         WriteData(data);
  309.         Set_Cs;
  310. }
  311. //-------------------------------------------------------------------------*
  312. //函数名: WriteIndex                                                       *
  313. //功  能: 写 SSD2119 控制器寄存器地址                                      *
  314. //参  数: index    寄存器地址                                              *
  315. //返  回: 无                                                               *
  316. //内部调用:                                                                *
  317. //说  明:  调用前需先选中控制器,内部函数                                  *
  318. //-------------------------------------------------------------------------*
  319. static void WriteIndex(uint16 index)
  320. {
  321.         Clr_Rs;
  322.         Set_nRd;
  323.         DATA_LO_PORT = index;
  324.         DATA_HI1_PORT = (index>>8);
  325.         DATA_HI2_PORT = (index>>12);
  326.         Clr_nWr;
  327.         Set_nWr;
  328.         Set_Rs;
  329. }
  330. //-------------------------------------------------------------------------*
  331. //函数名: WriteData                                                        *
  332. //功  能: 写 SSD2119 寄存器数据                                            *
  333. //参  数: data     寄存器数据                                              *
  334. //返  回: 无                                                               *
  335. //内部调用:                                                                *
  336. //说  明: 向控制器指定地址写入数据,调用前需先写寄存器地址,内部函数       *
  337. //-------------------------------------------------------------------------*
  338. static void WriteData(uint16 data)
  339. {
  340.         Set_Rs;
  341.         Set_nRd;
  342.           DATA_LO_PORT = data;
  343.         DATA_HI1_PORT = (data>>8);
  344.         DATA_HI2_PORT = (data>>12);
  345.         Clr_nWr;
  346.         Set_nWr;
  347. }
  348. //-------------------------------------------------------------------------*
  349. //函数名: ReadRegister                                                     *
  350. //功  能: 读取指定地址寄存器的值                                           *
  351. //参  数: index    寄存器地址                                              *
  352. //返  回: 寄存器值                                                         *
  353. //内部调用: WriteIndex, ReadData                                           *
  354. //说  明: 内部函数                                                         *
  355. //-------------------------------------------------------------------------*
  356. static uint16 ReadRegister(uint16 index)
  357. {
  358.         Clr_Cs;
  359.         WriteIndex(index);
  360.         Clr_nRd;
  361.         index = ReadData();
  362.         Set_nRd;
  363.         Set_Cs;
  364.         return index;
  365. }
  366. //-------------------------------------------------------------------------*
  367. //函数名: ReadData                                                         *
  368. //功  能: 读取控制器数据                                                   *
  369. //参  数: 无                                                               *
  370. //返  回:返回读取到的数据                                                  *
  371. //内部调用:                                                                *
  372. //说  明: 内部函数                                                         *
  373. //-------------------------------------------------------------------------*
  374. static uint16 ReadData(void)
  375. {
  376.         //========================================================================
  377.         // **                                                                   **
  378.         // ** nCS       ----\__________________________________________/------- **
  379.         // ** RS        ------\____________/----------------------------------- **
  380.         // ** nRD       -------------------------\_____/---------------------   **
  381.         // ** nWR       --------\_______/-------------------------------------- **
  382.         // ** DB[0:15]  ---------[index]----------[data]----------------------- **
  383.         // **                                                                   **
  384.         //========================================================================
  385.         uint16 tmp;
  386.         DATA_INPUT();
  387.         tmp =DATA_HI2_PORT;
  388.         tmp = (tmp << 4) + DATA_HI1_PORT;       /* 读入高8位数据*/
  389.         tmp = (tmp << 8) + DATA_LO_PORT;         /* 读入低8位数据*/
  390.         DATA_OUTPUT();
  391.         return tmp;
  392. }
  393. //-------------------------------------------------------------------------*
  394. //函数名: SetCursor                                                        *
  395. //功  能: 设置屏幕坐标                                                     *
  396. //参  数: x      行坐标                                                    *
  397. //        y      列坐标                                                    *
  398. //返  回:无                                                                *
  399. //内部调用:                                                                *
  400. //说  明: 内部函数                                                         *
  401. //-------------------------------------------------------------------------*
  402. static void SetCursor(uint16 x,uint16 y)
  403. {
  404.     WriteRegister(0x004e,x); // 行
  405.     WriteRegister(0x004f,y); // 列        
  406. }
  407. //-------------------------------------------------------------------------*
  408. //函数名: Reset                                                            *
  409. //功  能: 复位 SSD2119 控制器                                              *
  410. //参  数: 无                                                               *
  411. //返  回: 无                                                               *
  412. //内部调用:                                                                *
  413. //说  明: 复位控制器,内部函数                                             *
  414. //-------------------------------------------------------------------------*
  415. static void Reset()
  416. {
  417.         /***************************************
  418.         **                                    **
  419.         **  -------\______________/-------    **
  420.         **         | <---Tres---> |           **
  421.         **                                    **
  422.         **  Tres: Min.1ms                     **
  423.         ***************************************/  
  424.         Set_RST;  
  425.         Delay(50);
  426.         Cls_RST;
  427.         Delay(50);
  428.         Set_RST;
  429.         Delay(50);
  430. }
  431. //-------------------------------------------------------------------------*
  432. //函数名: LCDDrawChar                                                      *
  433. //功  能: 在给定的行、列画一个字符(8*16点阵)                               *
  434. //参  数: x :字符所在的行                                                  *
  435. //        y :字符所在的列                                                  *
  436. //        c*:指向字符的指针                                                *
  437. //返  回: 无                                                               *
  438. //内部调用:                                                                *
  439. //说  明: 内部函数                                                         *
  440. //-------------------------------------------------------------------------*
  441. static void LCDDrawChar(uint16 x, uint16 y, uint8 *c)
  442. {
  443.         uint16 index = 0;
  444.         int32  i = 0;
  445.         uint16 Xaddress = 0;   
  446.         Xaddress = x;
  447.         WriteRegister(0x4e, Xaddress);
  448.         WriteRegister(0x4f, y);
  449.         for(index = 0; index < 16; index++)
  450.         {
  451.                 Clr_Cs;
  452.                 WriteIndex(0x22);            
  453.                 for(i = 8; i >= 0; i--)
  454.                 {
  455.                         if((c[index] & (1 << i)) == 0x00)
  456.                         {
  457. ……………………

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

所有资料51hei提供下载:
SSD2119驱动.rar (5.03 KB, 下载次数: 60)





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

使用道具 举报

沙发
ID:110517 发表于 2017-9-6 12:26 | 只看该作者
TFT液晶!!!
回复

使用道具 举报

板凳
ID:351627 发表于 2018-6-14 09:53 | 只看该作者
SSD2119是不是也是属于8080接口的?
回复

使用道具 举报

地板
ID:392162 发表于 2018-8-31 22:53 | 只看该作者
本帖最后由 lg2911 于 2018-9-1 11:26 编辑
dengmcu 发表于 2018-6-14 09:53
SSD2119是不是也是属于8080接口的?

看手册确认了一下,SSD2119本身是8080和6800两种接口都支持的,用PS3-PS0来选择,要看液晶模组是否把这4根线引出来了,要是引出了,那就是可以使用者自己设置的
回复

使用道具 举报

5#
ID:418685 发表于 2018-11-13 13:52 | 只看该作者
谢谢分享,正在学习这方面知识
回复

使用道具 举报

6#
ID:513785 发表于 2021-6-15 16:15 | 只看该作者

谢谢分享,正在学习这方面知识
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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