[MM32硬件] 【灵动微电子MM32F5330测评】4.ST7735实现简单文本查看器

[复制链接]
1285|0
 楼主| sujingliang 发表于 2024-6-29 21:27 | 显示全部楼层 |阅读模式
本帖最后由 sujingliang 于 2024-7-1 19:08 编辑

前言
在前一篇评测里实现向spi flash写入中文字库bin的的功能,详见https://bbs.21ic.com/icview-3385598-1-1.html
接上文继续实现ST7735 SPI屏驱动和显示中文,并通过KEY控制中文文本翻页浏览。
功能框图
图片2.png
1、通过SPI2 驱动ST7735。
2、通过K2(PC5)、K4(PB2)控制查看文本向前翻页和向后翻页。
key.png
驱动ST7735
st7735.c
  1. #define _ST7735_C_

  2. #include "platform.h"
  3. #include "st7735.h"
  4. #include "charcode.h"


  5. #define LCD_CS_SET() SPI_CSInternalSelected(SPI2, DISABLE)
  6. #define LCD_CS_RESET() SPI_CSInternalSelected(SPI2, ENABLE)

  7. #define LCD_DC          GPIOB,GPIO_Pin_10                        //
  8. #define LCD_RST          GPIOB,GPIO_Pin_11                        //
  9. #define LCD_LED          GPIOB,GPIO_Pin_1                        //

  10. #define LCD_DC_SET()                 GPIO_WriteBit(LCD_DC, Bit_SET);
  11. #define LCD_DC_RESET()         GPIO_WriteBit(LCD_DC, Bit_RESET);

  12. #define LCD_RST_SET()         GPIO_WriteBit(LCD_RST, Bit_SET);
  13. #define LCD_RST_RESET() GPIO_WriteBit(LCD_RST, Bit_RESET);

  14. #define LCD_LED_SET()         GPIO_WriteBit(LCD_LED, Bit_SET);
  15. #define LCD_LED_RESET() GPIO_WriteBit(LCD_LED, Bit_RESET);

  16. extern void SPI_FLASH_FastRead(uint32_t Address, uint8_t *Buffer, uint32_t Length);
  17. uint8_t MatrixBuff[32];

  18. void getMatrix(const uint16_t nmCode)
  19. {
  20.         uint8_t i;
  21.         uint32_t offset;
  22.         uint8_t GBH,GBL;
  23.         uint16_t nm=nmCode;

  24.         GBH=nm>>8&0xff;
  25.         GBL=nm&0xff;

  26.         if(GBH>=0xb0)
  27.         {
  28.                 offset=((GBH-0xa7)*94+GBL-0xa1)*32;
  29.         }else
  30.         {
  31.                 offset=((GBH-0xa1)*94+GBL-0xa1)*32;
  32.         }
  33.         
  34.         SPI_FLASH_FastRead(offset,MatrixBuff,32);
  35.         
  36. }

  37. typedef struct {
  38.         uint8_t cmd;
  39.         uint8_t dat[16];
  40.         uint16_t datLen;
  41.         uint32_t sleep;
  42. } st7735_ini_str_t;

  43. st7735_ini_str_t lcd_ini_str[] = {
  44.         {0x11, {0x00}, 0, 120},                         /* software reset */
  45.         {0xB1, {0x02, 0x35, 0x36}, 3, 0},
  46.         {0xB2, {0x02,0x35,0x36}, 3 ,0}, //Frame rate 80Hz
  47.         {0xB3, {0x02,0x35,0x36,0x02,0x35,0x36}, 6 ,0},
  48.         {0xB4, {0x03}, 1, 0 },
  49.         {0xC0, {0xA2,0x02,0x84}, 3 ,0},
  50.         {0xC1, {0xC5}, 1 ,0},
  51.         {0xC2, {0x0D,0x00}, 2,0},
  52.         {0xC3, {0x8D,0x2A}, 2,0},
  53.         {0xC4, {0x8D,0xEE}, 2,0},
  54.         {0xC5, {0x0A}, 1,0},
  55.         {0xC7, {0x00},1,0},
  56.         {0x36, {0xC8}, 1,0},
  57.         {0xE0, {0x12,0x1C,0x10,0x18,0x33,0x2C,0x25,0x28,0x28,0x27,0x2F,0x3c,0x00,0x03,0x03,0x10}, 16,0},
  58.         {0xE1, {0x12,0x1C,0x10,0x18,0x2D,0x28,0x23,0x28,0x28,0x26,0x2F,0x3B,0x00,0x03,0x03,0x10}, 16,0},
  59.         {0x3A, {0x05}, 1, 0},  //65k mode
  60.         {0x29, {0x00}, 0, 0},  //Display on
  61.         {0x00, {0x00}, 0, 0}                            /* EOL */
  62. };


  63. void LCD_SPI_Configure(void)
  64. {
  65.                 GPIO_InitTypeDef GPIO_InitStruct;
  66.                 SPI_InitTypeDef  SPI_InitStruct;

  67.                 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  68.         
  69.                 SPI_StructInit(&SPI_InitStruct);
  70.     SPI_InitStruct.SPI_Mode      = SPI_Mode_Master;
  71.     SPI_InitStruct.SPI_DataSize  = SPI_DataSize_8b;
  72.     SPI_InitStruct.SPI_DataWidth = 8;
  73.     SPI_InitStruct.SPI_CPOL      = SPI_CPOL_Low;
  74.     SPI_InitStruct.SPI_CPHA      = SPI_CPHA_1Edge;
  75.     SPI_InitStruct.SPI_NSS       = SPI_NSS_Soft;
  76.     SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  77.     SPI_InitStruct.SPI_FirstBit  = SPI_FirstBit_MSB;
  78.     SPI_Init(SPI2, &SPI_InitStruct);
  79.         
  80.                 SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Rx);
  81.     SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Tx);
  82.         
  83.                 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  84.         
  85.                 GPIO_PinAFConfig(GPIOB, GPIO_PinSource12,  GPIO_AF_5);        //SPI2_NSS  PB12
  86.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource13,  GPIO_AF_5);        //SPI2_SCK  PB13
  87.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,  GPIO_AF_5);        //SPI2_MISO PB14
  88.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,  GPIO_AF_5);        //SPI2_MOSI PB15
  89.                
  90.                
  91.                 GPIO_StructInit(&GPIO_InitStruct);
  92.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_15;
  93.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  94.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
  95.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  96.                
  97.                 GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_14;
  98.                 GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;
  99.                 GPIO_Init(GPIOB, &GPIO_InitStruct);
  100.                
  101.                 SPI_Cmd(SPI2, ENABLE);
  102.                
  103.                
  104.                 //初始化LCD_DC、LCD_RST、LCD_LED
  105.                 GPIO_StructInit(&GPIO_InitStruct);
  106.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_1|GPIO_Pin_10|GPIO_Pin_11;
  107.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  108.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
  109.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  110.                
  111. }


  112. /***********************************************************************************************************************
  113.   * @brief
  114.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   none
  115.   * @param  none
  116.   * @retval none
  117.   *********************************************************************************************************************/
  118. void LCD_SPI_TxData_Polling(uint8_t *Buffer, uint8_t Length)
  119. {
  120.     uint8_t i = 0, Data = 0;

  121.     for (i = 0; i < Length; i++)
  122.     {
  123.         //SPI_SendData(SPI2, Buffer[i]);
  124.                                 SPI_SendByte(SPI2, Buffer[i]);

  125.         while (RESET == SPI_GetFlagStatus(SPI2, SPI_FLAG_TXEPT))
  126.         {
  127.         }

  128.         while (RESET == SPI_GetFlagStatus(SPI2, SPI_FLAG_RXAVL))
  129.         {
  130.         }

  131.         Data = SPI_ReceiveData(SPI2);
  132.     }
  133. }

  134. /*写指令*/
  135. void LCD_WriteIndex(uint8_t index)
  136. {
  137.         LCD_CS_RESET();
  138.         LCD_DC_RESET();
  139.         LCD_SPI_TxData_Polling(&index,1);
  140.         LCD_CS_SET();
  141. }
  142. /*写数据*/
  143. void LCD_WriteData(uint8_t *Buffer, uint8_t Length)
  144. {
  145.         LCD_CS_RESET();
  146.         LCD_DC_SET();
  147.         LCD_SPI_TxData_Polling(Buffer,Length);
  148.         LCD_CS_SET();
  149. }

  150. void ST7735_SendData(uint8_t cmd, uint8_t *data, uint16_t length)
  151. {
  152.         LCD_WriteIndex(cmd);
  153.         LCD_WriteData(data,length);
  154. }


  155. void ST7735_HardReset()
  156. {
  157.         LCD_RST_SET();
  158.         PLATFORM_DelayMS(100);
  159.         LCD_RST_RESET();
  160.         PLATFORM_DelayMS(100);
  161.         LCD_RST_SET();
  162.         PLATFORM_DelayMS(100);
  163. }

  164. /*发送初始化字符串*/
  165. void ST7735_SendInitStr()
  166. {
  167.         LCD_CS_RESET();
  168.         uint16_t i = 0;
  169.         while(lcd_ini_str[i].cmd != 0x00)
  170.         {
  171.                         uint8_t cmd = lcd_ini_str[i].cmd;
  172.                         uint16_t datLen = lcd_ini_str[i].datLen;
  173.                         uint8_t *dat;
  174.                         dat = &(lcd_ini_str[i].dat[0]);
  175.                         uint32_t slp = lcd_ini_str[i].sleep;

  176.                         LCD_DC_RESET();
  177.                         LCD_SPI_TxData_Polling(&cmd, 1);

  178.                         if(datLen > 0)
  179.                         {
  180.                                         LCD_DC_SET();
  181.                                         LCD_SPI_TxData_Polling(dat, datLen);
  182.                         }
  183.                         if(slp > 0)
  184.                         {
  185.                                         PLATFORM_DelayMS(slp);
  186.                         }
  187.                         i++;
  188.         }
  189.         LCD_CS_SET();
  190. }


  191. void ST7735_SetWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  192. {
  193.         /* CASET */
  194.         uint8_t cmd = 0x2A;
  195.         x=x+2;
  196.         y=y+2;
  197.         uint8_t buf4[4];
  198.         buf4[0] = (x >> 8) & 0xFF;
  199.         buf4[1] = x & 0xFF;
  200.         buf4[2] = ((x + w -1) >> 8) & 0xFF;
  201.         buf4[3] = (x + w -1) & 0xFF;
  202.         ST7735_SendData(cmd, buf4, 4);

  203.         /* RASET */
  204.         cmd = 0x2B;
  205.         buf4[0] = (y >> 8) & 0xFF;
  206.         buf4[1] = y & 0xFF;
  207.         buf4[2] = ((y + h - 1) >> 8) & 0xFF;
  208.         buf4[3] = (y + h - 1) & 0xFF;
  209.         ST7735_SendData(cmd, buf4, 4);
  210.         

  211.         LCD_WriteIndex(0x2C);
  212. }


  213. void Lcd_SetXY(unsigned int x,unsigned int y)
  214. {
  215.           ST7735_SetWindow(x,y,x,y);
  216. }

  217. void setPixel(uint16_t Color)
  218. {
  219.         
  220.         uint8_t color_h,color_l;
  221.         color_h=Color>>8;
  222.         color_l=Color&0xff;
  223.         
  224.         LCD_CS_RESET();
  225.         LCD_DC_SET();
  226.         LCD_SPI_TxData_Polling(&color_h,1);
  227.         LCD_SPI_TxData_Polling(&color_l,1);
  228.         LCD_CS_SET();


  229. }

  230. void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t Data)
  231. {
  232.         ST7735_SetWindow(x,y,x+1,y+1);
  233.         //LCD_WriteData((uint8_t*)&Data, 2);
  234.         setPixel(Data);

  235. }   


  236. //画线函数,使用Bresenham 画线算法
  237. void Gui_DrawLine(unsigned int x0, unsigned int y0,unsigned int x1, unsigned int y1,unsigned int Color)   
  238. {
  239. int dx,             // difference in x's
  240.     dy,             // difference in y's
  241.     dx2,            // dx,dy * 2
  242.     dy2,
  243.     x_inc,          // amount in pixel space to move during drawing
  244.     y_inc,          // amount in pixel space to move during drawing
  245.     error,          // the discriminant i.e. error i.e. decision variable
  246.     index;          // used for looping        


  247.         Lcd_SetXY(x0,y0);
  248.         dx = x1-x0;//计算x距离
  249.         dy = y1-y0;//计算y距离

  250.         if (dx>=0)
  251.         {
  252.                 x_inc = 1;
  253.         }
  254.         else
  255.         {
  256.                 x_inc = -1;
  257.                 dx    = -dx;  
  258.         }
  259.         
  260.         if (dy>=0)
  261.         {
  262.                 y_inc = 1;
  263.         }
  264.         else
  265.         {
  266.                 y_inc = -1;
  267.                 dy    = -dy;
  268.         }

  269.         dx2 = dx << 1;
  270.         dy2 = dy << 1;

  271.         if (dx > dy)//x距离大于y距离,那么每个x轴上只有一个点,每个y轴上有若干个点
  272.         {//且线的点数等于x距离,以x轴递增画点
  273.                 // initialize error term
  274.                 error = dy2 - dx;

  275.                 // draw the line
  276.                 for (index=0; index <= dx; index++)//要画的点数不会超过x距离
  277.                 {
  278.                         //画点
  279.                         LCD_DrawPoint(x0,y0,Color);
  280.                         
  281.                         // test if error has overflowed
  282.                         if (error >= 0) //是否需要增加y坐标值
  283.                         {
  284.                                 error-=dx2;

  285.                                 // move to next line
  286.                                 y0+=y_inc;//增加y坐标值
  287.                         } // end if error overflowed

  288.                         // adjust the error term
  289.                         error+=dy2;

  290.                         // move to the next pixel
  291.                         x0+=x_inc;//x坐标值每次画点后都递增1
  292.                 } // end for
  293.         } // end if |slope| <= 1
  294.         else//y轴大于x轴,则每个y轴上只有一个点,x轴若干个点
  295.         {//以y轴为递增画点
  296.                 // initialize error term
  297.                 error = dx2 - dy;

  298.                 // draw the line
  299.                 for (index=0; index <= dy; index++)
  300.                 {
  301.                         // set the pixel
  302.                         LCD_DrawPoint(x0,y0,Color);

  303.                         // test if error overflowed
  304.                         if (error >= 0)
  305.                         {
  306.                                 error-=dy2;

  307.                                 // move to next line
  308.                                 x0+=x_inc;
  309.                         } // end if error overflowed

  310.                         // adjust the error term
  311.                         error+=dx2;

  312.                         // move to the next pixel
  313.                         y0+=y_inc;
  314.                 } // end for
  315.         } // end else |slope| > 1
  316. }

  317. void Gui_DrawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,uint16_t bc)
  318. {
  319.         uint16_t i,j;
  320.         ST7735_SetWindow(x, y, w, h);
  321.   for(i=0;i<w;i++)
  322.   {
  323.                 for(j=0;j<h;j++)
  324.                 {
  325.                         setPixel(bc);
  326.                 }
  327.   }
  328. }

  329. void GUI_Clear(uint16_t Color)     //清屏
  330. {
  331.         uint16_t i,j;
  332.         
  333.   ST7735_SetWindow(0, 0, X_MAX_PIXEL, Y_MAX_PIXEL);
  334.   
  335.   for(i=0;i<Y_MAX_PIXEL;i++)
  336.   {
  337.                 for(j=0;j<X_MAX_PIXEL;j++)
  338.                 {
  339.                         setPixel(Color);
  340.                 }
  341.   }
  342. }





  343. void GUI_WriteASCII8x16(uint16_t  x, uint16_t  y, uint8_t  *p, uint16_t  wordColor, uint16_t  backColor)
  344. {
  345.     unsigned char j, wordByte,wordNum;
  346.                 uint16_t mx,my;
  347.     uint8_t color;
  348.         
  349.                 mx=x;
  350.     my=y;
  351.         
  352.     while(*p != '\0')
  353.     {
  354.                         if(mx>127){
  355.                                 mx=x;
  356.                                 my+=16;
  357.                         }
  358.         wordNum = *p - 32;
  359.         ST7735_SetWindow(mx,my,8, 16);
  360.                           
  361.         for (wordByte=0; wordByte<16; wordByte++)
  362.         {
  363.             color = Font_Data[wordNum].dat[wordByte];
  364.             for (j=0; j<8; j++)
  365.             {
  366.                 if ((color&0x80) == 0x80)
  367.                 {
  368.                     setPixel(wordColor);
  369.                 }
  370.                 else
  371.                 {
  372.                     setPixel(backColor);
  373.                 }
  374.                 color <<= 1;
  375.             }
  376.         }
  377.         p++;
  378.         mx +=8;
  379.     }
  380. }

  381. void GUI_WriteCN16x16(uint16_t x, uint16_t y,const uint8_t *cn, uint16_t wordColor, uint16_t backColor)
  382. {
  383.                 uint16_t i, j, mx,my,wordByte;
  384.                 uint16_t zm;
  385.                 uint16_t color;
  386.                 uint8_t wordNum;

  387.                 mx=x;
  388.                 my=y;
  389.                 while (*cn != '\0')
  390.                 {
  391.                         
  392.                         if(mx>119){
  393.                                 mx=x;
  394.                                 my+=16;
  395.                         }
  396.                         if(*cn<128){
  397.                                 wordNum = *cn - 32;
  398.                                 ST7735_SetWindow(mx,my,8, 16);
  399.                                 
  400.                                 for (wordByte=0; wordByte<16; wordByte++)
  401.                                 {
  402.                                                 color = Font_Data[wordNum].dat[wordByte];
  403.                                                 for (j=0; j<8; j++)
  404.                                                 {
  405.                                                                 if ((color&0x80) == 0x80)
  406.                                                                 {
  407.                                                                                 setPixel(wordColor);
  408.                                                                 }
  409.                                                                 else
  410.                                                                 {
  411.                                                                                 setPixel(backColor);
  412.                                                                 }
  413.                                                                 color <<= 1;
  414.                                                 }
  415.                                 }
  416.                                 cn++;
  417.                                 mx +=8;
  418.                                 }
  419.                                 else
  420.                                 {
  421.                                 ST7735_SetWindow(mx, my, 16, 16);
  422.                                                 zm=*cn;
  423.                                                 zm<<=8;
  424.                                                 zm|=*(cn+1);

  425.                                                 getMatrix(zm);

  426.                                                                 for(i=0; i<32; i++)
  427.                                                                 {   //MSK的位数
  428.                                                                                 color=MatrixBuff[i];
  429.                                                                                 for(j=0;j<8;j++)
  430.                                                                                 {
  431.                                                                                                 if((color&0x80)==0x80)
  432.                                                                                                 {
  433.                                                                                                                 setPixel(wordColor);
  434.                                                                                                 }
  435.                                                                                                 else
  436.                                                                                                 {
  437.                                                                                                                 setPixel(backColor);
  438.                                                                                                 }
  439.                                                                                                 color<<=1;
  440.                                                                                 }//for(j=0;j<8;j++)结束
  441.                                                                 }


  442.                                 cn += 2;
  443.                                 mx += 16;
  444.                                                         }
  445.                 }
  446. }




  447. void ST7735_Init(void)
  448. {
  449.         LCD_LED_SET();
  450.         LCD_SPI_Configure();
  451.         ST7735_HardReset();
  452.         ST7735_SendInitStr();

  453. }



其中,读取字库函数getMatrix,
1、之前将字库写到了外部flash的地址0
2、字库的偏移量计算:因为字库本身去掉了一些不常用的字,所有需要跳过部分地址对GBH>=0xb0进行特殊判断。
  1. void getMatrix(const uint16_t nmCode)
  2. {
  3.         uint8_t i;
  4.         uint32_t offset;
  5.         uint8_t GBH,GBL;
  6.         uint16_t nm=nmCode;

  7.         GBH=nm>>8&0xff;
  8.         GBL=nm&0xff;

  9.         if(GBH>=0xb0)
  10.         {
  11.                 offset=((GBH-0xa7)*94+GBL-0xa1)*32;
  12.         }else
  13.         {
  14.                 offset=((GBH-0xa1)*94+GBL-0xa1)*32;
  15.         }
  16.         
  17.         SPI_FLASH_FastRead(offset,MatrixBuff,32);
  18.         
  19. }


控制文本显示
gui_text.c

1、article是要显示文本全文。
2、textBuff是一屏可以显示的文本。
  1. #define _GUI_TEXT_C_

  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "st7735.h"
  5. #include "gui_text.h"

  6. #define HZNUM        64
  7. #define TEXTSIZE HZNUM*2
  8. uint8_t textBuff[TEXTSIZE];
  9. const uint8_t        article[]="天尊地卑,乾坤定矣。卑高以陈,贵贱位矣。动静有常,刚柔断矣。方以类聚,物以群分,吉凶生矣。在天成象,在地成形,变化见矣。乾知大始,坤作成物。乾以易知,坤以简能。易则易知,简则易从。易知则有亲,易从则有功。有亲则可久,有功则可大。可久则贤人之德,可大则贤人之业。易简,而天下矣之理矣;天下之理得,而成位乎其中矣。第二章圣人设卦观象,系辞焉而明吉凶,刚柔相推而生变化。是故,君子所居而安者,易之序也。所乐而玩者,爻之辞也。是故,君子居则观其象,而玩其辞;动则观其变,而玩其占。是故自**之,吉无不利。第三章彖者,言乎象也。爻者,言乎变者也。吉凶者,言乎其失得也。悔吝者,言乎其疵也。无咎者,善补过也。是故,列贵贱者,存乎位。齐大者,存乎卦。辩吉凶者,存乎辞。忧悔吝者,存乎介。震无咎者,存乎悔。是故,卦有大,辞有险易。辞也者,也各指其所之。第四章易与天地准,故能弥纶天地之道。仰以观於天文,俯以察於地理,是故知幽明之故。原始反终,故知死生之。精气为物,游魂为变,是故知鬼神之情状。与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧。安土敦乎仁,故能爱。范围天地之化而不过,曲成万物而不遗,通乎昼夜之道而知,故神无方而易无体。";
  10. uint16_t curPage=0;
  11. uint16_t maxPage=sizeof(article)/TEXTSIZE+1;

  12. void gui_DrawScroll(uint8_t pos)
  13. {
  14.         Gui_DrawRect(GUI_TEXT_WIDTH+10,0,2,GUI_TEXT_HEIGHT,COLOR_Blue);
  15.         Gui_DrawRect(GUI_TEXT_WIDTH+5,pos,10,pos+20,COLOR_Blue);
  16. }

  17. void gui_DrawWindow(void)
  18. {
  19.         Gui_DrawRect(0,0,GUI_TEXT_WIDTH,GUI_TEXT_HEIGHT,COLOR_DarkGreen);
  20.         gui_DrawScroll(10);
  21. }

  22. void gui_SetPage(uint16_t page)
  23. {
  24.         strncpy((char*)textBuff,(char*)(article+page*TEXTSIZE),TEXTSIZE);
  25. }


  26. void gui_DrawWindowText(void)
  27. {
  28. gui_SetPage(curPage);
  29.         GUI_WriteCN16x16(0,0,textBuff,COLOR_Yellow,COLOR_Black);
  30. }


  31. void gui_textDemo(void)
  32. {
  33.         GUI_Clear(COLOR_Black);
  34.         gui_DrawWindowText();

  35. }

KEY控制
exit_interrupt.c
  1. void EXTI_Configure(void)
  2. {
  3.     EXTI_InitTypeDef EXTI_InitStruct;
  4.     GPIO_InitTypeDef GPIO_InitStruct;
  5.     NVIC_InitTypeDef NVIC_InitStruct;

  6.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  7.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  8.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

  9.     /* K1->PC4->EXTI_Line4 */
  10.     GPIO_StructInit(&GPIO_InitStruct);
  11.     GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_4;
  12.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD;
  13.     GPIO_Init(GPIOC, &GPIO_InitStruct);

  14.     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource4);

  15.     EXTI_StructInit(&EXTI_InitStruct);
  16.     EXTI_InitStruct.EXTI_Line    = EXTI_Line4;
  17.     EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
  18.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
  19.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  20.     EXTI_Init(&EXTI_InitStruct);

  21.     /* K2->PC5->EXTI_Line5 */
  22.     GPIO_StructInit(&GPIO_InitStruct);
  23.     GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_5;
  24.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  25.     GPIO_Init(GPIOC, &GPIO_InitStruct);

  26.     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource5);

  27.     EXTI_StructInit(&EXTI_InitStruct);
  28.     EXTI_InitStruct.EXTI_Line    = EXTI_Line5;
  29.     EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
  30.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
  31.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  32.     EXTI_Init(&EXTI_InitStruct);

  33.     /* K3->PB1->EXTI_Line1 */
  34.     GPIO_StructInit(&GPIO_InitStruct);
  35.     GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_1;
  36.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  37.     GPIO_Init(GPIOB, &GPIO_InitStruct);

  38.     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource1);

  39.     EXTI_StructInit(&EXTI_InitStruct);
  40.     EXTI_InitStruct.EXTI_Line    = EXTI_Line1;
  41.     EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
  42.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
  43.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  44.     EXTI_Init(&EXTI_InitStruct);

  45.     /* K4->PB2->EXTI_Line2 */
  46.     GPIO_StructInit(&GPIO_InitStruct);
  47.     GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_2;
  48.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  49.     GPIO_Init(GPIOB, &GPIO_InitStruct);

  50.     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource2);

  51.     EXTI_StructInit(&EXTI_InitStruct);
  52.     EXTI_InitStruct.EXTI_Line    = EXTI_Line2;
  53.     EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
  54.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
  55.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  56.     EXTI_Init(&EXTI_InitStruct);

  57.     /* EXTI Interrupt */
  58.     NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;
  59.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  60.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  61.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  62.     NVIC_Init(&NVIC_InitStruct);

  63.     NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn;
  64.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  65.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  66.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  67.     NVIC_Init(&NVIC_InitStruct);

  68.     NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn;
  69.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  70.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  71.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  72.     NVIC_Init(&NVIC_InitStruct);

  73.     NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn;
  74.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  75.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  76.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  77.     NVIC_Init(&NVIC_InitStruct);
  78. }
mm32f5330_it.c
中断方式实现KEY翻页
  1. void EXTI2_IRQHandler(void)
  2. {
  3.     /* K4 */
  4.     if (SET == EXTI_GetITStatus(EXTI_Line2))
  5.     {
  6.         PLATFORM_LED_Toggle(LED4);
  7.                                 if(curPage<maxPage) {
  8.                                         curPage++;
  9.                                         gui_DrawWindowText();
  10.                                 }
  11.         EXTI_ClearITPendingBit(EXTI_Line2);
  12.     }
  13. }

  14. void EXTI9_5_IRQHandler(void)
  15. {
  16.     /* K2 */
  17.     if (SET == EXTI_GetITStatus(EXTI_Line5))
  18.     {
  19.         PLATFORM_LED_Toggle(LED2);
  20.                                 if(curPage>0)        {
  21.                                         curPage--;
  22.                                         gui_DrawWindowText();
  23.                                 }

  24.         EXTI_ClearITPendingBit(EXTI_Line5);
  25.     }
  26. }
微信图片_20240629211847.jpg

源码:
mini-f53330_USART_Polling.rar (187.81 KB, 下载次数: 4)

tutieshi_640x360_9s.gif



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

本版积分规则

84

主题

147

帖子

3

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