PIC18 LaurTec Library  3.3.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_old.c
Go to the documentation of this file.
1 /*******************************************************************************
2 
3 Author : Mauro Laurenti
4 Version : 1.2
5 Created on Date : 26/05/2012
6 Last update : 15/12/2014
7 
8 CopyRight 2006-2014 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 // GLCD_wait_busy_status
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 // GLCD_get_controller_status
66 //*********************************************
67 unsigned char GLCD_get_controller_status (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 // GLCD_write_command
90 //*********************************************
91 void GLCD_write_command (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 // GLCD_set_display
117 //*********************************************
118 void GLCD_set_display (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 // GLCD_set_page
149 //*********************************************
150 void GLCD_set_page (unsigned char controller_page) {
151 
153  write_command_GLCD (GLCD_COMM_PAGE_SELECT | controller_page);
155 }
156 
157 
158 //*********************************************
159 // GLCD_set_address
160 //*********************************************
161 void GLCD_set_address (unsigned char controller_address){
162 
164  write_command_GLCD (GLCD_COMM_ADDRESS_SELECT | controller_address);
166 }
167 
168 
169 //*********************************************
170 // GLCD_set_vertical_offset
171 //*********************************************
172 void GLCD_set_vertical_offset (unsigned char vertical_offset){
173 
175  GLCD_CS2 = LOW_GLCD;
176 
177  NOP_DELAY;
178 
179 // wait_busy_status_GLCD ();
180  write_command_GLCD (GLCD_COMM_START_LINE | vertical_offset);
181 // wait_busy_status_GLCD ();
182 
183  GLCD_CS1 = LOW_GLCD;
185 
186  NOP_DELAY;
187 
188 // wait_busy_status_GLCD ();
189  write_command_GLCD (GLCD_COMM_START_LINE | vertical_offset);
190 // wait_busy_status_GLCD ();
191 
192  GLCD_CS1 = LOW_GLCD;
193  GLCD_CS2 = LOW_GLCD;
194 
195  NOP_DELAY;
196 
197 }
198 
199 
200 //*********************************************
201 // GLCD_write_data
202 //*********************************************
203 void GLCD_write_data (unsigned char data_to_send) {
204 
205  NOP_DELAY;
206  GLCD_E = LOW_GLCD;
207  NOP_DELAY;
208  NOP_DELAY;
209  GLCD_R_W = LOW_GLCD;
211  NOP_DELAY;
212  NOP_DELAY;
213  GLCD_E = HIGH_GLCD;
214  NOP_DELAY;
215  NOP_DELAY;
216  NOP_DELAY;
217  NOP_DELAY;
218  NOP_DELAY;
219  GLCD_DATA_WRITE = data_to_send;
220  NOP_DELAY;
221  NOP_DELAY;
222  NOP_DELAY;
223  NOP_DELAY;
224  NOP_DELAY;
225  NOP_DELAY;
226  GLCD_E = LOW_GLCD;
227 
228  NOP_DELAY;
230  GLCD_D_I = LOW_GLCD;
231 
232  NOP_DELAY;
233  NOP_DELAY;
234  NOP_DELAY;
235  NOP_DELAY;
236 
237  GLCD_E = HIGH_GLCD;
238  NOP_DELAY;
239  NOP_DELAY;
240  NOP_DELAY;
241  NOP_DELAY;
242  NOP_DELAY;
243 
244 }
245 
246 
247 //*********************************************
248 // GLCD_clear
249 //*********************************************
250 void GLCD_clear (unsigned char filling_color) {
251 
252  unsigned char page = 0;
253  unsigned char address = 0;
254 
256  GLCD_CS2 = LOW_GLCD;
257 
258  set_address_GLCD (address);
259 
260  for (page = 0; page < GLCD_NUMBER_OF_PAGES; page++) {
261  set_page_GLCD (page);
262  for (address = 0; address < GLCD_Y_RESOLUTION; address++) {
263 
264  // wait_busy_status_GLCD ();
265  write_data_GLCD (filling_color);
266  // wait_busy_status_GLCD ();
267  }
268  }
269 
270  GLCD_CS1 = LOW_GLCD;
272 
273 
274  address = 0;
275  set_address_GLCD (address);
276 
277  for (page = 0; page < GLCD_NUMBER_OF_PAGES; page++) {
278  set_page_GLCD (page);
279 
280  for (address = 0; address < GLCD_Y_RESOLUTION; address++) {
281  // wait_busy_status_GLCD ();
282  write_data_GLCD (filling_color);
283  // wait_busy_status_GLCD ();
284  }
285  }
286 
287  GLCD_CS1 = LOW_GLCD;
288  GLCD_CS2 = LOW_GLCD;
289 
290 }
291 
292 //*********************************************
293 // GLCD_read_data
294 //*********************************************
295 unsigned char GLCD_read_data (void) {
296 
297  unsigned char data_out;
298 
300 
301  GLCD_E = LOW_GLCD;
302  NOP_DELAY;
303  NOP_DELAY;
306  NOP_DELAY;
307  GLCD_E = HIGH_GLCD;
308  NOP_DELAY;
309  NOP_DELAY;
310  NOP_DELAY;
311  data_out = GLCD_DATA_READ;
312  NOP_DELAY;
313  NOP_DELAY;
314  NOP_DELAY;
315  NOP_DELAY;
316  GLCD_E = LOW_GLCD;
317  NOP_DELAY;
318  NOP_DELAY;
319  NOP_DELAY;
320  GLCD_R_W = LOW_GLCD;
321  GLCD_D_I = LOW_GLCD;
322 
324 
325  return (data_out);
326 }
327 
328 //*********************************************
329 // GLCD_plot_xy
330 //*********************************************
331 void GLCD_plot_xy (unsigned char x_pos, unsigned char y_pos, unsigned char filling_color){
332 
333  unsigned char data_byte;
334  unsigned char data_mask;
335 
336  y_pos = GLCD_Y_RESOLUTION-y_pos -1;
337 
338  if (x_pos < GLCD_CONTROLLER_MAX_X) {
339 
341  GLCD_CS2 = LOW_GLCD;
342  set_address_GLCD (x_pos);
343  set_page_GLCD (y_pos >> 3);
344  //dummy read
345  data_byte = read_data_GLCD ();
346  data_byte = read_data_GLCD ();
347 
348  set_address_GLCD (x_pos);
349 
350  } else {
351 
352  GLCD_CS1 = LOW_GLCD;
355  set_page_GLCD (y_pos >> 3);
356  //dummy read
357  data_byte = read_data_GLCD ();
358  data_byte = read_data_GLCD ();
359 
361  }
362 
363  data_mask = 0x01 << (y_pos & 0x07);
364 
365  if (filling_color == FILLING_BLACK)
366  data_byte = data_byte | data_mask;
367  else
368  data_byte = data_byte & ~data_mask;
369 
370 // wait_busy_status_GLCD ();
371  write_data_GLCD (data_byte);
372 // wait_busy_status_GLCD ();
373 
374 
375  GLCD_CS1 = LOW_GLCD;
376  GLCD_CS2 = LOW_GLCD;
377 }
378 
379 
380 //*********************************************
381 // GLCD_draw_vertical_line
382 //*********************************************
383 void GLCD_draw_vertical_line (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){
384 
385  unsigned char i;
386 
387  for (i = 0 ; i<length; i++) {
388  plot_xy_GLCD (x_origin, y_origin + i, filling_color);
389  }
390 }
391 
392 
393 //*********************************************
394 // GLCD_draw_horizontal_line
395 //*********************************************
396 void GLCD_draw_horizontal_line (unsigned char x_origin, unsigned char y_origin, unsigned char length, unsigned char filling_color){
397 
398  unsigned char i;
399 
400  for (i = 0 ; i<length; i++) {
401  plot_xy_GLCD (x_origin + i, y_origin, filling_color);
402  }
403 }
404 
405 
406 //*********************************************
407 // GLCD_draw_window
408 //*********************************************
409 
410 void GLCD_draw_window (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){
411 
412  unsigned char i;
413 
414  draw_horizontal_line_GLCD (x_origin, y_origin, width, filling_color);
415  // + 1 is required to close the window
416  draw_horizontal_line_GLCD (x_origin, y_origin + height, width+1, filling_color);
417 
418  draw_vertical_line_GLCD (x_origin, y_origin, height, filling_color);
419  draw_vertical_line_GLCD (x_origin + width, y_origin, height, filling_color);
420 
421 }
422 
423 
424 //*********************************************
425 // GLCD_draw_box
426 //*********************************************
427 
428 void GLCD_draw_box (unsigned char x_origin, unsigned char y_origin, unsigned char width, unsigned char height, unsigned char filling_color){
429 
430  unsigned char i;
431 
432  for (i=0; i < width; i++)
433  draw_vertical_line_GLCD (x_origin +i, y_origin, height, filling_color);
434 
435 }
436 
437 //*********************************************
438 // GLCD_write_char
439 //*********************************************
440 void GLCD_write_char (unsigned char x_pos, unsigned char y_pos, unsigned char character){
441 
442  unsigned char i;
443  unsigned char data;
444  unsigned char x;
445  unsigned char bit_value;
446 
447  for (i = 0 ; i<font_width; i++){
448 
449  data = font_5x7[character - 32][i];
450 
451  for (x = 0 ; x<= font_height; x++) {
452 
453  if (data & (0x80 >> x))
454  bit_value = FILLING_BLACK;
455  else
456  bit_value = FILLING_WHITE;
457 
458  plot_xy_GLCD (x_pos + i, y_pos + x, bit_value);
459 
460  }
461  }
462 }
463 
464 //*********************************************
465 // GLCD_write_string
466 //*********************************************
467 
468 void GLCD_write_string (unsigned char x_pos, unsigned char y_pos, unsigned char * character_array){
469 
470 unsigned char i = 0;
471 
472  while (character_array[i]) {
473 
474  write_char_GLCD (x_pos + (i* (font_width+ GLCD_FONT_SPACE)) , y_pos,character_array[i]);
475 
476  i++;
477  }
478 }
479 
480 
481 //*********************************************
482 // GLCD_write_message
483 //*********************************************
484 #ifndef __XC8
485 void GLCD_write_message (unsigned char x_pos, unsigned char y_pos, const rom unsigned char * character) {
486 #endif
487 
488 #ifdef __XC8
489 void GLCD_write_message (unsigned char x_pos, unsigned char y_pos, const unsigned char * character) {
490 #endif
491 
492 unsigned char i = 0;
493 
494  while (character[i]) {
495 
496  write_char_GLCD (x_pos + (i*(font_width + GLCD_FONT_SPACE)), y_pos,character[i]);
497 
498  i++;
499  }
500 
501 }
502 
503 
504 //*********************************************
505 // GLCD_draw_picture
506 //*********************************************
507 #ifndef __XC8
508 void GLCD_draw_picture (rom unsigned char * picture_table){
509 #endif
510 
511 #ifdef __XC8
512 void GLCD_draw_picture (const unsigned char * picture_table){
513 #endif
514  unsigned char page = 0;
515  unsigned char address = 0;
516 
517 
518  for (page = 0; page < GLCD_NUMBER_OF_PAGES; page++) {
520  GLCD_CS2 = LOW_GLCD;
521 
522  set_address_GLCD (address);
523  set_page_GLCD (page);
524 
525  for (address = 0; address < GLCD_Y_RESOLUTION; address++) {
526 
527  write_data_GLCD (* picture_table);
528  picture_table++;
529  }
530 
531  GLCD_CS1 = LOW_GLCD;
533 
534  address = 0;
535  set_address_GLCD (address);
536  set_page_GLCD (page);
537 
538  for (address = 0; address < GLCD_Y_RESOLUTION; address++) {
539 
540  write_data_GLCD (* picture_table);
541  picture_table++;
542  }
543 
544  }
545 
546  GLCD_CS1 = LOW_GLCD;
547  GLCD_CS2 = LOW_GLCD;
548 }
549 
550 
551 
552 
553 
554 //*********************************************
555 // GLCD_write_integer
556 //*********************************************
557 void GLCD_write_integer (unsigned char x_pos, unsigned char y_pos, int value, char number_of_digits){
558 
559  // The array size is 5 plus end of string \0
560  unsigned char convertedInt [6] = {0,0,0,0,0,0};
561 
562  // Index used to shift to the right the digit
563  char index;
564 
565  // Integer is converted to string
566  #ifndef __XC8
567  itoa (value, (char*) convertedInt);
568  #endif
569 
570  #ifdef __XC8
571  itoa ((char*) convertedInt, value,10);
572  #endif
573 
574  if (number_of_digits >0 ) {
575 
576  convertedInt[number_of_digits] = '\0';
577 
578  // Shift the digit to the right removing the empty ones
579  while (!(convertedInt[number_of_digits-1] <= '9' && convertedInt[number_of_digits-1] >= '0')) {
580  for (index = number_of_digits-1; index > 0; index--){
581  convertedInt[index] = convertedInt[index-1];
582  convertedInt[index-1] = ' ';
583  }
584  }
585  }
586 
587  write_string_GLCD (x_pos, y_pos, convertedInt);
588 }
589 
590 
591 //*********************************************
592 // GLCD_set_font
593 //*********************************************
594 void GLCD_set_font (unsigned char width, unsigned char height){
595 
596  font_width = width;
597  font_height = height;
598 }
599 
600 
601 //*********************************************
602 // GLCD_backlight
603 //*********************************************
604 void GLCD_backlight(unsigned char backlight) {
605 
606  GLCD_LED = backlight;
607 }
608 
609 
610 //*********************************************
611 // GLCD_initialize
612 //*********************************************
613 void GLCD_initialize (void) {
614 
616 
617  GLCD_RST = LOW_GLCD;
618  GLCD_LED = LOW_GLCD;
619  GLCD_E = HIGH_GLCD;
621  GLCD_D_I = LOW_GLCD;
622  GLCD_CS1 = LOW_GLCD;
623  GLCD_CS2 = LOW_GLCD;
624 
625  delay_ms (100);
626 
628 
634 
635 }
636 
637 
638