| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /***************************************************************************** |
| 2 | * Module for Microchip Graphics Library |
||
| 3 | * Sino Wealth Microelectronic SH1101A OLED controller driver |
||
| 4 | * Solomon Systech SSD1303 LCD controller driver |
||
| 5 | ***************************************************************************** |
||
| 6 | * FileName: SH1101A_SSD1303.c |
||
| 7 | * Dependencies: Graphics.h |
||
| 8 | * Processor: PIC24 |
||
| 9 | * Compiler: MPLAB C30 |
||
| 10 | * Linker: MPLAB LINK30 |
||
| 11 | * Company: Microchip Technology Incorporated |
||
| 12 | * |
||
| 13 | * Software License Agreement |
||
| 14 | * |
||
| 15 | * Copyright © 2008 Microchip Technology Inc. All rights reserved. |
||
| 16 | * Microchip licenses to you the right to use, modify, copy and distribute |
||
| 17 | * Software only when embedded on a Microchip microcontroller or digital |
||
| 18 | * signal controller, which is integrated into your product or third party |
||
| 19 | * product (pursuant to the sublicense terms in the accompanying license |
||
| 20 | * agreement). |
||
| 21 | * |
||
| 22 | * You should refer to the license agreement accompanying this Software |
||
| 23 | * for additional information regarding your rights and obligations. |
||
| 24 | * |
||
| 25 | * SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY |
||
| 26 | * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY |
||
| 27 | * OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR |
||
| 28 | * PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR |
||
| 29 | * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, |
||
| 30 | * BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT |
||
| 31 | * DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, |
||
| 32 | * INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, |
||
| 33 | * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY |
||
| 34 | * CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), |
||
| 35 | * OR OTHER SIMILAR COSTS. |
||
| 36 | * |
||
| 37 | * Author Date Comment |
||
| 38 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 39 | * Rodger Richey 03/10/07 Original |
||
| 40 | * Paolo Tamayo 12/20/07 Ported to PIC24 Kit |
||
| 41 | * PAT 3/26/10 Fixed error on PutPixel() timing (PMP timing). |
||
| 42 | *****************************************************************************/ |
||
| 43 | #include "Graphics\Graphics.h" |
||
| 44 | |||
| 45 | // Color |
||
| 46 | BYTE _color; |
||
| 47 | |||
| 48 | // Clipping region control |
||
| 49 | SHORT _clipRgn; |
||
| 50 | |||
| 51 | // Clipping region borders |
||
| 52 | SHORT _clipLeft; |
||
| 53 | SHORT _clipTop; |
||
| 54 | SHORT _clipRight; |
||
| 55 | SHORT _clipBottom; |
||
| 56 | |||
| 57 | |||
| 58 | /********************************************************************* |
||
| 59 | * Macros: SetAddress(lowerAddr,higherAddr) |
||
| 60 | * |
||
| 61 | * Overview: Sets the page and the lower and higher address pointer |
||
| 62 | * of the display buffer. All parameters should be pre-calculated. |
||
| 63 | * |
||
| 64 | * PreCondition: none |
||
| 65 | * |
||
| 66 | * Input: page - Page value for the address. |
||
| 67 | * lowerAddr - Lower column address. |
||
| 68 | * higherAddr - Higher column address. |
||
| 69 | * |
||
| 70 | * Output: none |
||
| 71 | * |
||
| 72 | * Side Effects: none |
||
| 73 | * |
||
| 74 | ********************************************************************/ |
||
| 75 | #define SetAddress(page, lowerAddr, higherAddr) \ |
||
| 76 | DeviceSetCommand(); \ |
||
| 77 | DeviceWrite(page); \ |
||
| 78 | DeviceWrite(lowerAddr); \ |
||
| 79 | DeviceWrite(higherAddr); \ |
||
| 80 | DeviceSetData(); |
||
| 81 | |||
| 82 | |||
| 83 | /********************************************************************* |
||
| 84 | * Function: void ResetDevice() |
||
| 85 | * |
||
| 86 | * PreCondition: none |
||
| 87 | * |
||
| 88 | * Input: none |
||
| 89 | * |
||
| 90 | * Output: none |
||
| 91 | * |
||
| 92 | * Side Effects: none |
||
| 93 | * |
||
| 94 | * Overview: resets LCD, initializes PMP |
||
| 95 | * |
||
| 96 | * Note: none |
||
| 97 | * |
||
| 98 | ********************************************************************/ |
||
| 99 | void ResetDevice(void) |
||
| 100 | { |
||
| 101 | // Initialize the device |
||
| 102 | DeviceInit(); |
||
| 103 | |||
| 104 | DeviceSelect(); |
||
| 105 | DeviceSetCommand(); |
||
| 106 | |||
| 107 | #if (DISPLAY_CONTROLLER == SH1101A) |
||
| 108 | |||
| 109 | // Setup Display |
||
| 110 | DeviceWrite(0xAE); // turn off the display (AF=ON, AE=OFF) |
||
| 111 | DeviceWrite(0xDB); // set VCOMH |
||
| 112 | DeviceWrite(0x23); |
||
| 113 | DeviceWrite(0xD9); // set VP |
||
| 114 | DeviceWrite(0x22); |
||
| 115 | |||
| 116 | // Re-map |
||
| 117 | DeviceWrite(0xA1); // [A0]:column address 0 is map to SEG0 |
||
| 118 | |||
| 119 | // [A1]:column address 131 is map to SEG0 |
||
| 120 | // COM Output Scan Direction |
||
| 121 | DeviceWrite(0xC8); // C0 is COM0 to COMn, C8 is COMn to COM0 |
||
| 122 | |||
| 123 | // COM Pins Hardware Configuration |
||
| 124 | DeviceWrite(0xDA); // set pins hardware configuration |
||
| 125 | DeviceWrite(0x12); |
||
| 126 | |||
| 127 | // Multiplex Ratio |
||
| 128 | DeviceWrite(0xA8); // set multiplex ratio |
||
| 129 | DeviceWrite(0x3F); // set to 64 mux |
||
| 130 | |||
| 131 | // Display Clock Divide |
||
| 132 | DeviceWrite(0xD5); // set display clock divide |
||
| 133 | DeviceWrite(0xA0); // set to 100Hz |
||
| 134 | |||
| 135 | // Contrast Control Register |
||
| 136 | DeviceWrite(0x81); // Set contrast control |
||
| 137 | DeviceWrite(0x60); // display 0 ~ 127; 2C |
||
| 138 | |||
| 139 | // Display Offset |
||
| 140 | DeviceWrite(0xD3); // set display offset |
||
| 141 | DeviceWrite(0x00); // no offset |
||
| 142 | |||
| 143 | //Normal or Inverse Display |
||
| 144 | DeviceWrite(0xA6); // Normal display |
||
| 145 | DeviceWrite(0xAD); // Set DC-DC |
||
| 146 | DeviceWrite(0x8B); // 8B=ON, 8A=OFF |
||
| 147 | |||
| 148 | // Display ON/OFF |
||
| 149 | DeviceWrite(0xAF); // AF=ON, AE=OFF |
||
| 150 | DelayMs(150); |
||
| 151 | |||
| 152 | // Entire Display ON/OFF |
||
| 153 | DeviceWrite(0xA4); // A4=ON |
||
| 154 | |||
| 155 | // Display Start Line |
||
| 156 | DeviceWrite(0x40); // Set display start line |
||
| 157 | |||
| 158 | // Lower Column Address |
||
| 159 | DeviceWrite(0x00 + OFFSET); // Set lower column address |
||
| 160 | |||
| 161 | // Higher Column Address |
||
| 162 | DeviceWrite(0x10); // Set higher column address |
||
| 163 | DelayMs(1); |
||
| 164 | #elif (DISPLAY_CONTROLLER == SSD1303) |
||
| 165 | |||
| 166 | // Setup Display |
||
| 167 | DeviceWrite(0xAE); // turn off the display (AF=ON, AE=OFF) |
||
| 168 | DeviceWrite(0xDB); // set VCOMH |
||
| 169 | DeviceWrite(0x23); |
||
| 170 | DeviceWrite(0xD9); // set VP |
||
| 171 | DeviceWrite(0x22); |
||
| 172 | DeviceWrite(0xAD); // Set DC-DC |
||
| 173 | DeviceWrite(0x8B); // 8B=ON, 8A=OFF |
||
| 174 | DelayMs(1); |
||
| 175 | |||
| 176 | // Display ON/OFF |
||
| 177 | DeviceWrite(0xAF); // AF=ON, AE=OFF |
||
| 178 | |||
| 179 | // Init OLED display using SSD1303 driver |
||
| 180 | // Display Clock Divide |
||
| 181 | DeviceWrite(0xD5); // set display clock divide |
||
| 182 | DeviceWrite(0xA0); // set to 100Hz |
||
| 183 | |||
| 184 | // Display Offset |
||
| 185 | DeviceWrite(0xD3); // set display offset |
||
| 186 | DeviceWrite(0x00); // no offset |
||
| 187 | |||
| 188 | // Multiplex Ratio |
||
| 189 | DeviceWrite(0xA8); // set multiplex ratio |
||
| 190 | DeviceWrite(0x3F); // set to 64 mux |
||
| 191 | |||
| 192 | // Display Start Line |
||
| 193 | DeviceWrite(0x40); // Set display start line |
||
| 194 | |||
| 195 | // Re-map |
||
| 196 | DeviceWrite(0xA0); // [A0]:column address 0 is map to SEG0 |
||
| 197 | |||
| 198 | // [A1]:column address 131 is map to SEG0 |
||
| 199 | // COM Output Scan Direction |
||
| 200 | DeviceWrite(0xC8); // C0 is COM0 to COMn, C8 is COMn to COM0 |
||
| 201 | |||
| 202 | // COM Pins Hardware Configuration |
||
| 203 | DeviceWrite(0xDA); // set pins hardware configuration |
||
| 204 | DeviceWrite(0x12); |
||
| 205 | |||
| 206 | // Contrast Control Register |
||
| 207 | DeviceWrite(0x81); // Set contrast control |
||
| 208 | DeviceWrite(0x60); // display 0 ~ 127; 2C |
||
| 209 | |||
| 210 | // Entire Display ON/OFF |
||
| 211 | DeviceWrite(0xA4); // A4=ON |
||
| 212 | |||
| 213 | //Normal or Inverse Display |
||
| 214 | DeviceWrite(0xA6); // Normal display |
||
| 215 | |||
| 216 | #ifdef DISABLE_DC_DC_CONVERTER |
||
| 217 | DeviceWrite(0x8A); |
||
| 218 | #else |
||
| 219 | DeviceWrite(0x8B); // 8B=ON, 8A=OFF |
||
| 220 | #endif |
||
| 221 | |||
| 222 | // Lower Column Address |
||
| 223 | DeviceWrite(0x00 + OFFSET); // Set lower column address |
||
| 224 | |||
| 225 | // Higher Column Address |
||
| 226 | DeviceWrite(0x10); // Set higher column address |
||
| 227 | DelayMs(1); |
||
| 228 | #else |
||
| 229 | #error The controller is not supported. |
||
| 230 | #endif |
||
| 231 | DeviceDeselect(); |
||
| 232 | DeviceSetData(); |
||
| 233 | } |
||
| 234 | |||
| 235 | /********************************************************************* |
||
| 236 | * Function: void PutPixel(SHORT x, SHORT y) |
||
| 237 | * |
||
| 238 | * PreCondition: none |
||
| 239 | * |
||
| 240 | * Input: x,y - pixel coordinates |
||
| 241 | * |
||
| 242 | * Output: none |
||
| 243 | * |
||
| 244 | * Side Effects: none |
||
| 245 | * |
||
| 246 | * Overview: puts pixel |
||
| 247 | * |
||
| 248 | * Note: none |
||
| 249 | * |
||
| 250 | ********************************************************************/ |
||
| 251 | void PutPixel(SHORT x, SHORT y) |
||
| 252 | { |
||
| 253 | BYTE page, add, lAddr, hAddr; |
||
| 254 | BYTE mask, display; |
||
| 255 | |||
| 256 | // check if point is in clipping region |
||
| 257 | if(_clipRgn) |
||
| 258 | { |
||
| 259 | if(x < _clipLeft) |
||
| 260 | return; |
||
| 261 | if(x > _clipRight) |
||
| 262 | return; |
||
| 263 | if(y < _clipTop) |
||
| 264 | return; |
||
| 265 | if(y > _clipBottom) |
||
| 266 | return; |
||
| 267 | } |
||
| 268 | |||
| 269 | // Assign a page address |
||
| 270 | if(y < 8) |
||
| 271 | page = 0xB0; |
||
| 272 | else if(y < 16) |
||
| 273 | page = 0xB1; |
||
| 274 | else if(y < 24) |
||
| 275 | page = 0xB2; |
||
| 276 | else if(y < 32) |
||
| 277 | page = 0xB3; |
||
| 278 | else if(y < 40) |
||
| 279 | page = 0xB4; |
||
| 280 | else if(y < 48) |
||
| 281 | page = 0xB5; |
||
| 282 | else if(y < 56) |
||
| 283 | page = 0xB6; |
||
| 284 | else |
||
| 285 | page = 0xB7; |
||
| 286 | |||
| 287 | add = x + OFFSET; |
||
| 288 | lAddr = 0x0F & add; // Low address |
||
| 289 | hAddr = 0x10 | (add >> 4); // High address |
||
| 290 | |||
| 291 | // Calculate mask from rows basically do a y%8 and remainder is bit position |
||
| 292 | add = y >> 3; // Divide by 8 |
||
| 293 | add <<= 3; // Multiply by 8 |
||
| 294 | add = y - add; // Calculate bit position |
||
| 295 | mask = 1 << add; // Left shift 1 by bit position |
||
| 296 | DeviceSelect(); |
||
| 297 | |||
| 298 | SetAddress(page, lAddr, hAddr); // Set the address (sets the page, |
||
| 299 | |||
| 300 | |||
| 301 | // lower and higher column address pointers) |
||
| 302 | display = SingleDeviceRead(); // Read to initiate Read transaction on PMP and dummy read |
||
| 303 | // (requirement for data synchronization in the controller) |
||
| 304 | display = SingleDeviceRead(); // Read again as a requirement for data synchronization in the display controller |
||
| 305 | display = SingleDeviceRead(); // Read actual data from from display buffer |
||
| 306 | |||
| 307 | if(_color > 0) // If non-zero for pixel on |
||
| 308 | display |= mask; // or in mask |
||
| 309 | else |
||
| 310 | // If 0 for pixel off |
||
| 311 | display &= ~mask; // and with inverted mask |
||
| 312 | SetAddress(page, lAddr, hAddr); // Set the address (sets the page, |
||
| 313 | |||
| 314 | // lower and higher column address pointers) |
||
| 315 | DeviceWrite(display); // restore the byte with manipulated bit |
||
| 316 | DeviceDeselect(); |
||
| 317 | } |
||
| 318 | |||
| 319 | /********************************************************************* |
||
| 320 | * Function: BYTE GetPixel(SHORT x, SHORT y) |
||
| 321 | * |
||
| 322 | * PreCondition: none |
||
| 323 | * |
||
| 324 | * Input: x,y - pixel coordinates |
||
| 325 | * |
||
| 326 | * Output: pixel color |
||
| 327 | * |
||
| 328 | * Side Effects: none |
||
| 329 | * |
||
| 330 | * Overview: return pixel color at x,y position |
||
| 331 | * |
||
| 332 | * Note: none |
||
| 333 | * |
||
| 334 | ********************************************************************/ |
||
| 335 | BYTE GetPixel(SHORT x, SHORT y) |
||
| 336 | { |
||
| 337 | BYTE page, add, lAddr, hAddr; |
||
| 338 | BYTE mask, temp, display; |
||
| 339 | |||
| 340 | // check if point is in clipping region |
||
| 341 | if(_clipRgn) |
||
| 342 | { |
||
| 343 | if(x < _clipLeft) |
||
| 344 | return (0); |
||
| 345 | if(x > _clipRight) |
||
| 346 | return (0); |
||
| 347 | if(y < _clipTop) |
||
| 348 | return (0); |
||
| 349 | if(y > _clipBottom) |
||
| 350 | return (0); |
||
| 351 | } |
||
| 352 | |||
| 353 | // Assign a page address |
||
| 354 | if(y < 8) |
||
| 355 | page = 0xB0; |
||
| 356 | else if(y < 16) |
||
| 357 | page = 0xB1; |
||
| 358 | else if(y < 24) |
||
| 359 | page = 0xB2; |
||
| 360 | else if(y < 32) |
||
| 361 | page = 0xB3; |
||
| 362 | else if(y < 40) |
||
| 363 | page = 0xB4; |
||
| 364 | else if(y < 48) |
||
| 365 | page = 0xB5; |
||
| 366 | else if(y < 56) |
||
| 367 | page = 0xB6; |
||
| 368 | else |
||
| 369 | page = 0xB7; |
||
| 370 | |||
| 371 | add = x + OFFSET; |
||
| 372 | lAddr = 0x0F & add; // Low address |
||
| 373 | hAddr = 0x10 | (add >> 4); // High address |
||
| 374 | |||
| 375 | // Calculate mask from rows basically do a y%8 and remainder is bit position |
||
| 376 | temp = y >> 3; // Divide by 8 |
||
| 377 | temp <<= 3; // Multiply by 8 |
||
| 378 | temp = y - temp; // Calculate bit position |
||
| 379 | mask = 1 << temp; // Left shift 1 by bit position |
||
| 380 | DeviceSelect(); |
||
| 381 | |||
| 382 | SetAddress(page, lAddr, hAddr); // Set the address (sets the page, |
||
| 383 | |||
| 384 | // lower and higher column address pointers) |
||
| 385 | display = SingleDeviceRead(); // Read to initiate Read transaction on PMP |
||
| 386 | // Dummy Read (requirement for data synchronization in the controller) |
||
| 387 | display = DeviceRead(); // Read data from display buffer |
||
| 388 | DeviceDeselect(); |
||
| 389 | |||
| 390 | return (display & mask); // mask all other bits and return the result |
||
| 391 | } |
||
| 392 | |||
| 393 | /********************************************************************* |
||
| 394 | * Function: void ClearDevice(void) |
||
| 395 | * |
||
| 396 | * PreCondition: none |
||
| 397 | * |
||
| 398 | * Input: none |
||
| 399 | * |
||
| 400 | * Output: none |
||
| 401 | * |
||
| 402 | * Side Effects: none |
||
| 403 | * |
||
| 404 | * Overview: clears screen with current color |
||
| 405 | * |
||
| 406 | * Note: none |
||
| 407 | * |
||
| 408 | ********************************************************************/ |
||
| 409 | void ClearDevice(void) |
||
| 410 | { |
||
| 411 | BYTE i, j; |
||
| 412 | DeviceSelect(); |
||
| 413 | for(i = 0xB0; i < 0xB8; i++) |
||
| 414 | { // Go through all 8 pages |
||
| 415 | SetAddress(i, 0x00, 0x10); |
||
| 416 | for(j = 0; j < 132; j++) |
||
| 417 | { // Write to all 132 bytes |
||
| 418 | DeviceWrite(_color); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | DeviceDeselect(); |
||
| 423 | } |
Powered by WebSVN v2.8.3