STM8S/A Standard Peripherals Firmware Library
|
stm8s_eval_lcd.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8s_eval_lcd.c 00004 * @author MCD Application Team 00005 * @version V1.0.1 00006 * @date 30-September-2014 00007 * @brief This file includes driver for the dot matrix LCD Module mounted on 00008 * STM8L15xx-EVAL board. 00009 ****************************************************************************** 00010 * 00011 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00012 * You may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at: 00014 * 00015 * http://www.st.com/software_license_agreement_liberty_v2 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 * 00023 ****************************************************************************** 00024 */ 00025 00026 /* Includes ------------------------------------------------------------------*/ 00027 #include "stm8s_eval_lcd.h" 00028 00029 /** @addtogroup Utilities 00030 * @{ 00031 */ 00032 00033 /** @defgroup STM8S_EVAL_LCD 00034 * @{ 00035 */ 00036 00037 /** @defgroup STM8S_EVAL_LCD_Private_Types 00038 * @{ 00039 */ 00040 /** 00041 * @} 00042 */ 00043 00044 /** @defgroup STM8S_EVAL_LCD_Private_Defines 00045 * @{ 00046 */ 00047 00048 #define STATUS_TYPE 0xFC 00049 00050 /* This table contains the "S" of ST logo */ 00051 CONST uint8_t S_CGRAM[] = 00052 { 00053 /* 0~7 */ 00054 0x03, 0xff, 00055 0x02, 0x00, 00056 0x04, 0x00, 00057 0x04, 0x00, 00058 0x0c, 0x7f, 00059 0x0c, 0x7f, 00060 0x1c, 0x3f, 00061 0x1e, 0x1f, 00062 /* 8~15 */ 00063 0x3f, 0x0f, 00064 0x3f, 0x87, 00065 0x7f, 0xc3, 00066 0x7f, 0xe3, 00067 0x00, 0x03, 00068 0x00, 0x03, 00069 0x00, 0x07, 00070 0xff, 0xfe, 00071 }; 00072 00073 /* This table contains the "T" of ST logo */ 00074 CONST uint8_t T_CGRAM[] = 00075 { 00076 /* 0~7 */ 00077 0xff, 0xff, 00078 0x00, 0x00, 00079 0x00, 0x00, 00080 0x00, 0x00, 00081 0xf8, 0xf8, 00082 0xf0, 0xf8, 00083 0xf0, 0xf0, 00084 0xf0, 0xf0, 00085 /* 8~15 */ 00086 0xe1, 0xe0, 00087 0xe3, 0xe0, 00088 0xc3, 0xc0, 00089 0xc7, 0xc0, 00090 0x87, 0xc0, 00091 0x8f, 0x80, 00092 0x0f, 0x80, 00093 0x1f, 0x00 00094 }; 00095 00096 /** 00097 * @} 00098 */ 00099 00100 00101 /** @defgroup STM8S_EVAL_LCD_Private_Macros 00102 * @{ 00103 */ 00104 /** 00105 * @} 00106 */ 00107 00108 00109 /** @defgroup STM8S_EVAL_LCD_Private_Variables 00110 * @{ 00111 */ 00112 /** 00113 * @} 00114 */ 00115 00116 /** @defgroup STM8S_EVAL_LCD_Private_Function_Prototypes 00117 * @{ 00118 */ 00119 static void LCD_SPISendByte(uint8_t DataToSend); 00120 static void LCD_DisplayCGRAM0(uint8_t address, uint8_t *ptrTable); 00121 static void LCD_DisplayCGRAM1(uint8_t address, uint8_t *ptrTable); 00122 00123 /** 00124 * @brief The delay function implemented in this driver is not a precise one, 00125 * however it allows the insertion of 1ms delay when Fcpu is 16Mhz if 00126 * the passed parameter is 0x4000. 00127 * Any change in system clock frequency will impact this delay duration. 00128 * 00129 * User is given the possibility to develop a customized and accurate 00130 * delay function by the mean of timers for example. 00131 * Uncommenting " #define USE_Delay" line in the stm8s_eval_lcd.h file 00132 * will allow the consideration of the new function by this driver. 00133 */ 00134 static void delay(__IO uint32_t nCount); 00135 /** 00136 * @} 00137 */ 00138 00139 /** @defgroup STM8S_EVAL_LCD_Private_Functions 00140 * @{ 00141 */ 00142 00143 /** 00144 * @brief Initialize the LCD 00145 * @param None 00146 * @retval None 00147 */ 00148 void STM8S_EVAL_LCD_Init(void) 00149 { 00150 /* Enable SPI clock */ 00151 CLK_PeripheralClockConfig(LCD_SPI_CLK, ENABLE); 00152 00153 /* Configure SPI pins: SCK and MOSI */ 00154 GPIO_Init(LCD_SPI_GPIO_PORT,(GPIO_Pin_TypeDef)(LCD_SPI_SCK_PIN | LCD_SPI_MOSI_PIN), GPIO_MODE_OUT_PP_LOW_FAST); 00155 00156 /* Initialize SPI */ 00157 SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_64, SPI_MODE_MASTER, 00158 SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE, SPI_DATADIRECTION_1LINE_TX, 00159 SPI_NSS_SOFT, 0x07); 00160 SPI_Cmd(ENABLE); 00161 00162 /* Required to ensure proper LCD display when the board is powered-on ... */ 00163 _delay_(0x4000); /* 1ms _delay_ using Fcpu = 16Mhz*/ 00164 00165 /* Configure LCD ChipSelect pin (NCS) in Output push-pull mode */ 00166 GPIO_Init(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, GPIO_MODE_OUT_PP_LOW_FAST); 00167 00168 /* Configure LCD backlight pin in Output push-pull low level (backlight off) */ 00169 GPIO_Init(LCD_BACKLIGHT_PORT, LCD_BACKLIGHT_PIN, GPIO_MODE_OUT_PP_LOW_FAST); 00170 00171 /* Set the LCD in TEXT mode */ 00172 LCD_SendByte(COMMAND_TYPE, SET_TEXT_MODE); 00173 00174 /* Enable the display */ 00175 LCD_SendByte(COMMAND_TYPE, DISPLAY_ON); 00176 00177 /* Clear the LCD */ 00178 LCD_SendByte(COMMAND_TYPE, DISPLAY_CLR); 00179 00180 /* Delay required to complete LCD clear command */ 00181 _delay_(0x4000); /* 1ms _delay_ using Fcpu = 16Mhz*/ 00182 00183 /* Select the entry mode type */ 00184 LCD_SendByte(COMMAND_TYPE, ENTRY_MODE_SET_INC); 00185 } 00186 00187 /** 00188 * @brief Enable or Disable the LCD backlight 00189 * @param NewState Backlight state 00190 * @retval None 00191 */ 00192 void LCD_BacklightCmd(FunctionalState NewState) 00193 { 00194 if (NewState == DISABLE) 00195 { 00196 GPIO_WriteLow(LCD_BACKLIGHT_PORT, LCD_BACKLIGHT_PIN); 00197 } 00198 else 00199 { 00200 GPIO_WriteHigh(LCD_BACKLIGHT_PORT, LCD_BACKLIGHT_PIN); 00201 } 00202 } 00203 00204 /** 00205 * @brief Set the LCD cursor to the specified location 00206 * @param Line : line where the cursor will be set (LCD_LINE1 or LCD_LINE2) 00207 * @param Offset : is the position offset (only even position are supported) 00208 * @retval None 00209 */ 00210 void LCD_SetCursorPos(uint8_t Line, uint8_t Offset) 00211 { 00212 LCD_SendByte(COMMAND_TYPE, (uint8_t)(Line + Offset)); 00213 } 00214 00215 /** 00216 * @brief Send a byte to LCD 00217 * @param DataType Type of Data to be sent 00218 * @param DataToSend Data to be sent 00219 * @retval None 00220 */ 00221 void LCD_SendByte(uint8_t DataType, uint8_t DataToSend) 00222 { 00223 /* Enable access to LCD */ 00224 LCD_NCS_HIGH(); 00225 00226 /* Send Synchro/Mode byte */ 00227 LCD_SPISendByte(DataType); 00228 00229 /* Send byte high nibble */ 00230 LCD_SPISendByte((uint8_t)(DataToSend & (uint8_t)0xF0)); 00231 00232 /* Send byte low nibble */ 00233 LCD_SPISendByte((uint8_t)((uint8_t)(DataToSend << 4) & (uint8_t)0xF0)); 00234 _delay_(80); 00235 00236 /* Disable access to LCD */ 00237 LCD_NCS_LOW(); 00238 } 00239 00240 /** 00241 * @brief Clear the LCD 00242 * @param None 00243 * @retval None 00244 */ 00245 void LCD_Clear(void) 00246 { 00247 LCD_SendByte(COMMAND_TYPE, DISPLAY_CLR); /* Clear the LCD */ 00248 00249 /* Delay required to complete LCD clear command */ 00250 _delay_(0x4000); /* 1ms _delay_ using Fcpu = 16Mhz*/ 00251 00252 } 00253 00254 /** 00255 * @brief Display a string from current position of the LCD cursor 00256 * @param ptr : Pointer to the string to display 00257 * @retval None 00258 */ 00259 void LCD_Print(uint8_t *ptr) 00260 { 00261 __IO uint8_t charindex = 0x00; 00262 00263 /* Display the string */ 00264 while ((*ptr) && (charindex < 0x0F)) 00265 { 00266 LCD_SendByte(DATA_TYPE, *ptr++); 00267 charindex++; 00268 } 00269 } 00270 00271 /** 00272 * @brief Display a string in rolling mode 00273 * @param Line : line used for displaying the text (LCD_LINE1 or LCD_LINE2) 00274 * @param ptr : Pointer to the text to display 00275 * @param speed : Rolling speed 00276 * @retval 00277 * None 00278 */ 00279 void LCD_RollString(uint8_t Line, uint8_t *ptr, uint16_t speed) 00280 { 00281 00282 uint8_t CharPos = 0; 00283 uint8_t *ptr2; 00284 00285 /* Set cursor position at beginning of line */ 00286 LCD_SendByte(COMMAND_TYPE, Line); 00287 00288 ptr2 = ptr; 00289 00290 /* Display each character of the string */ 00291 while (*ptr2 != 0) 00292 { 00293 if (*ptr != 0) 00294 { 00295 LCD_SendByte(DATA_TYPE, *ptr); 00296 ptr++; 00297 } 00298 else 00299 { 00300 LCD_SendByte(DATA_TYPE, ' '); 00301 } 00302 00303 CharPos++; 00304 00305 if (CharPos == LCD_LINE_MAX_CHAR) 00306 { 00307 _delay_(speed); 00308 00309 /* Select the line to be cleared */ 00310 LCD_SendByte(COMMAND_TYPE, Line); 00311 00312 /* Clear the selected line */ 00313 for (CharPos = 0; CharPos < LCD_LINE_MAX_CHAR; CharPos++) 00314 { 00315 LCD_SendByte(DATA_TYPE, ' '); 00316 } 00317 LCD_SendByte(COMMAND_TYPE, Line); 00318 CharPos = 0; 00319 ptr2++; 00320 ptr = ptr2; 00321 } 00322 } 00323 } 00324 00325 /** 00326 * @brief Display ST logo 00327 * @param address : Display address (LINE1:0x80-0x87 and LINE2:0x90-0x97) 00328 * @retval None 00329 */ 00330 void LCD_DisplayLogo(uint8_t address) 00331 { 00332 LCD_DisplayCGRAM0(address, (uint8_t*)S_CGRAM); 00333 LCD_DisplayCGRAM1(address, (uint8_t*)T_CGRAM); 00334 } 00335 00336 /** 00337 * @} 00338 */ 00339 00340 /** @addtogroup LCD_Private_Functions 00341 * @{ 00342 */ 00343 00344 /** 00345 * @brief Send a byte to LCD through the SPI peripheral 00346 * @param DataToSend : Data to be sent 00347 * @retval None 00348 */ 00349 static void LCD_SPISendByte(uint8_t DataToSend) 00350 { 00351 00352 /* Send byte through the SPI peripheral */ 00353 LCD_SPI->DR = DataToSend; 00354 00355 while ((LCD_SPI->SR & SPI_SR_TXE) == 0) 00356 { 00357 /* Wait while the byte is transmitted */ 00358 } 00359 } 00360 00361 /** 00362 * @brief Display CGRAM on even address 00363 * @param address : Display address 00364 * @param ptrTable : Pointer a the CGRAM table to be displayed 00365 * @retval None 00366 */ 00367 static void LCD_DisplayCGRAM0(uint8_t address, uint8_t *ptrTable) 00368 { 00369 00370 uint8_t u = 32; /* Nb byte in the table */ 00371 00372 /* Set CGRAM Address */ 00373 LCD_SendByte(COMMAND_TYPE, (uint8_t)0x40); 00374 00375 while (u) 00376 { 00377 LCD_SendByte(DATA_TYPE, ptrTable[32 - u]); 00378 u--; 00379 } 00380 00381 /* Setup Display Address */ 00382 LCD_SendByte(COMMAND_TYPE, address); 00383 LCD_SendByte(DATA_TYPE, (uint8_t)0x00); 00384 LCD_SendByte(DATA_TYPE, (uint8_t)0x00); 00385 00386 } 00387 00388 /** 00389 * @brief Display CGRAM on odd address 00390 * @param address : Display address 00391 * @param ptrTable : Pointer a the CGRAM table to be displayed 00392 * @retval None 00393 */ 00394 static void LCD_DisplayCGRAM1(uint8_t address, uint8_t *ptrTable) 00395 { 00396 00397 uint8_t u = 32; /* Nb byte in the table */ 00398 00399 /* Set CGRAM Address */ 00400 LCD_SendByte(COMMAND_TYPE, (uint8_t)((uint8_t)0x40 | (uint8_t)0x10)); 00401 00402 while (u) 00403 { 00404 LCD_SendByte(DATA_TYPE, ptrTable[32 - u]); 00405 u--; 00406 } 00407 00408 /* Setup Display Address */ 00409 LCD_SendByte(COMMAND_TYPE, (uint8_t)(address + 1)); 00410 LCD_SendByte(DATA_TYPE, (uint8_t)0x00); 00411 LCD_SendByte(DATA_TYPE, (uint8_t)0x02); 00412 00413 } 00414 00415 #ifndef USE_Delay 00416 /** 00417 * @brief Inserts a delay time. 00418 * The delay function implemented in this driver is not a precise one, 00419 * however it allows the insertion of 1ms delay when Fcpu is 16Mhz if 00420 * the passed parameter is 0x4000. 00421 * Any change in system clock frequency will impact this delay duration. 00422 * 00423 * User is given the possibility to develop a customized and accurate 00424 * delay function by the mean of timers for example. 00425 * Uncommenting " #define USE_Delay" line in the stm8s_eval_lcd.h file 00426 * will allow the consideration of the new function by this driver. 00427 * 00428 * @param nCount: specifies the _delay_ time length. 00429 * @retval None 00430 */ 00431 static void delay(__IO uint32_t nCount) 00432 { 00433 /* Decrement nCount value */ 00434 while (nCount != 0) 00435 { 00436 nCount--; 00437 } 00438 } 00439 #endif /* USE_Delay*/ 00440 00441 /** 00442 * @} 00443 */ 00444 00445 00446 /** 00447 * @} 00448 */ 00449 00450 /** 00451 * @} 00452 */ 00453 00454 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/