stm3210b_eval_lcd.c

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