| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /********************************************************************* |
| 2 | * |
||
| 3 | * LCD Access Routines |
||
| 4 | * |
||
| 5 | ********************************************************************* |
||
| 6 | * FileName: LCDBlocking.c |
||
| 7 | * Dependencies: Compiler.h, HardwareProfile.h |
||
| 8 | * Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F |
||
| 9 | * Compiler: Microchip C18 v3.02 or higher |
||
| 10 | * Microchip C30 v2.01 or higher |
||
| 11 | * Company: Microchip Technology, Inc. |
||
| 12 | * |
||
| 13 | * Software License Agreement |
||
| 14 | * |
||
| 15 | * Copyright © 2002-2007 Microchip Technology Inc. All rights |
||
| 16 | * reserved. |
||
| 17 | * |
||
| 18 | * Microchip licenses to you the right to use, modify, copy, and |
||
| 19 | * distribute: |
||
| 20 | * (i) the Software when embedded on a Microchip microcontroller or |
||
| 21 | * digital signal controller product (Device) which is |
||
| 22 | * integrated into Licensees product; or |
||
| 23 | * (ii) ONLY the Software driver source files ENC28J60.c and |
||
| 24 | * ENC28J60.h ported to a non-Microchip device used in |
||
| 25 | * conjunction with a Microchip ethernet controller for the |
||
| 26 | * sole purpose of interfacing with the ethernet controller. |
||
| 27 | * |
||
| 28 | * You should refer to the license agreement accompanying this |
||
| 29 | * Software for additional information regarding your rights and |
||
| 30 | * obligations. |
||
| 31 | * |
||
| 32 | * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT |
||
| 33 | * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
||
| 34 | * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
||
| 35 | * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
||
| 36 | * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR |
||
| 37 | * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF |
||
| 38 | * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS |
||
| 39 | * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE |
||
| 40 | * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER |
||
| 41 | * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT |
||
| 42 | * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. |
||
| 43 | * |
||
| 44 | * |
||
| 45 | * Author Date Comment |
||
| 46 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 47 | * Howard Schlunder 4/03/06 Original |
||
| 48 | * Howard Schlunder 4/12/06 Changed from using PMP to LCDWrite() |
||
| 49 | * Howard Schlunder 8/10/06 Fixed a delay being too short |
||
| 50 | * when CLOCK_FREQ was a smaller |
||
| 51 | * value, added FOUR_BIT_MODE |
||
| 52 | ********************************************************************/ |
||
| 53 | #define __LCDBLOCKING_C |
||
| 54 | |||
| 55 | #include <string.h> |
||
| 56 | #include "Compiler.h" |
||
| 57 | #include "GenericTypeDefs.h" |
||
| 58 | #include "HardwareProfile.h" |
||
| 59 | #include "LCDBlocking.h" |
||
| 60 | #include "Timer.h" |
||
| 61 | |||
| 62 | |||
| 63 | //#define FOUR_BIT_MODE |
||
| 64 | #define SAMSUNG_S6A0032 // This LCD driver chip has a different means of entering 4-bit mode. |
||
| 65 | |||
| 66 | // LCDText is a 32 byte shadow of the LCD text. Write to it and |
||
| 67 | // then call LCDUpdate() to copy the string into the LCD module. |
||
| 68 | BYTE LCDText[16*2+1]; |
||
| 69 | |||
| 70 | /****************************************************************************** |
||
| 71 | * Function: static void LCDWrite(BYTE RS, BYTE Data) |
||
| 72 | * |
||
| 73 | * PreCondition: None |
||
| 74 | * |
||
| 75 | * Input: RS - Register Select - 1:RAM, 0:Config registers |
||
| 76 | * Data - 8 bits of data to write |
||
| 77 | * |
||
| 78 | * Output: None |
||
| 79 | * |
||
| 80 | * Side Effects: None |
||
| 81 | * |
||
| 82 | * Overview: Controls the Port I/O pins to cause an LCD write |
||
| 83 | * |
||
| 84 | * Note: None |
||
| 85 | *****************************************************************************/ |
||
| 86 | static void LCDWrite(BYTE RS, BYTE Data) |
||
| 87 | { |
||
| 88 | #if defined(LCD_DATA_TRIS) |
||
| 89 | LCD_DATA_TRIS = 0x00; |
||
| 90 | #else |
||
| 91 | LCD_DATA0_TRIS = 0; |
||
| 92 | LCD_DATA1_TRIS = 0; |
||
| 93 | LCD_DATA2_TRIS = 0; |
||
| 94 | LCD_DATA3_TRIS = 0; |
||
| 95 | #if !defined(FOUR_BIT_MODE) |
||
| 96 | LCD_DATA4_TRIS = 0; |
||
| 97 | LCD_DATA5_TRIS = 0; |
||
| 98 | LCD_DATA6_TRIS = 0; |
||
| 99 | LCD_DATA7_TRIS = 0; |
||
| 100 | #endif |
||
| 101 | #endif |
||
| 102 | LCD_RS_TRIS = 0; |
||
| 103 | LCD_RD_WR_TRIS = 0; |
||
| 104 | LCD_RD_WR_IO = 0; |
||
| 105 | LCD_RS_IO = RS; |
||
| 106 | |||
| 107 | #if defined(FOUR_BIT_MODE) |
||
| 108 | #if defined(LCD_DATA_IO) |
||
| 109 | LCD_DATA_IO = Data>>4; |
||
| 110 | #else |
||
| 111 | LCD_DATA0_IO = ((Data & 0x10) == 0x10); |
||
| 112 | LCD_DATA1_IO = ((Data & 0x20) == 0x20); |
||
| 113 | LCD_DATA2_IO = ((Data & 0x40) == 0x40); |
||
| 114 | LCD_DATA3_IO = ((Data & 0x80) == 0x80); |
||
| 115 | #endif |
||
| 116 | Nop(); // Wait Data setup time (min 40ns) |
||
| 117 | Nop(); |
||
| 118 | LCD_E_IO = 1; |
||
| 119 | Nop(); // Wait E Pulse width time (min 230ns) |
||
| 120 | Nop(); |
||
| 121 | Nop(); |
||
| 122 | Nop(); |
||
| 123 | Nop(); |
||
| 124 | Nop(); |
||
| 125 | Nop(); |
||
| 126 | Nop(); |
||
| 127 | Nop(); |
||
| 128 | LCD_E_IO = 0; |
||
| 129 | #endif |
||
| 130 | |||
| 131 | #if defined(LCD_DATA_IO) |
||
| 132 | LCD_DATA_IO = Data; |
||
| 133 | #else |
||
| 134 | LCD_DATA0_IO = ((Data & 0x01) == 0x01); |
||
| 135 | LCD_DATA1_IO = ((Data & 0x02) == 0x02); |
||
| 136 | LCD_DATA2_IO = ((Data & 0x04) == 0x04); |
||
| 137 | LCD_DATA3_IO = ((Data & 0x08) == 0x08); |
||
| 138 | #if !defined(FOUR_BIT_MODE) |
||
| 139 | LCD_DATA4_IO = ((Data & 0x10) == 0x10); |
||
| 140 | LCD_DATA5_IO = ((Data & 0x20) == 0x20); |
||
| 141 | LCD_DATA6_IO = ((Data & 0x40) == 0x40); |
||
| 142 | LCD_DATA7_IO = ((Data & 0x80) == 0x80); |
||
| 143 | #endif |
||
| 144 | #endif |
||
| 145 | Nop(); // Wait Data setup time (min 40ns) |
||
| 146 | Nop(); |
||
| 147 | LCD_E_IO = 1; |
||
| 148 | Nop(); // Wait E Pulse width time (min 230ns) |
||
| 149 | Nop(); |
||
| 150 | Nop(); |
||
| 151 | Nop(); |
||
| 152 | Nop(); |
||
| 153 | Nop(); |
||
| 154 | Nop(); |
||
| 155 | Nop(); |
||
| 156 | Nop(); |
||
| 157 | LCD_E_IO = 0; |
||
| 158 | |||
| 159 | #if defined(LCD_DATA_TRIS) |
||
| 160 | LCD_DATA_TRIS = 0xFF; |
||
| 161 | #else |
||
| 162 | LCD_DATA0_TRIS = 1; |
||
| 163 | LCD_DATA1_TRIS = 1; |
||
| 164 | LCD_DATA2_TRIS = 1; |
||
| 165 | LCD_DATA3_TRIS = 1; |
||
| 166 | #if !defined(FOUR_BIT_MODE) |
||
| 167 | LCD_DATA4_TRIS = 1; |
||
| 168 | LCD_DATA5_TRIS = 1; |
||
| 169 | LCD_DATA6_TRIS = 1; |
||
| 170 | LCD_DATA7_TRIS = 1; |
||
| 171 | #endif |
||
| 172 | #endif |
||
| 173 | LCD_RS_TRIS = 1; |
||
| 174 | LCD_RD_WR_TRIS = 1; |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | /****************************************************************************** |
||
| 179 | * Function: void LCDInit(void) |
||
| 180 | * |
||
| 181 | * PreCondition: None |
||
| 182 | * |
||
| 183 | * Input: None |
||
| 184 | * |
||
| 185 | * Output: None |
||
| 186 | * |
||
| 187 | * Side Effects: None |
||
| 188 | * |
||
| 189 | * Overview: LCDText[] is blanked, port I/O pin TRIS registers are |
||
| 190 | * configured, and the LCD is placed in the default state |
||
| 191 | * |
||
| 192 | * Note: None |
||
| 193 | *****************************************************************************/ |
||
| 194 | void LCDInit(void) |
||
| 195 | { |
||
| 196 | BYTE i; |
||
| 197 | |||
| 198 | memset(LCDText, ' ', sizeof(LCDText)-1); |
||
| 199 | LCDText[sizeof(LCDText)-1] = 0; |
||
| 200 | |||
| 201 | // Setup the I/O pins |
||
| 202 | LCD_E_IO = 0; |
||
| 203 | LCD_RD_WR_IO = 0; |
||
| 204 | |||
| 205 | |||
| 206 | #if defined(LCD_DATA_TRIS) |
||
| 207 | LCD_DATA_TRIS = 0x00; |
||
| 208 | #else |
||
| 209 | LCD_DATA0_TRIS = 0; |
||
| 210 | LCD_DATA1_TRIS = 0; |
||
| 211 | LCD_DATA2_TRIS = 0; |
||
| 212 | LCD_DATA3_TRIS = 0; |
||
| 213 | #if !defined(FOUR_BIT_MODE) |
||
| 214 | LCD_DATA4_TRIS = 0; |
||
| 215 | LCD_DATA5_TRIS = 0; |
||
| 216 | LCD_DATA6_TRIS = 0; |
||
| 217 | LCD_DATA7_TRIS = 0; |
||
| 218 | #endif |
||
| 219 | #endif |
||
| 220 | LCD_RD_WR_TRIS = 0; |
||
| 221 | LCD_RS_TRIS = 0; |
||
| 222 | LCD_E_TRIS = 0; |
||
| 223 | |||
| 224 | |||
| 225 | // Wait the required time for the LCD to reset |
||
| 226 | DelayMs(40); |
||
| 227 | |||
| 228 | // Set the default function |
||
| 229 | // Go to 8-bit mode first to reset the instruction state machine |
||
| 230 | // This is done in a loop 3 times to absolutely ensure that we get |
||
| 231 | // to 8-bit mode in case if the device was previously booted into |
||
| 232 | // 4-bit mode and our PIC got reset in the middle of the LCD |
||
| 233 | // receiving half (4-bits) of an 8-bit instruction |
||
| 234 | LCD_RS_IO = 0; |
||
| 235 | #if defined(LCD_DATA_IO) |
||
| 236 | LCD_DATA_IO = 0x03; |
||
| 237 | #else |
||
| 238 | LCD_DATA0_IO = 1; |
||
| 239 | LCD_DATA1_IO = 1; |
||
| 240 | LCD_DATA2_IO = 0; |
||
| 241 | LCD_DATA3_IO = 0; |
||
| 242 | #if !defined(FOUR_BIT_MODE) |
||
| 243 | LCD_DATA4_IO = 0; |
||
| 244 | LCD_DATA5_IO = 0; |
||
| 245 | LCD_DATA6_IO = 0; |
||
| 246 | LCD_DATA7_IO = 0; |
||
| 247 | #endif |
||
| 248 | #endif |
||
| 249 | Nop(); // Wait Data setup time (min 40ns) |
||
| 250 | Nop(); |
||
| 251 | for(i = 0; i < 3; i++) |
||
| 252 | { |
||
| 253 | LCD_E_IO = 1; |
||
| 254 | Delay10us(1); // Wait E Pulse width time (min 230ns) |
||
| 255 | LCD_E_IO = 0; |
||
| 256 | DelayMs(2); |
||
| 257 | } |
||
| 258 | |||
| 259 | #if defined(FOUR_BIT_MODE) |
||
| 260 | #if defined(SAMSUNG_S6A0032) |
||
| 261 | // Enter 4-bit mode (requires only 4-bits on the S6A0032) |
||
| 262 | #if defined(LCD_DATA_IO) |
||
| 263 | LCD_DATA_IO = 0x02; |
||
| 264 | #else |
||
| 265 | LCD_DATA0_IO = 0; |
||
| 266 | LCD_DATA1_IO = 1; |
||
| 267 | LCD_DATA2_IO = 0; |
||
| 268 | LCD_DATA3_IO = 0; |
||
| 269 | #endif |
||
| 270 | Nop(); // Wait Data setup time (min 40ns) |
||
| 271 | Nop(); |
||
| 272 | LCD_E_IO = 1; |
||
| 273 | Delay10us(1); // Wait E Pulse width time (min 230ns) |
||
| 274 | LCD_E_IO = 0; |
||
| 275 | #else |
||
| 276 | // Enter 4-bit mode with two lines (requires 8-bits on most LCD controllers) |
||
| 277 | LCDWrite(0, 0x28); |
||
| 278 | #endif |
||
| 279 | #else |
||
| 280 | // Use 8-bit mode with two lines |
||
| 281 | LCDWrite(0, 0x38); |
||
| 282 | #endif |
||
| 283 | Delay10us(5); |
||
| 284 | |||
| 285 | // Set the entry mode |
||
| 286 | LCDWrite(0, 0x06); // Increment after each write, do not shift |
||
| 287 | Delay10us(5); |
||
| 288 | |||
| 289 | // Set the display control |
||
| 290 | LCDWrite(0, 0x0C); // Turn display on, no cusor, no cursor blink |
||
| 291 | Delay10us(5); |
||
| 292 | |||
| 293 | // Clear the display |
||
| 294 | LCDWrite(0, 0x01); |
||
| 295 | DelayMs(2); |
||
| 296 | |||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | /****************************************************************************** |
||
| 301 | * Function: void LCDUpdate(void) |
||
| 302 | * |
||
| 303 | * PreCondition: LCDInit() must have been called once |
||
| 304 | * |
||
| 305 | * Input: LCDText[] |
||
| 306 | * |
||
| 307 | * Output: None |
||
| 308 | * |
||
| 309 | * Side Effects: None |
||
| 310 | * |
||
| 311 | * Overview: Copies the contents of the local LCDText[] array into the |
||
| 312 | * LCD's internal display buffer. Null terminators in |
||
| 313 | * LCDText[] terminate the current line, so strings may be |
||
| 314 | * printed directly to LCDText[]. |
||
| 315 | * |
||
| 316 | * Note: None |
||
| 317 | *****************************************************************************/ |
||
| 318 | void LCDUpdate(void) |
||
| 319 | { |
||
| 320 | BYTE i, j; |
||
| 321 | |||
| 322 | // Go home |
||
| 323 | LCDWrite(0, 0x02); |
||
| 324 | DelayMs(2); |
||
| 325 | |||
| 326 | // Output first line |
||
| 327 | for(i = 0; i < 16u; i++) |
||
| 328 | { |
||
| 329 | // Erase the rest of the line if a null char is |
||
| 330 | // encountered (good for printing strings directly) |
||
| 331 | if(LCDText[i] == 0u) |
||
| 332 | { |
||
| 333 | for(j=i; j < 16u; j++) |
||
| 334 | { |
||
| 335 | LCDText[j] = ' '; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | LCDWrite(1, LCDText[i]); |
||
| 339 | Delay10us(5); |
||
| 340 | } |
||
| 341 | |||
| 342 | // Set the address to the second line |
||
| 343 | LCDWrite(0, 0xC0); |
||
| 344 | Delay10us(5); |
||
| 345 | |||
| 346 | // Output second line |
||
| 347 | for(i = 16; i < 32u; i++) |
||
| 348 | { |
||
| 349 | // Erase the rest of the line if a null char is |
||
| 350 | // encountered (good for printing strings directly) |
||
| 351 | if(LCDText[i] == 0u) |
||
| 352 | { |
||
| 353 | for(j=i; j < 32u; j++) |
||
| 354 | { |
||
| 355 | LCDText[j] = ' '; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | LCDWrite(1, LCDText[i]); |
||
| 359 | Delay10us(5); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | /****************************************************************************** |
||
| 364 | * Function: void LCDErase(void) |
||
| 365 | * |
||
| 366 | * PreCondition: LCDInit() must have been called once |
||
| 367 | * |
||
| 368 | * Input: None |
||
| 369 | * |
||
| 370 | * Output: None |
||
| 371 | * |
||
| 372 | * Side Effects: None |
||
| 373 | * |
||
| 374 | * Overview: Clears LCDText[] and the LCD's internal display buffer |
||
| 375 | * |
||
| 376 | * Note: None |
||
| 377 | *****************************************************************************/ |
||
| 378 | void LCDErase(void) |
||
| 379 | { |
||
| 380 | // Clear display |
||
| 381 | LCDWrite(0, 0x01); |
||
| 382 | DelayMs(2); |
||
| 383 | |||
| 384 | // Clear local copy |
||
| 385 | memset(LCDText, ' ', 32); |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | |||
| 390 | void LCDWriteLine(WORD number, char *line) |
||
| 391 | { |
||
| 392 | BYTE i; |
||
| 393 | BYTE j; |
||
| 394 | |||
| 395 | j = 0; |
||
| 396 | i = 0; |
||
| 397 | |||
| 398 | if(number == 2) |
||
| 399 | { |
||
| 400 | while ((LCDText[j] != 0) && (j < 16)) |
||
| 401 | { |
||
| 402 | j++; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | do |
||
| 407 | { |
||
| 408 | LCDText[j++] = line[i++]; |
||
| 409 | } |
||
| 410 | while ((LCDText[j-1] != 0) && (j < 31)); |
||
| 411 | |||
| 412 | LCDUpdate(); |
||
| 413 | } |
||
| 414 | |||
| 415 |
Powered by WebSVN v2.8.3