stm32100b_eval_lcd.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32100b_eval_lcd.c
00004   * @author  MCD Application Team
00005   * @version V4.3.0
00006   * @date    10/15/2010
00007   * @brief   This file includes the LCD driver for AM-240320LTNQW00H (LCD_HX8312),
00008   *          AM-240320L8TNQW00H (LCD_ILI9320), AM-240320LDTNQW00H (LCD_SPFD5408B)
00009   *          Liquid Crystal Display Module of STM32100B-EVAL board.
00010   ******************************************************************************
00011   * @copy
00012   *
00013   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00014   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00015   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00016   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00017   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00018   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00019   *
00020   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00021   */ 
00022 
00023 /* Includes ------------------------------------------------------------------*/
00024 #include "stm32100b_eval_lcd.h"
00025 #include "../Common/fonts.c"
00026 
00027 /** @addtogroup Utilities
00028   * @{
00029   */
00030 
00031 /** @addtogroup STM32_EVAL
00032   * @{
00033   */ 
00034 
00035 /** @addtogroup STM32100B_EVAL
00036   * @{
00037   */
00038     
00039 /** @defgroup STM32100B_EVAL_LCD 
00040   * @brief   This file includes the LCD driver for AM-240320LTNQW00H (LCD_HX8312),
00041   *          AM-240320L8TNQW00H (LCD_ILI9320), AM-240320LDTNQW00H (LCD_SPFD5408B)
00042   *          Liquid Crystal Display Module of STM32100B-EVAL board.
00043   * @{
00044   */ 
00045 
00046 /** @defgroup STM32100B_EVAL_LCD_Private_Types
00047   * @{
00048   */ 
00049 /**
00050   * @}
00051   */ 
00052 
00053 /** @defgroup STM32100B_EVAL_LCD_Private_Defines
00054   * @{
00055   */ 
00056 #define LCD_ILI9320        0x9320
00057 #define LCD_HX8312         0x8312
00058 #define LCD_SPFD5408       0x5408
00059 #define START_BYTE         0x70
00060 #define SET_INDEX          0x00
00061 #define READ_STATUS        0x01
00062 #define LCD_WRITE_REG      0x02
00063 #define LCD_READ_REG       0x03
00064 #define MAX_POLY_CORNERS   200
00065 #define POLY_Y(Z)          ((int32_t)((Points + Z)->X))
00066 #define POLY_X(Z)          ((int32_t)((Points + Z)->Y))
00067 /**
00068   * @}
00069   */ 
00070 
00071 /** @defgroup STM32100B_EVAL_LCD_Private_Macros
00072   * @{
00073   */ 
00074 #define ABS(X)  ((X) > 0 ? (X) : -(X))  
00075 /**
00076   * @}
00077   */ 
00078 
00079 /** @defgroup STM32100B_EVAL_LCD_Private_Variables
00080   * @{
00081   */ 
00082 static sFONT *LCD_Currentfonts;
00083 /* Global variables to set the written text color */
00084 static __IO uint16_t TextColor = 0x0000, BackColor = 0xFFFF;
00085 static __IO uint32_t LCDType = LCD_ILI9320;
00086 /**
00087   * @}
00088   */ 
00089 
00090 /** @defgroup STM32100B_EVAL_LCD_Private_Function_Prototypes
00091   * @{
00092   */ 
00093 #ifndef USE_Delay
00094 static void delay(vu32 nCount);
00095 #endif /* USE_Delay*/
00096 
00097 static void LCD_WriteRegHX8312(uint8_t LCD_Reg, uint8_t LCD_RegValue);
00098 static void LCD_WriteRegILI9320(uint8_t LCD_Reg, uint16_t LCD_RegValue);
00099 static void PutPixel(int16_t x, int16_t y);
00100 static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed);
00101 
00102 /**
00103   * @}
00104   */ 
00105 
00106 /** @defgroup STM32100B_EVAL_LCD_Private_Functions
00107   * @{
00108   */ 
00109 
00110 /**
00111   * @brief  DeInitializes the LCD.
00112   * @param  None
00113   * @retval None
00114   */
00115 void LCD_DeInit(void)
00116 {
00117   GPIO_InitTypeDef GPIO_InitStructure;
00118 
00119   /*!< LCD Display Off */
00120   LCD_DisplayOff();
00121 
00122   /*!< LCD_SPI disable */
00123   SPI_Cmd(LCD_SPI, DISABLE);
00124   
00125   /*!< LCD_SPI DeInit */
00126   SPI_I2S_DeInit(LCD_SPI);
00127    
00128   /*!< Disable SPI clock  */
00129   RCC_APB1PeriphClockCmd(LCD_SPI_CLK, DISABLE);
00130     
00131   /* Configure NCS in Output Push-Pull mode */
00132   GPIO_InitStructure.GPIO_Pin = LCD_NCS_PIN;
00133   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00134   GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
00135 
00136   /* Configure NWR(RNW), RS in Output Push-Pull mode */
00137   GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
00138   GPIO_Init(LCD_RS_GPIO_PORT, &GPIO_InitStructure);
00139 
00140   GPIO_InitStructure.GPIO_Pin = LCD_NWR_PIN;
00141   GPIO_Init(LCD_NWR_GPIO_PORT, &GPIO_InitStructure);  
00142    
00143   /* Configure SPI pins: SCK, MISO and MOSI */
00144   GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
00145   GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
00146 
00147   GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
00148   GPIO_Init(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
00149   
00150   GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
00151   GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
00152 }
00153 
00154 /**
00155   * @brief  Setups the LCD.
00156   * @param  None
00157   * @retval None
00158   */
00159 void LCD_Setup(void)
00160 { 
00161 /* Configure the LCD Control pins --------------------------------------------*/
00162   LCD_CtrlLinesConfig();
00163   
00164 /* Configure the LCD_SPI interface ----------------------------------------------*/
00165   LCD_SPIConfig();
00166   
00167   if(LCDType == LCD_ILI9320)
00168   {
00169     _delay_(5); /* Delay 50 ms */
00170     /* Start Initial Sequence ------------------------------------------------*/
00171     LCD_WriteReg(LCD_REG_229, 0x8000); /* Set the internal vcore voltage */
00172     LCD_WriteReg(LCD_REG_0,  0x0001); /* Start internal OSC. */
00173     LCD_WriteReg(LCD_REG_1,  0x0100); /* set SS and SM bit */
00174     LCD_WriteReg(LCD_REG_2,  0x0700); /* set 1 line inversion */
00175     LCD_WriteReg(LCD_REG_3,  0x1030); /* set GRAM write direction and BGR=1. */
00176     LCD_WriteReg(LCD_REG_4,  0x0000); /* Resize register */
00177     LCD_WriteReg(LCD_REG_8,  0x0202); /* set the back porch and front porch */
00178     LCD_WriteReg(LCD_REG_9,  0x0000); /* set non-display area refresh cycle ISC[3:0] */
00179     LCD_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
00180     LCD_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
00181     LCD_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
00182     LCD_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
00183     /* Power On sequence -----------------------------------------------------*/
00184     LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
00185     LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
00186     LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
00187     LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
00188     _delay_(20);                      /* Dis-charge capacitor power voltage (200ms) */
00189     LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
00190     LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
00191     _delay_(5);                       /* Delay 50 ms */
00192     LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
00193     _delay_(5);                       /* Delay 50 ms */
00194     LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
00195     LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
00196     _delay_(5);                       /* Delay 50 ms */
00197     LCD_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
00198     LCD_WriteReg(LCD_REG_33, 0x0000); /* GRAM Vertical Address */
00199     /* Adjust the Gamma Curve ------------------------------------------------*/
00200     LCD_WriteReg(LCD_REG_48, 0x0006);
00201     LCD_WriteReg(LCD_REG_49, 0x0101);
00202     LCD_WriteReg(LCD_REG_50, 0x0003);
00203     LCD_WriteReg(LCD_REG_53, 0x0106);
00204     LCD_WriteReg(LCD_REG_54, 0x0b02);
00205     LCD_WriteReg(LCD_REG_55, 0x0302);
00206     LCD_WriteReg(LCD_REG_56, 0x0707);
00207     LCD_WriteReg(LCD_REG_57, 0x0007);
00208     LCD_WriteReg(LCD_REG_60, 0x0600);
00209     LCD_WriteReg(LCD_REG_61, 0x020b);
00210   
00211     /* Set GRAM area ---------------------------------------------------------*/
00212     LCD_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
00213     LCD_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
00214     LCD_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
00215     LCD_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
00216     LCD_WriteReg(LCD_REG_96,  0x2700); /* Gate Scan Line */
00217     LCD_WriteReg(LCD_REG_97,  0x0001); /* NDL,VLE, REV */
00218     LCD_WriteReg(LCD_REG_106, 0x0000); /* set scrolling line */
00219     /* Partial Display Control -----------------------------------------------*/
00220     LCD_WriteReg(LCD_REG_128, 0x0000);
00221     LCD_WriteReg(LCD_REG_129, 0x0000);
00222     LCD_WriteReg(LCD_REG_130, 0x0000);
00223     LCD_WriteReg(LCD_REG_131, 0x0000);
00224     LCD_WriteReg(LCD_REG_132, 0x0000);
00225     LCD_WriteReg(LCD_REG_133, 0x0000);
00226     /* Panel Control ---------------------------------------------------------*/
00227     LCD_WriteReg(LCD_REG_144, 0x0010);
00228     LCD_WriteReg(LCD_REG_146, 0x0000);
00229     LCD_WriteReg(LCD_REG_147, 0x0003);
00230     LCD_WriteReg(LCD_REG_149, 0x0110);
00231     LCD_WriteReg(LCD_REG_151, 0x0000);
00232     LCD_WriteReg(LCD_REG_152, 0x0000);
00233     /* Set GRAM write direction and BGR = 1 */
00234     /* I/D=01 (Horizontal : increment, Vertical : decrement) */
00235     /* AM=1 (address is updated in vertical writing direction) */
00236     LCD_WriteReg(LCD_REG_3, 0x1018);
00237     LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
00238   }
00239   else if(LCDType == LCD_SPFD5408)
00240   {   
00241     /* Start Initial Sequence --------------------------------------------------*/
00242     LCD_WriteReg(LCD_REG_227, 0x3008); /* Set internal timing */
00243     LCD_WriteReg(LCD_REG_231, 0x0012); /* Set internal timing */
00244     LCD_WriteReg(LCD_REG_239, 0x1231); /* Set internal timing */
00245     LCD_WriteReg(LCD_REG_1, 0x0100);   /* Set SS and SM bit */
00246     LCD_WriteReg(LCD_REG_2, 0x0700);   /* Set 1 line inversion */
00247     LCD_WriteReg(LCD_REG_3, 0x1030);   /* Set GRAM write direction and BGR=1. */
00248     LCD_WriteReg(LCD_REG_4, 0x0000);   /* Resize register */
00249     LCD_WriteReg(LCD_REG_8, 0x0202);   /* Set the back porch and front porch */
00250     LCD_WriteReg(LCD_REG_9, 0x0000);   /* Set non-display area refresh cycle ISC[3:0] */
00251     LCD_WriteReg(LCD_REG_10, 0x0000);  /* FMARK function */
00252     LCD_WriteReg(LCD_REG_12, 0x0000);  /* RGB interface setting */
00253     LCD_WriteReg(LCD_REG_13, 0x0000);  /* Frame marker Position */
00254     LCD_WriteReg(LCD_REG_15, 0x0000);  /* RGB interface polarity */
00255     /* Power On sequence -------------------------------------------------------*/
00256     LCD_WriteReg(LCD_REG_16, 0x0000);  /* SAP, BT[3:0], AP, DSTB, SLP, STB */
00257     LCD_WriteReg(LCD_REG_17, 0x0000);  /* DC1[2:0], DC0[2:0], VC[2:0] */
00258     LCD_WriteReg(LCD_REG_18, 0x0000);  /* VREG1OUT voltage */
00259     LCD_WriteReg(LCD_REG_19, 0x0000);  /* VDV[4:0] for VCOM amplitude */
00260     _delay_(20);                /* Dis-charge capacitor power voltage (200ms) */  
00261     LCD_WriteReg(LCD_REG_17, 0x0007);  /* DC1[2:0], DC0[2:0], VC[2:0] */
00262     _delay_(5);                 /* Delay 50 ms */
00263     LCD_WriteReg(LCD_REG_16, 0x12B0);  /* SAP, BT[3:0], AP, DSTB, SLP, STB */
00264     _delay_(5);                  /* Delay 50 ms */
00265     LCD_WriteReg(LCD_REG_18, 0x01BD);  /* External reference voltage= Vci */
00266     _delay_(5);                 /* Delay 50 ms */ 
00267     LCD_WriteReg(LCD_REG_19, 0x1400);       /* VDV[4:0] for VCOM amplitude */
00268     LCD_WriteReg(LCD_REG_41, 0x000E);  /* VCM[4:0] for VCOMH */
00269     _delay_(5);                 /* Delay 50 ms */
00270     LCD_WriteReg(LCD_REG_32, 0x0000);  /* GRAM horizontal Address */
00271     LCD_WriteReg(LCD_REG_33, 0x013F);  /* GRAM Vertical Address */
00272     /* Adjust the Gamma Curve --------------------------------------------------*/
00273     LCD_WriteReg(LCD_REG_48, 0x0007);
00274     LCD_WriteReg(LCD_REG_49, 0x0302);
00275     LCD_WriteReg(LCD_REG_50, 0x0105);
00276     LCD_WriteReg(LCD_REG_53, 0x0206);
00277     LCD_WriteReg(LCD_REG_54, 0x0808);
00278     LCD_WriteReg(LCD_REG_55, 0x0206);
00279     LCD_WriteReg(LCD_REG_56, 0x0504);
00280     LCD_WriteReg(LCD_REG_57, 0x0007);
00281     LCD_WriteReg(LCD_REG_60, 0x0105);
00282     LCD_WriteReg(LCD_REG_61, 0x0808);
00283     /* Set GRAM area -----------------------------------------------------------*/
00284     LCD_WriteReg(LCD_REG_80, 0x0000);  /* Horizontal GRAM Start Address */
00285     LCD_WriteReg(LCD_REG_81, 0x00EF);  /* Horizontal GRAM End Address */
00286     LCD_WriteReg(LCD_REG_82, 0x0000);  /* Vertical GRAM Start Address */
00287     LCD_WriteReg(LCD_REG_83, 0x013F);  /* Vertical GRAM End Address */
00288     LCD_WriteReg(LCD_REG_96,  0xA700); /* Gate Scan Line */
00289     LCD_WriteReg(LCD_REG_97,  0x0001); /* NDL,VLE, REV */
00290     LCD_WriteReg(LCD_REG_106, 0x0000); /* Set scrolling line */
00291     /* Partial Display Control -------------------------------------------------*/
00292     LCD_WriteReg(LCD_REG_128, 0x0000);
00293     LCD_WriteReg(LCD_REG_129, 0x0000);
00294     LCD_WriteReg(LCD_REG_130, 0x0000);
00295     LCD_WriteReg(LCD_REG_131, 0x0000);
00296     LCD_WriteReg(LCD_REG_132, 0x0000);
00297     LCD_WriteReg(LCD_REG_133, 0x0000);
00298     /* Panel Control -----------------------------------------------------------*/
00299     LCD_WriteReg(LCD_REG_144, 0x0010);
00300     LCD_WriteReg(LCD_REG_146, 0x0000);
00301     LCD_WriteReg(LCD_REG_147, 0x0003);
00302     LCD_WriteReg(LCD_REG_149, 0x0110);
00303     LCD_WriteReg(LCD_REG_151, 0x0000);
00304     LCD_WriteReg(LCD_REG_152, 0x0000);
00305     /* Set GRAM write direction and BGR = 1
00306        I/D=01 (Horizontal : increment, Vertical : decrement)
00307        AM=1 (address is updated in vertical writing direction) */
00308     LCD_WriteReg(LCD_REG_3, 0x1018);
00309     LCD_WriteReg(LCD_REG_7, 0x0112);   /* 262K color and display ON */
00310   }
00311   else if(LCDType == LCD_HX8312)
00312   {
00313     /* Enable the LCD Oscillator ---------------------------------------------*/
00314     LCD_WriteReg(LCD_REG_1, 0x10);
00315     LCD_WriteReg(LCD_REG_0, 0xA0);
00316     LCD_WriteReg(LCD_REG_3, 0x01);
00317     _delay_(1); /* Delay 10 ms */
00318     LCD_WriteReg(LCD_REG_3, 0x00);
00319     LCD_WriteReg(LCD_REG_43, 0x04);
00320   
00321     LCD_WriteReg(LCD_REG_40, 0x18);
00322     LCD_WriteReg(LCD_REG_26, 0x05);
00323     LCD_WriteReg(LCD_REG_37, 0x05);
00324     LCD_WriteReg(LCD_REG_25, 0x00);
00325     
00326     /* LCD Power On ----------------------------------------------------------*/
00327     LCD_WriteReg(LCD_REG_28, 0x73);
00328     LCD_WriteReg(LCD_REG_36, 0x74);
00329     LCD_WriteReg(LCD_REG_30, 0x01);
00330     LCD_WriteReg(LCD_REG_24, 0xC1);
00331     _delay_(1); /* Delay 10 ms */
00332     LCD_WriteReg(LCD_REG_24, 0xE1);
00333     LCD_WriteReg(LCD_REG_24, 0xF1);
00334     _delay_(6); /* Delay 60 ms */
00335     LCD_WriteReg(LCD_REG_24, 0xF5);
00336     _delay_(6); /* Delay 60 ms */
00337     LCD_WriteReg(LCD_REG_27, 0x09);
00338     _delay_(1); /* Delay 10 ms */
00339     LCD_WriteReg(LCD_REG_31, 0x11);
00340     LCD_WriteReg(LCD_REG_32, 0x0E);
00341     LCD_WriteReg(LCD_REG_30, 0x81);
00342     _delay_(1); /* Delay 10 ms */
00343     
00344     /* Chip Set --------------------------------------------------------------*/
00345     LCD_WriteReg(LCD_REG_157, 0x00);
00346     LCD_WriteReg(LCD_REG_192, 0x00);
00347    
00348     LCD_WriteReg(LCD_REG_14, 0x00);
00349     LCD_WriteReg(LCD_REG_15, 0x00);
00350     LCD_WriteReg(LCD_REG_16, 0x00);
00351     LCD_WriteReg(LCD_REG_17, 0x00);
00352     LCD_WriteReg(LCD_REG_18, 0x00);
00353     LCD_WriteReg(LCD_REG_19, 0x00);
00354     LCD_WriteReg(LCD_REG_20, 0x00);
00355     LCD_WriteReg(LCD_REG_21, 0x00);
00356     LCD_WriteReg(LCD_REG_22, 0x00);
00357     LCD_WriteReg(LCD_REG_23, 0x00);
00358    
00359     LCD_WriteReg(LCD_REG_52, 0x01);
00360     LCD_WriteReg(LCD_REG_53, 0x00);
00361     LCD_WriteReg(LCD_REG_75, 0x00);
00362     LCD_WriteReg(LCD_REG_76, 0x00);
00363     LCD_WriteReg(LCD_REG_78, 0x00);
00364     LCD_WriteReg(LCD_REG_79, 0x00);
00365     LCD_WriteReg(LCD_REG_80, 0x00);
00366   
00367     LCD_WriteReg(LCD_REG_60, 0x00);
00368     LCD_WriteReg(LCD_REG_61, 0x00);
00369     LCD_WriteReg(LCD_REG_62, 0x01);
00370     LCD_WriteReg(LCD_REG_63, 0x3F);
00371     LCD_WriteReg(LCD_REG_64, 0x02);
00372     LCD_WriteReg(LCD_REG_65, 0x02);
00373     LCD_WriteReg(LCD_REG_66, 0x00);
00374     LCD_WriteReg(LCD_REG_67, 0x00);
00375     LCD_WriteReg(LCD_REG_68, 0x00);
00376     LCD_WriteReg(LCD_REG_69, 0x00);
00377     LCD_WriteReg(LCD_REG_70, 0xEF);
00378     LCD_WriteReg(LCD_REG_71, 0x00);
00379     LCD_WriteReg(LCD_REG_72, 0x00);
00380     LCD_WriteReg(LCD_REG_73, 0x01);
00381     LCD_WriteReg(LCD_REG_74, 0x3F);
00382   
00383     LCD_WriteReg(LCD_REG_29, 0x08);  /* R29:Gate scan direction setting */
00384   
00385     LCD_WriteReg(LCD_REG_134, 0x00);
00386     LCD_WriteReg(LCD_REG_135, 0x30);
00387     LCD_WriteReg(LCD_REG_136, 0x02);
00388     LCD_WriteReg(LCD_REG_137, 0x05);
00389   
00390     LCD_WriteReg(LCD_REG_141, 0x01);  /* R141:Register set-up mode for one line clock */
00391     LCD_WriteReg(LCD_REG_139, 0x20);  /* R139:One line SYSCLK number in one-line */
00392     LCD_WriteReg(LCD_REG_51, 0x01);  /* R51:N line inversion setting */
00393     LCD_WriteReg(LCD_REG_55, 0x01);  /* R55:Scanning method setting */
00394     LCD_WriteReg(LCD_REG_118, 0x00);
00395    
00396     /* Gamma Set -------------------------------------------------------------*/
00397     LCD_WriteReg(LCD_REG_143, 0x10);
00398     LCD_WriteReg(LCD_REG_144, 0x67);
00399     LCD_WriteReg(LCD_REG_145, 0x07);
00400     LCD_WriteReg(LCD_REG_146, 0x65);
00401     LCD_WriteReg(LCD_REG_147, 0x07);
00402     LCD_WriteReg(LCD_REG_148, 0x01);
00403     LCD_WriteReg(LCD_REG_149, 0x76);
00404     LCD_WriteReg(LCD_REG_150, 0x56);
00405     LCD_WriteReg(LCD_REG_151, 0x00);
00406     LCD_WriteReg(LCD_REG_152, 0x06);
00407     LCD_WriteReg(LCD_REG_153, 0x03);
00408     LCD_WriteReg(LCD_REG_154, 0x00);
00409   
00410     /* Display On ------------------------------------------------------------*/
00411     LCD_WriteReg(LCD_REG_1, 0x50);
00412     LCD_WriteReg(LCD_REG_5, 0x04);
00413     LCD_WriteReg(LCD_REG_0, 0x80);
00414     LCD_WriteReg(LCD_REG_59, 0x01);
00415     _delay_(4);  /* Delay 40 ms */
00416     LCD_WriteReg(LCD_REG_0, 0x20);
00417   }  
00418 }
00419 
00420 
00421 /**
00422   * @brief  Initializes the LCD.
00423   * @param  None
00424   * @retval None
00425   */
00426 void STM32100B_LCD_Init(void)
00427 {
00428   /* Setups the LCD */
00429   LCD_Setup();
00430   /* Try to read new LCD controller ID 0x9320 */
00431   if (LCD_ReadReg(LCD_REG_0) == LCD_ILI9320)
00432   {
00433     LCDType = LCD_ILI9320;
00434   }
00435   else
00436   {
00437     LCDType = LCD_SPFD5408;
00438     /* Setups the LCD */
00439     LCD_Setup();
00440     /* Try to read new LCD controller ID 0x5408 */
00441     if (LCD_ReadReg(LCD_REG_0) != LCD_SPFD5408)
00442     {
00443       LCDType = LCD_HX8312;
00444       /* Setups the LCD */
00445       LCD_Setup();
00446     }
00447   }
00448   LCD_SetFont(&LCD_DEFAULT_FONT);
00449 }
00450 
00451 /**
00452   * @brief  Sets the LCD Text and Background colors.
00453   * @param  _TextColor: specifies the Text Color.
00454   * @param  _BackColor: specifies the Background Color.
00455   * @retval None
00456   */
00457 void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor)
00458 {
00459   TextColor = _TextColor; 
00460   BackColor = _BackColor;
00461 }
00462 
00463 /**
00464   * @brief  Gets the LCD Text and Background colors.
00465   * @param  _TextColor: pointer to the variable that will contain the Text 
00466             Color.
00467   * @param  _BackColor: pointer to the variable that will contain the Background 
00468             Color.
00469   * @retval None
00470   */
00471 void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor)
00472 {
00473   *_TextColor = TextColor; *_BackColor = BackColor;
00474 }
00475 
00476 /**
00477   * @brief  Sets the Text color.
00478   * @param  Color: specifies the Text color code RGB(5-6-5).
00479   * @retval None
00480   */
00481 void LCD_SetTextColor(__IO uint16_t Color)
00482 {
00483   TextColor = Color;
00484 }
00485 
00486 
00487 /**
00488   * @brief  Sets the Background color.
00489   * @param  Color: specifies the Background color code RGB(5-6-5).
00490   * @retval None
00491   */
00492 void LCD_SetBackColor(__IO uint16_t Color)
00493 {
00494   BackColor = Color;
00495 }
00496 
00497 /**
00498   * @brief  Sets the Text Font.
00499   * @param  fonts: specifies the font to be used.
00500   * @retval None
00501   */
00502 void LCD_SetFont(sFONT *fonts)
00503 {
00504   LCD_Currentfonts = fonts;
00505 }
00506 
00507 /**
00508   * @brief  Gets the Text Font.
00509   * @param  None.
00510   * @retval the used font.
00511   */
00512 sFONT *LCD_GetFont(void)
00513 {
00514   return LCD_Currentfonts;
00515 }
00516 
00517 /**
00518   * @brief  Clears the selected line.
00519   * @param  Line: the Line to be cleared.
00520   *   This parameter can be one of the following values:
00521   *     @arg Linex: where x can be 0..n
00522   * @retval None
00523   */
00524 void LCD_ClearLine(uint8_t Line)
00525 {
00526   uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
00527   /* Send the string character by character on lCD */
00528   while (((refcolumn + 1) & 0xFFFF) >= LCD_Currentfonts->Width)
00529   {
00530     /* Display one character on LCD */
00531     LCD_DisplayChar(Line, refcolumn, ' ');
00532     /* Decrement the column position by 16 */
00533     refcolumn -= LCD_Currentfonts->Width;
00534   }
00535 }
00536 
00537 
00538 /**
00539   * @brief  Clears the hole LCD.
00540   * @param  Color: the color of the background.
00541   * @retval None
00542   */
00543 void LCD_Clear(uint16_t Color)
00544 {
00545   uint32_t index = 0;
00546   
00547   LCD_SetCursor(0x00, 0x013F); 
00548   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00549   {
00550     LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
00551   }
00552   for(index = 0; index < 76800; index++)
00553   {
00554     LCD_WriteRAM(Color);
00555   }
00556   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00557   {
00558     LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET); 
00559   }  
00560 }
00561 
00562 
00563 /**
00564   * @brief  Sets the cursor position.
00565   * @param  Xpos: specifies the X position.
00566   * @param  Ypos: specifies the Y position. 
00567   * @retval None
00568   */
00569 void LCD_SetCursor(uint8_t Xpos, uint16_t Ypos)
00570 {
00571   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00572   {
00573     LCD_WriteReg(LCD_REG_32, Xpos);
00574     LCD_WriteReg(LCD_REG_33, Ypos);
00575   }
00576   else if(LCDType == LCD_HX8312)
00577   {
00578     LCD_WriteReg(LCD_REG_66, Xpos);
00579     LCD_WriteReg(LCD_REG_67, ((Ypos & 0x100)>> 8));
00580     LCD_WriteReg(LCD_REG_68, (Ypos & 0xFF));
00581   }
00582 }
00583 
00584 
00585 /**
00586   * @brief  Draws a character on LCD.
00587   * @param  Xpos: the Line where to display the character shape.
00588   * @param  Ypos: start column address.
00589   * @param  c: pointer to the character data.
00590   * @retval None
00591   */
00592 void LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c)
00593 {
00594   uint32_t index = 0, i = 0;
00595   uint8_t Xaddress = 0;
00596    
00597   Xaddress = Xpos;
00598   
00599   LCD_SetCursor(Xaddress, Ypos);
00600   
00601   for(index = 0; index < LCD_Currentfonts->Height; index++)
00602   {
00603     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00604     {
00605       LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
00606     }
00607     for(i = 0; i < LCD_Currentfonts->Width; i++)
00608     {
00609       if((((c[index] & ((0x80 << ((LCD_Currentfonts->Width / 12 ) * 8 ) ) >> i)) == 0x00) &&(LCD_Currentfonts->Width <= 12))||
00610         (((c[index] & (0x1 << i)) == 0x00)&&(LCD_Currentfonts->Width > 12 )))
00611 
00612       {
00613         LCD_WriteRAM(BackColor);
00614       }
00615       else
00616       {
00617         LCD_WriteRAM(TextColor);
00618       }
00619     }
00620     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00621     {
00622       LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
00623     }  
00624     Xaddress++;
00625     LCD_SetCursor(Xaddress, Ypos);
00626   }
00627 }
00628 
00629 
00630 /**
00631   * @brief  Displays one character (16dots width, 24dots height).
00632   * @param  Line: the Line where to display the character shape .
00633   *   This parameter can be one of the following values:
00634   *     @arg Linex: where x can be 0..9
00635   * @param  Column: start column address.
00636   * @param  Ascii: character ascii code, must be between 0x20 and 0x7E.
00637   * @retval None
00638   */
00639 void LCD_DisplayChar(uint8_t Line, uint16_t Column, uint8_t Ascii)
00640 {
00641   Ascii -= 32;
00642   LCD_DrawChar(Line, Column, &LCD_Currentfonts->table[Ascii * LCD_Currentfonts->Height]);
00643 }
00644 
00645 
00646 /**
00647   * @brief  Displays a maximum of 20 char on the LCD.
00648   * @param  Line: the Line where to display the character shape .
00649   *   This parameter can be one of the following values:
00650   *     @arg Linex: where x can be 0..9
00651   * @param  *ptr: pointer to string to display on LCD.
00652   * @retval None
00653   */
00654 void LCD_DisplayStringLine(uint8_t Line, uint8_t *ptr)
00655 {
00656   uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
00657 
00658   /* Send the string character by character on lCD */
00659   while ((*ptr != 0) & (((refcolumn + 1) & 0xFFFF) >= LCD_Currentfonts->Width))
00660   {
00661     /* Display one character on LCD */
00662     LCD_DisplayChar(Line, refcolumn, *ptr);
00663     /* Decrement the column position by 16 */
00664     refcolumn -= LCD_Currentfonts->Width;
00665     /* Point on the next character */
00666     ptr++;
00667   }
00668 }
00669 
00670 
00671 /**
00672   * @brief  Sets a display window
00673   * @param  Xpos: specifies the X buttom left position.
00674   * @param  Ypos: specifies the Y buttom left position.
00675   * @param  Height: display window height.
00676   * @param  Width: display window width.
00677   * @retval None
00678   */
00679 void LCD_SetDisplayWindow(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
00680 {
00681   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00682   {
00683     /* Horizontal GRAM Start Address */
00684     if(Xpos >= Height)
00685     {
00686       LCD_WriteReg(LCD_REG_80, (Xpos - Height + 1));
00687     }
00688     else
00689     {
00690       LCD_WriteReg(LCD_REG_80, 0);
00691     }
00692     /* Horizontal GRAM End Address */
00693     LCD_WriteReg(LCD_REG_81, Xpos);
00694     /* Vertical GRAM Start Address */
00695     if(Ypos >= Width)
00696     {
00697       LCD_WriteReg(LCD_REG_82, (Ypos - Width + 1));
00698     }  
00699     else
00700     {
00701       LCD_WriteReg(LCD_REG_82, 0);
00702     }
00703     /* Vertical GRAM End Address */
00704     LCD_WriteReg(LCD_REG_83, Ypos);
00705   }
00706   else if(LCDType == LCD_HX8312)
00707   {  
00708     LCD_WriteReg(LCD_REG_1, 0xD0);
00709     LCD_WriteReg(LCD_REG_5, 0x14);
00710   
00711     LCD_WriteReg(LCD_REG_69, (Xpos - Height + 1));
00712     LCD_WriteReg(LCD_REG_70, Xpos);
00713  
00714     LCD_WriteReg(LCD_REG_71, (((Ypos - Width + 1) & 0x100)>> 8));
00715     LCD_WriteReg(LCD_REG_72, ((Ypos - Width + 1) & 0xFF));
00716     LCD_WriteReg(LCD_REG_73, ((Ypos & 0x100)>> 8));
00717     LCD_WriteReg(LCD_REG_74, (Ypos & 0xFF));
00718   }
00719   LCD_SetCursor(Xpos, Ypos);
00720 }
00721 
00722 
00723 /**
00724   * @brief  Disables LCD Window mode.
00725   * @param  None
00726   * @retval None
00727   */
00728 void LCD_WindowModeDisable(void)
00729 {
00730   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00731   {
00732     LCD_SetDisplayWindow(239, 0x13F, 240, 320);
00733     LCD_WriteReg(LCD_REG_3, 0x1018);
00734   }
00735   else if(LCDType == LCD_HX8312)
00736   {
00737     LCD_WriteReg(LCD_REG_1, 0x50);
00738     LCD_WriteReg(LCD_REG_5, 0x04); 
00739   }
00740     
00741 }
00742 
00743 /**
00744   * @brief  Displays a line.
00745   * @param  Xpos: specifies the X position.
00746   * @param  Ypos: specifies the Y position.
00747   * @param  Length: line length.
00748   * @param  Direction: line direction.
00749   *   This parameter can be one of the following values: Vertical or Horizontal.
00750   * @retval None
00751   */
00752 void LCD_DrawLine(uint8_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
00753 {
00754   uint32_t i = 0;
00755   
00756   LCD_SetCursor(Xpos, Ypos);
00757   if(Direction == LCD_DIR_HORIZONTAL)
00758   { 
00759     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00760     {
00761       LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
00762     }
00763     for(i = 0; i < Length; i++)
00764     {
00765       LCD_WriteRAM(TextColor);
00766     }
00767     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00768     {
00769       LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
00770     }
00771   }
00772   else
00773   {
00774    for(i = 0; i < Length; i++)
00775     {
00776       if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00777       {
00778         LCD_WriteRAMWord(TextColor);
00779       }
00780       else  if(LCDType == LCD_HX8312)
00781       {
00782         LCD_WriteRAM(TextColor);
00783       }
00784       Xpos++;
00785       LCD_SetCursor(Xpos, Ypos);
00786     }
00787   }
00788 }
00789 
00790 
00791 /**
00792   * @brief  Displays a rectangle.
00793   * @param  Xpos: specifies the X position.
00794   * @param  Ypos: specifies the Y position.
00795   * @param  Height: display rectangle height.
00796   * @param  Width: display rectangle width.
00797   * @retval None
00798   */
00799 void LCD_DrawRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
00800 {
00801   LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
00802   LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
00803   
00804   LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
00805   LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, LCD_DIR_VERTICAL);
00806 }
00807 
00808 
00809 /**
00810   * @brief  Displays a circle.
00811   * @param  Xpos: specifies the X position.
00812   * @param  Ypos: specifies the Y position.
00813   * @param  Radius
00814   * @retval None
00815   */
00816 void LCD_DrawCircle(uint8_t Xpos, uint16_t Ypos, uint16_t Radius)
00817 {
00818   int32_t  D;/* Decision Variable */ 
00819   uint32_t  CurX;/* Current X Value */
00820   uint32_t  CurY;/* Current Y Value */ 
00821   
00822   D = 3 - (Radius << 1);
00823   CurX = 0;
00824   CurY = Radius;
00825   
00826   while (CurX <= CurY)
00827   {
00828     LCD_SetCursor(Xpos + CurX, Ypos + CurY);
00829     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00830     {
00831       LCD_WriteRAMWord(TextColor);
00832     }
00833     else if(LCDType == LCD_HX8312)
00834     {
00835       LCD_WriteRAM(TextColor);
00836     }
00837     LCD_SetCursor(Xpos + CurX, Ypos - CurY);
00838 
00839     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00840     {
00841       LCD_WriteRAMWord(TextColor);
00842     }
00843     else if(LCDType == LCD_HX8312)
00844     {
00845       LCD_WriteRAM(TextColor);
00846     }
00847     LCD_SetCursor(Xpos - CurX, Ypos + CurY);
00848 
00849     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00850     {
00851       LCD_WriteRAMWord(TextColor);
00852     }
00853     else if(LCDType == LCD_HX8312)
00854     {
00855       LCD_WriteRAM(TextColor);
00856     }
00857     LCD_SetCursor(Xpos - CurX, Ypos - CurY);
00858 
00859     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00860     {
00861       LCD_WriteRAMWord(TextColor);
00862     }
00863     else if(LCDType == LCD_HX8312)
00864     {
00865       LCD_WriteRAM(TextColor);
00866     }
00867     LCD_SetCursor(Xpos + CurY, Ypos + CurX);
00868     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00869     {
00870       LCD_WriteRAMWord(TextColor);
00871     }
00872     else if(LCDType == LCD_HX8312)
00873     {
00874       LCD_WriteRAM(TextColor);
00875     }
00876     LCD_SetCursor(Xpos + CurY, Ypos - CurX);
00877 
00878     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00879     {
00880       LCD_WriteRAMWord(TextColor);
00881     }
00882     else if(LCDType == LCD_HX8312)
00883     {
00884       LCD_WriteRAM(TextColor);
00885     }
00886     LCD_SetCursor(Xpos - CurY, Ypos + CurX);
00887 
00888     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00889     {
00890       LCD_WriteRAMWord(TextColor);
00891     }
00892     else if(LCDType == LCD_HX8312)
00893     {
00894       LCD_WriteRAM(TextColor);
00895     }
00896     LCD_SetCursor(Xpos - CurY, Ypos - CurX);
00897 
00898     if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00899     {
00900       LCD_WriteRAMWord(TextColor);
00901     }
00902     else if(LCDType == LCD_HX8312)
00903     {
00904       LCD_WriteRAM(TextColor);
00905     }
00906     if (D < 0)
00907     { 
00908       D += (CurX << 2) + 6;
00909     }
00910     else
00911     {
00912       D += ((CurX - CurY) << 2) + 10;
00913       CurY--;
00914     }
00915     CurX++;
00916   }
00917 }
00918 
00919 
00920 /**
00921   * @brief  Displays a monocolor picture.
00922   * @param  Pict: pointer to the picture array.
00923   * @retval None
00924   */
00925 void LCD_DrawMonoPict(const uint32_t *Pict)
00926 {
00927   uint32_t index = 0, i = 0;
00928   LCD_SetCursor(0, (LCD_PIXEL_WIDTH - 1)); 
00929 
00930   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00931   {
00932     LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
00933   }
00934 
00935   for(index = 0; index < 2400; index++)
00936   {
00937     for(i = 0; i < 32; i++)
00938     {
00939       if((Pict[index] & (1 << i)) == 0x00)
00940       {
00941         LCD_WriteRAM(BackColor);
00942       }
00943       else
00944       {
00945         LCD_WriteRAM(TextColor);
00946       }
00947     }
00948   }
00949 
00950   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00951   {
00952     LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
00953   }
00954 }
00955 
00956 #ifdef USE_LCD_DrawBMP 
00957 /**
00958   * @brief  Displays a bitmap picture loaded in the SPI Flash.
00959   * @param  BmpAddress: Bmp picture address in the SPI Flash.
00960   * @retval None
00961   */
00962 void LCD_DrawBMP(uint32_t BmpAddress)
00963 {
00964   uint32_t i = 0, size = 0;
00965   /* Read bitmap size */
00966   sFLASH_ReadBuffer((uint8_t*)&size, BmpAddress + 2, 4);
00967   /* get bitmap data address offset */
00968   sFLASH_ReadBuffer((uint8_t*)&i, BmpAddress + 10, 4);
00969   
00970   size = (size - i)/2;
00971   sFLASH_StartReadSequence(BmpAddress + i);
00972   /* Disable LCD_SPI  */
00973   SPI_Cmd(LCD_SPI, DISABLE);
00974   /* SPI in 16-bit mode */
00975   SPI_DataSizeConfig(LCD_SPI, SPI_DataSize_16b);
00976   /* Enable LCD_SPI  */
00977   SPI_Cmd(LCD_SPI, ENABLE);
00978   
00979   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00980   {
00981     /* Set GRAM write direction and BGR = 1 */
00982     /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
00983     /* AM=1 (address is updated in vertical writing direction) */
00984     LCD_WriteReg(LCD_REG_3, 0x1008);
00985     LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
00986   }
00987   
00988   /* Read bitmap data from SPI Flash and send them to LCD */
00989   for(i = 0; i < size; i++)
00990   {
00991     LCD_WriteRAM(__REV16(sFLASH_SendHalfWord(0xA5A5)));
00992   }
00993   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
00994   {
00995     LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
00996   }
00997 
00998   /* Deselect the FLASH: Chip Select high */
00999   sFLASH_CS_HIGH();
01000   /* Disable LCD_SPI  */
01001   SPI_Cmd(LCD_SPI, DISABLE);
01002   /* SPI in 8-bit mode */
01003   SPI_DataSizeConfig(LCD_SPI, SPI_DataSize_8b);
01004   /* Enable LCD_SPI  */
01005   SPI_Cmd(LCD_SPI, ENABLE);
01006 
01007   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01008   {
01009     /* Set GRAM write direction and BGR = 1 */
01010     /* I/D = 01 (Horizontal : increment, Vertical : decrement) */
01011     /* AM = 1 (address is updated in vertical writing direction) */
01012     LCD_WriteReg(LCD_REG_3, 0x1018);
01013   }
01014 }
01015 #endif /* USE_LCD_DrawBMP */
01016  
01017 /**
01018   * @brief  Displays a full rectangle.
01019   * @param  Xpos: specifies the X position.
01020   * @param  Ypos: specifies the Y position.
01021   * @param  Height: rectangle height.
01022   * @param  Width: rectangle width.
01023   * @retval None
01024   */
01025 void LCD_DrawFullRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
01026 {
01027   LCD_SetTextColor(TextColor);
01028 
01029   LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
01030   LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
01031   
01032   LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
01033   LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, LCD_DIR_VERTICAL);
01034 
01035   Width -= 2;
01036   Height--;
01037   Ypos--;
01038 
01039   LCD_SetTextColor(BackColor);
01040 
01041   while(Height--)
01042   {
01043     LCD_DrawLine(++Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);    
01044   }
01045 
01046   LCD_SetTextColor(TextColor);
01047 }
01048 
01049 /**
01050   * @brief  Displays a full circle.
01051   * @param  Xpos: specifies the X position.
01052   * @param  Ypos: specifies the Y position.
01053   * @param  Radius
01054   * @retval None
01055   */
01056 void LCD_DrawFullCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
01057 {
01058   int32_t  D;    /* Decision Variable */ 
01059   uint32_t  CurX;/* Current X Value */
01060   uint32_t  CurY;/* Current Y Value */ 
01061   
01062   D = 3 - (Radius << 1);
01063 
01064   CurX = 0;
01065   CurY = Radius;
01066   
01067   LCD_SetTextColor(BackColor);
01068 
01069   while (CurX <= CurY)
01070   {
01071     if(CurY > 0) 
01072     {
01073       LCD_DrawLine(Xpos - CurX, Ypos + CurY, 2*CurY, LCD_DIR_HORIZONTAL);
01074       LCD_DrawLine(Xpos + CurX, Ypos + CurY, 2*CurY, LCD_DIR_HORIZONTAL);
01075     }
01076 
01077     if(CurX > 0) 
01078     {
01079       LCD_DrawLine(Xpos - CurY, Ypos + CurX, 2*CurX, LCD_DIR_HORIZONTAL);
01080       LCD_DrawLine(Xpos + CurY, Ypos + CurX, 2*CurX, LCD_DIR_HORIZONTAL);
01081     }
01082     if (D < 0)
01083     { 
01084       D += (CurX << 2) + 6;
01085     }
01086     else
01087     {
01088       D += ((CurX - CurY) << 2) + 10;
01089       CurY--;
01090     }
01091     CurX++;
01092   }
01093 
01094   LCD_SetTextColor(TextColor);
01095   LCD_DrawCircle(Xpos, Ypos, Radius);
01096 }
01097 
01098 /**
01099   * @brief  Displays an uni line (between two points).
01100   * @param  x1: specifies the point 1 x position.
01101   * @param  y1: specifies the point 1 y position.
01102   * @param  x2: specifies the point 2 x position.
01103   * @param  y2: specifies the point 2 y position.
01104   * @retval None
01105   */
01106 void LCD_DrawUniLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
01107 {
01108   int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0, 
01109   yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0, 
01110   curpixel = 0;
01111   
01112   deltax = ABS(x2 - x1);        /* The difference between the x's */
01113   deltay = ABS(y2 - y1);        /* The difference between the y's */
01114   x = x1;                       /* Start x off at the first pixel */
01115   y = y1;                       /* Start y off at the first pixel */
01116   
01117   if (x2 >= x1)                 /* The x-values are increasing */
01118   {
01119     xinc1 = 1;
01120     xinc2 = 1;
01121   }
01122   else                          /* The x-values are decreasing */
01123   {
01124     xinc1 = -1;
01125     xinc2 = -1;
01126   }
01127   
01128   if (y2 >= y1)                 /* The y-values are increasing */
01129   {
01130     yinc1 = 1;
01131     yinc2 = 1;
01132   }
01133   else                          /* The y-values are decreasing */
01134   {
01135     yinc1 = -1;
01136     yinc2 = -1;
01137   }
01138   
01139   if (deltax >= deltay)         /* There is at least one x-value for every y-value */
01140   {
01141     xinc1 = 0;                  /* Don't change the x when numerator >= denominator */
01142     yinc2 = 0;                  /* Don't change the y for every iteration */
01143     den = deltax;
01144     num = deltax / 2;
01145     numadd = deltay;
01146     numpixels = deltax;         /* There are more x-values than y-values */
01147   }
01148   else                          /* There is at least one y-value for every x-value */
01149   {
01150     xinc2 = 0;                  /* Don't change the x for every iteration */
01151     yinc1 = 0;                  /* Don't change the y when numerator >= denominator */
01152     den = deltay;
01153     num = deltay / 2;
01154     numadd = deltax;
01155     numpixels = deltay;         /* There are more y-values than x-values */
01156   }
01157   
01158   for (curpixel = 0; curpixel <= numpixels; curpixel++)
01159   {
01160     PutPixel(x, y);             /* Draw the current pixel */
01161     num += numadd;              /* Increase the numerator by the top of the fraction */
01162     if (num >= den)             /* Check if numerator >= denominator */
01163     {
01164       num -= den;               /* Calculate the new numerator value */
01165       x += xinc1;               /* Change the x as appropriate */
01166       y += yinc1;               /* Change the y as appropriate */
01167     }
01168     x += xinc2;                 /* Change the x as appropriate */
01169     y += yinc2;                 /* Change the y as appropriate */
01170   }
01171 }
01172 
01173 /**
01174   * @brief  Displays an polyline (between many points).
01175   * @param  Points: pointer to the points array.
01176   * @param  PointCount: Number of points.
01177   * @retval None
01178   */
01179 void LCD_PolyLine(pPoint Points, uint16_t PointCount)
01180 {
01181   int16_t X = 0, Y = 0;
01182 
01183   if(PointCount < 2)
01184   {
01185     return;
01186   }
01187 
01188   while(--PointCount)
01189   {
01190     X = Points->X;
01191     Y = Points->Y;
01192     Points++;
01193     LCD_DrawUniLine(X, Y, Points->X, Points->Y);
01194   }
01195 }
01196 
01197 /**
01198   * @brief  Displays an relative polyline (between many points).
01199   * @param  Points: pointer to the points array.
01200   * @param  PointCount: Number of points.
01201   * @param  Closed: specifies if the draw is closed or not.
01202   *           1: closed, 0 : not closed.
01203   * @retval None
01204   */
01205 static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed)
01206 {
01207   int16_t X = 0, Y = 0;
01208   pPoint First = Points;
01209 
01210   if(PointCount < 2)
01211   {
01212     return;
01213   }  
01214   X = Points->X;
01215   Y = Points->Y;
01216   while(--PointCount)
01217   {
01218     Points++;
01219     LCD_DrawUniLine(X, Y, X + Points->X, Y + Points->Y);
01220     X = X + Points->X;
01221     Y = Y + Points->Y;
01222   }
01223   if(Closed)
01224   {
01225     LCD_DrawUniLine(First->X, First->Y, X, Y);
01226   }  
01227 }
01228 
01229 /**
01230   * @brief  Displays a closed polyline (between many points).
01231   * @param  Points: pointer to the points array.
01232   * @param  PointCount: Number of points.
01233   * @retval None
01234   */
01235 void LCD_ClosedPolyLine(pPoint Points, uint16_t PointCount)
01236 {
01237   LCD_PolyLine(Points, PointCount);
01238   LCD_DrawUniLine(Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
01239 }
01240 
01241 /**
01242   * @brief  Displays a relative polyline (between many points).
01243   * @param  Points: pointer to the points array.
01244   * @param  PointCount: Number of points.
01245   * @retval None
01246   */
01247 void LCD_PolyLineRelative(pPoint Points, uint16_t PointCount)
01248 {
01249   LCD_PolyLineRelativeClosed(Points, PointCount, 0);
01250 }
01251 
01252 /**
01253   * @brief  Displays a closed relative polyline (between many points).
01254   * @param  Points: pointer to the points array.
01255   * @param  PointCount: Number of points.
01256   * @retval None
01257   */
01258 void LCD_ClosedPolyLineRelative(pPoint Points, uint16_t PointCount)
01259 {
01260   LCD_PolyLineRelativeClosed(Points, PointCount, 1);
01261 }
01262 
01263 
01264 /**
01265   * @brief  Displays a  full polyline (between many points).
01266   * @param  Points: pointer to the points array.
01267   * @param  PointCount: Number of points.
01268   * @retval None
01269   */
01270 void LCD_FillPolyLine(pPoint Points, uint16_t PointCount)
01271 {
01272   /*  public-domain code by Darel Rex Finley, 2007 */
01273   uint16_t  nodes = 0, nodeX[MAX_POLY_CORNERS], pixelX = 0, pixelY = 0, i = 0,
01274   j = 0, swap = 0;
01275   uint16_t  IMAGE_LEFT = 0, IMAGE_RIGHT = 0, IMAGE_TOP = 0, IMAGE_BOTTOM = 0;
01276 
01277   IMAGE_LEFT = IMAGE_RIGHT = Points->X;
01278   IMAGE_TOP= IMAGE_BOTTOM = Points->Y;
01279 
01280   for(i = 1; i < PointCount; i++)
01281   {
01282     pixelX = POLY_X(i);
01283     if(pixelX < IMAGE_LEFT)
01284     {
01285       IMAGE_LEFT = pixelX;
01286     }
01287     if(pixelX > IMAGE_RIGHT)
01288     {
01289       IMAGE_RIGHT = pixelX;
01290     }
01291     
01292     pixelY = POLY_Y(i);
01293     if(pixelY < IMAGE_TOP)
01294     { 
01295       IMAGE_TOP = pixelY;
01296     }
01297     if(pixelY > IMAGE_BOTTOM)
01298     {
01299       IMAGE_BOTTOM = pixelY;
01300     }
01301   }
01302   
01303   LCD_SetTextColor(BackColor);  
01304 
01305   /*  Loop through the rows of the image. */
01306   for (pixelY = IMAGE_TOP; pixelY < IMAGE_BOTTOM; pixelY++) 
01307   {  
01308     /* Build a list of nodes. */
01309     nodes = 0; j = PointCount-1;
01310 
01311     for (i = 0; i < PointCount; i++) 
01312     {
01313       if (POLY_Y(i)<(double) pixelY && POLY_Y(j)>=(double) pixelY || POLY_Y(j)<(double) pixelY && POLY_Y(i)>=(double) pixelY) 
01314       {
01315         nodeX[nodes++]=(int) (POLY_X(i)+((pixelY-POLY_Y(i))*(POLY_X(j)-POLY_X(i)))/(POLY_Y(j)-POLY_Y(i))); 
01316       }
01317       j = i; 
01318     }
01319   
01320     /* Sort the nodes, via a simple “Bubble” sort. */
01321     i = 0;
01322     while (i < nodes-1) 
01323     {
01324       if (nodeX[i]>nodeX[i+1]) 
01325       {
01326         swap = nodeX[i]; 
01327         nodeX[i] = nodeX[i+1]; 
01328         nodeX[i+1] = swap; 
01329         if(i)
01330         {
01331           i--; 
01332         }
01333       }
01334       else 
01335       {
01336         i++;
01337       }
01338     }
01339   
01340     /*  Fill the pixels between node pairs. */
01341     for (i = 0; i < nodes; i+=2) 
01342     {
01343       if(nodeX[i] >= IMAGE_RIGHT) 
01344       {
01345         break;
01346       }
01347       if(nodeX[i+1] > IMAGE_LEFT) 
01348       {
01349         if (nodeX[i] < IMAGE_LEFT)
01350         {
01351           nodeX[i]=IMAGE_LEFT;
01352         }
01353         if(nodeX[i+1] > IMAGE_RIGHT)
01354         {
01355           nodeX[i+1] = IMAGE_RIGHT;
01356         }
01357         LCD_SetTextColor(BackColor);
01358         LCD_DrawLine(pixelY, nodeX[i+1], nodeX[i+1] - nodeX[i], LCD_DIR_HORIZONTAL);
01359         LCD_SetTextColor(TextColor);
01360         PutPixel(pixelY, nodeX[i+1]);
01361         PutPixel(pixelY, nodeX[i]);
01362         /* for (j=nodeX[i]; j<nodeX[i+1]; j++) PutPixel(j,pixelY); */
01363       }
01364     }
01365   } 
01366 
01367   /* draw the edges */
01368   LCD_SetTextColor(TextColor);
01369 }
01370 
01371 /**
01372   * @brief  Reset LCD control line(/CS) and Send Start-Byte
01373   * @param  Start_Byte: the Start-Byte to be sent
01374   * @retval None
01375   */
01376 void LCD_nCS_StartByte(uint8_t Start_Byte)
01377 {
01378   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_RESET);
01379 
01380   SPI_I2S_SendData(LCD_SPI, Start_Byte);
01381 
01382   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01383   {
01384   }
01385 }
01386 
01387 
01388 /**
01389   * @brief  Writes index to select the LCD register.
01390   * @param  LCD_Reg: address of the selected register.
01391   * @retval None
01392   */
01393 void LCD_WriteRegIndex(uint8_t LCD_Reg)
01394 {
01395   /* Reset LCD control line(/CS) and Send Start-Byte */
01396   LCD_nCS_StartByte(START_BYTE | SET_INDEX);
01397 
01398   /* Write 16-bit Reg Index (High Byte is 0) */
01399   SPI_I2S_SendData(LCD_SPI, 0x00);
01400 
01401   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01402   {
01403   }
01404 
01405   SPI_I2S_SendData(LCD_SPI, LCD_Reg);
01406 
01407   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01408   {
01409   }
01410 
01411   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
01412 }
01413 
01414 
01415 /**
01416   * @brief  Writes to the selected LCD ILI9320 register.
01417   * @param  LCD_Reg: address of the selected register.
01418   * @param  LCD_RegValue: value to write to the selected register.
01419   * @retval None
01420   */
01421 static void LCD_WriteRegILI9320(uint8_t LCD_Reg, uint16_t LCD_RegValue)
01422 {
01423   /* Write 16-bit Index (then Write Reg) */
01424   LCD_WriteRegIndex(LCD_Reg);
01425 
01426   /* Write 16-bit Reg */
01427   /* Reset LCD control line(/CS) and Send Start-Byte */
01428   LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG);
01429 
01430   SPI_I2S_SendData(LCD_SPI, LCD_RegValue>>8);
01431 
01432   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01433   {
01434   }
01435 
01436   SPI_I2S_SendData(LCD_SPI, (LCD_RegValue & 0xFF));
01437 
01438   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01439   {
01440   }
01441 
01442   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
01443 }
01444 
01445 
01446 /**
01447   * @brief  Reads the selected LCD Register.
01448   * @param  LCD_Reg: address of the selected register.
01449   * @retval LCD Register Value.
01450   */
01451 uint16_t LCD_ReadReg(uint8_t LCD_Reg)
01452 {
01453   uint16_t tmp = 0;
01454   uint8_t i = 0;
01455   
01456   /* LCD_SPI prescaler: 4 */
01457   LCD_SPI->CR1 &= 0xFFC7;
01458   LCD_SPI->CR1 |= 0x0008;
01459   /* Write 16-bit Index (then Read Reg) */
01460   LCD_WriteRegIndex(LCD_Reg);
01461   /* Read 16-bit Reg */
01462   /* Reset LCD control line(/CS) and Send Start-Byte */
01463   LCD_nCS_StartByte(START_BYTE | LCD_READ_REG);
01464   
01465   for(i = 0; i < 5; i++)
01466   {
01467     SPI_I2S_SendData(LCD_SPI, 0xFF);
01468     while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01469     {
01470     }
01471     /* One byte of invalid dummy data read after the start byte */
01472     while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
01473     {    
01474     }
01475     SPI_I2S_ReceiveData(LCD_SPI); 
01476   }
01477 
01478   SPI_I2S_SendData(LCD_SPI, 0xFF);
01479 
01480   /* Read upper byte */
01481   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01482   {
01483   }
01484 
01485   /* Read lower byte */
01486   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
01487   {
01488   }
01489   tmp = SPI_I2S_ReceiveData(LCD_SPI);
01490   
01491   
01492   SPI_I2S_SendData(LCD_SPI, 0xFF);
01493   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01494   {
01495   }
01496 
01497   /* Read lower byte */
01498   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
01499   {
01500   }
01501 
01502   tmp = ((tmp & 0xFF) << 8) | SPI_I2S_ReceiveData(LCD_SPI);
01503   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
01504 
01505   /* LCD_SPI prescaler: 2 */
01506   LCD_SPI->CR1 &= 0xFFC7;
01507 
01508   return tmp;
01509 }
01510 
01511 
01512 /**
01513   * @brief  Prepare to write to the LCD RAM.
01514   * @param  None
01515   * @retval None
01516   */
01517 void LCD_WriteRAM_Prepare(void)
01518 {
01519   LCD_WriteRegIndex(LCD_REG_34); /* Select GRAM Reg */
01520 
01521   /* Reset LCD control line(/CS) and Send Start-Byte */
01522   LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG);
01523 }
01524 
01525 
01526 /**
01527   * @brief  Writes 1 word to the LCD RAM.
01528   * @param  RGB_Code: the pixel color in RGB mode (5-6-5).
01529   * @retval None
01530   */
01531 void LCD_WriteRAMWord(uint16_t RGB_Code)
01532 {
01533 
01534   LCD_WriteRAM_Prepare();
01535 
01536   LCD_WriteRAM(RGB_Code);
01537 
01538   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
01539 }
01540 
01541 
01542 /**
01543   * @brief  Writes to the selected LCD HX8312 register.
01544   * @param  LCD_Reg: address of the selected register.
01545   * @param  LCD_RegValue: value to write to the selected register.
01546   * @retval None
01547   */
01548 static void LCD_WriteRegHX8312(uint8_t LCD_Reg, uint8_t LCD_RegValue)
01549 {
01550   uint16_t tmp = 0;
01551   
01552   LCD_CtrlLinesWrite(LCD_NWR_GPIO_PORT, LCD_NWR_PIN, Bit_RESET);
01553   LCD_CtrlLinesWrite(LCD_RS_GPIO_PORT, LCD_RS_PIN, Bit_RESET);
01554   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_RESET);
01555   
01556   tmp = LCD_Reg << 8;
01557   tmp |= LCD_RegValue;
01558    
01559   SPI_I2S_SendData(LCD_SPI, tmp);
01560 
01561   while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01562   {
01563   }
01564   
01565   LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
01566 }
01567 
01568 
01569 /**
01570   * @brief  Writes to the selected LCD register.
01571   * @param  LCD_Reg: address of the selected register.
01572   * @param  LCD_RegValue: value to write to the selected register.
01573   * @retval None
01574   */
01575 void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue)
01576 {
01577   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01578   {
01579     LCD_WriteRegILI9320(LCD_Reg, LCD_RegValue);
01580   }
01581   else if(LCDType == LCD_HX8312)
01582   {
01583     LCD_WriteRegHX8312(LCD_Reg, ((uint8_t)LCD_RegValue));
01584   }
01585 }
01586 
01587 
01588 /**
01589   * @brief  Writes to the LCD RAM.
01590   * @param  RGB_Code: the pixel color in RGB mode (5-6-5).
01591   * @retval None
01592   */
01593 void LCD_WriteRAM(uint16_t RGB_Code)
01594 {
01595   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01596   {
01597     SPI_I2S_SendData(LCD_SPI, RGB_Code >> 8);
01598     while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01599     {
01600     }
01601     SPI_I2S_SendData(LCD_SPI, RGB_Code & 0xFF);
01602     while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01603     {
01604     }
01605   }
01606 
01607   if(LCDType == LCD_HX8312)
01608   {
01609     LCD_CtrlLinesWrite(LCD_NWR_GPIO_PORT, LCD_NWR_PIN, Bit_RESET);
01610     LCD_CtrlLinesWrite(LCD_RS_GPIO_PORT, LCD_RS_PIN, Bit_SET);
01611     LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_RESET);
01612     SPI_I2S_SendData(LCD_SPI, RGB_Code);
01613 
01614     while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
01615     {
01616     }
01617   
01618     LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
01619   }
01620 }
01621 
01622 
01623 /**
01624   * @brief  Power on the LCD.
01625   * @param  None
01626   * @retval None
01627   */
01628 void LCD_PowerOn(void)
01629 {
01630   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01631   {
01632     /* Power On sequence ---------------------------------------------------------*/
01633     LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
01634     LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
01635     LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
01636     LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
01637     _delay_(20);               /* Dis-charge capacitor power voltage (200ms) */
01638     LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
01639     LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
01640     _delay_(5);                /* Delay 50 ms */
01641     LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
01642     _delay_(5);                /* delay 50 ms */
01643     LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
01644     LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
01645     _delay_(5);                /* delay 50 ms */
01646     LCD_WriteReg(LCD_REG_7, 0x0173);  /* 262K color and display ON */
01647   }
01648   else if(LCDType == LCD_HX8312)
01649   {  
01650     /* Power On Set */
01651     LCD_WriteReg(LCD_REG_28, 0x73);
01652     LCD_WriteReg(LCD_REG_36, 0x74);
01653     LCD_WriteReg(LCD_REG_30, 0x01);
01654     LCD_WriteReg(LCD_REG_24, 0xC1);
01655     _delay_(1); /* Delay 10 ms */
01656     LCD_WriteReg(LCD_REG_24, 0xE1);
01657     LCD_WriteReg(LCD_REG_24, 0xF1);
01658     _delay_(6); /* Delay 60 ms */
01659     LCD_WriteReg(LCD_REG_24, 0xF5);
01660     _delay_(6); /* Delay 60 ms */
01661     LCD_WriteReg(LCD_REG_27, 0x09);
01662     _delay_(1); /* Delay 10 ms */
01663     LCD_WriteReg(LCD_REG_31, 0x11);
01664     LCD_WriteReg(LCD_REG_32, 0x0E);
01665     LCD_WriteReg(LCD_REG_30, 0x81);
01666     _delay_(1); /* Delay 10 ms */
01667   }
01668 }
01669 
01670 
01671 /**
01672   * @brief  Enables the Display.
01673   * @param  None
01674   * @retval None
01675   */
01676 void LCD_DisplayOn(void)
01677 {
01678   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01679   {
01680     /* Display On */
01681     LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
01682   }
01683   else if(LCDType == LCD_HX8312)
01684   {  
01685     LCD_WriteReg(LCD_REG_1, 0x50);
01686     LCD_WriteReg(LCD_REG_5, 0x04);
01687     /* Display On */
01688     LCD_WriteReg(LCD_REG_0, 0x80);
01689     LCD_WriteReg(LCD_REG_59, 0x01);
01690     _delay_(4); /* Delay 40 ms */
01691     LCD_WriteReg(LCD_REG_0, 0x20);
01692   }  
01693 }
01694 
01695 
01696 /**
01697   * @brief  Disables the Display.
01698   * @param  None
01699   * @retval None
01700   */
01701 void LCD_DisplayOff(void)
01702 {
01703   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01704   {
01705     /* Display Off */
01706     LCD_WriteReg(LCD_REG_7, 0x0);
01707   }
01708   else if(LCDType == LCD_HX8312)
01709   { 
01710     /* Display Off */
01711     LCD_WriteReg(LCD_REG_0, 0xA0);
01712     _delay_(4);  /* Delay 40 ms */
01713     LCD_WriteReg(LCD_REG_59, 0x00);
01714   } 
01715 }
01716 
01717 
01718 /**
01719   * @brief  Configures LCD control lines in Output Push-Pull mode.
01720   * @param  None
01721   * @retval None
01722   */
01723 void LCD_CtrlLinesConfig(void)
01724 {
01725   GPIO_InitTypeDef GPIO_InitStructure;
01726 
01727   /* Enable GPIO clock */
01728   RCC_APB2PeriphClockCmd(LCD_NCS_GPIO_CLK | LCD_NWR_GPIO_CLK | LCD_RS_GPIO_CLK, ENABLE);
01729 
01730   /* Configure NCS in Output Push-Pull mode */
01731   GPIO_InitStructure.GPIO_Pin = LCD_NCS_PIN;
01732   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
01733   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
01734   GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
01735   
01736   /* Configure NWR(RNW), RS in Output Push-Pull mode */
01737   GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
01738   GPIO_Init(LCD_RS_GPIO_PORT, &GPIO_InitStructure);
01739 
01740   GPIO_InitStructure.GPIO_Pin = LCD_NWR_PIN;
01741   GPIO_Init(LCD_NWR_GPIO_PORT, &GPIO_InitStructure);
01742   
01743   LCD_CtrlLinesWrite(LCD_NWR_GPIO_PORT, LCD_NWR_PIN, Bit_SET);
01744   LCD_CtrlLinesWrite(LCD_RS_GPIO_PORT, LCD_RS_PIN, Bit_SET);
01745 }
01746 
01747 
01748 /**
01749   * @brief  Sets or reset LCD control lines.
01750   * @param  GPIOx: where x can be B or D to select the GPIO peripheral.
01751   * @param  CtrlPins: the Control line.
01752   *   This parameter can be:
01753   *     @arg LCD_NCS_PIN: Chip Select pin
01754   *     @arg LCD_NWR_PIN: Read/Write Selection pin
01755   *     @arg LCD_RS_PIN: Register/RAM Selection pin
01756   * @param  BitVal: specifies the value to be written to the selected bit.
01757   *   This parameter can be:
01758   *     @arg Bit_RESET: to clear the port pin
01759   *     @arg Bit_SET: to set the port pin
01760   * @retval None
01761   */
01762 void LCD_CtrlLinesWrite(GPIO_TypeDef* GPIOx, uint16_t CtrlPins, BitAction BitVal)
01763 {
01764   /* Set or Reset the control line */
01765   GPIO_WriteBit(GPIOx, CtrlPins, BitVal);
01766 }
01767 
01768 
01769 /**
01770   * @brief  Configures the LCD_SPI interface.
01771   * @param  None
01772   * @retval None
01773   */
01774 void LCD_SPIConfig(void)
01775 {
01776   SPI_InitTypeDef    SPI_InitStructure;
01777   GPIO_InitTypeDef   GPIO_InitStructure;
01778 
01779   /* Enable GPIO clock */
01780   RCC_APB2PeriphClockCmd(LCD_SPI_SCK_GPIO_CLK | LCD_SPI_MISO_GPIO_CLK | LCD_SPI_MOSI_GPIO_CLK, ENABLE);
01781 
01782   /* Enable SPI clock  */
01783   RCC_APB1PeriphClockCmd(LCD_SPI_CLK, ENABLE);
01784 
01785   /* Configure SPI pins: SCK, MISO and MOSI */
01786   GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
01787   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
01788   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
01789   GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
01790 
01791   GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
01792   GPIO_Init(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
01793   
01794   GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
01795   GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
01796 
01797   SPI_I2S_DeInit(LCD_SPI);
01798   
01799   /* SPI Config */
01800   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
01801   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
01802 
01803   if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
01804   {
01805     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
01806     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
01807     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
01808   }
01809   else if(LCDType == LCD_HX8312)
01810   {
01811     SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
01812     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
01813     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
01814   }
01815   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
01816   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
01817   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
01818 
01819   SPI_Init(LCD_SPI, &SPI_InitStructure);
01820 
01821   /* SPI enable */
01822   SPI_Cmd(LCD_SPI, ENABLE);
01823 }
01824 
01825 /**
01826   * @brief  Displays a pixel.
01827   * @param  x: pixel x.
01828   * @param  y: pixel y.  
01829   * @retval None
01830   */
01831 static void PutPixel(int16_t x, int16_t y)
01832 { 
01833   if(x < 0 || x > 239 || y < 0 || y > 319)
01834   {
01835     return;  
01836   }
01837   LCD_DrawLine(x, y, 1, LCD_DIR_HORIZONTAL);
01838 }
01839 
01840 #ifndef USE_Delay
01841 /**
01842   * @brief  Inserts a delay time.
01843   * @param  nCount: specifies the delay time length.
01844   * @retval None
01845   */
01846 static void delay(vu32 nCount)
01847 {
01848   vu32 index = 0; 
01849   for(index = (34000 * nCount); index != 0; index--)
01850   {
01851   }
01852 }
01853 #endif /* USE_Delay*/
01854 /**
01855   * @}
01856   */ 
01857 
01858 /**
01859   * @}
01860   */ 
01861 
01862 /**
01863   * @}
01864   */ 
01865 
01866 /**
01867   * @}
01868   */ 
01869 
01870 /**
01871   * @}
01872   */ 
01873     
01874 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32