[PIC®/AVR®/dsPIC®产品] 【CuriosityNano测评报告】+PIC18F16Q41 驱动SPI屏

[复制链接]
5005|20
 楼主| qjp1988113 发表于 2021-8-30 10:21 | 显示全部楼层 |阅读模式
本帖最后由 pzsh 于 2021-9-13 13:24 编辑

电脑重装系统了,耽搁了好几天。重装的MAPLABX 莫名的出错,老是报错,说是啥编译器。编译器明明装了啊。又重新装了一遍,又装了些插件,然后就莫名的好了~真是让人一头雾水~
今天呢,来驱动一个SPI的小屏幕(ST7789,分辨率240X240)~到底是8位MCU,图片稍微整大些,就报错,超出范围了。

对于MCU得pack包升级到最新版本,新建工程,打开MCC配置,这里完全GPIO模拟,没什么好说的。
其中注意,LCD初始化时,要先控制的几个GPIO初始化要为高电平~否则屏幕显示不正常~
时钟:

gpio:

下面就开始程序:
lcd_init.h
  1. /*
  2. * File:   lcd_init.h
  3. * Author: Administrator
  4. *
  5. * Created on August 19, 2021, 3:23 PM
  6. */

  7. #ifndef __LCD_INIT_H_
  8. #define        __LCD_INIT_H_

  9. #ifdef        __cplusplus
  10. extern "C" {
  11. #endif

  12. #include "type.h"
  13.    
  14. #define USE_HORIZONTAL 0  //?????????? 0?1??? 2?3???


  15. #define LCD_W 240
  16. #define LCD_H 240


  17. //-----------------LCD????----------------
  18.    
  19. #define LCD_SCLK_Clr() LCD_SCL_SetLow()//SCL=SCLK
  20. #define LCD_SCLK_Set() LCD_SCL_SetHigh()

  21. #define LCD_MOSI_Clr() LCD_SDA_SetLow()//SDA=MOSI
  22. #define LCD_MOSI_Set() LCD_SDA_SetHigh()

  23. #define LCD_RES_Clr()  LCD_RES_SetLow()//RES
  24. #define LCD_RES_Set()  LCD_RES_SetHigh()

  25. #define LCD_DC_Clr()   LCD_DC_SetLow()//DC
  26. #define LCD_DC_Set()   LCD_DC_SetHigh()

  27. #define LCD_BLK_Clr()  LCD_BLK_SetLow()//BLK
  28. #define LCD_BLK_Set()  LCD_BLK_SetHigh()


  29. void LCD_GPIO_Init(void);//???GPIO
  30. void LCD_Writ_Bus(u8 dat);//??SPI??
  31. void LCD_WR_DATA8(u8 dat);//??????
  32. void LCD_WR_DATA(u16 dat);//??????
  33. void LCD_WR_REG(u8 dat);//??????
  34. void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2);//??????
  35. void LCD_Init(void);//LCD???


  36. #ifdef        __cplusplus
  37. }
  38. #endif

  39. #endif        /* LCD_INIT_H */

lcd_init.c
  1. #include "lcd_init.h"
  2. #include "../mcc_generated_files/mcc.h"

  3. void LCD_GPIO_Init(void)
  4. {
  5.     LCD_RES_Set();
  6.     LCD_DC_Set();
  7.     LCD_SCLK_Set();
  8.     LCD_MOSI_Set();
  9.     LCD_BLK_Set();
  10.    
  11.     __delay_ms(1000);//ENOUGH TIME
  12. }


  13. /******************************************************************************
  14.       ?????LCD????????
  15.       ?????dat  ????????
  16.       ????  ?
  17. ******************************************************************************/
  18. void LCD_Writ_Bus(u8 dat)
  19. {        
  20.         u8 i;
  21.         for(i=0;i<8;i++)
  22.         {                          
  23.                 LCD_SCLK_Clr();
  24.                 if(dat&0x80)
  25.                 {
  26.                    LCD_MOSI_Set();
  27.                 }
  28.                 else
  29.                 {
  30.                    LCD_MOSI_Clr();
  31.                 }
  32.                 LCD_SCLK_Set();
  33.                 dat<<=1;
  34.         }        
  35. }


  36. /******************************************************************************
  37.       ?????LCD????
  38.       ?????dat ?????
  39.       ????  ?
  40. ******************************************************************************/
  41. void LCD_WR_DATA8(u8 dat)
  42. {
  43.         LCD_Writ_Bus(dat);
  44. }


  45. /******************************************************************************
  46.       ?????LCD????
  47.       ?????dat ?????
  48.       ????  ?
  49. ******************************************************************************/
  50. void LCD_WR_DATA(u16 dat)
  51. {
  52.         LCD_Writ_Bus(dat>>8);
  53.         LCD_Writ_Bus(dat);
  54. }


  55. /******************************************************************************
  56.       ?????LCD????
  57.       ?????dat ?????
  58.       ????  ?
  59. ******************************************************************************/
  60. void LCD_WR_REG(u8 dat)
  61. {
  62.         LCD_DC_Clr();//???
  63.         LCD_Writ_Bus(dat);
  64.         LCD_DC_Set();//???
  65. }


  66. /******************************************************************************
  67.       ??????????????
  68.       ?????x1,x2 ???????????
  69.                 y1,y2 ???????????
  70.       ????  ?
  71. ******************************************************************************/
  72. void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2)
  73. {
  74.         if(USE_HORIZONTAL==0)
  75.         {
  76.                 LCD_WR_REG(0x2a);//?????
  77.                 LCD_WR_DATA(x1);
  78.                 LCD_WR_DATA(x2);
  79.                 LCD_WR_REG(0x2b);//?????
  80.                 LCD_WR_DATA(y1);
  81.                 LCD_WR_DATA(y2);
  82.                 LCD_WR_REG(0x2c);//????
  83.         }
  84.         else if(USE_HORIZONTAL==1)
  85.         {
  86.                 LCD_WR_REG(0x2a);//?????
  87.                 LCD_WR_DATA(x1);
  88.                 LCD_WR_DATA(x2);
  89.                 LCD_WR_REG(0x2b);//?????
  90.                 LCD_WR_DATA(y1+80);
  91.                 LCD_WR_DATA(y2+80);
  92.                 LCD_WR_REG(0x2c);//????
  93.         }
  94.         else if(USE_HORIZONTAL==2)
  95.         {
  96.                 LCD_WR_REG(0x2a);//?????
  97.                 LCD_WR_DATA(x1);
  98.                 LCD_WR_DATA(x2);
  99.                 LCD_WR_REG(0x2b);//?????
  100.                 LCD_WR_DATA(y1);
  101.                 LCD_WR_DATA(y2);
  102.                 LCD_WR_REG(0x2c);//????
  103.         }
  104.         else
  105.         {
  106.                 LCD_WR_REG(0x2a);//?????
  107.                 LCD_WR_DATA(x1+80);
  108.                 LCD_WR_DATA(x2+80);
  109.                 LCD_WR_REG(0x2b);//?????
  110.                 LCD_WR_DATA(y1);
  111.                 LCD_WR_DATA(y2);
  112.                 LCD_WR_REG(0x2c);//????
  113.         }
  114. }

  115. void LCD_Init(void)
  116. {
  117.         LCD_GPIO_Init();//???GPIO
  118.         
  119.         LCD_RES_Clr();//??
  120.         __delay_ms(100);
  121.         LCD_RES_Set();
  122.         __delay_ms(100);
  123.         
  124.         LCD_BLK_Set();//????
  125.     __delay_ms(1000);
  126.         
  127.         //************* Start Initial Sequence **********//
  128.         LCD_WR_REG(0x11); //Sleep out
  129.         __delay_ms(120);              //Delay 120ms
  130.         //************* Start Initial Sequence **********//
  131.         LCD_WR_REG(0x36);
  132.         if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00);
  133.         else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0);
  134.         else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70);
  135.         else LCD_WR_DATA8(0xA0);

  136.         LCD_WR_REG(0x3A);
  137.         LCD_WR_DATA8(0x05);

  138.         LCD_WR_REG(0xB2);
  139.         LCD_WR_DATA8(0x0C);
  140.         LCD_WR_DATA8(0x0C);
  141.         LCD_WR_DATA8(0x00);
  142.         LCD_WR_DATA8(0x33);
  143.         LCD_WR_DATA8(0x33);

  144.         LCD_WR_REG(0xB7);
  145.         LCD_WR_DATA8(0x35);  

  146.         LCD_WR_REG(0xBB);
  147.         LCD_WR_DATA8(0x19);

  148.         LCD_WR_REG(0xC0);
  149.         LCD_WR_DATA8(0x2C);

  150.         LCD_WR_REG(0xC2);
  151.         LCD_WR_DATA8(0x01);

  152.         LCD_WR_REG(0xC3);
  153.         LCD_WR_DATA8(0x12);   

  154.         LCD_WR_REG(0xC4);
  155.         LCD_WR_DATA8(0x20);  

  156.         LCD_WR_REG(0xC6);
  157.         LCD_WR_DATA8(0x0F);   

  158.         LCD_WR_REG(0xD0);
  159.         LCD_WR_DATA8(0xA4);
  160.         LCD_WR_DATA8(0xA1);

  161.         LCD_WR_REG(0xE0);
  162.         LCD_WR_DATA8(0xD0);
  163.         LCD_WR_DATA8(0x04);
  164.         LCD_WR_DATA8(0x0D);
  165.         LCD_WR_DATA8(0x11);
  166.         LCD_WR_DATA8(0x13);
  167.         LCD_WR_DATA8(0x2B);
  168.         LCD_WR_DATA8(0x3F);
  169.         LCD_WR_DATA8(0x54);
  170.         LCD_WR_DATA8(0x4C);
  171.         LCD_WR_DATA8(0x18);
  172.         LCD_WR_DATA8(0x0D);
  173.         LCD_WR_DATA8(0x0B);
  174.         LCD_WR_DATA8(0x1F);
  175.         LCD_WR_DATA8(0x23);

  176.         LCD_WR_REG(0xE1);
  177.         LCD_WR_DATA8(0xD0);
  178.         LCD_WR_DATA8(0x04);
  179.         LCD_WR_DATA8(0x0C);
  180.         LCD_WR_DATA8(0x11);
  181.         LCD_WR_DATA8(0x13);
  182.         LCD_WR_DATA8(0x2C);
  183.         LCD_WR_DATA8(0x3F);
  184.         LCD_WR_DATA8(0x44);
  185.         LCD_WR_DATA8(0x51);
  186.         LCD_WR_DATA8(0x2F);
  187.         LCD_WR_DATA8(0x1F);
  188.         LCD_WR_DATA8(0x1F);
  189.         LCD_WR_DATA8(0x20);
  190.         LCD_WR_DATA8(0x23);
  191.         LCD_WR_REG(0x21);

  192.         LCD_WR_REG(0x29);
  193. }

lcd.h
  1. /*
  2. * File:   lcd.h
  3. * Author: Administrator
  4. *
  5. * Created on August 19, 2021, 3:22 PM
  6. */

  7. #ifndef __LCD_H_
  8. #define        __LCD_H_

  9. #ifdef        __cplusplus
  10. extern "C" {
  11. #endif
  12. #include "type.h"

  13.     //????
  14. #define WHITE                  0xFFFF
  15. #define BLACK                  0x0000         
  16. #define BLUE                    0x001F  
  17. #define BRED             0XF81F
  18. #define GRED                          0XFFE0
  19. #define GBLUE                         0X07FF
  20. #define RED                    0xF800
  21. #define MAGENTA                0xF81F
  22. #define GREEN                  0x07E0
  23. #define CYAN                   0x7FFF
  24. #define YELLOW                 0xFFE0
  25. #define BROWN                          0XBC40 //??
  26. #define BRRED                          0XFC07 //???
  27. #define GRAY                           0X8430 //??
  28. #define DARKBLUE               0X01CF        //???
  29. #define LIGHTBLUE               0X7D7C        //???  
  30. #define GRAYBLUE                0X5458 //???
  31. #define LIGHTGREEN              0X841F //???
  32. #define LGRAY                          0XC618 //???(PANNEL),?????
  33. #define LGRAYBLUE        0XA651 //????(?????)
  34. #define LBBLUE           0X2B12 //????(???????)
  35.    
  36. void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//????????
  37. void LCD_DrawPoint(u16 x,u16 y,u16 color);//?????????
  38. void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//?????????
  39. void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//??????????
  40. void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color);//?????????

  41. void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//?????
  42. void LCD_ShowChinese16x16(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//????16x16??
  43. void LCD_ShowChinese24x24(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//????24x24??
  44. void LCD_ShowChinese32x32(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//????32x32??

  45. void LCD_ShowChar(u16 x,u16 y,u8 num,u16 fc,u16 bc,u8 sizey,u8 mode);//??????
  46. void LCD_ShowString(u16 x,u16 y,const u8 *p,u16 fc,u16 bc,u8 sizey,u8 mode);//?????
  47. u32 mypow(u8 m,u8 n);//??
  48. void LCD_ShowIntNum(u16 x,u16 y,u16 num,u8 len,u16 fc,u16 bc,u8 sizey);//??????
  49. void LCD_ShowFloatNum1(u16 x,u16 y,float num,u8 len,u16 fc,u16 bc,u8 sizey);//????????

  50. void LCD_ShowPicture(u16 x,u16 y,u16 length,u16 width,const u8 pic[]);//????




  51. #ifdef        __cplusplus
  52. }
  53. #endif

  54. #endif        /* LCD_H */

lcd.c

  1. #include "lcd.h"
  2. #include "lcd_init.h"
  3. #include "lcdfont.h"



  4. /******************************************************************************
  5.       ??????????????
  6.       ?????xsta,ysta   ????
  7.                 xend,yend   ????
  8.                                                                 color       ??????
  9.       ????  ?
  10. ******************************************************************************/
  11. void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
  12. {         
  13.         u16 i,j;
  14.         LCD_Address_Set(xsta,ysta,xend-1,yend-1);//??????
  15.         for(i=ysta;i<yend;i++)
  16.         {                                                                                                                           
  17.                 for(j=xsta;j<xend;j++)
  18.                 {
  19.                         LCD_WR_DATA(color);
  20.                 }
  21.         }                                                      
  22. }

  23. /******************************************************************************
  24.       ????????????
  25.       ?????x,y ????
  26.                 color ????
  27.       ????  ?
  28. ******************************************************************************/
  29. void LCD_DrawPoint(u16 x,u16 y,u16 color)
  30. {
  31.         LCD_Address_Set(x,y,x,y);//??????
  32.         LCD_WR_DATA(color);
  33. }


  34. /******************************************************************************
  35.       ???????
  36.       ?????x1,y1   ????
  37.                 x2,y2   ????
  38.                 color   ????
  39.       ????  ?
  40. ******************************************************************************/
  41. void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color)
  42. {
  43.         u16 t;
  44.         int xerr=0,yerr=0,delta_x,delta_y,distance;
  45.         int incx,incy,uRow,uCol;
  46.         delta_x=x2-x1; //??????
  47.         delta_y=y2-y1;
  48.         uRow=x1;//??????
  49.         uCol=y1;
  50.         if(delta_x>0)incx=1; //??????
  51.         else if (delta_x==0)incx=0;//???
  52.         else {incx=-1;delta_x=-delta_x;}
  53.         if(delta_y>0)incy=1;
  54.         else if (delta_y==0)incy=0;//???
  55.         else {incy=-1;delta_y=-delta_x;}
  56.         if(delta_x>delta_y)distance=delta_x; //?????????
  57.         else distance=delta_y;
  58.         for(t=0;t<distance+1;t++)
  59.         {
  60.                 LCD_DrawPoint(uRow,uCol,color);//??
  61.                 xerr+=delta_x;
  62.                 yerr+=delta_y;
  63.                 if(xerr>distance)
  64.                 {
  65.                         xerr-=distance;
  66.                         uRow+=incx;
  67.                 }
  68.                 if(yerr>distance)
  69.                 {
  70.                         yerr-=distance;
  71.                         uCol+=incy;
  72.                 }
  73.         }
  74. }


  75. /******************************************************************************
  76.       ????????
  77.       ?????x1,y1   ????
  78.                 x2,y2   ????
  79.                 color   ?????
  80.       ????  ?
  81. ******************************************************************************/
  82. void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color)
  83. {
  84.         LCD_DrawLine(x1,y1,x2,y1,color);
  85.         LCD_DrawLine(x1,y1,x1,y2,color);
  86.         LCD_DrawLine(x1,y2,x2,y2,color);
  87.         LCD_DrawLine(x2,y1,x2,y2,color);
  88. }


  89. /******************************************************************************
  90.       ???????
  91.       ?????x0,y0   ????
  92.                 r       ??
  93.                 color   ????
  94.       ????  ?
  95. ******************************************************************************/
  96. void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color)
  97. {
  98.         int a,b;
  99.         a=0;b=r;         
  100.         while(a<=b)
  101.         {
  102.                 LCD_DrawPoint(x0-b,y0-a,color);             //3           
  103.                 LCD_DrawPoint(x0+b,y0-a,color);             //0           
  104.                 LCD_DrawPoint(x0-a,y0+b,color);             //1               
  105.                 LCD_DrawPoint(x0-a,y0-b,color);             //2            
  106.                 LCD_DrawPoint(x0+b,y0+a,color);             //4               
  107.                 LCD_DrawPoint(x0+a,y0-b,color);             //5
  108.                 LCD_DrawPoint(x0+a,y0+b,color);             //6
  109.                 LCD_DrawPoint(x0-b,y0+a,color);             //7
  110.                 a++;
  111.                 if((a*a+b*b)>(r*r))//??????????
  112.                 {
  113.                         b--;
  114.                 }
  115.         }
  116. }

  117. /******************************************************************************
  118.       ??????????
  119.       ?????x,y????
  120.                 *s ???????
  121.                 fc ????
  122.                 bc ?????
  123.                 sizey ?? ?? 16 24 32
  124.                 mode:  0?????  1????
  125.       ????  ?
  126. ******************************************************************************/
  127. void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
  128. {
  129.         while(*s!=0)
  130.         {
  131.                 if(sizey==16) LCD_ShowChinese16x16(x,y,s,fc,bc,sizey,mode);
  132.                 else if(sizey==24) LCD_ShowChinese24x24(x,y,s,fc,bc,sizey,mode);
  133.                 else if(sizey==32) LCD_ShowChinese32x32(x,y,s,fc,bc,sizey,mode);
  134.                 else return;
  135.                 s+=2;
  136.                 x+=sizey;
  137.         }
  138. }

  139. /******************************************************************************
  140.       ?????????16x16??
  141.       ?????x,y????
  142.                 *s ??????
  143.                 fc ????
  144.                 bc ?????
  145.                 sizey ??
  146.                 mode:  0?????  1????
  147.       ????  ?
  148. ******************************************************************************/
  149. void LCD_ShowChinese16x16(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
  150. {
  151.         u8 i,j;
  152.         u16 k;
  153.         u16 HZnum;//????
  154.         u16 TypefaceNum;//??????????
  155.         u16 x0=x;
  156.         TypefaceNum=sizey/8*sizey;//??????????????????8??????
  157.                                   //?????????????,??????????
  158.         HZnum=sizeof(tfont16)/sizeof(typFNT_GB16);        //??????
  159.         for(k=0;k<HZnum;k++)
  160.         {
  161.                 if ((tfont16[k].Index[0]==*(s))&&(tfont16[k].Index[1]==*(s+1)))
  162.                 {         
  163.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  164.                         for(i=0;i<TypefaceNum;i++)
  165.                         {
  166.                                 for(j=0;j<8;j++)
  167.                                 {        
  168.                                         if(!mode)//?????
  169.                                         {
  170.                                                 if(tfont16[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  171.                                                 else LCD_WR_DATA(bc);
  172.                                         }
  173.                                         else//????
  174.                                         {
  175.                                                 if(tfont16[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//????
  176.                                                 x++;
  177.                                                 if((x-x0)==sizey)
  178.                                                 {
  179.                                                         x=x0;
  180.                                                         y++;
  181.                                                         break;
  182.                                                 }
  183.                                         }
  184.                                 }
  185.                         }
  186.                 }                                          
  187.                 continue;  //????????????????????????????
  188.         }
  189. }


  190. /******************************************************************************
  191.       ?????????24x24??
  192.       ?????x,y????
  193.                 *s ??????
  194.                 fc ????
  195.                 bc ?????
  196.                 sizey ??
  197.                 mode:  0?????  1????
  198.       ????  ?
  199. ******************************************************************************/
  200. void LCD_ShowChinese24x24(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
  201. {
  202.         u8 i,j;
  203.         u16 k;
  204.         u16 HZnum;//????
  205.         u16 TypefaceNum;//??????????
  206.         u16 x0=x;
  207.         TypefaceNum=sizey/8*sizey;//??????????????????8??????
  208.                                   //?????????????,??????????
  209.         HZnum=sizeof(tfont24)/sizeof(typFNT_GB24);        //??????
  210.         for(k=0;k<HZnum;k++)
  211.         {
  212.                 if ((tfont24[k].Index[0]==*(s))&&(tfont24[k].Index[1]==*(s+1)))
  213.                 {         
  214.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  215.                         for(i=0;i<TypefaceNum;i++)
  216.                         {
  217.                                 for(j=0;j<8;j++)
  218.                                 {        
  219.                                         if(!mode)//?????
  220.                                         {
  221.                                                 if(tfont24[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  222.                                                 else LCD_WR_DATA(bc);
  223.                                         }
  224.                                         else//????
  225.                                         {
  226.                                                 if(tfont24[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//????
  227.                                                 x++;
  228.                                                 if((x-x0)==sizey)
  229.                                                 {
  230.                                                         x=x0;
  231.                                                         y++;
  232.                                                         break;
  233.                                                 }
  234.                                         }
  235.                                 }
  236.                         }
  237.                 }                                          
  238.                 continue;  //????????????????????????????
  239.         }
  240. }

  241. /******************************************************************************
  242.       ?????????32x32??
  243.       ?????x,y????
  244.                 *s ??????
  245.                 fc ????
  246.                 bc ?????
  247.                 sizey ??
  248.                 mode:  0?????  1????
  249.       ????  ?
  250. ******************************************************************************/
  251. void LCD_ShowChinese32x32(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
  252. {
  253.         u8 i,j;
  254.         u16 k;
  255.         u16 HZnum;//????
  256.         u16 TypefaceNum;//??????????
  257.         u16 x0=x;
  258.         TypefaceNum=sizey/8*sizey;//??????????????????8??????
  259.                                   //?????????????,??????????
  260.         HZnum=sizeof(tfont32)/sizeof(typFNT_GB32);        //??????
  261.         for(k=0;k<HZnum;k++)
  262.         {
  263.                 if ((tfont32[k].Index[0]==*(s))&&(tfont32[k].Index[1]==*(s+1)))
  264.                 {         
  265.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  266.                         for(i=0;i<TypefaceNum;i++)
  267.                         {
  268.                                 for(j=0;j<8;j++)
  269.                                 {        
  270.                                         if(!mode)//?????
  271.                                         {
  272.                                                 if(tfont32[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  273.                                                 else LCD_WR_DATA(bc);
  274.                                         }
  275.                                         else//????
  276.                                         {
  277.                                                 if(tfont32[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//????
  278.                                                 x++;
  279.                                                 if((x-x0)==sizey)
  280.                                                 {
  281.                                                         x=x0;
  282.                                                         y++;
  283.                                                         break;
  284.                                                 }
  285.                                         }
  286.                                 }
  287.                         }
  288.                 }                                          
  289.                 continue;  //????????????????????????????
  290.         }
  291. }


  292. /******************************************************************************
  293.       ???????????
  294.       ?????x,y????
  295.                 num ??????
  296.                 fc ????
  297.                 bc ?????
  298.                 sizey ??
  299.                 mode:  0?????  1????
  300.       ????  ?
  301. ******************************************************************************/
  302. void LCD_ShowChar(u16 x,u16 y,u8 num,u16 fc,u16 bc,u8 sizey,u8 mode)
  303. {
  304.         u8 temp,sizex,t;
  305.         u16 i,TypefaceNum;//??????????
  306.         u16 x0=x;
  307.         sizex=sizey/2;
  308.         TypefaceNum=sizex/8*sizey;
  309.         num=num-' ';    //???????
  310.         LCD_Address_Set(x,y,x+sizex-1,y+sizey-1);  //??????
  311.         for(i=0;i<TypefaceNum;i++)
  312.         {
  313.                 if(sizey==16)temp=ascii_1608[num][i];                       //??8x16??
  314.                 else if(sizey==32)temp=ascii_3216[num][i];                 //??16x32??
  315.                 else return;
  316.                 for(t=0;t<8;t++)
  317.                 {
  318.                         if(!mode)//?????
  319.                         {
  320.                                 if(temp&(0x01<<t))LCD_WR_DATA(fc);
  321.                                 else LCD_WR_DATA(bc);
  322.                         }
  323.                         else//????
  324.                         {
  325.                                 if(temp&(0x01<<t))LCD_DrawPoint(x,y,fc);//????
  326.                                 x++;
  327.                                 if((x-x0)==sizex)
  328.                                 {
  329.                                         x=x0;
  330.                                         y++;
  331.                                         break;
  332.                                 }
  333.                         }
  334.                 }
  335.         }                     
  336. }


  337. /******************************************************************************
  338.       ??????????
  339.       ?????x,y????
  340.                 *p ???????
  341.                 fc ????
  342.                 bc ?????
  343.                 sizey ??
  344.                 mode:  0?????  1????
  345.       ????  ?
  346. ******************************************************************************/
  347. void LCD_ShowString(u16 x,u16 y,const u8 *p,u16 fc,u16 bc,u8 sizey,u8 mode)
  348. {         
  349.         while(*p!='\0')
  350.         {      
  351.                 LCD_ShowChar(x,y,*p,fc,bc,sizey,mode);
  352.                 x+=sizey/2;
  353.                 p++;
  354.         }  
  355. }


  356. /******************************************************************************
  357.       ?????????
  358.       ?????m???n??
  359.       ????  ?
  360. ******************************************************************************/
  361. u32 mypow(u8 m,u8 n)
  362. {
  363.         u32 result=1;         
  364.         while(n--)result*=m;
  365.         return result;
  366. }


  367. /******************************************************************************
  368.       ???????????
  369.       ?????x,y????
  370.                 num ???????
  371.                 len ??????
  372.                 fc ????
  373.                 bc ?????
  374.                 sizey ??
  375.       ????  ?
  376. ******************************************************************************/
  377. void LCD_ShowIntNum(u16 x,u16 y,u16 num,u8 len,u16 fc,u16 bc,u8 sizey)
  378. {                 
  379.         u8 t,temp;
  380.         u8 enshow=0;
  381.         u8 sizex=sizey/2;
  382.         for(t=0;t<len;t++)
  383.         {
  384.                 temp=(num/mypow(10,len-t-1))%10;
  385.                 if(enshow==0&&t<(len-1))
  386.                 {
  387.                         if(temp==0)
  388.                         {
  389.                                 LCD_ShowChar(x+t*sizex,y,' ',fc,bc,sizey,0);
  390.                                 continue;
  391.                         }else enshow=1;
  392.                           
  393.                 }
  394.                  LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
  395.         }
  396. }


  397. /******************************************************************************
  398.       ?????????????
  399.       ?????x,y????
  400.                 num ???????
  401.                 len ??????
  402.                 fc ????
  403.                 bc ?????
  404.                 sizey ??
  405.       ????  ?
  406. ******************************************************************************/
  407. void LCD_ShowFloatNum1(u16 x,u16 y,float num,u8 len,u16 fc,u16 bc,u8 sizey)
  408. {                 
  409.         u8 t,temp,sizex;
  410.         u16 num1;
  411.         sizex=sizey/2;
  412.         num1=num*100;
  413.         for(t=0;t<len;t++)
  414.         {
  415.                 temp=(num1/mypow(10,len-t-1))%10;
  416.                 if(t==(len-2))
  417.                 {
  418.                         LCD_ShowChar(x+(len-2)*sizex,y,'.',fc,bc,sizey,0);
  419.                         t++;
  420.                         len+=1;
  421.                 }
  422.                  LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
  423.         }
  424. }


  425. /******************************************************************************
  426.       ?????????
  427.       ?????x,y????
  428.                 length ????
  429.                 width  ????
  430.                 pic[]  ????   
  431.       ????  ?
  432. ******************************************************************************/
  433. void LCD_ShowPicture(u16 x,u16 y,u16 length,u16 width,const u8 pic[])
  434. {
  435.         u16 i,j,k=0;
  436.         LCD_Address_Set(x,y,x+length-1,y+width-1);
  437.         for(i=0;i<length;i++)
  438.         {
  439.                 for(j=0;j<width;j++)
  440.                 {
  441.                         LCD_WR_DATA8(pic[k*2]);
  442.                         LCD_WR_DATA8(pic[k*2+1]);
  443.                         k++;
  444.                 }
  445.         }                        
  446. }



数据类型的通用定义:
type.h
  1. /*
  2. * File:   type.h
  3. * Author: Administrator
  4. *
  5. * Created on August 19, 2021, 3:25 PM
  6. */

  7. #ifndef TYPE_H
  8. #define        TYPE_H

  9. #ifdef        __cplusplus
  10. extern "C" {
  11. #endif

  12. #include "stdint.h"

  13. typedef int32_t  s32;
  14. typedef int16_t s16;
  15. typedef int8_t  s8;

  16. typedef const int32_t sc32;  /*!< Read Only */
  17. typedef const int16_t sc16;  /*!< Read Only */
  18. typedef const int8_t sc8;   /*!< Read Only */

  19. typedef uint32_t  u32;
  20. typedef uint16_t u16;
  21. typedef uint8_t  u8;

  22. typedef const uint32_t uc32;  /*!< Read Only */
  23. typedef const uint16_t uc16;  /*!< Read Only */
  24. typedef const uint8_t uc8;   /*!< Read Only */




  25. #ifdef        __cplusplus
  26. }
  27. #endif

  28. #endif        /* TYPE_H */

在main函数里面调用:

  1. #include "mcc_generated_files/mcc.h"
  2. #include "LCD/lcd_init.h"
  3. #include "LCD/lcd.h"
  4. #include "LCD/picture.h"
  5. /*
  6.                          Main application
  7. */
  8. void main(void)
  9. {
  10.     //u8 i,j;
  11.         float t=0;//22DAY  S
  12.     // Initialize the device
  13.     SYSTEM_Initialize();

  14.     // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
  15.     // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
  16.     // Use the following macros to:

  17.     // Enable the Global Interrupts
  18.     //INTERRUPT_GlobalInterruptEnable();

  19.     // Disable the Global Interrupts
  20.     //INTERRUPT_GlobalInterruptDisable();
  21.         LCD_Init();//LCD???
  22.         LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
  23.     //LCD_ShowPicture(0,0,89,80,gImage_ZQ3);//89*80
  24.     //while(1){}
  25.     while (1)
  26.     {
  27.         // Add your application code
  28.         LCD_ShowChinese(50,0,"中秋快乐",RED,WHITE,32,0);
  29.         LCD_ShowString(0,40,"HappyMid-Autumn",RED,WHITE,32,0);
  30.         
  31.         LCD_ShowString(0,80+16,"Increaseing Nun:",RED,WHITE,16,0);
  32.                 LCD_ShowFloatNum1(128,80+16,t,5,RED,WHITE,16);
  33.         t+=0.1;

  34.         LCD_ShowPicture(31,120+16,89,80,gImage_ZQ3);//89*80
  35.         LCD_ShowPicture(121,120+16,89,80,gImage_ZQ3);//89*80
  36.     }
  37. }
  38. /**
  39. End of File
  40. */
这里载入了一个很小的“中秋快乐”的小图片~

下载,查看:


好了,驱动SPI屏幕就到这了~




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
kkzz 发表于 2021-9-1 16:33 | 显示全部楼层
ST7789V初始化代码真复杂。   
hudi008 发表于 2021-9-1 16:34 | 显示全部楼层
SPI接口的TFT屏ILI9341代码有吗   
lzmm 发表于 2021-9-1 16:34 | 显示全部楼层
LCD尺寸有比较大的吗
minzisc 发表于 2021-9-1 16:35 | 显示全部楼层
通过SPI接口实现对屏幕的驱动
selongli 发表于 2021-9-1 16:35 | 显示全部楼层
ILI9341 SPI接口的屏幕很好用,有30帧/s
fentianyou 发表于 2021-9-1 16:35 | 显示全部楼层
显示屏的驱动代码一般商家会提供一个Demo  
xiaoyaodz 发表于 2021-9-1 16:36 | 显示全部楼层
用过4线SPI模式点OLED   
febgxu 发表于 2021-9-1 16:36 | 显示全部楼层
现在是采用IO口来模拟SPI
sdlls 发表于 2021-9-1 16:37 | 显示全部楼层
大多数还是TFT屏幕。  
pixhw 发表于 2021-9-1 16:37 | 显示全部楼层
分享一点spi屏幕相关软件和工具吧   
febgxu 发表于 2021-9-1 16:38 | 显示全部楼层
可以硬件spi驱动吗?   
hudi008 发表于 2021-9-1 16:38 | 显示全部楼层
如何 连接SPI接口TFT屏幕  
xiaoyaodz 发表于 2021-9-1 16:38 | 显示全部楼层
这个多彩的oled没有用过呢。   
lzmm 发表于 2021-9-1 16:38 | 显示全部楼层
可以支持多大的lcd呢   
fentianyou 发表于 2021-9-1 16:38 | 显示全部楼层
在开发板上驱动液晶屏  
minzisc 发表于 2021-9-1 16:38 | 显示全部楼层
内存是多大呢?   
selongli 发表于 2021-9-1 16:38 | 显示全部楼层
ST7789可以到多少帧呢?   
kkzz 发表于 2021-9-1 16:38 | 显示全部楼层
ST7789V控制器的LCD屏
pixhw 发表于 2021-9-1 16:38 | 显示全部楼层
最近在做lcd的选型呢。   
您需要登录后才可以回帖 登录 | 注册

本版积分规则

111

主题

627

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部