找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 9150|回复: 4
打印 上一主题 下一主题
收起左侧

stm32F103 spi驱动240*320液晶屏幕源代码

[复制链接]
跳转到指定楼层
楼主
ID:256683 发表于 2018-7-30 08:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "stm32f10x.h"
  2. #include "Lcd_Driver.h"
  3. #include "delay.h"


  4. /***************************************************************************************
  5. STM32测试平台介绍:
  6. 开发板:正点原子MiniSTM32开发板
  7. MCU :STM32_F103_RBT6
  8. 晶振 :12MHZ
  9. 主频 :72MHZ
  10. 接线说明:
  11. //-------------------------------------------------------------------------------------
  12. #define LCD_CTRL                     GPIOB                //定义TFT数据端口
  13. #define LCD_LED                GPIO_Pin_9  //PB9 连接至TFT -LED
  14. #define LCD_RS                 GPIO_Pin_10        //PB10连接至TFT --RS
  15. #define LCD_CS                GPIO_Pin_11 //PB11 连接至TFT --CS
  16. #define LCD_RST             GPIO_Pin_12        //PB12连接至TFT --RST
  17. #define LCD_SCL                GPIO_Pin_13        //PB13连接至TFT -- CLK
  18. #define LCD_SDA                GPIO_Pin_15        //PB15连接至TFT - SDI
  19. //VCC:可以接5V也可以接3.3V
  20. //LED:可以接5V也可以接3.3V或者使用任意空闲IO控制(高电平使能)
  21. //GND:接电源地
  22. //说明:如需要尽可能少占用IO,可以将LCD_CS接地,LCD_LED接3.3V,LCD_RST接至单片机复位端,
  23. //将可以释放3个可用IO
  24. //接口定义在Lcd_Driver.h内定义,
  25. //如需变更IO接法,请根据您的实际接线修改相应IO初始化LCD_GPIO_Init()
  26. //-----------------------------------------------------------------------------------------
  27. 例程功能说明:
  28. 1.        简单刷屏测试
  29. 2.        英文显示测试示例
  30. 3.        中文显示测试示例
  31. 4.        数码管字体显示示例
  32. 5.        图片显示示例
  33. 6.        2D按键菜单示例
  34. 7.        本例程支持横屏/竖屏切换(开启宏USE_HORIZONTAL,详见Lcd_Driver.h)
  35. 8.        本例程支持软件模拟SPI/硬件SPI切换(开启宏USE_HARDWARE_SPI,详见Lcd_Driver.h)
  36. **********************************************************************************************/


  37. //---------------------------------function----------------------------------------------------//

  38. /****************************************************************************
  39. * 名    称:void LCD_GPIO_Init(void)
  40. * 功    能:STM32_模拟SPI所用到的GPIO初始化
  41. * 入口参数:无
  42. * 出口参数:无
  43. * 说    明:初始化模拟SPI所用的GPIO
  44. ****************************************************************************/
  45. void LCD_GPIO_Init(void)
  46. {

  47.         GPIO_InitTypeDef  GPIO_InitStructure;
  48.               
  49.         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB ,ENABLE);
  50.         
  51.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15;
  52.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  53.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  54.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  55. }
  56. /****************************************************************************
  57. * 名    称:void  SPIv_WriteData(u8 Data)
  58. * 功    能:STM32_模拟SPI写一个字节数据底层函数
  59. * 入口参数:Data
  60. * 出口参数:无
  61. * 说    明:STM32_模拟SPI读写一个字节数据底层函数
  62. ****************************************************************************/
  63. void  SPIv_WriteData(u8 Data)
  64. {
  65.         unsigned char i=0;
  66.         for(i=8;i>0;i--)
  67.         {
  68.                 if(Data&0x80)        
  69.           LCD_SDA_SET; //输出数据
  70.       else LCD_SDA_CLR;
  71.            
  72.       LCD_SCL_CLR;      
  73.       LCD_SCL_SET;
  74.       Data<<=1;
  75.         }
  76. }

  77. /****************************************************************************
  78. * 名    称:u8 SPI_WriteByte(SPI_TypeDef* SPIx,u8 Byte)
  79. * 功    能:STM32_硬件SPI读写一个字节数据底层函数
  80. * 入口参数:SPIx,Byte
  81. * 出口参数:返回总线收到的数据
  82. * 说    明:STM32_硬件SPI读写一个字节数据底层函数
  83. ****************************************************************************/
  84. u8 SPI_WriteByte(SPI_TypeDef* SPIx,u8 Byte)
  85. {
  86.         while((SPIx->SR&SPI_I2S_FLAG_TXE)==RESET);                //等待发送区空         
  87.         SPIx->DR=Byte;                 //发送一个byte   
  88.         while((SPIx->SR&SPI_I2S_FLAG_RXNE)==RESET);//等待接收完一个byte  
  89.         return SPIx->DR;                       //返回收到的数据                        
  90. }

  91. /****************************************************************************
  92. * 名    称:void SPI_SetSpeed(SPI_TypeDef* SPIx,u8 SpeedSet)
  93. * 功    能:设置SPI的速度
  94. * 入口参数:SPIx,SpeedSet
  95. * 出口参数:无
  96. * 说    明:SpeedSet:1,高速;0,低速;
  97. ****************************************************************************/
  98. void SPI_SetSpeed(SPI_TypeDef* SPIx,u8 SpeedSet)
  99. {
  100.         SPIx->CR1&=0XFFC7;
  101.         if(SpeedSet==1)//高速
  102.         {
  103.                 SPIx->CR1|=SPI_BaudRatePrescaler_2;//Fsck=Fpclk/2        
  104.         }
  105.         else//低速
  106.         {
  107.                 SPIx->CR1|=SPI_BaudRatePrescaler_32; //Fsck=Fpclk/32
  108.         }
  109.         SPIx->CR1|=1<<6; //SPI设备使能
  110. }

  111. /****************************************************************************
  112. * 名    称:SPI2_Init(void)
  113. * 功    能:STM32_SPI2硬件配置初始化
  114. * 入口参数:无
  115. * 出口参数:无
  116. * 说    明:STM32_SPI2硬件配置初始化
  117. ****************************************************************************/
  118. void SPI2_Init(void)        
  119. {
  120.         SPI_InitTypeDef  SPI_InitStructure;
  121.         GPIO_InitTypeDef GPIO_InitStructure;
  122.          
  123.         //配置SPI2管脚
  124.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE);
  125.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
  126.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  127.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  128.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  129.         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14;   
  130.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  131.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  132.         GPIO_Init(GPIOB, &GPIO_InitStructure);  

  133.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10| GPIO_Pin_11| GPIO_Pin_12;
  134.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  135.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  136.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  137.         
  138.         //SPI2配置选项
  139.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 ,ENABLE);
  140.            
  141.         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  142.         SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  143.         SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  144.         SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  145.         SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  146.         SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  147.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  148.         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  149.         SPI_InitStructure.SPI_CRCPolynomial = 7;
  150.         SPI_Init(SPI2, &SPI_InitStructure);

  151.         //使能SPI2
  152.         SPI_Cmd(SPI2, ENABLE);   
  153. }

  154. /****************************************************************************
  155. * 名    称:Lcd_WriteIndex(u8 Index)
  156. * 功    能:向液晶屏写一个8位指令
  157. * 入口参数:Index   寄存器地址
  158. * 出口参数:无
  159. * 说    明:调用前需先选中控制器,内部函数
  160. ****************************************************************************/
  161. void Lcd_WriteIndex(u8 Index)
  162. {
  163.    LCD_CS_CLR;
  164.    LCD_RS_CLR;
  165. #if USE_HARDWARE_SPI   
  166.    SPI_WriteByte(SPI2,Index);
  167. #else
  168.    SPIv_WriteData(Index);
  169. #endif
  170.    LCD_CS_SET;
  171. }

  172. /****************************************************************************
  173. * 名    称:Lcd_WriteData(u8 Data)
  174. * 功    能:向液晶屏写一个8位数据
  175. * 入口参数:dat     寄存器数据
  176. * 出口参数:无
  177. * 说    明:向控制器指定地址写入数据,内部函数
  178. ****************************************************************************/
  179. void Lcd_WriteData(u8 Data)
  180. {
  181.    LCD_CS_CLR;
  182.    LCD_RS_SET;
  183. #if USE_HARDWARE_SPI   
  184.    SPI_WriteByte(SPI2,Data);
  185. #else
  186.    SPIv_WriteData(Data);
  187. #endif
  188.    LCD_CS_SET;
  189. }

  190. /****************************************************************************
  191. * 名    称:void LCD_WriteReg(u8 Index,u16 Data)
  192. * 功    能:写寄存器数据
  193. * 入口参数:Index,Data
  194. * 出口参数:无
  195. * 说    明:本函数为组合函数,向Index地址的寄存器写入Data值
  196. ****************************************************************************/
  197. void LCD_WriteReg(u8 Index,u16 Data)
  198. {
  199.         Lcd_WriteIndex(Index);
  200.           Lcd_WriteData_16Bit(Data);
  201. }

  202. /****************************************************************************
  203. * 名    称:void Lcd_WriteData_16Bit(u16 Data)
  204. * 功    能:向液晶屏写一个16位数据
  205. * 入口参数:Data
  206. * 出口参数:无
  207. * 说    明:向控制器指定地址写入一个16位数据
  208. ****************************************************************************/
  209. void Lcd_WriteData_16Bit(u16 Data)
  210. {        
  211.         Lcd_WriteData(Data>>8);
  212.         Lcd_WriteData(Data);        
  213. }

  214. /****************************************************************************
  215. * 名    称:void Lcd_Reset(void)
  216. * 功    能:液晶硬复位函数
  217. * 入口参数:无
  218. * 出口参数:无
  219. * 说    明:液晶初始化前需执行一次复位操作
  220. ****************************************************************************/
  221. void Lcd_Reset(void)
  222. {
  223.         LCD_RST_CLR;
  224.         delay_ms(100);
  225.         LCD_RST_SET;
  226.         delay_ms(50);
  227. }
  228. /****************************************************************************
  229. * 名    称:void Lcd_Init(void)
  230. * 功    能:液晶初始化函数
  231. * 入口参数:无
  232. * 出口参数:无
  233. * 说    明:液晶初始化_ILI9225_176X220
  234. ****************************************************************************/
  235. void Lcd_Init(void)
  236. {        
  237. #if USE_HARDWARE_SPI //使用硬件SPI
  238.         SPI2_Init();
  239. #else        
  240.         LCD_GPIO_Init();//使用模拟SPI
  241. #endif
  242.         Lcd_Reset(); //Reset before LCD Init.

  243.         Lcd_WriteIndex(0xCB);  
  244.         Lcd_WriteData(0x39);
  245.         Lcd_WriteData(0x2C);
  246.         Lcd_WriteData(0x00);
  247.         Lcd_WriteData(0x34);
  248.         Lcd_WriteData(0x02);

  249.         Lcd_WriteIndex(0xCF);  
  250.         Lcd_WriteData(0x00);
  251.         Lcd_WriteData(0XC1);
  252.         Lcd_WriteData(0X30);

  253.         Lcd_WriteIndex(0xE8);  
  254.         Lcd_WriteData(0x85);
  255.         Lcd_WriteData(0x00);
  256.         Lcd_WriteData(0x78);

  257.         Lcd_WriteIndex(0xEA);  
  258.         Lcd_WriteData(0x00);
  259.         Lcd_WriteData(0x00);

  260.         Lcd_WriteIndex(0xED);  
  261.         Lcd_WriteData(0x64);
  262.         Lcd_WriteData(0x03);
  263.         Lcd_WriteData(0X12);
  264.         Lcd_WriteData(0X81);

  265.         Lcd_WriteIndex(0xF7);  
  266.         Lcd_WriteData(0x20);

  267.         Lcd_WriteIndex(0xC0);    //Power control
  268.         Lcd_WriteData(0x23);   //VRH[5:0]

  269.         Lcd_WriteIndex(0xC1);    //Power control
  270.         Lcd_WriteData(0x10);   //SAP[2:0];BT[3:0]

  271.         Lcd_WriteIndex(0xC5);    //VCM control
  272.         Lcd_WriteData(0x3e); //对比度调节
  273.         Lcd_WriteData(0x28);

  274.         Lcd_WriteIndex(0xC7);    //VCM control2
  275.         Lcd_WriteData(0x86);  //--

  276.         Lcd_WriteIndex(0x36);    // Memory Access Control
  277. #ifdef USE_HORIZONTAL
  278.         Lcd_WriteData(0xE8); //C8           //48 68竖屏//28 E8 横屏
  279. #else
  280.                 Lcd_WriteData(0x48);
  281. #endif

  282.         Lcd_WriteIndex(0x3A);   
  283.         Lcd_WriteData(0x55);

  284.         Lcd_WriteIndex(0xB1);   
  285.         Lcd_WriteData(0x00);  
  286.         Lcd_WriteData(0x18);

  287.         Lcd_WriteIndex(0xB6);    // Display Function Control
  288.         Lcd_WriteData(0x08);
  289.         Lcd_WriteData(0x82);
  290.         Lcd_WriteData(0x27);  

  291.         Lcd_WriteIndex(0xF2);    // 3Gamma Function Disable
  292.         Lcd_WriteData(0x00);

  293.         Lcd_WriteIndex(0x26);    //Gamma curve selected
  294.         Lcd_WriteData(0x01);

  295.         Lcd_WriteIndex(0xE0);    //Set Gamma
  296.         Lcd_WriteData(0x0F);
  297.         Lcd_WriteData(0x31);
  298.         Lcd_WriteData(0x2B);
  299.         Lcd_WriteData(0x0C);
  300.         Lcd_WriteData(0x0E);
  301.         Lcd_WriteData(0x08);
  302.         Lcd_WriteData(0x4E);
  303.         Lcd_WriteData(0xF1);
  304.         Lcd_WriteData(0x37);
  305.         Lcd_WriteData(0x07);
  306.         Lcd_WriteData(0x10);
  307.         Lcd_WriteData(0x03);
  308.         Lcd_WriteData(0x0E);
  309.         Lcd_WriteData(0x09);
  310.         Lcd_WriteData(0x00);

  311.         Lcd_WriteIndex(0XE1);    //Set Gamma
  312.         Lcd_WriteData(0x00);
  313.         Lcd_WriteData(0x0E);
  314.         Lcd_WriteData(0x14);
  315.         Lcd_WriteData(0x03);
  316.         Lcd_WriteData(0x11);
  317.         Lcd_WriteData(0x07);
  318.         Lcd_WriteData(0x31);
  319.         Lcd_WriteData(0xC1);
  320.         Lcd_WriteData(0x48);
  321.         Lcd_WriteData(0x08);
  322.         Lcd_WriteData(0x0F);
  323.         Lcd_WriteData(0x0C);
  324.         Lcd_WriteData(0x31);
  325.         Lcd_WriteData(0x36);
  326.         Lcd_WriteData(0x0F);

  327.         Lcd_WriteIndex(0x11);    //Exit Sleep
  328.         delay_ms(120);
  329.                                 
  330.         Lcd_WriteIndex(0x29);    //Display on
  331.         Lcd_WriteIndex(0x2c);
  332.         
  333. }



  334. /*************************************************
  335. 函数名:LCD_Set_XY
  336. 功能:设置lcd显示起始点
  337. 入口参数:xy坐标
  338. 返回值:无
  339. *************************************************/
  340. void Lcd_SetXY(u16 Xpos, u16 Ypos)
  341. {        
  342.         Lcd_WriteIndex(0x2a);
  343.         Lcd_WriteData_16Bit(Xpos);
  344.         Lcd_WriteIndex(0x2b);
  345.         Lcd_WriteData_16Bit(Ypos);
  346.         Lcd_WriteIndex(0x2c);        
  347. }
  348. /*************************************************
  349. 函数名:LCD_Set_Region
  350. 功能:设置lcd显示区域,在此区域写点数据自动换行
  351. 入口参数:xy起点和终点
  352. 返回值:无
  353. *************************************************/
  354. //设置显示窗口
  355. void Lcd_SetRegion(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd)
  356. {
  357.         Lcd_WriteIndex(0x2a);
  358.         Lcd_WriteData_16Bit(xStar);
  359.         Lcd_WriteData_16Bit(xEnd);
  360.         Lcd_WriteIndex(0x2b);
  361.         Lcd_WriteData_16Bit(yStar);
  362.         Lcd_WriteData_16Bit(yEnd);
  363.         Lcd_WriteIndex(0x2c);
  364. }

  365.         
  366. /*************************************************
  367. 函数名:LCD_DrawPoint
  368. 功能:画一个点
  369. 入口参数:xy坐标和颜色数据
  370. 返回值:无
  371. *************************************************/
  372. void Gui_DrawPoint(u16 x,u16 y,u16 Data)
  373. {
  374.         Lcd_SetXY(x,y);
  375.         Lcd_WriteData_16Bit(Data);

  376. }   

  377. /*************************************************
  378. 函数名:Lcd_Clear
  379. 功能:全屏清屏函数
  380. 入口参数:填充颜色COLOR
  381. 返回值:无
  382. *************************************************/
  383. void Lcd_Clear(u16 Color)               
  384. {        
  385.    unsigned int i,m;
  386.    Lcd_SetRegion(0,0,X_MAX_PIXEL-1,Y_MAX_PIXEL-1);
  387.    for(i=0;i<X_MAX_PIXEL;i++)
  388.     for(m=0;m<Y_MAX_PIXEL;m++)
  389.     {        
  390.                   Lcd_WriteData_16Bit(Color);
  391.     }   
  392. }



  393. #include "stm32f10x.h"
  394. #include "Lcd_Driver.h"
  395. #include "GUI.h"
  396. #include "delay.h"
  397. #include "font.h"

  398. //从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。
  399. //通过该函数转换
  400. //c:GBR格式的颜色值
  401. //返回值:RGB格式的颜色值
  402. u16 LCD_BGR2RGB(u16 c)
  403. {
  404.   u16  r,g,b,rgb;   
  405.   b=(c>>0)&0x1f;
  406.   g=(c>>5)&0x3f;
  407.   r=(c>>11)&0x1f;         
  408.   rgb=(b<<11)+(g<<5)+(r<<0);                 
  409.   return(rgb);

  410. }




  411. void Gui_Circle(u16 X,u16 Y,u16 R,u16 fc)
  412. {//Bresenham算法
  413.     unsigned short  a,b;
  414.     int c;
  415.     a=0;
  416.     b=R;
  417.     c=3-2*R;
  418.     while (a<b)
  419.     {
  420.         Gui_DrawPoint(X+a,Y+b,fc);     //        7
  421.         Gui_DrawPoint(X-a,Y+b,fc);     //        6
  422.         Gui_DrawPoint(X+a,Y-b,fc);     //        2
  423.         Gui_DrawPoint(X-a,Y-b,fc);     //        3
  424.         Gui_DrawPoint(X+b,Y+a,fc);     //        8
  425.         Gui_DrawPoint(X-b,Y+a,fc);     //        5
  426.         Gui_DrawPoint(X+b,Y-a,fc);     //        1
  427.         Gui_DrawPoint(X-b,Y-a,fc);     //        4

  428.         if(c<0) c=c+4*a+6;
  429.         else
  430.         {
  431.             c=c+4*(a-b)+10;
  432.             b-=1;
  433.         }
  434.        a+=1;
  435.     }
  436.     if (a==b)
  437.     {
  438.         Gui_DrawPoint(X+a,Y+b,fc);
  439.         Gui_DrawPoint(X+a,Y+b,fc);
  440.         Gui_DrawPoint(X+a,Y-b,fc);
  441.         Gui_DrawPoint(X-a,Y-b,fc);
  442.         Gui_DrawPoint(X+b,Y+a,fc);
  443.         Gui_DrawPoint(X-b,Y+a,fc);
  444.         Gui_DrawPoint(X+b,Y-a,fc);
  445.         Gui_DrawPoint(X-b,Y-a,fc);
  446.     }
  447.         
  448. }
  449. //画线函数,使用Bresenham 画线算法
  450. void Gui_DrawLine(u16 x0, u16 y0,u16 x1, u16 y1,u16 Color)   
  451. {
  452. int dx,             // difference in x's
  453.     dy,             // difference in y's
  454.     dx2,            // dx,dy * 2
  455.     dy2,
  456.     x_inc,          // amount in pixel space to move during drawing
  457.     y_inc,          // amount in pixel space to move during drawing
  458.     error,          // the discriminant i.e. error i.e. decision variable
  459.     index;          // used for looping        


  460.         Lcd_SetXY(x0,y0);
  461.         dx = x1-x0;//计算x距离
  462.         dy = y1-y0;//计算y距离

  463.         if (dx>=0)
  464.         {
  465.                 x_inc = 1;
  466.         }
  467.         else
  468.         {
  469.                 x_inc = -1;
  470.                 dx    = -dx;  
  471.         }
  472.         
  473.         if (dy>=0)
  474.         {
  475.                 y_inc = 1;
  476.         }
  477.         else
  478.         {
  479.                 y_inc = -1;
  480.                 dy    = -dy;
  481.         }

  482.         dx2 = dx << 1;
  483.         dy2 = dy << 1;

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

  488.                 // draw the line
  489.                 for (index=0; index <= dx; index++)//要画的点数不会超过x距离
  490.                 {
  491.                         //画点
  492.                         Gui_DrawPoint(x0,y0,Color);
  493.                         
  494.                         // test if error has overflowed
  495.                         if (error >= 0) //是否需要增加y坐标值
  496.                         {
  497.                                 error-=dx2;

  498.                                 // move to next line
  499.                                 y0+=y_inc;//增加y坐标值
  500.                         } // end if error overflowed

  501.                         // adjust the error term
  502.                         error+=dy2;

  503.                         // move to the next pixel
  504.                         x0+=x_inc;//x坐标值每次画点后都递增1
  505.                 } // end for
  506.         } // end if |slope| <= 1
  507.         else//y轴大于x轴,则每个y轴上只有一个点,x轴若干个点
  508.         {//以y轴为递增画点
  509.                 // initialize error term
  510.                 error = dx2 - dy;

  511.                 // draw the line
  512.                 for (index=0; index <= dy; index++)
  513.                 {
  514.                         // set the pixel
  515.                         Gui_DrawPoint(x0,y0,Color);

  516.                         // test if error overflowed
  517.                         if (error >= 0)
  518.                         {
  519.                                 error-=dy2;

  520.                                 // move to next line
  521.                                 x0+=x_inc;
  522.                         } // end if error overflowed

  523.                         // adjust the error term
  524.                         error+=dx2;

  525.                         // move to the next pixel
  526.                         y0+=y_inc;
  527.                 } // end for
  528.         } // end else |slope| > 1
  529. }



  530. void Gui_box(u16 x, u16 y, u16 w, u16 h,u16 bc)
  531. {
  532.         Gui_DrawLine(x,y,x+w,y,0xEF7D);
  533.         Gui_DrawLine(x+w-1,y+1,x+w-1,y+1+h,0x2965);
  534.         Gui_DrawLine(x,y+h,x+w,y+h,0x2965);
  535.         Gui_DrawLine(x,y,x,y+h,0xEF7D);
  536.     Gui_DrawLine(x+1,y+1,x+1+w-2,y+1+h-2,bc);
  537. }
  538. void Gui_box2(u16 x,u16 y,u16 w,u16 h, u8 mode)
  539. {
  540.         if (mode==0)        {
  541.                 Gui_DrawLine(x,y,x+w,y,0xEF7D);
  542.                 Gui_DrawLine(x+w-1,y+1,x+w-1,y+1+h,0x2965);
  543.                 Gui_DrawLine(x,y+h,x+w,y+h,0x2965);
  544.                 Gui_DrawLine(x,y,x,y+h,0xEF7D);
  545.                 }
  546.         if (mode==1)        {
  547.                 Gui_DrawLine(x,y,x+w,y,0x2965);
  548.                 Gui_DrawLine(x+w-1,y+1,x+w-1,y+1+h,0xEF7D);
  549.                 Gui_DrawLine(x,y+h,x+w,y+h,0xEF7D);
  550.                 Gui_DrawLine(x,y,x,y+h,0x2965);
  551.         }
  552.         if (mode==2)        {
  553.                 Gui_DrawLine(x,y,x+w,y,0xffff);
  554.                 Gui_DrawLine(x+w-1,y+1,x+w-1,y+1+h,0xffff);
  555.                 Gui_DrawLine(x,y+h,x+w,y+h,0xffff);
  556.                 Gui_DrawLine(x,y,x,y+h,0xffff);
  557.         }
  558. }


  559. /**************************************************************************************
  560. 功能描述: 在屏幕显示一凸起的按钮框
  561. 输    入: u16 x1,y1,x2,y2 按钮框左上角和右下角坐标
  562. 输    出: 无
  563. **************************************************************************************/
  564. void DisplayButtonDown(u16 x1,u16 y1,u16 x2,u16 y2)
  565. {
  566.         Gui_DrawLine(x1,  y1,  x2,y1, GRAY2);  //H
  567.         Gui_DrawLine(x1+1,y1+1,x2,y1+1, GRAY1);  //H
  568.         Gui_DrawLine(x1,  y1,  x1,y2, GRAY2);  //V
  569.         Gui_DrawLine(x1+1,y1+1,x1+1,y2, GRAY1);  //V
  570.         Gui_DrawLine(x1,  y2,  x2,y2, WHITE);  //H
  571.         Gui_DrawLine(x2,  y1,  x2,y2, WHITE);  //V
  572. }

  573. /**************************************************************************************
  574. 功能描述: 在屏幕显示一凹下的按钮框
  575. 输    入: u16 x1,y1,x2,y2 按钮框左上角和右下角坐标
  576. 输    出: 无
  577. **************************************************************************************/
  578. void DisplayButtonUp(u16 x1,u16 y1,u16 x2,u16 y2)
  579. {
  580.         Gui_DrawLine(x1,  y1,  x2,y1, WHITE); //H
  581.         Gui_DrawLine(x1,  y1,  x1,y2, WHITE); //V
  582.         
  583.         Gui_DrawLine(x1+1,y2-1,x2,y2-1, GRAY1);  //H
  584.         Gui_DrawLine(x1,  y2,  x2,y2, GRAY2);  //H
  585.         Gui_DrawLine(x2-1,y1+1,x2-1,y2, GRAY1);  //V
  586.     Gui_DrawLine(x2  ,y1  ,x2,y2, GRAY2); //V
  587. }


  588. void Gui_DrawFont_GBK16(u16 x, u16 y, u16 fc, u16 bc, u8 *s)
  589. {
  590.         unsigned char i,j;
  591.         unsigned short k,x0;
  592.         x0=x;

  593.         while(*s)
  594.         {        
  595.                 if((*s) < 128)
  596.                 {
  597.                         k=*s;
  598.                         if (k==13)
  599.                         {
  600.                                 x=x0;
  601.                                 y+=16;
  602.                         }
  603.                         else
  604.                         {
  605.                                 if (k>32) k-=32; else k=0;
  606.         
  607.                             for(i=0;i<16;i++)
  608.                                 for(j=0;j<8;j++)
  609.                                         {
  610.                                             if(asc16[k*16+i]&(0x80>>j))        Gui_DrawPoint(x+j,y+i,fc);
  611.                                                 else
  612.                                                 {
  613.                                                         if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);
  614.                                                 }
  615.                                         }
  616.                                 x+=8;
  617.                         }
  618.                         s++;
  619.                 }
  620.                         
  621.                 else
  622.                 {
  623.                

  624.                         for (k=0;k<hz16_num;k++)
  625.                         {
  626.                           if ((hz16[k].Index[0]==*(s))&&(hz16[k].Index[1]==*(s+1)))
  627.                           {
  628.                                     for(i=0;i<16;i++)
  629.                                     {
  630.                                                 for(j=0;j<8;j++)
  631.                                                         {
  632.                                                             if(hz16[k].Msk[i*2]&(0x80>>j))        Gui_DrawPoint(x+j,y+i,fc);
  633.                                                                 else {
  634.                                                                         if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);
  635.                                                                 }
  636.                                                         }
  637.                                                 for(j=0;j<8;j++)
  638.                                                         {
  639.                                                             if(hz16[k].Msk[i*2+1]&(0x80>>j))        Gui_DrawPoint(x+j+8,y+i,fc);
  640.                                                                 else
  641.                                                                 {
  642.                                                                         if (fc!=bc) Gui_DrawPoint(x+j+8,y+i,bc);
  643.                                                                 }
  644.                                                         }
  645.                                     }
  646.                                 }
  647.                           }
  648.                         s+=2;x+=16;
  649.                 }
  650.                
  651.         }
  652. }

  653. void Gui_DrawFont_GBK24(u16 x, u16 y, u16 fc, u16 bc, u8 *s)
  654. {
  655.         unsigned char i,j;
  656.         unsigned short k;

  657.         while(*s)
  658.         {
  659.                 if( *s < 0x80 )
  660.                 {
  661.                         k=*s;
  662.                         if (k>32) k-=32; else k=0;

  663.                     for(i=0;i<16;i++)
  664.                         for(j=0;j<8;j++)
  665.                                 {
  666.                                     if(asc16[k*16+i]&(0x80>>j))        
  667.                                         Gui_DrawPoint(x+j,y+i,fc);
  668.                                         else
  669.                                         {
  670.                                                 if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);
  671.                                         }
  672.                                 }
  673.                         s++;x+=8;
  674.                 }
  675.                 else
  676.                 {

  677.                         for (k=0;k<hz24_num;k++)
  678.                         {
  679.                           if ((hz24[k].Index[0]==*(s))&&(hz24[k].Index[1]==*(s+1)))
  680.                           {
  681.                                     for(i=0;i<24;i++)
  682.                                     {
  683.                                                 for(j=0;j<8;j++)
  684.                                                         {
  685.                                                             if(hz24[k].Msk[i*3]&(0x80>>j))
  686.                                                                 Gui_DrawPoint(x+j,y+i,fc);
  687.                                                                 else
  688.                                                                 {
  689.                                                                         if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);
  690.                                                                 }
  691.                                                         }
  692.                                                 for(j=0;j<8;j++)
  693.                                                         {
  694.                                                             if(hz24[k].Msk[i*3+1]&(0x80>>j))        Gui_DrawPoint(x+j+8,y+i,fc);
  695.                                                                 else {
  696.                                                                         if (fc!=bc) Gui_DrawPoint(x+j+8,y+i,bc);
  697.                                                                 }
  698.                                                         }
  699.                                                 for(j=0;j<8;j++)
  700.                                                         {
  701.                                                             if(hz24[k].Msk[i*3+2]&(0x80>>j))        
  702.                                                                 Gui_DrawPoint(x+j+16,y+i,fc);
  703.                                                                 else
  704.                                                                 {
  705.                                                                         if (fc!=bc) Gui_DrawPoint(x+j+16,y+i,bc);
  706.                                                                 }
  707.                                                         }
  708.                                     }
  709.                           }
  710.                         }
  711.                         s+=2;x+=24;
  712.                 }
  713.         }
  714. }
  715. void Gui_DrawFont_Num32(u16 x, u16 y, u16 fc, u16 bc, u16 num)
  716. {
  717.         unsigned char i,j,k,c;
  718.         //lcd_text_any(x+94+i*42,y+34,32,32,0x7E8,0x0,sz32,knum[i]);
  719. //        w=w/8;

  720.     for(i=0;i<32;i++)
  721.         {
  722.                 for(j=0;j<4;j++)
  723.                 {
  724.                         c=*(sz32+num*32*4+i*4+j);
  725.                         for (k=0;k<8;k++)        
  726.                         {
  727.         
  728.                             if(c&(0x80>>k))        Gui_DrawPoint(x+j*8+k,y+i,fc);
  729.                                 else {
  730.                                         if (fc!=bc) Gui_DrawPoint(x+j*8+k,y+i,bc);
  731.                                 }
  732.                         }
  733.                 }
  734.         }
  735. }

  736. /* --------------------------Includes ---------------------------------------------*/
  737. #include "stm32f10x.h"
  738. #include "Lcd_Driver.h"
  739. #include "GUI.h"
  740. #include "delay.h"
  741. #include "Picture.h"
  742. #include "QDTFT_demo.h"
  743. /* ----------------------End of Includes ---------------------------------------------*/

  744. unsigned char Num[10]={0,1,2,3,4,5,6,7,8,9};
  745. //绘制测试菜单
  746. //2D按键按钮示例
  747. void Redraw_Mainmenu(void)
  748. {

  749.         Lcd_Clear(GRAY0);
  750.         
  751.         Gui_DrawFont_GBK16(16,2,BLUE,GRAY0,"全动电子技术");
  752.         Gui_DrawFont_GBK16(16,20,RED,GRAY0,"液晶测试程序");

  753.         DisplayButtonUp(15,38,113,58); //x1,y1,x2,y2
  754.         Gui_DrawFont_GBK16(16,40,GREEN,GRAY0,"颜色填充测试");

  755.         DisplayButtonUp(15,68,113,88); //x1,y1,x2,y2
  756.         Gui_DrawFont_GBK16(16,70,BLUE,GRAY0,"文字显示测试");

  757.         DisplayButtonUp(15,98,113,118); //x1,y1,x2,y2
  758.         Gui_DrawFont_GBK16(16,100,RED,GRAY0,"图片显示测试");


  759.         //Gui_DrawFont_GBK16(16,120,BLUE,GRAY0,"Welcome");
  760.         Gui_DrawFont_GBK16(16,140,RED,GRAY0, "Welcome");
  761.         
  762.         Gui_DrawFont_Num32(100,125,RED,GRAY0,Num[5]);
  763.         delay_ms(1000);
  764.         Gui_DrawFont_Num32(100,125,RED,GRAY0,Num[4]);
  765.         delay_ms(1000);
  766.         Gui_DrawFont_Num32(100,125,RED,GRAY0,Num[3]);
  767.         delay_ms(1000);
  768.         Gui_DrawFont_Num32(100,125,RED,GRAY0,Num[2]);
  769.         delay_ms(1000);
  770.         Gui_DrawFont_Num32(100,125,RED,GRAY0,Num[1]);
  771.         delay_ms(1000);
  772.         Gui_DrawFont_Num32(100,125,RED,GRAY0,Num[0]);        
  773. }
  774. //测试数码管字体
  775. void Num_Test(void)
  776. {
  777.         u8 i=0;
  778.         Lcd_Clear(GRAY0);
  779.         Gui_DrawFont_GBK16(16,20,RED,GRAY0,"Num Test");
  780.         delay_ms(1000);
  781.         Lcd_Clear(GRAY0);

  782.         for(i=0;i<10;i++)
  783.         {
  784.         Gui_DrawFont_Num32((i%3)*40,32*(i/3)+30,RED,GRAY0,Num[i+1]);
  785.         delay_ms(100);
  786.         }
  787.         
  788. }
  789. //中英文显示测试
  790. void Font_Test(void)
  791. {
  792.         Lcd_Clear(GRAY0);
  793.         Gui_DrawFont_GBK16(16,10,BLUE,GRAY0,"文字显示测试");

  794.         delay_ms(1000);
  795.         Lcd_Clear(GRAY0);
  796.         Gui_DrawFont_GBK16(16,30,RED,GRAY0,"全动电子技术");
  797.         Gui_DrawFont_GBK16(16,50,BLUE,GRAY0,"专注液晶批发");
  798.         Gui_DrawFont_GBK16(16,70,RED,GRAY0, "全程技术支持");
  799.         Gui_DrawFont_GBK16(0,100,BLUE,GRAY0,"Tel:15989313508");
  800.         Gui_DrawFont_GBK16(0,130,RED,GRAY0, "www*qdtech*net");        
  801.         delay_ms(1500);        
  802. }
  803. //简单刷屏测试
  804. void Color_Test(void)
  805. {
  806.         u8 i=1;
  807.         Lcd_Clear(GRAY0);
  808.         
  809.         Gui_DrawFont_GBK16(20,10,BLUE,GRAY0,"颜色填充测试");
  810.         delay_ms(1200);

  811.         while(i--)
  812.         {
  813.                 Lcd_Clear(WHITE); delay_ms(500);
  814.                 Lcd_Clear(BLACK); delay_ms(500);
  815.                 Lcd_Clear(RED);          delay_ms(500);
  816.                   Lcd_Clear(GREEN); delay_ms(500);
  817.                   Lcd_Clear(BLUE);  delay_ms(500);
  818.         }               
  819. }

  820. //文字显示测试
  821. //16位BMP 40X40 QQ图像取模数据
  822. //Image2LCD取模选项设置
  823. //水平扫描
  824. //16位
  825. //40X40
  826. //不包含图像头数据
  827. //自左至右
  828. //自顶至底
  829. //低位在前
  830. void showimage(const unsigned char *p) //显示40*40 QQ图片
  831. {
  832.           int i,j,k;
  833.         unsigned char picH,picL;
  834.         Lcd_Clear(GRAY0);
  835.         Gui_DrawFont_GBK16(16,10,BLUE,GRAY0,"图片显示测试");
  836.         delay_ms(1000);

  837.         Lcd_Clear(GRAY0);
  838.         for(k=0;k<6;k++)
  839.         {
  840.                    for(j=0;j<8;j++)
  841.                 {        
  842.                         Lcd_SetRegion(40*j,40*k,40*j+39,40*k+39);                //坐标设置
  843.                     for(i=0;i<40*40;i++)
  844.                          {        
  845.                                  picL=*(p+i*2);        //数据低位在前
  846.                                 picH=*(p+i*2+1);                                
  847.                                 Lcd_WriteData_16Bit(picH<<8|picL);                                                  
  848.                          }        
  849.                  }
  850.         }               
  851. }
  852. //综合测试函数
  853. void QDTFT_Test_Demo(void)
  854. {
  855.         Lcd_Init();
  856.         LCD_LED_SET;//通过IO控制背光亮                                
  857.         Redraw_Mainmenu();//绘制主菜单(部分内容由于分辨率超出物理值可能无法显示)
  858.         Color_Test();//简单纯色填充测试
  859.         Num_Test();//数码管字体测试
  860.         Font_Test();//中英文显示测试               
  861.         showimage(gImage_qq);//图片显示示例
  862.         delay_ms(1500);
  863.         LCD_LED_CLR;//IO控制背光灭        
  864. }


  865. //////////////////////////////////////////////////////////////////////////////////         
  866. //本程序只供学习使用,未经作者许可,不得用于其它任何用途
  867. //测试硬件:正点原子战舰 STM32开发板/mini开发板
  868. //1.8寸SPI串口TFT液晶驱动
  869. //xiao冯@ShenZhen QDtech co.,LTD
  870. //我司提供技术支持,任何技术问题欢迎随时交流学习
  871. //固话(传真) :+86 0755-23594567
  872. //手机:15989313508(冯工)
  873. //邮箱:QDtech2008@gmail.com
  874. //Skype:QDtech2008
  875. //创建日期:2013/5/13
  876. //版本:V1.1
  877. //版权所有,盗版必究。
  878. //Copyright(C) 深圳市全动电子技术有限公司 2009-2019
  879. //All rights reserved
  880. //////////////////////////////////////////////////////////////////////////////////        
  881. /***************************************************************************************
  882. STM32测试平台介绍:
  883. 开发板:正点原子MiniSTM32开发板
  884. MCU :STM32_F103_RBT6
  885. 晶振 :12MHZ
  886. 主频 :72MHZ
  887. 接线说明:
  888. //-------------------------------------------------------------------------------------
  889. #define LCD_CTRL                     GPIOB                //定义TFT数据端口
  890. #define LCD_LED                GPIO_Pin_9  //PB9 连接至TFT -LED
  891. #define LCD_RS                 GPIO_Pin_10        //PB10连接至TFT --RS
  892. #define LCD_CS                GPIO_Pin_11 //PB11 连接至TFT --CS
  893. #define LCD_RST             GPIO_Pin_12        //PB12连接至TFT --RST
  894. #define LCD_SCL                GPIO_Pin_13        //PB13连接至TFT -- CLK
  895. #define LCD_SDA                GPIO_Pin_15        //PB15连接至TFT - SDI
  896. //VCC:可以接5V也可以接3.3V
  897. //LED:可以接5V也可以接3.3V或者使用任意空闲IO控制(高电平使能)
  898. //GND:接电源地
  899. //说明:如需要尽可能少占用IO,可以将LCD_CS接地,LCD_LED接3.3V,LCD_RST接至单片机a复位端,
  900. //将可以释放3个可用IO
  901. //接口定义在Lcd_Driver.h内定义,
  902. //如需变更IO接法,请根据您的实际接线修改相应IO初始化LCD_GPIO_Init()
  903. //-----------------------------------------------------------------------------------------
  904. 例程功能说明:
  905. 1.        简单刷屏测试
  906. 2.        英文显示测试示例
  907. 3.        中文显示测试示例
  908. 4.        数码管字体显示示例
  909. 5.        图片显示示例
  910. 6.        2D按键菜单示例
  911. 7.        本例程支持横屏/竖屏切换(开启宏USE_HORIZONTAL,详见Lcd_Driver.h)
  912. 8.        本例程支持软件模拟SPI/硬件SPI切换(开启宏USE_HARDWARE_SPI,详见Lcd_Driver.h)
  913. **********************************************************************************************/
  914. /* Includes ------------------------------------------------------------------*/
  915. #include "stm32f10x.h"
  916. #include "delay.h"
  917. #include "QDTFT_demo.h"


  918. int main(void)
  919. {

  920.   SystemInit();        //System init.
  921.   delay_init(72);//Delay init.

  922.   while(1)
  923.   {  
  924.         QDTFT_Test_Demo();        //See the test details in QDTFT_Demo.c               
  925.   }

  926. }
  927. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:189966 发表于 2019-8-14 14:05 | 只看该作者
你好,你用的什么型号的屏幕,
回复

使用道具 举报

板凳
ID:105596 发表于 2019-9-30 08:15 | 只看该作者
请问一下你有完整的代码包吗?麻烦发一下wumingboa@126.com,谢谢
回复

使用道具 举报

地板
ID:671136 发表于 2019-12-28 18:47 | 只看该作者
请问有完整的代码包么  415543934@qq.com  可以发一下么
回复

使用道具 举报

5#
ID:739683 发表于 2020-4-29 18:46 | 只看该作者
完整代码方便发一下吗
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表