【GD32F350開發分享五】GPIO模擬SPI控制液晶顯示屏

大家都應該知道,用GPIO模擬SPI的話,涉及GPIO的輸出高低電平、讀取電平,先來看GPIO的寄存器


(1)GPIOx_CTL寄存器控制GPIO的模式(Pin0~Pin15)
00:輸入模式(復位值) 01:GPIO輸出模式 10:備用功能模式 11:模擬模式
(2)GPIOx_OMODE寄存器控制GPIO的輸出模式(Pin0~Pin15)
該位由軟件置位和清除。 0:輸出推輓模式(復位值) 1:輸出開漏模式 
(3)GPIOx_OSPD0寄存器控制GPIO的輸出速度(Pin0~Pin15)
該位由軟件置位和清除。 x0:輸出最大速度2M(復位值) 01:輸出最大速度10M 11:輸出最大速度50M 
(4)GPIOx_PUD寄存器控制GPIO的上下拉
該位由軟件置位和清除。 00:懸空模式,無上拉和下拉(復位值) 01:端口上拉模式 10:端口下拉模式 11:保留 
(5)GPIOx_ISTAT寄存器爲端口輸入狀態寄存器,低16位有效
這些位由軟件置位和清除。 0:引腳輸入信號爲低電平 1:引腳輸入信號爲高電平 
(6)GPIOx_OCTL寄存器爲端口輸出控制寄存器,低16位有效
該位由軟件置位和清除。 0:引腳輸出低電平 1:引腳輸出高電平 

下面爲液晶顯示屏的驅動程序

  1.  
  2. #include "gd32f3x0.h"
  3. #include <stdio.h>
  4. #include "gd32f3x0_eval.h"
  5. #include "systick.h"
  6. #include "lcd.h"
  7.  
  8. static unsigned char LcdSystab[] = {0x30,0x87,0x07,0x28,0x2f,0x0f0,0x28,0x00};          
  9. static unsigned char LcdScrtab[] = {0x00,0x00,0x0f0,0x00,0x40,0x0f0,0x00,0x80,0x00,0x00};
  10.  
  11. #define CLOCK 9
  12. /*------------------------------------------------------------
  13.                          usÑóê±oˉêy 
  14. ------------------------------------------------------------*/
  15. void Delay_Nus(unsigned int us)
  16. {
  17.         uint8_t n;                    
  18.         while(us--)for(n=0;n<CLOCK;n++);          
  19. }
  20.  
  21. /*------------------------------------------------------------
  22.                          msÑóê±oˉêy
  23. ------------------------------------------------------------*/
  24. void Delay_Nms(unsigned int ms)
  25. {
  26.         while(ms--)Delay_Nus(1000);         
  27. }
  28.  
  29. void LcdDelay(void)
  30. {
  31.    Delay_Nus(5);
  32. }
  33. void chk_busy(void)
  34. {
  35.     GPIO_BC(GPIOC)=GPIO_PIN_6;
  36.     GPIO_CTL(GPIOB) = (GPIO_CTL(GPIOB)  & 0xf0ffffff) | 0x08000000; 
  37.     
  38.     WR_1;    
  39.     A0_0;
  40.     RD_0;
  41.     Delay_Nus(0);
  42.     while(BUSY==1)
  43.     {
  44.     }
  45.     RD_1;    
  46.     GPIO_CTL(GPIOB)= (GPIO_CTL(GPIOB) & 0xf0ffffff) | 0x03000000;
  47. }
  48.  
  49. void send_cmd(uchar cmd)
  50. {
  51.     chk_busy();
  52.  
  53.     RD_1;
  54.     A0_1; 
  55.     DataOUT = ((DataOUT & 0x00ff) | (((uint16_t)cmd)<<8));
  56.     WR_0;
  57.     Delay_Nus(1);
  58.     WR_1;
  59. }
  60.  
  61. void send_dat(uchar dat)        
  62. {
  63.     chk_busy ();
  64.     
  65.     RD_1;
  66.     A0_0;   
  67.     DataOUT = ((DataOUT & 0x00ff) | (((uint16_t)dat)<<8));
  68.     WR_0;
  69.     Delay_Nus(1);
  70.     WR_1;
  71. }
  72.  
  73. uchar Read_data(void)
  74. {
  75.     uint8_t temp = 0;
  76.     
  77.     GPIO_OCTL(GPIOB) &= 0x00ff;
  78.     GPIO_CTL(GPIOB) = (GPIO_CTL(GPIOB) & 0x00000000) | 0x88888888;
  79.     
  80.     A0_1;
  81.     WR_1;
  82.     RD_0;
  83.     temp = DataIN>>8;
  84.     RD_1;
  85.     GPIO_CTL(GPIOB) = (GPIO_CTL(GPIOB) & 0x00000000) | 0x33333333;  
  86.     return temp;
  87. }
  88.  
  89. // -------------------?????????------------------
  90. void  LcdCsrW ( unsigned char x, unsigned char y)
  91. {
  92.         send_cmd(CsrW);
  93.         LcdDelay();
  94.         send_dat(x);
  95.         LcdDelay();
  96.         send_dat(y);
  97.         LcdDelay();
  98.         return;         
  99. // ????===================================     
  100. void LcdClear(void)
  101. {
  102.     unsigned int x,y; 
  103.     send_cmd(CsrDirR); //???? 
  104.     LcdDelay(); 
  105.  
  106.     LcdCsrW (0,0);
  107.     send_cmd(mWrite);                                       
  108.     for(x = 0;x<40;x++)
  109.       for(y = 0;y<240;y++)
  110.       {    
  111.         LcdDelay();  
  112.         send_dat(0x0ff);      
  113.       } 
  114. }
  115.  
  116. void LcdHZ(unsigned char x, unsigned int y,unsigned char *pdata,unsigned char flag)
  117. {
  118.     unsigned char i,j; 
  119.     unsigned int Addr,tpInt;
  120.     unsigned char a ,b;
  121.     send_cmd(CsrDirR); 
  122.     LcdDelay();                   
  123.  
  124.     for(j = 0; j<2; j++)
  125.     {
  126.     if (x+j>=40)  
  127.        return;
  128.        
  129.     for (i = 0; i<16; i++)
  130.       { 
  131.         Addr  =  (y+i)*40 + (x + j);
  132.         a  =  Addr & 0x0FF;
  133.         b  =  Addr >> 8;
  134.     
  135.         LcdCsrW (a,b);
  136.         LcdDelay(); 
  137.         
  138.         send_cmd(mWrite);
  139.         LcdDelay(); 
  140.         
  141.         a=j*16+i;
  142.         
  143.         tpInt=pdata[a>>1];
  144.         if (a & 0x0001)
  145.           b=tpInt & 0x00FF;
  146.         else
  147.           b=(tpInt>>8) & 0x00FF;  
  148.         
  149.         if(!flag)  
  150.         {
  151.           b=~b;
  152.         }
  153.         send_dat(b); 
  154.            
  155.     }
  156.   }
  157. }
  158.  
  159.  
  160. //---------------------??------x 0?39   y 0?239 ------8 X 16---------------
  161. void LcdChar(unsigned char x, unsigned char y, unsigned char *pdata, unsigned char flag) //flag  0??  1??
  162. {
  163.     unsigned char i,a,b; 
  164.     unsigned int Addr,tpInt;
  165.  
  166.     send_cmd(CsrDirD);  //????
  167.     LcdDelay();      
  168.  
  169.     if (x>=40)  //  x 0?39 
  170.        return;
  171.        
  172.     for (i = 0; i<16; i++)
  173.     { 
  174.         Addr  =  (y+i)*40 + x ;
  175.         a  =  Addr & 0x0FF;
  176.         b  =  Addr >> 8;
  177.     
  178.         LcdCsrW (a,b);
  179.         LcdDelay(); 
  180.         
  181.         send_cmd(mWrite);
  182.         LcdDelay(); 
  183.  
  184.         tpInt=pdata[i>>1];
  185.         if (i & 0x0001)
  186.             b=tpInt & 0x00FF;
  187.         else
  188.             b=(tpInt>>8) & 0x00FF;
  189.         
  190.         if(!flag)  
  191.         {
  192.           b=~b;
  193.         }      
  194.         send_dat(b);
  195.     }
  196.  
  197. //-----------x=0 ? 319   y=0 ? 239 ----------------------------
  198. void LcdPoint( unsigned int x, unsigned int y, unsigned char flag) //flag = 0 ??(?), bf=1 ??(?), bf=2 ????(??)
  199. {
  200.     unsigned int Addr;
  201.     unsigned int a ,b;
  202.     unsigned char temp ;
  203.     unsigned int bitAddr = 0;
  204.     unsigned char bit1;
  205.  
  206.     a  =  x >> 3;
  207.     //temp  = Sed[a][y];
  208.     bitAddr = x - ( a << 3 );
  209.         
  210.     b  =  y*40;
  211.     Addr  =  b + a;
  212.     
  213.     a  =  Addr & 0x0FF;
  214.     b  =  Addr >> 8;
  215.  
  216.     bit1 = 0x80;
  217.     if(bitAddr)
  218.       bit1 =  bit1>>bitAddr;
  219.  
  220.     if(flag  ==  0)//?
  221.        temp = bit1 | temp;
  222.     else if(flag  ==  1) //?
  223.     { 
  224.        bit1 = 255- bit1;
  225.        temp = (bit1 & temp);
  226.     }   
  227.     else if(flag  ==  2)//??
  228.        temp = bit1 ^ temp;
  229.     
  230.     send_cmd(CsrDirD);  //????
  231.     LcdDelay(); 
  232.     LcdCsrW( a, b);
  233.     LcdDelay();
  234.     send_cmd(mWrite);
  235.     LcdDelay();
  236.  
  237.     //Sed[x >> 3][y] = temp; //should be here,befor LcdWDataAddr  = temp;
  238.     send_dat(temp);
  239.  
  240. }
  241.  
  242. //-------------x=0 ? 319   y=0 ? 239 ------------------------
  243. void LcdLine(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned char flag)//flag 0 ??, bf=1 ??, bf=2 ????
  244. {
  245.     register unsigned char t;
  246.     int    xerr=0,yerr=0,delta_x,delta_y,distance;
  247.     int    incx,incy;
  248.     unsigned int row,col;
  249.     
  250.     if (x0>x1)
  251.     {
  252.       t=x0;
  253.       x0=x1;
  254.       x1=t;
  255.     }// x0?  x1?
  256.  
  257.     if (y0>y1)
  258.     {
  259.       t=y0;
  260.       y0=y1;
  261.       y1=t;
  262.     }// y0?  y1?
  263.     
  264.     if (x1>319)
  265.       x1=319;
  266.     if (y1>239)
  267.       y1=239;
  268.       
  269.     delta_x    = x1-x0;                                        //??????
  270.     delta_y    = y1-y0;
  271.     col    = x0;
  272.     row    = y0;
  273.     if(delta_x>0) incx=1;                                    //??????
  274.     else
  275.     {
  276.         if( delta_x==0    ) incx=0;                            //???
  277.         else {incx=-1;delta_x=-delta_x;}
  278.     }
  279.     if(delta_y>0) incy=1;
  280.     else
  281.     {
  282.         if( delta_y==0    ) incy=0;                            //???
  283.         else {incy=-1;delta_y=-delta_y;}
  284.     }
  285.     if(    delta_x    > delta_y )    distance=delta_x;                //?????????
  286.     else distance=delta_y;
  287.  
  288.     for( t=0;t <= distance+1; t++ )                            //????
  289.     {                            
  290.         LcdPoint(col, row, flag);                        //??
  291.         xerr +=    delta_x    ;
  292.         yerr +=    delta_y    ;
  293.  
  294.         if(    xerr > distance    )
  295.         {
  296.             xerr-=distance;
  297.             col+=incx;
  298.         }
  299.         if(    yerr > distance )
  300.         {
  301.             yerr-=distance;
  302.             row+=incy;
  303.         }
  304.     }
  305.  
  306. }
  307.  
  308. // ==========????????=========================================
  309. void LcdInit(void) 
  310. {
  311.     unsigned char    i = 0;     
  312.     send_cmd(SystemSet);
  313.     LcdDelay(); 
  314.     for ( i = 0; i<8; i++){ 
  315.         LcdDelay(); 
  316.         send_dat(LcdSystab[i]);
  317.         }
  318.     
  319.     send_cmd(Scroll);
  320.     LcdDelay(); 
  321.     for ( i = 0; i<10; i++){
  322.         LcdDelay();  
  323.         send_dat(LcdScrtab[i]);             
  324.         }
  325.         
  326.     send_cmd(HdotSet);
  327.     LcdDelay(); 
  328.     send_dat(0x00);
  329.     LcdDelay(); 
  330.     
  331.     send_cmd(Ovlay);
  332.     LcdDelay(); 
  333.     send_dat(0x0C);
  334.     LcdDelay();                  
  335.     
  336.     LcdClear();   
  337.     
  338.     send_cmd(DispOn);
  339.     LcdDelay(); 
  340.     send_dat(0x04);
  341.     LcdDelay();          
  342. }
  343.  

複製代碼

  1. #ifndef __LCD_H
  2. #define __LCD_H                
  3. #include "gd32f3x0.h"
  4. #include <stdio.h>
  5. #include "gd32f3x0_eval.h"
  6. #include "systick.h"
  7. #define uchar      unsigned char
  8. #define uint       unsigned int
  9.  
  10. #define A0_0  GPIO_BOP(GPIOC)=GPIO_PIN_2
  11. #define A0_1  GPIO_BC(GPIOC)=GPIO_PIN_2
  12.  
  13. #define WR_0  GPIO_BOP(GPIOC)=GPIO_PIN_3
  14. #define WR_1  GPIO_BC(GPIOC)=GPIO_PIN_3
  15.  
  16. #define RD_0  GPIO_BOP(GPIOC)=GPIO_PIN_4
  17. #define RD_1  GPIO_BC(GPIOC)=GPIO_PIN_4
  18.  
  19. #define CS_0  GPIO_BOP(GPIOC)=GPIO_PIN_5
  20. #define CS_1  GPIO_BC(GPIOC)=GPIO_PIN_5
  21.  
  22. #define BUSY  gpio_input_bit_get(GPIOC,GPIO_PIN_6)
  23.  
  24. #define DataOUT GPIO_OCTL(GPIOB)
  25.  
  26. #define DataIN  GPIO_ISTAT(GPIOB)
  27.  
  28.  
  29. /*--------------------------------------------------------------*/
  30. //???????????
  31.  
  32. #define                SystemSet        0x40
  33. #define                SleepIn                0x53
  34. #define                DispOn                0x59
  35. #define                DispOff                0x58
  36. #define                Scroll                0x44
  37. #define                Csrform                0x5d
  38. #define                CgramAdr        0x50
  39. #define                CsrDirR                0x4c
  40. #define                CsrDirL                0x4d
  41. #define                CsrDirU                0x4e
  42. #define                CsrDirD                0x4f        
  43. #define                HdotSet                0x5a
  44. #define                Ovlay                0x5b
  45. #define                CsrW                0x46
  46. #define                CsrR                0x47
  47. #define                mWrite                0x42        
  48. #define                mRead                0x43
  49.  
  50. void LCD_PinInit(void);
  51. void chk_busy(void);
  52. void send_cmd(uchar cmd);
  53. void send_dat(uchar dat);
  54.  
  55. void Delay_Nms(uint n);
  56. void Delay_Nus(uint n);
  57. #define LCD_SCROLL                0x44
  58.  
  59. void LcdClear(void);
  60. //------------flag  0??  1??-----------------------------------------------
  61. void LcdHZ(unsigned char x, unsigned int y,unsigned char *pdata,unsigned char flag); 
  62. void LcdChar(unsigned char x, unsigned char y, unsigned char *pdata, unsigned char flag);
  63. //-----------flag = 0 ??(?), bf=1 ??(?), bf=2 ????(??)-----------
  64. void LcdPoint( unsigned int x, unsigned int y, unsigned char flag); 
  65. //-----------flag = 0 ??(?), bf=1 ??(?), bf=2 ????(??)-----------
  66. void LcdLine(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned char flag);
  67. void LcdInit(void); 
  68. void  LcdCsrW ( unsigned char x, unsigned char y);
  69. void Delay_ns(); 
  70.  
  71.  
  72. #endif
  73.  
  74.  
  75.  

複製代碼


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章