PIC18 LaurTec Library  3.2.0
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 acknowledgement 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 #endif
39 
40 #include "GLCD_KS0108B.h"
41 
42 
43 unsigned char font_width = 0;
44 unsigned char font_height = 0;
45 
46 
47 //*********************************************
48 // get_busy_status_GLCD
49 //*********************************************
51 
52  unsigned char busy_flag = 1;
53 
54  while (busy_flag) {
55 
57  busy_flag = 1;
58  else
59  busy_flag = 0;
60  }
61 }
62 
63 
64 //*********************************************
65 // get_controller_status_GLCD
66 //*********************************************
67 unsigned char get_controller_status_GLCD (void) {
68 
69  unsigned char controller_status;
70 
72 
73  GLCD_E = LOW_GLCD;
74  NOP_DELAY;
77  NOP_DELAY;
78  GLCD_E = HIGH_GLCD;
79  NOP_DELAY;
80  controller_status = GLCD_DATA_READ;
81 
83 
84  return (controller_status);
85 }
86 
87 
88 //*********************************************
89 // write_command_GLCD
90 //*********************************************
91 void write_command_GLCD (unsigned char command_to_send) {
92 
93  GLCD_E = LOW_GLCD;
94  NOP_DELAY;
95  NOP_DELAY;
98  NOP_DELAY;
99 
100  GLCD_E = HIGH_GLCD;
101  GLCD_DATA_WRITE = command_to_send;
102  NOP_DELAY;
103  GLCD_E = LOW_GLCD;
104 
105  NOP_DELAY;
107  GLCD_D_I = LOW_GLCD;
108 
109  NOP_DELAY;
110 
111  GLCD_E = HIGH_GLCD;
112 }
113 
114 
115 //*********************************************
116 // set_display_GLCD
117 //*********************************************
118 void set_display_GLCD (unsigned char display_status){
119 
121  GLCD_CS2 = LOW_GLCD;
122 
124  if (display_status)
126  else
128 
130 
131  GLCD_CS1 = LOW_GLCD;
133 
135  if (display_status)
137  else
139 
141 
142  GLCD_CS1 = LOW_GLCD;
143  GLCD_CS2 = LOW_GLCD;
144 
145 }
146 
147 //*********************************************
148 // set_page_GLCD
149 //*********************************************
150 void set_page_GLCD (unsigned char controller_page) {
151 
153  write_command_GLCD (COMM_PAGE_SELECT | controller_page);
155 }
156 
157 
158 //*********************************************
159 // set_address_GLCD
160 //*********************************************
161 void set_address_GLCD (unsigned char controller_address){
162 
164  write_command_GLCD (COMM_ADDRESS_SELECT | controller_address);
166 }
167 
168 
169 //*********************************************
170 // set_vertical_offset_GLCD
171 //*********************************************
172 void set_vertical_offset_GLCD (unsigned char vertical_offset){
173 
175  GLCD_CS2 = LOW_GLCD;
176 
177 // wait_busy_status_GLCD ();
178  write_command_GLCD (COMM_START_LINE | vertical_offset);
179 // wait_busy_status_GLCD ();
180 
181  GLCD_CS1 = LOW_GLCD;
183 
184 // wait_busy_status_GLCD ();
185  write_command_GLCD (COMM_START_LINE | vertical_offset);
186 // wait_busy_status_GLCD ();
187 
188  GLCD_CS1 = LOW_GLCD;
189  GLCD_CS2 = LOW_GLCD;
190 
191 }
192 
193 
194 //*********************************************
195 // write_data_GLCD
196 //*********************************************
197 void write_data_GLCD (unsigned char data_to_send) {
198 
199  GLCD_E = LOW_GLCD;
200  NOP_DELAY;
201  NOP_DELAY;
202  GLCD_R_W = LOW_GLCD;
204  NOP_DELAY;
205  NOP_DELAY;
206  GLCD_E = HIGH_GLCD;
207  GLCD_DATA_WRITE = data_to_send;
208  NOP_DELAY;
209  NOP_DELAY;
210  NOP_DELAY;
211  GLCD_E = LOW_GLCD;
212 
213  NOP_DELAY;
215  GLCD_D_I = LOW_GLCD;
216 
217  NOP_DELAY;
218 
219  GLCD_E = HIGH_GLCD;
220 
221 }
222 
223 
224 //*********************************************
225 // clear_GLCD
226 //*********************************************
227 void clear_GLCD (unsigned char filling_color) {
228 
229  unsigned char page = 0;
230  unsigned char address = 0;
231 
233  GLCD_CS2 = LOW_GLCD;
234 
235  set_address_GLCD (address);
236 
237  for (page = 0; page < NUMBER_OF_PAGES; page++) {
238  set_page_GLCD (page);
239  for (address = 0; address < Y_RESOLUTION; address++) {
240 
241  // wait_busy_status_GLCD ();
242  write_data_GLCD (filling_color);
243  // wait_busy_status_GLCD ();
244  }
245  }
246 
247  GLCD_CS1 = LOW_GLCD;
249 
250 
251  address = 0;
252  set_address_GLCD (address);
253 
254  for (page = 0; page < NUMBER_OF_PAGES; page++) {
255  set_page_GLCD (page);
256 
257  for (address = 0; address < Y_RESOLUTION; address++) {
258  // wait_busy_status_GLCD ();
259  write_data_GLCD (filling_color);
260  // wait_busy_status_GLCD ();
261  }
262  }
263 
264  GLCD_CS1 = LOW_GLCD;
265  GLCD_CS2 = LOW_GLCD;
266 
267 }
268 
269 //*********************************************
270 // read_data_GLCD
271 //*********************************************
272 unsigned char read_data_GLCD (void) {
273 
274  unsigned char data_out;
275 
277 
278  GLCD_E = LOW_GLCD;
279  NOP_DELAY;
280  NOP_DELAY;
283  NOP_DELAY;
284  GLCD_E = HIGH_GLCD;
285  NOP_DELAY;
286  data_out = GLCD_DATA_READ;
287  NOP_DELAY;
288  NOP_DELAY;
289  GLCD_E = LOW_GLCD;
290  NOP_DELAY;
291  GLCD_R_W = LOW_GLCD;
292  GLCD_D_I = LOW_GLCD;
293 
295 
296  return (data_out);
297 }
298 
299 //*********************************************
300 // plot_xy_GLCD
301 //*********************************************
302 void plot_xy_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char filling_color){
303 
304  unsigned char data_byte;
305  unsigned char data_mask;
306 
307  y_pos = Y_RESOLUTION-y_pos -1;
308 
309  if (x_pos < CONTROLLER_MAX_X) {
310 
312  GLCD_CS2 = LOW_GLCD;
313  set_address_GLCD (x_pos);
314  set_page_GLCD (y_pos >> 3);
315  //dummy read
316  data_byte = read_data_GLCD ();
317  data_byte = read_data_GLCD ();
318 
319  set_address_GLCD (x_pos);
320 
321  } else {
322 
323  GLCD_CS1 = LOW_GLCD;
326  set_page_GLCD (y_pos >> 3);
327  //dummy read
328  data_byte = read_data_GLCD ();
329  data_byte = read_data_GLCD ();
330 
332  }
333 
334  data_mask = 0x01 << (y_pos & 0x07);
335 
336  if (filling_color == FILLING_BLACK)
337  data_byte = data_byte | data_mask;
338  else
339  data_byte = data_byte & ~data_mask;
340 
341 // wait_busy_status_GLCD ();
342  write_data_GLCD (data_byte);
343 // wait_busy_status_GLCD ();
344 
345 
346  GLCD_CS1 = LOW_GLCD;
347  GLCD_CS2 = LOW_GLCD;
348 }
349 
350 
351 //*********************************************
352 // draw_vertical_line_GLCD
353 //*********************************************
354 void draw_vertical_line_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){
355 
356  unsigned char i;
357 
358  for (i = 0 ; i<length; i++) {
359  plot_xy_GLCD (x_origin, y_origin + i, filling_color);
360  }
361 }
362 
363 
364 //*********************************************
365 // draw_horizontal_line_GLCD
366 //*********************************************
367 void draw_horizontal_line_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){
368 
369  unsigned char i;
370 
371  for (i = 0 ; i<length; i++) {
372  plot_xy_GLCD (x_origin + i, y_origin, filling_color);
373  }
374 }
375 
376 
377 //*********************************************
378 // draw_window_GLCD
379 //*********************************************
380 
381 void draw_window_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){
382 
383  unsigned char i;
384 
385  draw_horizontal_line_GLCD (x_origin, y_origin, width, filling_color);
386  // + 1 is required to close the window
387  draw_horizontal_line_GLCD (x_origin, y_origin + height, width+1, filling_color);
388 
389  draw_vertical_line_GLCD (x_origin, y_origin, height, filling_color);
390  draw_vertical_line_GLCD (x_origin + width, y_origin, height, filling_color);
391 
392 }
393 
394 
395 //*********************************************
396 // draw_box_GLCD
397 //*********************************************
398 
399 void draw_box_GLCD (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){
400 
401  unsigned char i;
402 
403  for (i=0; i < width; i++)
404  draw_vertical_line_GLCD (x_origin +i, y_origin, height, filling_color);
405 
406 }
407 
408 //*********************************************
409 // write_char_GLCD
410 //*********************************************
411 void write_char_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char character){
412 
413  unsigned char i;
414  unsigned char data;
415  unsigned char x;
416  unsigned char bit_value;
417 
418  for (i = 0 ; i<font_width; i++){
419 
420  data = font_5x7[character - 32][i];
421 
422  for (x = 0 ; x<= font_height; x++) {
423 
424  if (data & (0x80 >> x))
425  bit_value = FILLING_BLACK;
426  else
427  bit_value = FILLING_WHITE;
428 
429  plot_xy_GLCD (x_pos + i, y_pos + x, bit_value);
430 
431  }
432  }
433 }
434 
435 //*********************************************
436 // write_string_GLCD
437 //*********************************************
438 
439 void write_string_GLCD (unsigned char x_pos, unsigned char y_pos, unsigned char * character_array){
440 
441 unsigned char i = 0;
442 
443  while (character_array[i]) {
444 
445  write_char_GLCD (x_pos + (i* (font_width+ GLCD_FONT_SPACE)) , y_pos,character_array[i]);
446 
447  i++;
448  }
449 }
450 
451 
452 //*********************************************
453 // write_message_GLCD
454 //*********************************************
455 #ifndef __XC8
456 void write_message_GLCD (unsigned char x_pos, unsigned char y_pos, const rom unsigned char * character) {
457 #endif
458 
459 #ifdef __XC8
460 void write_message_GLCD (unsigned char x_pos, unsigned char y_pos, const unsigned char * character) {
461 #endif
462 
463 unsigned char i = 0;
464 
465  while (character[i]) {
466 
467  write_char_GLCD (x_pos + (i*(font_width + GLCD_FONT_SPACE)), y_pos,character[i]);
468 
469  i++;
470  }
471 
472 }
473 
474 
475 //*********************************************
476 // draw_picture_GLCD
477 //*********************************************
478 #ifndef __XC8
479 void draw_picture_GLCD (rom unsigned char * picture_table){
480 #endif
481 
482 #ifdef __XC8
483 void draw_picture_GLCD (const unsigned char * picture_table){
484 #endif
485  unsigned char page = 0;
486  unsigned char address = 0;
487 
488 
489  for (page = 0; page < NUMBER_OF_PAGES; page++) {
491  GLCD_CS2 = LOW_GLCD;
492 
493  set_address_GLCD (address);
494  set_page_GLCD (page);
495 
496  for (address = 0; address < Y_RESOLUTION; address++) {
497 
498  write_data_GLCD (* picture_table);
499  picture_table++;
500  }
501 
502  GLCD_CS1 = LOW_GLCD;
504 
505  address = 0;
506  set_address_GLCD (address);
507  set_page_GLCD (page);
508 
509  for (address = 0; address < Y_RESOLUTION; address++) {
510 
511  write_data_GLCD (* picture_table);
512  picture_table++;
513  }
514 
515  }
516 
517  GLCD_CS1 = LOW_GLCD;
518  GLCD_CS2 = LOW_GLCD;
519 }
520 
521 
522 //*********************************************
523 // write_integer_GLCD
524 //*********************************************
525 void write_integer_GLCD (unsigned char x_pos, unsigned char y_pos, int value, char number_of_digits){
526 
527  // The array size is 5 plus end of string \0
528  unsigned char convertedInt [6] = {0,0,0,0,0,0};
529 
530  // Index used to shift to the right the digit
531  char index;
532 
533  // Integer is converted to string
534  #ifndef __XC8
535  itoa (value, (char*) convertedInt);
536  #endif
537 
538  #ifdef __XC8
539  itoa ((char*) convertedInt, value,10);
540  #endif
541 
542  if (number_of_digits >0 ) {
543 
544  convertedInt[number_of_digits] = '\0';
545 
546  // Shift the digit to the right removing the empty ones
547  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')) {
548  for (index = number_of_digits-1; index > 0; index--){
549  convertedInt[index] = convertedInt[index-1];
550  convertedInt[index-1] = ' ';
551  }
552  }
553  }
554 
555  write_string_GLCD (x_pos, y_pos, convertedInt);
556 }
557 
558 
559 //*********************************************
560 // set_font_GLCD
561 //*********************************************
562 void set_font_GLCD (unsigned char width, unsigned char height){
563 
564  font_width = width;
565  font_height = height;
566 }
567 
568 
569 //*********************************************
570 // backlight_GLCD
571 //*********************************************
572 void backlight_GLCD(unsigned char backlight) {
573 
574  GLCD_LED = backlight;
575 }
576 
577 
578 //*********************************************
579 // initialize_GLCD
580 //*********************************************
581 void initialize_GLCD (void) {
582 
584 
585  GLCD_RST = LOW_GLCD;
586  GLCD_LED = LOW_GLCD;
587  GLCD_E = HIGH_GLCD;
589  GLCD_D_I = LOW_GLCD;
590  GLCD_CS1 = LOW_GLCD;
591  GLCD_CS2 = LOW_GLCD;
592 
593  delay_ms (100);
594 
596 
602 
603 }
604 
605 
606