[N32G45x] 国芯N32G457代替STM32F107驱动LCD显示屏

[复制链接]
1960|3
 楼主| tlled 发表于 2022-4-6 00:11 | 显示全部楼层 |阅读模式
本帖最后由 tlled 于 2022-4-6 06:57 编辑

#申请原创#              
    测试下使用N32F457芯片代替STM32F107芯片,通过驱动LCD测试来看下移植过程。

    一、硬件部分

    1.1、N32G457与STM32F107(一样的LQFP100封装)
    1.1.1、STM32F107芯片引脚定义
    001.png
    1.1.2、N32G457芯片引脚定义
    002.png
    从引脚定义上看,两款芯片的引脚是完全兼容的。
    STM32F107芯片内核是M3,主频最大72MHz。
    n32g457芯片内核是M4+FPU,主频可达144MHz。

    2、显示屏硬件驱动LCD部分电路
    驱动LCD方式是使用16位总线,模拟时序来驱动的,使用到的端口如下:
    003.png

    3、硬件电路焊接对比
    3.1、N32G457
    100.jpg
    3.2、STM32F107
    101.jpg


    二、程序部分

    之前是使用STM32F107的库文件程序来配置驱动设置端口,移植驱动主要是外部端口的配置,移植到N32G457有那些改动,下面开始对比。
    2.1、STM32驱动端口的配置
  1. void LCD_Pins_Config(void)
  2. {
  3.   GPIO_InitTypeDef GPIO_InitStructure;

  4.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOD , ENABLE);

  5.   // DB15--0
  6.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  7.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  9.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  10.   //LCD_Pin_WR  PB15
  11.         //LCD_Pin_RS  PB14
  12.         //LCD_Pin_CS  PB10
  13.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_14 |GPIO_Pin_15;
  14.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  16.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  17.         //LCD_Pin_RST  PD11
  18.   //LCD_Pin_RD  PD15
  19.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_15;
  20.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  21.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  22.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  23.         
  24.         //LCD_BK  PA3
  25.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  26.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  28.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  29.         
  30.         GPIO_SetBits(GPIOA,GPIO_Pin_3);
  31. }

  32. void ST7789_BUS_OutPut(void)
  33. {
  34.   GPIO_InitTypeDef GPIO_InitStructure;

  35.   // DE15--0
  36.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  37.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  39.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  40. }

  41. void LCD_DB_AS_InPut(void)
  42. {
  43.   GPIO_InitTypeDef GPIO_InitStructure;

  44.   // DE15--0
  45.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  46.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  47.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  48.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  49. }


    2.2、N32G457驱动端口的配置

  1. void LCD_Pins_Config(void)
  2. {        
  3.         //N32G457设置如下
  4.         GPIO_InitType GPIO_InitStructure;
  5.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_GPIOD |RCC_APB2_PERIPH_GPIOE, ENABLE);

  6.   // DB15--0        
  7.         GPIO_InitStructure.Pin        = GPIO_PIN_ALL;
  8.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  9.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  10.         GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
  11.         
  12.   //LCD_Pin_WR  PB15
  13.         //LCD_Pin_RS  PB14
  14.         //LCD_Pin_CS  PB10
  15.   GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_14 |GPIO_PIN_15;
  16.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  17.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18.   GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  19.         //LCD_Pin_RST  PD11
  20.   //LCD_Pin_RD  PD15
  21.   GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_15;
  22.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  24.   GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
  25.         
  26.         //LCD_BK  PA3
  27.   GPIO_InitStructure.Pin = GPIO_PIN_3;
  28.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  29.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  30.   GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  31.         
  32.         GPIO_SetBits(GPIOA,GPIO_PIN_3);
  33. }

  34. void ST7789_BUS_OutPut(void)
  35. {
  36.   GPIO_InitType GPIO_InitStructure;

  37.   // DB15--0
  38.   GPIO_InitStructure.Pin = GPIO_PIN_ALL;
  39.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  40.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  41.   GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
  42. }

  43. void LCD_DB_AS_InPut(void)
  44. {
  45.         GPIO_InitType GPIO_InitStructure;

  46.   // DB15--0
  47.   GPIO_InitStructure.Pin = GPIO_PIN_ALL;
  48.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  49.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  50.   GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
  51. }
   从库文件端口配置来看,驱动程序函数定义名称是很相似的,名称定义的大小写有点区别,比如   

    STM32 -- 使用的引脚定义名称是GPIO_Pin_10
    N32G  -- 使用的引脚定义名称是GPIO_PIN_10

    修改比较容易,程序移植的工作量小。


    2.3、显示屏驱动
    2.3.1、st7789.c
  1. #include <stdio.h>
  2. #include "main.h"
  3. //#include "lcd.h"
  4. #include "Image.h"
  5. #include "delay.h"
  6. #include "font.h"
  7. #include "st7789.h"
  8. #include "delay.h"

  9. //LCD的画笔颜色和背景色           
  10. u16 POINT_COLOR=0x0000;        //画笔颜色
  11. u16 BACK_COLOR=0xFFFF;  //背景色
  12.   
  13. //管理LCD重要参数
  14. //默认为竖屏
  15. _lcd_dev lcddev;



  16. //设置LCD显示方向
  17. //dir:0,竖屏;1,横屏
  18. void LCD_Display_Dir(u8 dir)
  19. {
  20.         if(dir==0)                        //竖屏
  21.         {
  22.                 lcddev.dir=0;        //竖屏
  23.                 lcddev.width=240;
  24.                 lcddev.height=320;
  25.         }else                                 //横屏
  26.         {                                          
  27.                 lcddev.dir=1;        //横屏
  28.                 lcddev.width=320;
  29.                 lcddev.height=240;
  30.         }
  31.         //LCD_Scan_Dir(DFT_SCAN_DIR);        //默认扫描方向
  32. }

  33. void LCD_Pins_Config(void)
  34. {        
  35.         //N32G457设置如下
  36.         GPIO_InitType GPIO_InitStructure;
  37.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_GPIOD |RCC_APB2_PERIPH_GPIOE, ENABLE);

  38.   // DB15--0        
  39.         GPIO_InitStructure.Pin        = GPIO_PIN_ALL;
  40.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  41.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  42.         GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
  43.         
  44.   //LCD_Pin_WR  PB15
  45.         //LCD_Pin_RS  PB14
  46.         //LCD_Pin_CS  PB10
  47.   GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_14 |GPIO_PIN_15;
  48.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  49.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  50.   GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
  51.         //LCD_Pin_RST  PD11
  52.   //LCD_Pin_RD  PD15
  53.   GPIO_InitStructure.Pin = GPIO_PIN_11 | GPIO_PIN_15;
  54.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  55.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  56.   GPIO_InitPeripheral(GPIOD, &GPIO_InitStructure);
  57.         
  58.         //LCD_BK  PA3
  59.   GPIO_InitStructure.Pin = GPIO_PIN_3;
  60.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  61.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  62.   GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  63.         
  64.         GPIO_SetBits(GPIOA,GPIO_PIN_3);
  65. }

  66. void ST7789_BUS_OutPut(void)
  67. {
  68.   GPIO_InitType GPIO_InitStructure;

  69.   // DB15--0
  70.   GPIO_InitStructure.Pin = GPIO_PIN_ALL;
  71.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  72.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  73.   GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
  74. }

  75. void LCD_DB_AS_InPut(void)
  76. {
  77.         GPIO_InitType GPIO_InitStructure;

  78.   // DB15--0
  79.   GPIO_InitStructure.Pin = GPIO_PIN_ALL;
  80.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  81.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  82.   GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);
  83. }
  84. void delay_ms(u16 nms)
  85. {
  86.         systick_delay_ms(nms);
  87. }

  88. void LCD_Init(void)
  89. {
  90.   LCD_Pins_Config();
  91. }

  92. void TFT_24_7789_Write_Data(u16 dat)
  93. {
  94.         LCD_SetRs();  //rs=1
  95.   LCD_ClrCs();  //cs=0
  96.   LCD_SetRd();  //rd=1        
  97.         LCD_ClrWr();  //wr=0
  98.         GPIO_Write(GPIOE, dat);
  99.         LCD_SetWr();   //wr=1
  100.   LCD_SetCs();   //cs=1
  101. }
  102. void TFT_24_7789_Write_Command(u16 cmd)
  103. {
  104.         LCD_ClrCs();  //cs=0
  105.   LCD_ClrRs();  //rs=0
  106.         LCD_SetRd();  //rd=1
  107.   LCD_ClrWr();  //wr=0
  108.         GPIO_Write(GPIOE, cmd);
  109.   LCD_SetWr();   //wr=1
  110.   LCD_SetCs();   //cs=1
  111.         LCD_SetRs();   //rs=1
  112. }

  113. void ST7789_WriteData(u16 dat)
  114. {
  115.         LCD_SetRs();  //rs=1
  116.   LCD_ClrCs();  //cs=0
  117.   LCD_SetRd();  //rd=1        
  118.         LCD_ClrWr();  //wr=0
  119.         GPIO_Write(GPIOE, dat);
  120.         LCD_SetWr();   //wr=1
  121.   LCD_SetCs();   //cs=1
  122. }
  123. void ST7789_WriteCommand(u16 cmd)
  124. {
  125.         LCD_ClrCs();  //cs=0
  126.   LCD_ClrRs();  //rs=0
  127.         LCD_SetRd();  //rd=1
  128.   LCD_ClrWr();  //wr=0
  129.         GPIO_Write(GPIOE, cmd);
  130.   LCD_SetWr();   //wr=1
  131.   LCD_SetCs();   //cs=1
  132.         LCD_SetRs();   //rs=1
  133. }

  134. //void TFT_24_7789_Read_Data(void)
  135. //{
  136. //        uint16_t id=0;
  137. //        TFT_24_7789_Write_Command(0x0004);
  138. //        LCD_DB_AS_InPut();
  139. //        
  140. //        GPIO_ResetBits(GPIOB, GPIO_Pin_10);  //CS=0
  141. //        GPIO_SetBits(GPIOB, GPIO_Pin_14);  //RS=1
  142. //        GPIO_SetBits(GPIOB, GPIO_Pin_15);         //WR=1
  143. //        
  144. //        //GPIO_SetBits(GPIOD, GPIO_Pin_15);    //RD=1  
  145. //  GPIO_ResetBits(GPIOD, GPIO_Pin_15);         //RD=0
  146. //        //GPIO_Write(GPIOE, dat);//when using 16-bit interface (DB17:10,DB8:1)//when using 8-bit interface (DB17:10)
  147. //        id=LCD_Read();
  148. //        delay_ms(1);
  149. //        GPIO_SetBits(GPIOD, GPIO_Pin_15);   //RD=1;
  150. //        delay_ms(1);
  151. //        printf("\n\r ######DeviceIdCode = 0x%x ###### ", id);
  152. //        LCD_DB_AS_OutPut();
  153. //}

  154. static void ST7789_Reset(void)
  155. {
  156.     LCD_SetRst();   
  157.     delay_ms(1);
  158.     LCD_ClrRst();  
  159.     delay_ms(100); // Delayms 10ms               // This delay time is necessary
  160.     LCD_SetRst();   
  161.     delay_ms(120); // Delayms 120 ms            
  162. }

  163. void ST7789_Init(void)
  164. {
  165.         int n;
  166.         
  167.         LCD_Pins_Config();
  168.         ST7789_Reset();
  169.         
  170.         ST7789_WriteCommand(0x0011);                //exit SLEEP mode
  171.         delay_ms(120);

  172.         ST7789_WriteCommand(0x0036);
  173.         ST7789_WriteData(0x0060);                        //MADCTL: memory data access control
  174.         ST7789_WriteCommand(0x003A);
  175.         ST7789_WriteData(0x0005);                        //COLMOD: Interface Pixel format *** I use 262K-colors in 18bit/pixel format when using 8-bit interface to allow 3-bytes per pixel
  176. //        TFT_24_7789_Write_Command(0x003A);TFT_24_7789_Write_Data(0x0055);//COLMOD: Interface Pixel format  *** I use 65K-colors in 16bit/pixel (5-6-5) format when using 16-bit interface to allow 1-byte per pixel
  177.         ST7789_WriteCommand(0x00B2);
  178.         ST7789_WriteData(0x000C);
  179.         ST7789_WriteData(0x000C);
  180.         ST7789_WriteData(0x0000);
  181.         ST7789_WriteData(0x0033);
  182.         ST7789_WriteData(0x0033);                        //PORCTRK: Porch setting
  183.         ST7789_WriteCommand(0x00B7);
  184.         ST7789_WriteData(0x0035);                        //GCTRL: Gate Control
  185.         ST7789_WriteCommand(0x00BB);
  186.         ST7789_WriteData(0x001c);                        //VCOMS: VCOM setting
  187.         ST7789_WriteCommand(0x00C0);
  188.         ST7789_WriteData(0x002C);                        //LCMCTRL: LCM Control
  189.         ST7789_WriteCommand(0x00C2);
  190.         ST7789_WriteData(0x0001);
  191.         ST7789_WriteData(0x00FF);                        //VDVVRHEN: VDV and VRH Command Enable
  192.         ST7789_WriteCommand(0x00C3);
  193.         ST7789_WriteData(0x000B);                        //VRHS: VRH Set
  194.         ST7789_WriteCommand(0x00C4);
  195.         ST7789_WriteData(0x0020);                        //VDVS: VDV Set
  196.         ST7789_WriteCommand(0x00C6);
  197.         ST7789_WriteData(0x000F);                        //FRCTRL2: Frame Rate control in normal mode
  198.         ST7789_WriteCommand(0x00D0);
  199.         ST7789_WriteData(0x00A4);
  200.         ST7789_WriteData(0x00A1);                        //PWCTRL1: Power Control 1
  201.         ST7789_WriteCommand(0x00E0);
  202.         ST7789_WriteData(0x00D0);
  203.         ST7789_WriteData(0x0000);
  204.         ST7789_WriteData(0x0003);
  205.         ST7789_WriteData(0x0009);
  206.         ST7789_WriteData(0x0013);
  207.         ST7789_WriteData(0x001c);
  208.         ST7789_WriteData(0x003a);
  209.         ST7789_WriteData(0x0055);
  210.         ST7789_WriteData(0x0048);
  211.         ST7789_WriteData(0x0018);
  212.         ST7789_WriteData(0x0012);
  213.         ST7789_WriteData(0x000e);
  214.         ST7789_WriteData(0x0019);
  215.         ST7789_WriteData(0x001e);                        //PVGAMCTRL: Positive Voltage Gamma control
  216.         ST7789_WriteCommand(0x00E1);
  217.         ST7789_WriteData(0x00D0);
  218.         ST7789_WriteData(0x0000);
  219.         ST7789_WriteData(0x0003);
  220.         ST7789_WriteData(0x0009);
  221.         ST7789_WriteData(0x0005);
  222.         ST7789_WriteData(0x0025);
  223.         ST7789_WriteData(0x003a);
  224.         ST7789_WriteData(0x0055);
  225.         ST7789_WriteData(0x0050);
  226.         ST7789_WriteData(0x003d);
  227.         ST7789_WriteData(0x001c);
  228.         ST7789_WriteData(0x001d);
  229.         ST7789_WriteData(0x001d);
  230.         ST7789_WriteData(0x001e);                        
  231.         ST7789_WriteCommand(0x0029);                                //display ON
  232.         ST7789_WriteCommand(0x002c);        

  233.         LCD_Display_Dir(1);
  234. }

  235. void LCD_WriteOneDot(u16 color)
  236. {
  237.     TFT_24_7789_Write_Data(color);
  238. }

  239. void Lcd_SetBox(u16 xStart,u16 yStart,u16 xlong,u16 ylong)
  240. {
  241.         u16 xEnd=0, yEnd=0;
  242.         xEnd=xStart+xlong-1;
  243.         yEnd=yStart+ylong-1;
  244.         
  245.         TFT_24_7789_Write_Command(0x2a);   
  246.         TFT_24_7789_Write_Data(xStart>>8);
  247.         TFT_24_7789_Write_Data(xStart);
  248.         TFT_24_7789_Write_Data(xEnd>>8);
  249.         TFT_24_7789_Write_Data(xEnd);

  250.         TFT_24_7789_Write_Command(0x2b);   
  251.         TFT_24_7789_Write_Data(yStart>>8);
  252.         TFT_24_7789_Write_Data(yStart);
  253.         TFT_24_7789_Write_Data(yEnd>>8);
  254.         TFT_24_7789_Write_Data(yEnd);

  255.         TFT_24_7789_Write_Command(0x2c);               
  256. }

  257. void Lcd_SetBox1(u16 xStart,u16 xEnd,u16 yStart,u16 yEnd)
  258. {
  259.         TFT_24_7789_Write_Command(0x2a);   
  260.         TFT_24_7789_Write_Data(xStart>>8);
  261.         TFT_24_7789_Write_Data(xStart);
  262.         TFT_24_7789_Write_Data(xEnd>>8);
  263.         TFT_24_7789_Write_Data(xEnd);

  264.         TFT_24_7789_Write_Command(0x2b);   
  265.         TFT_24_7789_Write_Data(yStart>>8);
  266.         TFT_24_7789_Write_Data(yStart);
  267.         TFT_24_7789_Write_Data(yEnd>>8);
  268.         TFT_24_7789_Write_Data(yEnd);

  269.         TFT_24_7789_Write_Command(0x2c);               
  270. }

  271. void LCD_Clear(u16 Color)
  272. {
  273.    u32 i;  
  274.    Lcd_SetBox(0,0,320,240);  
  275.    for(i=0;i<78900;i++){      
  276.        LCD_WriteOneDot(Color);
  277.   }
  278. }

  279. void LCD_Picture(u16 x,u16 y,u16 width,u16 height,const unsigned  char *color)
  280. {  
  281.         u16 i,j,temp;
  282.         j=width*height;
  283.         Lcd_SetBox(x,y,width,height);
  284.             
  285.          for(i=0;i<j;i++)
  286.         {
  287.           temp=color[i*2]<<8&0xFF00;
  288.           temp+=color[i*2+1];
  289.           LCD_WriteOneDot(temp);
  290.         }         
  291. }

  292. void LCD_Clear1(u16 xStart,u16 yStart,u16 xlong,u16 ylong,u16 Color)
  293. {
  294.    u32 i,j;  
  295.    j=xlong*ylong;
  296.    Lcd_SetBox(xStart,yStart,xlong,ylong);  
  297.    for(i=0;i<j;i++){      
  298.        LCD_WriteOneDot(Color);
  299.   }
  300. }

  301. /**********************************************
  302. 函数名:LCD_write_english
  303. 功能:选定Lcd上写一个英文或数字
  304. 入口参数:data 要写的字符
  305.           color 字符的颜色
  306.           xcolor 字符的背景色
  307.           size  字体的大小
  308. 返回值:无
  309. ***********************************************/
  310. static void LCD_write_english(u8 data,u16 color,u16 xcolor ,u8 mode)//写字符
  311. {
  312.   u8 i=0,j=0,n=0;
  313.   u8 avl=0;
  314.   data -=32;                     
  315.   for (i=0;i<120;i++) //为 20x40字库      
  316.   {     
  317.     avl=english[data][i];         
  318.     for (j=0;j<8;j++)           
  319.     {
  320.       n++;
  321.       if(avl&0x80)LCD_WriteOneDot(color);        
  322.       else if(mode==0) LCD_WriteOneDot(xcolor);
  323.       avl<<=1;
  324.       if(n>19) {
  325.         n=0;
  326.         break;
  327.       }//部分字体如英文20*40,形成的字库3个8位一组,每一组最后4位不显示,用该语句进行判断有几位不需要显示
  328.     }     
  329.    }
  330. }

  331. void LCD_write_english_string(u16 x, u16 y, char *str,u8 mode)//英文字符串显示
  332. {
  333. u16 k = 0;
  334.   while ((*str<='~')&&(*str>=' '))
  335.   {
  336.      Lcd_SetBox(x+k,y,20,40);
  337.      LCD_write_english( *str,WORDCOLOR,BACKCOLOR, mode);
  338.      k+=20;
  339.      str++;      
  340.   }
  341. }

  342. static void LCD_write_chinese(u16 data, u16 color, u16 xcolor,u8 mode )//写字符
  343. {
  344.   u8 i=0,j=0;
  345.   u32 avl=0;
  346.   //data -=' ';                      //(ASCII)- 32 = 字库数组行数
  347. //===========为 48x24字库       32*16=512个像素点保存形式为8位数据故有512/8=64个 每一位代表“字”或“背景”然后再把每个坐标的颜色显示出来==========  要是显示在什么位置 显示的大小为什么是32X16看LCD_write_english_string函数  其中有个Lcd_SetBox
  348.   for (i=0;i<128;i++)
  349.   {     
  350.     avl=Chinese[data][i];         //取第一个8位
  351.     for (j=0;j<8;j++)            //两个8位组成一个16位循环16次完成16个点的判断 每行显示几个像素点由函数LCD_write_english_string的输入参数定
  352.     {
  353.       if(avl&0x80)LCD_WriteOneDot(color);        
  354.       else if(mode==0) LCD_WriteOneDot(xcolor);     //“0”显示为背景部分 即 背景颜色     
  355.       avl<<=1;
  356.     }
  357.   }
  358. }
  359. void LCD_write_chinese_string(u16 x, u16 y,u16 address,u16 len,u8 mode)
  360. {
  361.   int i;
  362.   u16 k = 0;
  363.   for(i=0;i<len;i++)
  364.   {
  365.      Lcd_SetBox(x+k,y,32,32);
  366.      LCD_write_chinese(address,WORDCOLOR,BACKCOLOR, mode);
  367.      k+=32;
  368.      address++;
  369.      
  370.   }
  371. }
  372. void ST7789_SetPoint(u16 x,u16 y,u16 color)
  373. {
  374.         Lcd_SetBox1(x,x+1,y,y+1);
  375.         LCD_WriteOneDot(color);
  376. }

  377. //在指定位置显示一个字符
  378. //x,y:起始坐标
  379. //num:要显示的字符:" "--->"~"
  380. //size:字体大小 12/16/24
  381. //mode:叠加方式(1)还是非叠加方式(0)
  382. void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
  383. {                                                            
  384.     u8 temp,t1,t;
  385.         u16 y0=y;
  386.         u8 csize=(size/8+((size%8)?1:0))*(size/2);                //得到字体一个字符对应点阵集所占的字节数        
  387.          num=num-' ';//得到偏移后的值(ASCII字库是从空格开始取模,所以-' '就是对应字符的字库)
  388.         for(t=0;t<csize;t++)
  389.         {   
  390.                 if(size==12)temp=asc2_1206[num][t];                  //调用1206字体
  391.                 else if(size==16)temp=asc2_1608[num][t];        //调用1608字体
  392.                 else if(size==24)temp=asc2_2412[num][t];        //调用2412字体
  393.                 else return;                                                                //没有的字库
  394.                 for(t1=0;t1<8;t1++)
  395.                 {                           
  396.                         if(temp&0x80)ST7789_SetPoint(x,y,POINT_COLOR);
  397.                         else if(mode==0)ST7789_SetPoint(x,y,BACK_COLOR);
  398.                         temp<<=1;
  399.                         y++;
  400.                         if(y>=lcddev.height)return;                //超区域了
  401.                         if((y-y0)==size)
  402.                         {
  403.                                 y=y0;
  404.                                 x++;
  405.                                 if(x>=lcddev.width)return;        //超区域了
  406.                                 break;
  407.                         }
  408.                 }           
  409.         }                                            
  410. }


  411. //显示字符串
  412. //x,y:起点坐标
  413. //width,height:区域大小  
  414. //size:字体大小
  415. //*p:字符串起始地址                  
  416. void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
  417. {         
  418.         u8 x0=x;
  419.         width+=x;
  420.         height+=y;
  421.     while((*p<='~')&&(*p>=' '))//判断是不是非法字符!
  422.     {      
  423.         if(x>=width){x=x0;y+=size;}
  424.         if(y>=height)break;//退出
  425.         LCD_ShowChar(x,y,*p,size,0);
  426.         x+=size/2;
  427.         p++;
  428.     }  
  429. }

  430.         
  431.         
   
    2.3.2、main.c   
  1. #include "main.h"
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include "led.h"
  5. #include "delay.h"
  6. #include "st7789.h"

  7. int main(void)
  8. {
  9.         LED_Init();
  10.         ST7789_Init();
  11.         POINT_COLOR=RED;
  12.         LCD_Clear(WHITE);
  13.         LCD_ShowString(30,40,210,24,24,"N32G457");
  14.         LCD_ShowString(30,70,200,16,16,"TFTLCD TEST");
  15.         LCD_ShowString(30,90,200,16,16,"tlled@NATION");
  16.          LCD_ShowString(30,110,200,16,16,"ST7789");                 
  17.         LCD_ShowString(30,130,200,12,12,"2022/04/05");
  18.         LCD_ShowString(30,150,210,24,24,"www.nationstech.com");

  19.         while (1)
  20.         {
  21.                 led0_on();systick_delay_ms(500);
  22.                 led0_off();systick_delay_ms(500);
  23.         }
  24. }
  25. /**
  26. * @}
  27. */
   2.4、程序源码:
    N32G45x_test.rar (3.54 MB, 下载次数: 50)

    三、运行显示内容
    102.jpg
    @21小跑堂  

七毛钱 发表于 2022-4-6 16:37 来自手机 | 显示全部楼层
能完美代替st的国产芯片现在真的越来越多了
电子之心 发表于 2022-4-6 22:01 | 显示全部楼层
国民代理,需要开发板测试或开发可与我联系 QQ:184530093
WoodData 发表于 2022-5-30 22:17 | 显示全部楼层
学习一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

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