PIC18 LaurTec Library  3.1.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
GLCD_KS0108B.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.1
5 Created on Date : 26/05/2012
6 Last update : 25/01/2013
7 
8 CopyRight 2006-2013 all rights are reserved
9 
10 ********************************************************
11 SOFTWARE LICENSE AGREEMENT
12 ********************************************************
13 
14 The usage of the supplied software imply the acceptance of the following license.
15 
16 The software supplied herewith by Mauro Laurenti (the Author) is intended for
17 use solely and exclusively on Microchip PIC Microcontroller (registered mark).
18 The software is owned by the Author, and is protected under applicable
19 copyright laws. All rights are reserved.
20 Any use in violation of the foregoing restrictions may subject the
21 user to criminal sanctions under applicable laws, as well as to civil liability
22 for the breach of the terms and conditions of this license.
23 Commercial use is forbidden without a written acknowledgment with the Author.
24 Personal or educational use is allowed if the application containing the
25 following software doesn't aim to commercial use or monetary earning of any kind.
26 
27 THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
28 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE AUTHOR SHALL NOT,
31 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 
34 *******************************************************************************/
35 
36 #ifdef __XC8
37  #include <xc.h>
38  #include <stdlib.h>
39 #endif
40 
41 #include "GLCD_KS0108B.h"
42 
43 #include "delay.h"
44 #include "delays.h"
45 
46 
47 unsigned char font_width = 0;
48 unsigned char font_height = 0;
49 
50 
51 //*********************************************
52 // get_busy_status_GLCD
53 //*********************************************
55 
56  unsigned char busy_flag = 1;
57 
58  while (busy_flag) {
59 
61  busy_flag = 1;
62  else
63  busy_flag = 0;
64  }
65 }
66 
67 
68 //*********************************************
69 // get_controller_status_GLCD
70 //*********************************************
71 unsigned char get_controller_status_GLCD (void) {
72 
73  unsigned char controller_status;
74 
76 
77  GLCD_E = LOW_GLCD;
78  Delay1TCY();
81  Delay1TCY();
82  GLCD_E = HIGH_GLCD;
83  Delay1TCY();
84  controller_status = GLCD_DATA_READ;
85 
87 
88  return (controller_status);
89 }
90 
91 
92 //*********************************************
93 // write_command_GLCD
94 //*********************************************
95 void write_command_GLCD (unsigned char command_to_send) {
96 
97  GLCD_E = LOW_GLCD;
98  Delay1TCY();
99  Delay1TCY();
100  GLCD_R_W = LOW_GLCD;
101  GLCD_D_I = LOW_GLCD;
102  Delay1TCY();
103 
104  GLCD_E = HIGH_GLCD;
105  GLCD_DATA_WRITE = command_to_send;
106  Delay1TCY();
107  GLCD_E = LOW_GLCD;
108 
109  Delay1TCY();
111  GLCD_D_I = LOW_GLCD;
112 
113  Delay1TCY();
114 
115  GLCD_E = HIGH_GLCD;
116 }
117 
118 
119 //*********************************************
120 // set_display_GLCD
121 //*********************************************
122 void set_display_GLCD (unsigned char display_status){
123 
125  GLCD_CS2 = LOW_GLCD;
126 
128  if (display_status)
130  else
132 
134 
135  GLCD_CS1 = LOW_GLCD;
137 
139  if (display_status)
141  else
143 
145 
146  GLCD_CS1 = LOW_GLCD;
147  GLCD_CS2 = LOW_GLCD;
148 
149 }
150 
151 //*********************************************
152 // set_page_GLCD
153 //*********************************************
154 void set_page_GLCD (unsigned char controller_page) {
155 
157  write_command_GLCD (COMM_PAGE_SELECT | controller_page);
159 }
160 
161 
162 //*********************************************
163 // set_address_GLCD
164 //*********************************************
165 void set_address_GLCD (unsigned char controller_address){
166 
168  write_command_GLCD (COMM_ADDRESS_SELECT | controller_address);
170 }
171 
172 
173 //*********************************************
174 // set_vertical_offset_GLCD
175 //*********************************************
176 void set_vertical_offset_GLCD (unsigned char vertical_offset){
177 
179  GLCD_CS2 = LOW_GLCD;
180 
181 // wait_busy_status_GLCD ();
182  write_command_GLCD (COMM_START_LINE | vertical_offset);
183 // wait_busy_status_GLCD ();
184 
185  GLCD_CS1 = LOW_GLCD;
187 
188 // wait_busy_status_GLCD ();
189  write_command_GLCD (COMM_START_LINE | vertical_offset);
190 // wait_busy_status_GLCD ();
191 
192  GLCD_CS1 = LOW_GLCD;
193  GLCD_CS2 = LOW_GLCD;
194 
195 }
196 
197 
198 //*********************************************
199 // write_data_GLCD
200 //*********************************************
201 void write_data_GLCD (unsigned char data_to_send) {
202 
203  GLCD_E = LOW_GLCD;
204  Delay1TCY();
205  Delay1TCY();
206  GLCD_R_W = LOW_GLCD;
208  Delay1TCY();
209  Delay1TCY();
210  GLCD_E = HIGH_GLCD;
211  GLCD_DATA_WRITE = data_to_send;
212  Delay1TCY();
213  Delay1TCY();
214  Delay1TCY();
215  GLCD_E = LOW_GLCD;
216 
217  Delay1TCY();
219  GLCD_D_I = LOW_GLCD;
220 
221  Delay1TCY();
222 
223  GLCD_E = HIGH_GLCD;
224 
225 }
226 
227 
228 //*********************************************
229 // clear_GLCD
230 //*********************************************
231 void clear_GLCD (unsigned char filling_color) {
232 
233  unsigned char page = 0;
234  unsigned char address = 0;
235 
237  GLCD_CS2 = LOW_GLCD;
238 
239  set_address_GLCD (address);
240 
241  for (page = 0; page < NUMBER_OF_PAGES; page++) {
242  set_page_GLCD (page);
243  for (address = 0; address < Y_RESOLUTION; address++) {
244 
245  // wait_busy_status_GLCD ();
246  write_data_GLCD (filling_color);
247  // wait_busy_status_GLCD ();
248  }
249  }
250 
251  GLCD_CS1 = LOW_GLCD;
253 
254 
255  address = 0;
256  set_address_GLCD (address);
257 
258  for (page = 0; page < NUMBER_OF_PAGES; page++) {
259  set_page_GLCD (page);
260 
261  for (address = 0; address < Y_RESOLUTION; address++) {
262  // wait_busy_status_GLCD ();
263  write_data_GLCD (filling_color);
264  // wait_busy_status_GLCD ();
265  }
266  }
267 
268  GLCD_CS1 = LOW_GLCD;
269  GLCD_CS2 = LOW_GLCD;
270 
271 }
272 
273 //*********************************************
274 // read_data_GLCD
275 //*********************************************
276 unsigned char read_data_GLCD (void) {
277 
278  unsigned char data_out;
279 
281 
282  GLCD_E = LOW_GLCD;
283  Delay1TCY();
284  Delay1TCY();
287  Delay1TCY();
288  GLCD_E = HIGH_GLCD;
289  Delay1TCY();
290  data_out = GLCD_DATA_READ;
291  Delay1TCY();
292  Delay1TCY();
293  GLCD_E = LOW_GLCD;
294  Delay1TCY();
295  GLCD_R_W = LOW_GLCD;
296  GLCD_D_I = LOW_GLCD;
297 
299 
300  return (data_out);
301 }
302 
303 //*********************************************
304 // plot_xy_GLCD
305 //*********************************************
306 void plot_xy_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char filling_color){
307 
308  unsigned char data_byte;
309  unsigned char data_mask;
310 
311  y_pos = Y_RESOLUTION-y_pos -1;
312 
313  if (x_pos < CONTROLLER_MAX_X) {
314 
316  GLCD_CS2 = LOW_GLCD;
317  set_address_GLCD (x_pos);
318  set_page_GLCD (y_pos >> 3);
319  //dummy read
320  data_byte = read_data_GLCD ();
321  data_byte = read_data_GLCD ();
322 
323  set_address_GLCD (x_pos);
324 
325  } else {
326 
327  GLCD_CS1 = LOW_GLCD;
330  set_page_GLCD (y_pos >> 3);
331  //dummy read
332  data_byte = read_data_GLCD ();
333  data_byte = read_data_GLCD ();
334 
336  }
337 
338  data_mask = 0x01 << (y_pos & 0x07);
339 
340  if (filling_color == FILLING_BLACK)
341  data_byte = data_byte | data_mask;
342  else
343  data_byte = data_byte & ~data_mask;
344 
345 // wait_busy_status_GLCD ();
346  write_data_GLCD (data_byte);
347 // wait_busy_status_GLCD ();
348 
349 
350  GLCD_CS1 = LOW_GLCD;
351  GLCD_CS2 = LOW_GLCD;
352 }
353 
354 
355 //*********************************************
356 // draw_vertical_line_GLCD
357 //*********************************************
358 void draw_vertical_line_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){
359 
360  unsigned char i;
361 
362  for (i = 0 ; i<length; i++) {
363  plot_xy_GLCD (x_origin, y_origin + i, filling_color);
364  }
365 }
366 
367 
368 //*********************************************
369 // draw_horizontal_line_GLCD
370 //*********************************************
371 void draw_horizontal_line_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){
372 
373  unsigned char i;
374 
375  for (i = 0 ; i<length; i++) {
376  plot_xy_GLCD (x_origin + i, y_origin, filling_color);
377  }
378 }
379 
380 
381 //*********************************************
382 // draw_window_GLCD
383 //*********************************************
384 
385 void draw_window_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){
386 
387  unsigned char i;
388 
389  draw_horizontal_line_GLCD (x_origin, y_origin, width, filling_color);
390  // + 1 is required to close the window
391  draw_horizontal_line_GLCD (x_origin, y_origin + height, width+1, filling_color);
392 
393  draw_vertical_line_GLCD (x_origin, y_origin, height, filling_color);
394  draw_vertical_line_GLCD (x_origin + width, y_origin, height, filling_color);
395 
396 }
397 
398 
399 //*********************************************
400 // draw_box_GLCD
401 //*********************************************
402 
403 void draw_box_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){
404 
405  unsigned char i;
406 
407  for (i=0; i < width; i++)
408  draw_vertical_line_GLCD (x_origin +i, y_origin, height, filling_color);
409 
410 }
411 
412 //*********************************************
413 // write_char_GLCD
414 //*********************************************
415 void write_char_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char character){
416 
417  unsigned char i;
418  unsigned char data;
419  unsigned char x;
420  unsigned char bit_value;
421 
422  for (i = 0 ; i<font_width; i++){
423 
424  data = font_5x7[character - 32][i];
425 
426  for (x = 0 ; x<= font_height; x++) {
427 
428  if (data & (0x80 >> x))
429  bit_value = FILLING_BLACK;
430  else
431  bit_value = FILLING_WHITE;
432 
433  plot_xy_GLCD (x_pos + i, y_pos + x, bit_value);
434 
435  }
436  }
437 }
438 
439 //*********************************************
440 // write_string_GLCD
441 //*********************************************
442 
443 void write_string_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char * character_array){
444 
445 unsigned char i = 0;
446 
447  while (character_array[i]) {
448 
449  write_char_GLCD (x_pos + (i* (font_width+ GLCD_FONT_SPACE)) , y_pos,character_array[i]);
450 
451  i++;
452  }
453 }
454 
455 
456 //*********************************************
457 // write_message_GLCD
458 //*********************************************
459 #ifndef __XC8
460 void write_message_GLCD (unsigned char x_pos, unsigned char y_pos, const rom unsigned char * character) {
461 #endif
462 
463 #ifdef __XC8
464 void write_message_GLCD (unsigned char x_pos, unsigned char y_pos, const unsigned char * character) {
465 #endif
466 
467 unsigned char i = 0;
468 
469  while (character[i]) {
470 
471  write_char_GLCD (x_pos + (i*(font_width + GLCD_FONT_SPACE)), y_pos,character[i]);
472 
473  i++;
474  }
475 
476 }
477 
478 
479 //*********************************************
480 // draw_picture_GLCD
481 //*********************************************
482 #ifndef __XC8
483 void draw_picture_GLCD (rom unsigned char * picture_table){
484 #endif
485 
486 #ifdef __XC8
487 void draw_picture_GLCD (const unsigned char * picture_table){
488 #endif
489  unsigned char page = 0;
490  unsigned char address = 0;
491 
492 
493  for (page = 0; page < NUMBER_OF_PAGES; page++) {
495  GLCD_CS2 = LOW_GLCD;
496 
497  set_address_GLCD (address);
498  set_page_GLCD (page);
499 
500  for (address = 0; address < Y_RESOLUTION; address++) {
501 
502  write_data_GLCD (* picture_table);
503  picture_table++;
504  }
505 
506  GLCD_CS1 = LOW_GLCD;
508 
509  address = 0;
510  set_address_GLCD (address);
511  set_page_GLCD (page);
512 
513  for (address = 0; address < Y_RESOLUTION; address++) {
514 
515  write_data_GLCD (* picture_table);
516  picture_table++;
517  }
518 
519  }
520 
521  GLCD_CS1 = LOW_GLCD;
522  GLCD_CS2 = LOW_GLCD;
523 }
524 
525 
526 //*********************************************
527 // write_integer_GLCD
528 //*********************************************
529 void write_integer_GLCD (unsigned char x_pos, unsigned char y_pos, int value, char number_of_digits){
530 
531  // The array size is 5 plus end of string \0
532  unsigned char convertedInt [6];
533 
534  // Index used to shift to the right the digit
535  char index;
536 
537  // Integer is converted to string
538  #ifndef __XC8
539  itoa (value, (char*) convertedInt);
540  #endif
541 
542  #ifdef __XC8
543  itoa ((char*) convertedInt, value,10);
544  #endif
545 
546  if (number_of_digits >0 ) {
547 
548  convertedInt[number_of_digits] = '\0';
549 
550  // Shift the digit to the right removing the empty ones
551  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')) {
552  for (index = number_of_digits-1; index > 0; index--){
553  convertedInt[index] = convertedInt[index-1];
554  convertedInt[index-1] = ' ';
555  }
556  }
557  }
558 
559  write_string_GLCD (x_pos, y_pos, convertedInt);
560 }
561 
562 
563 //*********************************************
564 // set_font_GLCD
565 //*********************************************
566 void set_font_GLCD (unsigned char width, unsigned char height){
567 
568  font_width = width;
569  font_height = height;
570 }
571 
572 
573 //*********************************************
574 // backlight_GLCD
575 //*********************************************
576 void backlight_GLCD(unsigned char backlight) {
577 
578  GLCD_LED = backlight;
579 }
580 
581 
582 //*********************************************
583 // initialize_GLCD
584 //*********************************************
585 void initialize_GLCD (void) {
586 
588 
589  GLCD_RST = LOW_GLCD;
590  GLCD_LED = LOW_GLCD;
591  GLCD_E = HIGH_GLCD;
593  GLCD_D_I = LOW_GLCD;
594  GLCD_CS1 = LOW_GLCD;
595  GLCD_CS2 = LOW_GLCD;
596 
597  delay_ms (100);
598 
600 
606 
607 }
608 
609 
610