1回答

0收藏

【Avnet | NXP FRDM-MCXN947试用活动】硬件SPI驱动LCD显示屏

NXP NXP 1963 人阅读 | 1 人回复 | 2024-12-03

测试SPI驱动LCD显示屏。


一、硬件部分


1.1、硬件SPI接口部分
使用FC1接口的SPI,对应的电路图部分



1.2、测试的LCD显示模块



接口定义



二、程序部分


2.1、LCD接口硬件配置


2.1.1、SPI引脚配置



2.1.2、控制引脚配置



2.2、SPI初始化部分
spi.c
  1. #include "main.h"

  2. void init_spi(void)
  3. {
  4.         uint32_t srcClock_Hz;
  5.         lpspi_master_config_t masterConfig;
  6.         edma_config_t userConfig = {0};
  7.         /* attach FRO 12M to FLEXCOMM1 */
  8.         CLOCK_SetClkDiv(kCLOCK_DivFlexcom1Clk, 1u);
  9.         CLOCK_AttachClk(kFRO12M_to_FLEXCOMM1);
  10.         
  11.         init_spi1_pins();
  12.         
  13.         /*Master config*/
  14.         LPSPI_MasterGetDefaultConfig(&masterConfig);
  15.         masterConfig.baudRate = TRANSFER_BAUDRATE;
  16.         masterConfig.whichPcs = EXAMPLE_LPSPI_MASTER_PCS_FOR_INIT;
  17.         masterConfig.pcsToSckDelayInNanoSec        = 1000000000U / (masterConfig.baudRate * 2U);
  18.         masterConfig.lastSckToPcsDelayInNanoSec    = 1000000000U / (masterConfig.baudRate * 2U);
  19.         masterConfig.betweenTransferDelayInNanoSec = 1000000000U / (masterConfig.baudRate * 2U);

  20.         srcClock_Hz = LPSPI_MASTER_CLK_FREQ;
  21.         LPSPI_MasterInit(EXAMPLE_LPSPI_MASTER_BASEADDR, &masterConfig, srcClock_Hz);
  22. }
  23. uint8_t spi1_writedat(uint8_t dat)
  24. {
  25.         uint8_t srcBuff[2];
  26.         uint8_t destBuff[2];
  27.         srcBuff[0]=dat;
  28.         lpspi_transfer_t masterXfer;
  29. //                /*Start master transfer*/
  30.         masterXfer.txData   = srcBuff;//masterTxData;
  31.         masterXfer.rxData   = destBuff;
  32.         masterXfer.dataSize = 1;
  33.         masterXfer.configFlags =
  34.             EXAMPLE_LPSPI_MASTER_PCS_FOR_TRANSFER | kLPSPI_MasterPcsContinuous | kLPSPI_MasterByteSwap;
  35.         LPSPI_MasterTransferBlocking(EXAMPLE_LPSPI_MASTER_BASEADDR, &masterXfer);  
  36.         return destBuff[0];
  37. }
复制代码


spi.h
  1. #ifndef __SPI_H
  2. #define __SPI_H

  3. #define LPSPI_MASTER_CLK_FREQ                 CLOCK_GetLPFlexCommClkFreq(1u)
  4. #define EXAMPLE_LPSPI_MASTER_BASEADDR         (LPSPI1)
  5. #define EXAMPLE_LPSPI_MASTER_PCS_FOR_INIT     (kLPSPI_Pcs0)
  6. #define EXAMPLE_LPSPI_MASTER_PCS_FOR_TRANSFER (kLPSPI_MasterPcs0)
  7. #define EXAMPLE_LPSPI_MASTER_DMA_BASE         DMA0
  8. #define EXAMPLE_LPSPI_MASTER_DMA_RX_CHANNEL   0U
  9. #define EXAMPLE_LPSPI_MASTER_DMA_TX_CHANNEL   1U
  10. #define DEMO_LPSPI_TRANSMIT_EDMA_CHANNEL      kDmaRequestMuxLpFlexcomm1Tx
  11. #define DEMO_LPSPI_RECEIVE_EDMA_CHANNEL       kDmaRequestMuxLpFlexcomm1Rx
  12. #define TRANSFER_SIZE     64U      
  13. #define TRANSFER_BAUDRATE 50000000U  

  14. void init_spi(void);
  15. uint8_t spi1_writedat(uint8_t dat);

  16. #endif
复制代码


2.2、lcd驱动程序
lcd_init.c
  1. #include "main.h"

  2. void LCD_GPIO_Init(void)
  3. {
  4.         gpio_pin_config_t spilcd_config = {
  5.                 kGPIO_DigitalOutput,
  6.                 0,
  7. };
  8.         
  9.         CLOCK_EnableClock(kCLOCK_Gpio1);
  10.         init_spi_lcd_pins();
  11.         GPIO_PinInit(LCD_RES_GPIO, LCD_RES_GPIO_PIN, &spilcd_config);
  12.   GPIO_PinInit(LCD_DC_GPIO, LCD_DC_GPIO_PIN, &spilcd_config);
  13.   GPIO_PinInit(LCD_CS_GPIO, LCD_CS_GPIO_PIN, &spilcd_config);
  14. }

  15. void LCD_Writ_Bus(uint8_t dat)
  16. {        
  17.         uint8_t i;
  18.         LCD_CS_Clr();
  19.         spi1_writedat(dat);
  20.   LCD_CS_Set();        
  21. }

  22. void LCD_WR_DATA8(uint8_t dat)
  23. {
  24.         LCD_Writ_Bus(dat);
  25. }

  26. void LCD_WR_DATA(uint16_t dat)
  27. {
  28.         LCD_Writ_Bus(dat>>8);
  29.         LCD_Writ_Bus(dat);
  30. }

  31. void LCD_WR_REG(uint8_t dat)
  32. {
  33.         LCD_DC_Clr();//写命令
  34.         LCD_Writ_Bus(dat);
  35.         LCD_DC_Set();//写数据
  36. }

  37. void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
  38. {
  39.         if(USE_HORIZONTAL==0)
  40.         {
  41.                 LCD_WR_REG(0x2a);//列地址设置
  42.                 LCD_WR_DATA(x1+2);
  43.                 LCD_WR_DATA(x2+2);
  44.                 LCD_WR_REG(0x2b);//行地址设置
  45.                 LCD_WR_DATA(y1+1);
  46.                 LCD_WR_DATA(y2+1);
  47.                 LCD_WR_REG(0x2c);//储存器写
  48.         }
  49.         else if(USE_HORIZONTAL==1)
  50.         {
  51.                 LCD_WR_REG(0x2a);//列地址设置
  52.                 LCD_WR_DATA(x1+2);
  53.                 LCD_WR_DATA(x2+2);
  54.                 LCD_WR_REG(0x2b);//行地址设置
  55.                 LCD_WR_DATA(y1+1);
  56.                 LCD_WR_DATA(y2+1);
  57.                 LCD_WR_REG(0x2c);//储存器写
  58.         }
  59.         else if(USE_HORIZONTAL==2)
  60.         {
  61.                 LCD_WR_REG(0x2a);//列地址设置
  62.                 LCD_WR_DATA(x1+1);
  63.                 LCD_WR_DATA(x2+1);
  64.                 LCD_WR_REG(0x2b);//行地址设置
  65.                 LCD_WR_DATA(y1+2);
  66.                 LCD_WR_DATA(y2+2);
  67.                 LCD_WR_REG(0x2c);//储存器写
  68.         }
  69.         else
  70.         {
  71.                 LCD_WR_REG(0x2a);//列地址设置
  72.                 LCD_WR_DATA(x1+1);
  73.                 LCD_WR_DATA(x2+1);
  74.                 LCD_WR_REG(0x2b);//行地址设置
  75.                 LCD_WR_DATA(y1+2);
  76.                 LCD_WR_DATA(y2+2);
  77.                 LCD_WR_REG(0x2c);//储存器写
  78.         }
  79. }

  80. void LCD_Init(void)
  81. {
  82.         
  83.         
  84.         LCD_GPIO_Init();//初始化GPIO
  85.         
  86.         LCD_RES_Clr();//复位
  87.         SysTick_Delay_ms(100);
  88.         LCD_RES_Set();
  89.         SysTick_Delay_ms(100);
  90.         
  91.         //LCD_BLK_Set();//打开背光
  92.   SysTick_Delay_ms(100);
  93.         
  94.         //************* Start Initial Sequence **********//
  95.         LCD_WR_REG(0x11); //Sleep out
  96.         SysTick_Delay_ms(120);              //Delay 120ms
  97.         //------------------------------------ST7735S Frame Rate-----------------------------------------//
  98.         LCD_WR_REG(0xB1);
  99.         LCD_WR_DATA8(0x05);
  100.         LCD_WR_DATA8(0x3C);
  101.         LCD_WR_DATA8(0x3C);
  102.         LCD_WR_REG(0xB2);
  103.         LCD_WR_DATA8(0x05);
  104.         LCD_WR_DATA8(0x3C);
  105.         LCD_WR_DATA8(0x3C);
  106.         LCD_WR_REG(0xB3);
  107.         LCD_WR_DATA8(0x05);
  108.         LCD_WR_DATA8(0x3C);
  109.         LCD_WR_DATA8(0x3C);
  110.         LCD_WR_DATA8(0x05);
  111.         LCD_WR_DATA8(0x3C);
  112.         LCD_WR_DATA8(0x3C);
  113.         //------------------------------------End ST7735S Frame Rate---------------------------------//
  114.         LCD_WR_REG(0xB4); //Dot inversion
  115.         LCD_WR_DATA8(0x03);
  116.         //------------------------------------ST7735S Power Sequence---------------------------------//
  117.         LCD_WR_REG(0xC0);
  118.         LCD_WR_DATA8(0x28);
  119.         LCD_WR_DATA8(0x08);
  120.         LCD_WR_DATA8(0x04);
  121.         LCD_WR_REG(0xC1);
  122.         LCD_WR_DATA8(0XC0);
  123.         LCD_WR_REG(0xC2);
  124.         LCD_WR_DATA8(0x0D);
  125.         LCD_WR_DATA8(0x00);
  126.         LCD_WR_REG(0xC3);
  127.         LCD_WR_DATA8(0x8D);
  128.         LCD_WR_DATA8(0x2A);
  129.         LCD_WR_REG(0xC4);
  130.         LCD_WR_DATA8(0x8D);
  131.         LCD_WR_DATA8(0xEE);
  132.         //---------------------------------End ST7735S Power Sequence-------------------------------------//
  133.         LCD_WR_REG(0xC5); //VCOM
  134.         LCD_WR_DATA8(0x1A);
  135.         LCD_WR_REG(0x36); //MX, MY, RGB mode
  136.         if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00);
  137.         else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0);
  138.         else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70);
  139.         else LCD_WR_DATA8(0xA0);
  140.         //------------------------------------ST7735S Gamma Sequence---------------------------------//
  141.         LCD_WR_REG(0xE0);
  142.         LCD_WR_DATA8(0x04);
  143.         LCD_WR_DATA8(0x22);
  144.         LCD_WR_DATA8(0x07);
  145.         LCD_WR_DATA8(0x0A);
  146.         LCD_WR_DATA8(0x2E);
  147.         LCD_WR_DATA8(0x30);
  148.         LCD_WR_DATA8(0x25);
  149.         LCD_WR_DATA8(0x2A);
  150.         LCD_WR_DATA8(0x28);
  151.         LCD_WR_DATA8(0x26);
  152.         LCD_WR_DATA8(0x2E);
  153.         LCD_WR_DATA8(0x3A);
  154.         LCD_WR_DATA8(0x00);
  155.         LCD_WR_DATA8(0x01);
  156.         LCD_WR_DATA8(0x03);
  157.         LCD_WR_DATA8(0x13);
  158.         LCD_WR_REG(0xE1);
  159.         LCD_WR_DATA8(0x04);
  160.         LCD_WR_DATA8(0x16);
  161.         LCD_WR_DATA8(0x06);
  162.         LCD_WR_DATA8(0x0D);
  163.         LCD_WR_DATA8(0x2D);
  164.         LCD_WR_DATA8(0x26);
  165.         LCD_WR_DATA8(0x23);
  166.         LCD_WR_DATA8(0x27);
  167.         LCD_WR_DATA8(0x27);
  168.         LCD_WR_DATA8(0x25);
  169.         LCD_WR_DATA8(0x2D);
  170.         LCD_WR_DATA8(0x3B);
  171.         LCD_WR_DATA8(0x00);
  172.         LCD_WR_DATA8(0x01);
  173.         LCD_WR_DATA8(0x04);
  174.         LCD_WR_DATA8(0x13);
  175.         //------------------------------------End ST7735S Gamma Sequence-----------------------------//
  176.         LCD_WR_REG(0x3A); //65k mode
  177.         LCD_WR_DATA8(0x05);
  178.         LCD_WR_REG(0x29); //Display on
  179. }

复制代码


lcd.c
  1. #include "main.h"
  2. #include "spilcd/lcdfont.h"

  3. void LCD_Fill(uint16_t xsta,uint16_t ysta,uint16_t xend,uint16_t yend,uint16_t color)
  4. {         
  5.         uint16_t i,j;
  6.         LCD_Address_Set(xsta,ysta,xend-1,yend-1);
  7.         for(i=ysta;i<yend;i++)
  8.         {                                                                                                                           
  9.                 for(j=xsta;j<xend;j++)
  10.                 {
  11.                         LCD_WR_DATA(color);
  12.                 }
  13.         }                                                      
  14. }

  15. void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
  16. {
  17.         LCD_Address_Set(x,y,x,y);
  18.         LCD_WR_DATA(color);
  19. }

  20. void LCD_DrawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color)
  21. {
  22.         uint16_t t;
  23.         int xerr=0,yerr=0,delta_x,delta_y,distance;
  24.         int incx,incy,uRow,uCol;
  25.         delta_x=x2-x1;  
  26.         delta_y=y2-y1;
  27.         uRow=x1;
  28.         uCol=y1;
  29.         if(delta_x>0)incx=1;
  30.         else if (delta_x==0)incx=0;
  31.         else {incx=-1;delta_x=-delta_x;}
  32.         if(delta_y>0)incy=1;
  33.         else if (delta_y==0)incy=0;
  34.         else {incy=-1;delta_y=-delta_y;}
  35.         if(delta_x>delta_y)distance=delta_x;
  36.         else distance=delta_y;
  37.         for(t=0;t<distance+1;t++)
  38.         {
  39.                 LCD_DrawPoint(uRow,uCol,color);
  40.                 xerr+=delta_x;
  41.                 yerr+=delta_y;
  42.                 if(xerr>distance)
  43.                 {
  44.                         xerr-=distance;
  45.                         uRow+=incx;
  46.                 }
  47.                 if(yerr>distance)
  48.                 {
  49.                         yerr-=distance;
  50.                         uCol+=incy;
  51.                 }
  52.         }
  53. }

  54. void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint16_t color)
  55. {
  56.         LCD_DrawLine(x1,y1,x2,y1,color);
  57.         LCD_DrawLine(x1,y1,x1,y2,color);
  58.         LCD_DrawLine(x1,y2,x2,y2,color);
  59.         LCD_DrawLine(x2,y1,x2,y2,color);
  60. }

  61. void Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r,uint16_t color)
  62. {
  63.         int a,b;
  64.         a=0;b=r;         
  65.         while(a<=b)
  66.         {
  67.                 LCD_DrawPoint(x0-b,y0-a,color);             //3           
  68.                 LCD_DrawPoint(x0+b,y0-a,color);             //0           
  69.                 LCD_DrawPoint(x0-a,y0+b,color);             //1               
  70.                 LCD_DrawPoint(x0-a,y0-b,color);             //2            
  71.                 LCD_DrawPoint(x0+b,y0+a,color);             //4               
  72.                 LCD_DrawPoint(x0+a,y0-b,color);             //5
  73.                 LCD_DrawPoint(x0+a,y0+b,color);             //6
  74.                 LCD_DrawPoint(x0-b,y0+a,color);             //7
  75.                 a++;
  76.                 if((a*a+b*b)>(r*r))//判断要画的点是否过远
  77.                 {
  78.                         b--;
  79.                 }
  80.         }
  81. }

  82. void LCD_ShowChinese(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  83. {
  84.         while(*s!=0)
  85.         {
  86.                 if(sizey==12) LCD_ShowChinese12x12(x,y,s,fc,bc,sizey,mode);
  87.                 else if(sizey==16) LCD_ShowChinese16x16(x,y,s,fc,bc,sizey,mode);
  88.                 else if(sizey==24) LCD_ShowChinese24x24(x,y,s,fc,bc,sizey,mode);
  89.                 else if(sizey==32) LCD_ShowChinese32x32(x,y,s,fc,bc,sizey,mode);
  90.                 else return;
  91.                 s+=2;
  92.                 x+=sizey;
  93.         }
  94. }

  95. void LCD_ShowChinese12x12(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  96. {
  97.         uint8_t i,j,m=0;
  98.         uint16_t k;
  99.         uint16_t HZnum;//汉字数目
  100.         uint16_t TypefaceNum;//一个字符所占字节大小
  101.         uint16_t x0=x;
  102.         TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
  103.                                  
  104.         HZnum=sizeof(tfont12)/sizeof(typFNT_GB12);        //统计汉字数目
  105.         for(k=0;k<HZnum;k++)
  106.         {
  107.                 if((tfont12[k].Index[0]==*(s))&&(tfont12[k].Index[1]==*(s+1)))
  108.                 {         
  109.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  110.                         for(i=0;i<TypefaceNum;i++)
  111.                         {
  112.                                 for(j=0;j<8;j++)
  113.                                 {        
  114.                                         if(!mode)//非叠加方式
  115.                                         {
  116.                                                 if(tfont12[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  117.                                                 else LCD_WR_DATA(bc);
  118.                                                 m++;
  119.                                                 if(m%sizey==0)
  120.                                                 {
  121.                                                         m=0;
  122.                                                         break;
  123.                                                 }
  124.                                         }
  125.                                         else//叠加方式
  126.                                         {
  127.                                                 if(tfont12[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//画一个点
  128.                                                 x++;
  129.                                                 if((x-x0)==sizey)
  130.                                                 {
  131.                                                         x=x0;
  132.                                                         y++;
  133.                                                         break;
  134.                                                 }
  135.                                         }
  136.                                 }
  137.                         }
  138.                 }                                          
  139.                 continue;  //查找到对应点阵字库立即退出,防止多个汉字重复取模带来影响
  140.         }
  141. }

  142. void LCD_ShowChinese16x16(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  143. {
  144.         uint8_t i,j,m=0;
  145.         uint16_t k;
  146.         uint16_t HZnum;//汉字数目
  147.         uint16_t TypefaceNum;//一个字符所占字节大小
  148.         uint16_t x0=x;
  149.   TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
  150.         HZnum=sizeof(tfont16)/sizeof(typFNT_GB16);        //统计汉字数目
  151.         for(k=0;k<HZnum;k++)
  152.         {
  153.                 if ((tfont16[k].Index[0]==*(s))&&(tfont16[k].Index[1]==*(s+1)))
  154.                 {         
  155.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  156.                         for(i=0;i<TypefaceNum;i++)
  157.                         {
  158.                                 for(j=0;j<8;j++)
  159.                                 {        
  160.                                         if(!mode)//非叠加方式
  161.                                         {
  162.                                                 if(tfont16[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  163.                                                 else LCD_WR_DATA(bc);
  164.                                                 m++;
  165.                                                 if(m%sizey==0)
  166.                                                 {
  167.                                                         m=0;
  168.                                                         break;
  169.                                                 }
  170.                                         }
  171.                                         else//叠加方式
  172.                                         {
  173.                                                 if(tfont16[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//画一个点
  174.                                                 x++;
  175.                                                 if((x-x0)==sizey)
  176.                                                 {
  177.                                                         x=x0;
  178.                                                         y++;
  179.                                                         break;
  180.                                                 }
  181.                                         }
  182.                                 }
  183.                         }
  184.                 }                                          
  185.                 continue;  //查找到对应点阵字库立即退出,防止多个汉字重复取模带来影响
  186.         }
  187. }

  188. void LCD_ShowChinese24x24(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  189. {
  190.         uint8_t i,j,m=0;
  191.         uint16_t k;
  192.         uint16_t HZnum;//汉字数目
  193.         uint16_t TypefaceNum;//一个字符所占字节大小
  194.         uint16_t x0=x;
  195.         TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
  196.         HZnum=sizeof(tfont24)/sizeof(typFNT_GB24);        //统计汉字数目
  197.         for(k=0;k<HZnum;k++)
  198.         {
  199.                 if ((tfont24[k].Index[0]==*(s))&&(tfont24[k].Index[1]==*(s+1)))
  200.                 {         
  201.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  202.                         for(i=0;i<TypefaceNum;i++)
  203.                         {
  204.                                 for(j=0;j<8;j++)
  205.                                 {        
  206.                                         if(!mode)//非叠加方式
  207.                                         {
  208.                                                 if(tfont24[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  209.                                                 else LCD_WR_DATA(bc);
  210.                                                 m++;
  211.                                                 if(m%sizey==0)
  212.                                                 {
  213.                                                         m=0;
  214.                                                         break;
  215.                                                 }
  216.                                         }
  217.                                         else//叠加方式
  218.                                         {
  219.                                                 if(tfont24[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//画一个点
  220.                                                 x++;
  221.                                                 if((x-x0)==sizey)
  222.                                                 {
  223.                                                         x=x0;
  224.                                                         y++;
  225.                                                         break;
  226.                                                 }
  227.                                         }
  228.                                 }
  229.                         }
  230.                 }                                          
  231.                 continue;  //查找到对应点阵字库立即退出,防止多个汉字重复取模带来影响
  232.         }
  233. }

  234. void LCD_ShowChinese32x32(uint16_t x,uint16_t y,uint8_t *s,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  235. {
  236.         uint8_t i,j,m=0;
  237.         uint16_t k;
  238.         uint16_t HZnum;//汉字数目
  239.         uint16_t TypefaceNum;//一个字符所占字节大小
  240.         uint16_t x0=x;
  241.         TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
  242.         HZnum=sizeof(tfont32)/sizeof(typFNT_GB32);        //统计汉字数目
  243.         for(k=0;k<HZnum;k++)
  244.         {
  245.                 if ((tfont32[k].Index[0]==*(s))&&(tfont32[k].Index[1]==*(s+1)))
  246.                 {         
  247.                         LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
  248.                         for(i=0;i<TypefaceNum;i++)
  249.                         {
  250.                                 for(j=0;j<8;j++)
  251.                                 {        
  252.                                         if(!mode)//非叠加方式
  253.                                         {
  254.                                                 if(tfont32[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
  255.                                                 else LCD_WR_DATA(bc);
  256.                                                 m++;
  257.                                                 if(m%sizey==0)
  258.                                                 {
  259.                                                         m=0;
  260.                                                         break;
  261.                                                 }
  262.                                         }
  263.                                         else//叠加方式
  264.                                         {
  265.                                                 if(tfont32[k].Msk[i]&(0x01<<j))        LCD_DrawPoint(x,y,fc);//画一个点
  266.                                                 x++;
  267.                                                 if((x-x0)==sizey)
  268.                                                 {
  269.                                                         x=x0;
  270.                                                         y++;
  271.                                                         break;
  272.                                                 }
  273.                                         }
  274.                                 }
  275.                         }
  276.                 }                                          
  277.                 continue;  //查找到对应点阵字库立即退出,防止多个汉字重复取模带来影响
  278.         }
  279. }

  280. void LCD_ShowChar(uint16_t x,uint16_t y,uint8_t num,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  281. {
  282.         uint8_t temp,sizex,t,m=0;
  283.         uint16_t i,TypefaceNum;//一个字符所占字节大小
  284.         uint16_t x0=x;
  285.         sizex=sizey/2;
  286.         TypefaceNum=(sizex/8+((sizex%8)?1:0))*sizey;
  287.         num=num-' ';    //得到偏移后的值
  288.         LCD_Address_Set(x,y,x+sizex-1,y+sizey-1);  //设置光标位置
  289.         for(i=0;i<TypefaceNum;i++)
  290.         {
  291.                 if(sizey==12)temp=ascii_1206[num][i];                       //调用6x12字体
  292.                 else if(sizey==16)temp=ascii_1608[num][i];                 //调用8x16字体
  293.                 else if(sizey==24)temp=ascii_2412[num][i];                 //调用12x24字体
  294.                 else if(sizey==32)temp=ascii_3216[num][i];                 //调用16x32字体
  295.                 else return;
  296.                 for(t=0;t<8;t++)
  297.                 {
  298.                         if(!mode)//非叠加模式
  299.                         {
  300.                                 if(temp&(0x01<<t))LCD_WR_DATA(fc);
  301.                                 else LCD_WR_DATA(bc);
  302.                                 m++;
  303.                                 if(m%sizex==0)
  304.                                 {
  305.                                         m=0;
  306.                                         break;
  307.                                 }
  308.                         }
  309.                         else//叠加模式
  310.                         {
  311.                                 if(temp&(0x01<<t))LCD_DrawPoint(x,y,fc);//画一个点
  312.                                 x++;
  313.                                 if((x-x0)==sizex)
  314.                                 {
  315.                                         x=x0;
  316.                                         y++;
  317.                                         break;
  318.                                 }
  319.                         }
  320.                 }
  321.         }                     
  322. }

  323. void LCD_ShowString(uint16_t x,uint16_t y,const uint8_t *p,uint16_t fc,uint16_t bc,uint8_t sizey,uint8_t mode)
  324. {         
  325.         while(*p!='\0')
  326.         {      
  327.                 LCD_ShowChar(x,y,*p,fc,bc,sizey,mode);
  328.                 x+=sizey/2;
  329.                 p++;
  330.         }  
  331. }

  332. uint32_t mypow(uint8_t m,uint8_t n)
  333. {
  334.         uint32_t result=1;         
  335.         while(n--)result*=m;
  336.         return result;
  337. }

  338. void LCD_ShowIntNum(uint16_t x,uint16_t y,uint16_t num,uint8_t len,uint16_t fc,uint16_t bc,uint8_t sizey)
  339. {                 
  340.         uint8_t t,temp;
  341.         uint8_t enshow=0;
  342.         uint8_t sizex=sizey/2;
  343.         for(t=0;t<len;t++)
  344.         {
  345.                 temp=(num/mypow(10,len-t-1))%10;
  346.                 if(enshow==0&&t<(len-1))
  347.                 {
  348.                         if(temp==0)
  349.                         {
  350.                                 LCD_ShowChar(x+t*sizex,y,' ',fc,bc,sizey,0);
  351.                                 continue;
  352.                         }else enshow=1;
  353.                           
  354.                 }
  355.                  LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
  356.         }
  357. }

  358. void LCD_ShowFloatNum1(uint16_t x,uint16_t y,float num,uint8_t len,uint16_t fc,uint16_t bc,uint8_t sizey)
  359. {                 
  360.         uint8_t t,temp,sizex;
  361.         uint16_t num1;
  362.         sizex=sizey/2;
  363.         num1=num*100;
  364.         for(t=0;t<len;t++)
  365.         {
  366.                 temp=(num1/mypow(10,len-t-1))%10;
  367.                 if(t==(len-2))
  368.                 {
  369.                         LCD_ShowChar(x+(len-2)*sizex,y,'.',fc,bc,sizey,0);
  370.                         t++;
  371.                         len+=1;
  372.                 }
  373.                  LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
  374.         }
  375. }

  376. void LCD_ShowPicture(uint16_t x,uint16_t y,uint16_t length,uint16_t width,const uint8_t pic[])
  377. {
  378.         uint16_t i,j;
  379.         uint32_t k=0;
  380.         LCD_Address_Set(x,y,x+length-1,y+width-1);
  381.         for(i=0;i<length;i++)
  382.         {
  383.                 for(j=0;j<width;j++)
  384.                 {
  385.                         LCD_WR_DATA8(pic[k*2]);
  386.                         LCD_WR_DATA8(pic[k*2+1]);
  387.                         k++;
  388.                 }
  389.         }                        
  390. }


复制代码


2.3、main.c
  1. #include "main.h"

  2. int main(void)
  3. {
  4.         uint8_t i;

  5.   CLOCK_EnableClock(kCLOCK_Gpio0);

  6.         BOARD_InitDEBUG_UARTPins();
  7.         BOARD_PowerMode_OD();
  8.         BOARD_InitBootClocks();
  9.         BOARD_InitDebugConsole();

  10.         SysTick_Init();
  11.         init_led();
  12.         init_spi();
  13.         LCD_Init();//LCD初始化
  14.         
  15.         LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
  16.         
  17.         LCD_ShowChinese(16,1,"与非网论坛",RED,WHITE,16,0);
  18.         LCD_DrawLine(0,18,128,18,BLUE);
  19.         LCD_ShowString(0,19,"IC:MCXN947",RED,WHITE,16,0);
  20.         LCD_ShowString(0,36,"LCD:ST7735",GREEN,WHITE,16,0);
  21.         LCD_ShowString(0,52,"MODE:SPI",BLUE,WHITE,16,0);

  22.         while (1)
  23.         {
  24.                 i++;
  25.                 SysTick_Delay_ms(100);
  26.                 LCD_ShowIntNum(0,78,i,3,BLUE,WHITE,16);
  27.                 led_green_tog();
  28.         }
  29. }
复制代码


三、程序运行

程序运行结果


分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

stm1024

发表于 2024-12-3 20:45:23 | 只看该作者

支持一下大佬~~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条