找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5332|回復(fù): 5
收起左側(cè)

TFT液晶SSD2119的stm32單片機(jī)驅(qū)動(dòng)

[復(fù)制鏈接]
ID:110517 發(fā)表于 2017-9-6 12:25 | 顯示全部樓層 |閱讀模式
SSD2119 ID:9919;基于stm32f1的
0.png

單片機(jī)源程序如下:
  1. //-------------------------------------------------------------------------*
  2. //文件名:  LCD.c (LCD操作函數(shù)文件)                                         *
  3. //-------------------------------------------------------------------------*
  4. #include "LCD.h"
  5. #include "font.h"
  6. //內(nèi)部調(diào)用的函數(shù)聲明
  7. static void WriteRegister(uint16 index,uint16 data);//寫指定地址寄存器的值
  8. static void WriteIndex(uint16 index);//寫 SSD2119 控制器寄存器地址
  9. static void WriteData(uint16 data);//寫 SSD2119 寄存器數(shù)據(jù)
  10. static uint16 ReadRegister(uint16 index);//讀取指定地址寄存器的值
  11. static uint16 ReadData(void);//讀取控制器數(shù)據(jù)
  12. static void SetCursor(uint16 x,uint16 y);//設(shè)置屏幕坐標(biāo)
  13. static void Reset();//復(fù)位 SSD2119 控制器
  14. static void LCDDrawChar(uint16 x, uint16 y, uint8 *c);//在屏幕上畫一個(gè)字符
  15. static void LCDDrawChinese(uint16 x, uint16 y, uint16 *c);//在屏幕上畫一個(gè)漢字
  16. //-------------------------------------------------------------------------*
  17. //函數(shù)名: LCDInit                                                          *
  18. //功  能: 液晶初始化程序                                                   *
  19. //參  數(shù): 無                                                               *
  20. //返  回: 無                                                               *
  21. //內(nèi)部調(diào)用: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.         //設(shè)置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. //函數(shù)名: LCDClear                                                         *
  91. //功  能: 將屏幕填充成指定的顏色,如清屏,則填充 0xffff                    *
  92. //參  數(shù): ClearColor:填充顏色                                              *
  93. //返  回: 無                                                               *
  94. //內(nèi)部調(diào)用: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. //函數(shù)名: LCDTest                                                          *
  114. //功  能: 測試液晶屏                                                       *
  115. //參  數(shù): 無                                                               *
  116. //返  回: 無                                                               *
  117. //內(nèi)部調(diào)用: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. //函數(shù)名: LCDSetPixel                                                      *
  143. //功  能: 在指定座標(biāo)畫點(diǎn)                                                   *
  144. //參  數(shù): x      行座標(biāo)                                                    *
  145. //        y      列座標(biāo)                                                    *
  146. //        PixcelColor  像素的顏色                                          *
  147. //返  回: 無                                                               *
  148. //內(nèi)部調(diào)用: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. //函數(shù)名: LCDShowChar                                                      *
  162. //功  能: 顯示一個(gè)字符(8*16點(diǎn)陣)                                           *
  163. //參  數(shù): ln:字符要顯示的行                                                *
  164. //       col:字符要顯示的列                                                *
  165. //         c:要顯示的字符                                                  *
  166. //返  回: 無                                                               *
  167. //內(nèi)部調(diào)用: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. //函數(shù)名: LCDShowStringLn                                                  *
  177. //功  能: 在指定的行顯示字符串                                             *
  178. //參  數(shù): ln:要顯示的行                                                    *
  179. //       *s: 指向字符串的指針                                              *
  180. //返  回: 無                                                               *
  181. //內(nèi)部調(diào)用: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.         //統(tǒng)計(jì)字符個(gè)數(shù)
  193.         while(*p!= 0)
  194.         {
  195.                 Count++;
  196.                 p++;        
  197.         }
  198.         UartSend1(0,Count);
  199.     while ((*s != 0) && (i <= Count))  //在屏幕上依次寫每個(gè)字符
  200.         {
  201.                 LCDShowChar(ln, refcolumn, *s);                        
  202.                    refcolumn -= 8; //列減去一個(gè)字符
  203.                 s++;                                             
  204.                 if((i%30)==0)  
  205.                 {
  206.                         refcolumn=240;
  207.                         ln+=16;//換行
  208.                 }            
  209.                 i++;        //統(tǒng)計(jì)字符的個(gè)數(shù)            
  210.         }
  211. }
  212. //-------------------------------------------------------------------------*
  213. //函數(shù)名: LCDShowChinese                                                   *
  214. //功  能: 在指定的行、列顯示一個(gè)漢字                                       *
  215. //參  數(shù): ln:漢字要顯示的行                                                *
  216. //        col:漢字要顯示的列                                               *
  217. //        start:要顯示的漢字串的開始下標(biāo)                                   *
  218. //返  回: 無                                                               *
  219. //內(nèi)部調(diào)用:LCDDrawChinese                                                  *
  220. //說  明:                                                                  *
  221. //-------------------------------------------------------------------------*
  222. void LCDShowChinese(uint16 ln, uint16 col, uint8 start)
  223. {
  224.         LCDDrawChinese(ln, col, &ASCII_Chinese[16*start]);
  225. }
  226. //-------------------------------------------------------------------------*
  227. //函數(shù)名: LCDShowChineseLine                                               *
  228. //功  能: 在指定的行顯示一串漢字                                           *
  229. //參  數(shù): ln:漢字要顯示的行                                                *
  230. //        start:要顯示的漢字串的開始下標(biāo)                                   *
  231. //        count:一行中要顯示的漢字個(gè)數(shù)                                     *
  232. //返  回: 無                                                               *
  233. //內(nèi)部調(diào)用: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. //函數(shù)名: LCDShowBitmap                                                    *
  255. //功  能: 顯示一幅圖片,x為水平坐標(biāo),y為垂直坐標(biāo),每個(gè)像素用16位二進(jìn)制表示    *
  256. //        5:6:5格式                                                        *
  257. //參  數(shù): x: 水平起始位置                                                  *
  258. //        y: 垂直起始位置                                                  *
  259. //        w: 圖片的寬度                                                    *
  260. //        h: 圖片的高度                                                    *
  261. //        bitmap: 指向圖片數(shù)組的指針                                       *
  262. //返  回: 無                                                               *
  263. //內(nèi)部調(diào)用:                                                                *
  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. //內(nèi)部調(diào)用的函數(shù)
  286. //-------------------------------------------------------------------------*
  287. //函數(shù)名: WriteRegister                                                    *
  288. //功  能: 寫指定地址寄存器的值                                             *
  289. //參  數(shù): index    寄存器地址                                              *
  290. //        data      寄存器值                                               *
  291. //返  回: 無                                                               *
  292. //內(nèi)部調(diào)用:WriteIndex,WriteData                                            *
  293. //說  明:  內(nèi)部函數(shù)                                                        *
  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. //函數(shù)名: WriteIndex                                                       *
  313. //功  能: 寫 SSD2119 控制器寄存器地址                                      *
  314. //參  數(shù): index    寄存器地址                                              *
  315. //返  回: 無                                                               *
  316. //內(nèi)部調(diào)用:                                                                *
  317. //說  明:  調(diào)用前需先選中控制器,內(nèi)部函數(shù)                                  *
  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. //函數(shù)名: WriteData                                                        *
  332. //功  能: 寫 SSD2119 寄存器數(shù)據(jù)                                            *
  333. //參  數(shù): data     寄存器數(shù)據(jù)                                              *
  334. //返  回: 無                                                               *
  335. //內(nèi)部調(diào)用:                                                                *
  336. //說  明: 向控制器指定地址寫入數(shù)據(jù),調(diào)用前需先寫寄存器地址,內(nèi)部函數(shù)       *
  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. //函數(shù)名: ReadRegister                                                     *
  350. //功  能: 讀取指定地址寄存器的值                                           *
  351. //參  數(shù): index    寄存器地址                                              *
  352. //返  回: 寄存器值                                                         *
  353. //內(nèi)部調(diào)用: WriteIndex, ReadData                                           *
  354. //說  明: 內(nèi)部函數(shù)                                                         *
  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. //函數(shù)名: ReadData                                                         *
  368. //功  能: 讀取控制器數(shù)據(jù)                                                   *
  369. //參  數(shù): 無                                                               *
  370. //返  回:返回讀取到的數(shù)據(jù)                                                  *
  371. //內(nèi)部調(diào)用:                                                                *
  372. //說  明: 內(nèi)部函數(shù)                                                         *
  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位數(shù)據(jù)*/
  389.         tmp = (tmp << 8) + DATA_LO_PORT;         /* 讀入低8位數(shù)據(jù)*/
  390.         DATA_OUTPUT();
  391.         return tmp;
  392. }
  393. //-------------------------------------------------------------------------*
  394. //函數(shù)名: SetCursor                                                        *
  395. //功  能: 設(shè)置屏幕坐標(biāo)                                                     *
  396. //參  數(shù): x      行坐標(biāo)                                                    *
  397. //        y      列坐標(biāo)                                                    *
  398. //返  回:無                                                                *
  399. //內(nèi)部調(diào)用:                                                                *
  400. //說  明: 內(nèi)部函數(shù)                                                         *
  401. //-------------------------------------------------------------------------*
  402. static void SetCursor(uint16 x,uint16 y)
  403. {
  404.     WriteRegister(0x004e,x); // 行
  405.     WriteRegister(0x004f,y); // 列        
  406. }
  407. //-------------------------------------------------------------------------*
  408. //函數(shù)名: Reset                                                            *
  409. //功  能: 復(fù)位 SSD2119 控制器                                              *
  410. //參  數(shù): 無                                                               *
  411. //返  回: 無                                                               *
  412. //內(nèi)部調(diào)用:                                                                *
  413. //說  明: 復(fù)位控制器,內(nèi)部函數(shù)                                             *
  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. //函數(shù)名: LCDDrawChar                                                      *
  433. //功  能: 在給定的行、列畫一個(gè)字符(8*16點(diǎn)陣)                               *
  434. //參  數(shù): x :字符所在的行                                                  *
  435. //        y :字符所在的列                                                  *
  436. //        c*:指向字符的指針                                                *
  437. //返  回: 無                                                               *
  438. //內(nèi)部調(diào)用:                                                                *
  439. //說  明: 內(nèi)部函數(shù)                                                         *
  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黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
SSD2119驅(qū)動(dòng).rar (5.03 KB, 下載次數(shù): 60)





回復(fù)

使用道具 舉報(bào)

ID:110517 發(fā)表于 2017-9-6 12:26 | 顯示全部樓層
TFT液晶。!
回復(fù)

使用道具 舉報(bào)

ID:351627 發(fā)表于 2018-6-14 09:53 | 顯示全部樓層
SSD2119是不是也是屬于8080接口的?
回復(fù)

使用道具 舉報(bào)

ID:392162 發(fā)表于 2018-8-31 22:53 | 顯示全部樓層
本帖最后由 lg2911 于 2018-9-1 11:26 編輯
dengmcu 發(fā)表于 2018-6-14 09:53
SSD2119是不是也是屬于8080接口的?

看手冊確認(rèn)了一下,SSD2119本身是8080和6800兩種接口都支持的,用PS3-PS0來選擇,要看液晶模組是否把這4根線引出來了,要是引出了,那就是可以使用者自己設(shè)置的
回復(fù)

使用道具 舉報(bào)

ID:418685 發(fā)表于 2018-11-13 13:52 | 顯示全部樓層
謝謝分享,正在學(xué)習(xí)這方面知識(shí)
回復(fù)

使用道具 舉報(bào)

ID:513785 發(fā)表于 2021-6-15 16:15 | 顯示全部樓層

謝謝分享,正在學(xué)習(xí)這方面知識(shí)
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表