C18 LaurTec Library
2.5
Open Source C Library for PIC18 Microcontrollers
|
00001 /********************************************************************************************** 00002 00003 Author : Mauro Laurenti 00004 Version : 1.0 00005 Date : 26/05/2012 00006 00007 CopyRight 2006-2012 all rights are reserved 00008 00009 ******************************************************** 00010 SOFTWARE LICENSE AGREEMENT 00011 ******************************************************** 00012 00013 The usage of the supplied software imply the acceptance of the following license. 00014 00015 The software supplied herewith by Mauro Laurenti (the Author) 00016 is intended for use solely and exclusively on Microchip PIC Microcontroller (registered mark). 00017 The software is owned by the Author, and is protected under applicable copyright laws. 00018 All rights are reserved. 00019 Any use in violation of the foregoing restrictions may subject the 00020 user to criminal sanctions under applicable laws (Italian or International ones), as well as to 00021 civil liability for the breach of the terms and conditions of this license. 00022 Commercial use is forbidden without a written acknowledgment with the Author. 00023 Personal or educational use is allowed if the application containing the following 00024 software doesn't aim to commercial use or monetary earning of any kind. 00025 00026 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 00027 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 00028 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00029 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT, 00030 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00031 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00032 00033 **********************************************************************************************/ 00034 00035 00036 #include "GLCD_KS0108B.h" 00037 #include "delay.h" 00038 #include "delays.h" 00039 00040 00041 unsigned char font_width = 0; 00042 unsigned char font_height = 0; 00043 00044 00045 //********************************************* 00046 // get_busy_status_GLCD 00047 //********************************************* 00048 void wait_busy_status_GLCD (void){ 00049 00050 unsigned char busy_flag = 1; 00051 00052 while (busy_flag) { 00053 00054 if (get_controller_status_GLCD () & GLCD_BUSY_FLAG) 00055 busy_flag = 1; 00056 else 00057 busy_flag = 0; 00058 } 00059 } 00060 00061 00062 //********************************************* 00063 // get_controller_status_GLCD 00064 //********************************************* 00065 unsigned char get_controller_status_GLCD (void) { 00066 00067 unsigned char controller_status; 00068 00069 GLCD_DATA_DIRECTION = INPUT; 00070 00071 GLCD_E = LOW; 00072 Delay1TCY () ; 00073 GLCD_R_W = HIGH; 00074 GLCD_D_I = LOW; 00075 Delay1TCY (); 00076 GLCD_E = HIGH; 00077 Delay1TCY (); 00078 controller_status = GLCD_DATA_READ; 00079 00080 GLCD_DATA_DIRECTION = OUTPUT; 00081 00082 return (controller_status); 00083 } 00084 00085 00086 //********************************************* 00087 // write_command_GLCD 00088 //********************************************* 00089 void write_command_GLCD (unsigned char command_to_send) { 00090 00091 GLCD_E = LOW; 00092 Delay1TCY (); 00093 Delay1TCY (); 00094 GLCD_R_W = LOW; 00095 GLCD_D_I = LOW; 00096 Delay1TCY (); 00097 00098 GLCD_E = HIGH; 00099 GLCD_DATA_WRITE = command_to_send; 00100 Delay1TCY (); 00101 GLCD_E = LOW; 00102 00103 Delay1TCY (); 00104 GLCD_R_W = HIGH; 00105 GLCD_D_I = LOW; 00106 00107 Delay1TCY (); 00108 00109 GLCD_E = HIGH; 00110 } 00111 00112 00113 //********************************************* 00114 // set_display_GLCD 00115 //********************************************* 00116 void set_display_GLCD (unsigned char display_status){ 00117 00118 GLCD_CS1 = HIGH; 00119 GLCD_CS2 = LOW; 00120 00121 wait_busy_status_GLCD (); 00122 if (display_status) 00123 write_command_GLCD (COMM_DISPLAY_ON); 00124 else 00125 write_command_GLCD (COMM_DISPLAY_OFF); 00126 00127 wait_busy_status_GLCD (); 00128 00129 GLCD_CS1 = LOW; 00130 GLCD_CS2 = HIGH; 00131 00132 wait_busy_status_GLCD (); 00133 if (display_status) 00134 write_command_GLCD (COMM_DISPLAY_ON); 00135 else 00136 write_command_GLCD (COMM_DISPLAY_OFF); 00137 00138 wait_busy_status_GLCD (); 00139 00140 GLCD_CS1 = LOW; 00141 GLCD_CS2 = LOW; 00142 00143 } 00144 00145 //********************************************* 00146 // set_page_GLCD 00147 //********************************************* 00148 void set_page_GLCD (unsigned char controller_page) { 00149 00150 wait_busy_status_GLCD (); 00151 write_command_GLCD (COMM_PAGE_SELECT | controller_page); 00152 wait_busy_status_GLCD (); 00153 } 00154 00155 00156 //********************************************* 00157 // set_address_GLCD 00158 //********************************************* 00159 void set_address_GLCD (unsigned char controller_address){ 00160 00161 wait_busy_status_GLCD (); 00162 write_command_GLCD (COMM_ADDRESS_SELECT | controller_address); 00163 wait_busy_status_GLCD (); 00164 } 00165 00166 00167 //********************************************* 00168 // set_vertical_offset_GLCD 00169 //********************************************* 00170 void set_vertical_offset_GLCD (unsigned char vertical_offset){ 00171 00172 GLCD_CS1 = HIGH; 00173 GLCD_CS2 = LOW; 00174 00175 // wait_busy_status_GLCD (); 00176 write_command_GLCD (COMM_START_LINE | vertical_offset); 00177 // wait_busy_status_GLCD (); 00178 00179 GLCD_CS1 = LOW; 00180 GLCD_CS2 = HIGH; 00181 00182 // wait_busy_status_GLCD (); 00183 write_command_GLCD (COMM_START_LINE | vertical_offset); 00184 // wait_busy_status_GLCD (); 00185 00186 GLCD_CS1 = LOW; 00187 GLCD_CS2 = LOW; 00188 00189 } 00190 00191 00192 //********************************************* 00193 // write_data_GLCD 00194 //********************************************* 00195 void write_data_GLCD (unsigned char data_to_send) { 00196 00197 GLCD_E = LOW; 00198 Delay1TCY (); 00199 Delay1TCY (); 00200 GLCD_R_W = LOW; 00201 GLCD_D_I = HIGH; 00202 Delay1TCY (); 00203 Delay1TCY (); 00204 GLCD_E = HIGH; 00205 GLCD_DATA_WRITE = data_to_send; 00206 Delay1TCY (); 00207 Delay1TCY (); 00208 Delay1TCY (); 00209 GLCD_E = LOW; 00210 00211 Delay1TCY (); 00212 GLCD_R_W = HIGH; 00213 GLCD_D_I = LOW; 00214 00215 Delay1TCY (); 00216 00217 GLCD_E = HIGH; 00218 00219 } 00220 00221 00222 //********************************************* 00223 // clear_GLCD 00224 //********************************************* 00225 void clear_GLCD (unsigned char filling_color) { 00226 00227 unsigned char page = 0; 00228 unsigned char address = 0; 00229 00230 GLCD_CS1 = HIGH; 00231 GLCD_CS2 = LOW; 00232 00233 set_address_GLCD (address); 00234 00235 for (page = 0; page < NUMBER_OF_PAGES; page++) { 00236 set_page_GLCD (page); 00237 for (address = 0; address < Y_RESOLUTION; address++) { 00238 00239 // wait_busy_status_GLCD (); 00240 write_data_GLCD (filling_color); 00241 // wait_busy_status_GLCD (); 00242 } 00243 } 00244 00245 GLCD_CS1 = LOW; 00246 GLCD_CS2 = HIGH; 00247 00248 00249 address = 0; 00250 set_address_GLCD (address); 00251 00252 for (page = 0; page < NUMBER_OF_PAGES; page++) { 00253 set_page_GLCD (page); 00254 00255 for (address = 0; address < Y_RESOLUTION; address++) { 00256 // wait_busy_status_GLCD (); 00257 write_data_GLCD (filling_color); 00258 // wait_busy_status_GLCD (); 00259 } 00260 } 00261 00262 GLCD_CS1 = LOW; 00263 GLCD_CS2 = LOW; 00264 00265 } 00266 00267 //********************************************* 00268 // read_data_GLCD 00269 //********************************************* 00270 unsigned char read_data_GLCD (void) { 00271 00272 unsigned char data_out; 00273 00274 GLCD_DATA_DIRECTION = INPUT; 00275 00276 GLCD_E = LOW; 00277 Delay1TCY (); 00278 Delay1TCY (); 00279 GLCD_R_W = HIGH; 00280 GLCD_D_I = HIGH; 00281 Delay1TCY (); 00282 GLCD_E = HIGH; 00283 Delay1TCY (); 00284 data_out = GLCD_DATA_READ; 00285 Delay1TCY (); 00286 Delay1TCY (); 00287 GLCD_E = LOW; 00288 Delay1TCY (); 00289 GLCD_R_W = LOW; 00290 GLCD_D_I = LOW; 00291 00292 GLCD_DATA_DIRECTION = OUTPUT; 00293 00294 return (data_out); 00295 } 00296 00297 //********************************************* 00298 // plot_xy_GLCD 00299 //********************************************* 00300 void plot_xy_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char filling_color){ 00301 00302 unsigned char data_byte; 00303 unsigned char data_mask; 00304 00305 y_pos = Y_RESOLUTION-y_pos -1; 00306 00307 if (x_pos < CONTROLLER_MAX_X) { 00308 00309 GLCD_CS1 = HIGH; 00310 GLCD_CS2 = LOW; 00311 set_address_GLCD (x_pos); 00312 set_page_GLCD (y_pos >> 3); 00313 //dummy read 00314 data_byte = read_data_GLCD (); 00315 data_byte = read_data_GLCD (); 00316 00317 set_address_GLCD (x_pos); 00318 00319 } else { 00320 00321 GLCD_CS1 = LOW; 00322 GLCD_CS2 = HIGH; 00323 set_address_GLCD (x_pos-CONTROLLER_MAX_X); 00324 set_page_GLCD (y_pos >> 3); 00325 //dummy read 00326 data_byte = read_data_GLCD (); 00327 data_byte = read_data_GLCD (); 00328 00329 set_address_GLCD (x_pos-CONTROLLER_MAX_X); 00330 } 00331 00332 data_mask = 0x01 << (y_pos & 0x07); 00333 00334 if (filling_color == FILLING_BLACK) 00335 data_byte = data_byte | data_mask; 00336 else 00337 data_byte = data_byte & ~data_mask; 00338 00339 // wait_busy_status_GLCD (); 00340 write_data_GLCD (data_byte); 00341 // wait_busy_status_GLCD (); 00342 00343 00344 GLCD_CS1 = LOW; 00345 GLCD_CS2 = LOW; 00346 } 00347 00348 00349 //********************************************* 00350 // draw_vertical_line_GLCD 00351 //********************************************* 00352 void draw_vertical_line_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){ 00353 00354 unsigned char i; 00355 00356 for (i = 0 ; i<length; i++) { 00357 plot_xy_GLCD (x_origin, y_origin + i, filling_color); 00358 } 00359 } 00360 00361 00362 //********************************************* 00363 // draw_horizontal_line_GLCD 00364 //********************************************* 00365 void draw_horizontal_line_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){ 00366 00367 unsigned char i; 00368 00369 for (i = 0 ; i<length; i++) { 00370 plot_xy_GLCD (x_origin + i, y_origin, filling_color); 00371 } 00372 } 00373 00374 00375 //********************************************* 00376 // draw_window_GLCD 00377 //********************************************* 00378 00379 void draw_window_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){ 00380 00381 unsigned char i; 00382 00383 draw_horizontal_line_GLCD (x_origin, y_origin, width, filling_color); 00384 // + 1 is required to close the window 00385 draw_horizontal_line_GLCD (x_origin, y_origin + height, width+1, filling_color); 00386 00387 draw_vertical_line_GLCD (x_origin, y_origin, height, filling_color); 00388 draw_vertical_line_GLCD (x_origin + width, y_origin, height, filling_color); 00389 00390 } 00391 00392 00393 //********************************************* 00394 // draw_box_GLCD 00395 //********************************************* 00396 00397 void draw_box_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){ 00398 00399 unsigned char i; 00400 00401 for (i=0; i < width; i++) 00402 draw_vertical_line_GLCD (x_origin +i, y_origin, height, filling_color); 00403 00404 } 00405 00406 //********************************************* 00407 // write_char_GLCD 00408 //********************************************* 00409 void write_char_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char character){ 00410 00411 unsigned char i; 00412 unsigned char data; 00413 unsigned char x; 00414 unsigned char bit_value; 00415 int data_index; 00416 00417 for (i = 0 ; i<font_width; i++){ 00418 00419 data = font_5x7[character - 32][i]; 00420 00421 for (x = 0 ; x<= font_height; x++) { 00422 00423 if (data & (0x80 >> x)) 00424 bit_value = FILLING_BLACK; 00425 else 00426 bit_value = FILLING_WHITE; 00427 00428 plot_xy_GLCD (x_pos + i, y_pos + x, bit_value); 00429 00430 } 00431 } 00432 } 00433 00434 //********************************************* 00435 // write_string_GLCD 00436 //********************************************* 00437 00438 void write_string_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char * character_array){ 00439 00440 unsigned char i = 0; 00441 00442 while (character_array[i]) { 00443 00444 write_char_GLCD (x_pos + (i* (font_width+ GLCD_FONT_SPACE)) , y_pos,character_array[i]); 00445 00446 i++; 00447 } 00448 } 00449 00450 00451 //********************************************* 00452 // write_message_GLCD 00453 //********************************************* 00454 void write_message_GLCD (unsigned char x_pos, unsigned char y_pos, const rom unsigned char * character) { 00455 00456 unsigned char i = 0; 00457 00458 while (character[i]) { 00459 00460 write_char_GLCD (x_pos + (i*(font_width + GLCD_FONT_SPACE)), y_pos,character[i]); 00461 00462 i++; 00463 } 00464 00465 } 00466 00467 00468 //********************************************* 00469 // draw_picture_GLCD 00470 //********************************************* 00471 void draw_picture_GLCD (rom unsigned char * picture_table){ 00472 00473 unsigned char page = 0; 00474 unsigned char address = 0; 00475 00476 00477 for (page = 0; page < NUMBER_OF_PAGES; page++) { 00478 GLCD_CS1 = HIGH; 00479 GLCD_CS2 = LOW; 00480 00481 set_address_GLCD (address); 00482 set_page_GLCD (page); 00483 00484 for (address = 0; address < Y_RESOLUTION; address++) { 00485 00486 write_data_GLCD (* picture_table); 00487 picture_table++; 00488 } 00489 00490 GLCD_CS1 = LOW; 00491 GLCD_CS2 = HIGH; 00492 00493 address = 0; 00494 set_address_GLCD (address); 00495 set_page_GLCD (page); 00496 00497 for (address = 0; address < Y_RESOLUTION; address++) { 00498 00499 write_data_GLCD (* picture_table); 00500 picture_table++; 00501 } 00502 00503 } 00504 00505 GLCD_CS1 = LOW; 00506 GLCD_CS2 = LOW; 00507 } 00508 00509 00510 //********************************************* 00511 // write_integer_GLCD 00512 //********************************************* 00513 void write_integer_GLCD (unsigned char x_pos, unsigned char y_pos, int value, char number_of_digits){ 00514 00515 // The array size is 5 plus end of string \0 00516 unsigned char convertedInt [6]; 00517 00518 // Index used to shift to the right the digit 00519 char index; 00520 00521 // Integer is converted to string 00522 itoa (value, (char*) convertedInt); 00523 00524 if (number_of_digits >0 ) { 00525 00526 convertedInt[number_of_digits] = '\0'; 00527 00528 // Shift the digit to the right removing the empty one 00529 while (!isdigit(convertedInt[number_of_digits-1])) { 00530 00531 for (index = number_of_digits-1; index > 0; index--){ 00532 convertedInt[index] = convertedInt[index-1]; 00533 convertedInt[index-1] = ' '; 00534 } 00535 } 00536 } 00537 00538 write_string_GLCD (x_pos, y_pos, convertedInt); 00539 } 00540 00541 00542 //********************************************* 00543 // set_font_GLCD 00544 //********************************************* 00545 void set_font_GLCD (unsigned char width, unsigned char height){ 00546 00547 font_width = width; 00548 font_height = height; 00549 } 00550 00551 00552 //********************************************* 00553 // backlight_GLCD 00554 //********************************************* 00555 void backlight_GLCD(unsigned char backlight) { 00556 00557 GLCD_LED = backlight; 00558 } 00559 00560 00561 //********************************************* 00562 // initialize_GLCD 00563 //********************************************* 00564 void initialize_GLCD (void) { 00565 00566 GLCD_DATA_DIRECTION = OUTPUT; 00567 00568 GLCD_RST = LOW; 00569 GLCD_LED = LOW; 00570 GLCD_E = HIGH; 00571 GLCD_R_W = HIGH; 00572 GLCD_D_I = LOW; 00573 GLCD_CS1 = LOW; 00574 GLCD_CS2 = LOW; 00575 00576 delay_ms (100); 00577 00578 GLCD_RST = HIGH; 00579 00580 backlight_GLCD (TURN_OFF_LED); 00581 set_vertical_offset_GLCD (0); 00582 clear_GLCD (FILLING_WHITE); 00583 set_display_GLCD (TURN_ON_DISPLAY); 00584 set_font_GLCD (GLCD_FONT_WIDTH_5, GLCD_FONT_HEIGTH_7); 00585 00586 } 00587 00588 00589