Sunday, August 23, 2020

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD

Overview

A graphical LCD can show variety of data including texts, image, symbols, graphic, etc. It usually has an LCD controller inside. A popular LCD controller was KS0108 manufactured by Samsung Electronics. Currently there are many equivalent controller chips. This controller chip was release a few decade ago but it still in use in some small embedded controller projects.

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Sample Program

An example of using this LCD controller chip is a small 128x64 GLCD module. It use a parallel port interface with a few control lines. However some newer equivalent controller chips have an I2C and SPI interface, reducing the number of I/O pins that interface between the LCD and the microprocessor.

Using this type of graphical LCD is very straight forward. Most of electronic hobbyists use Arduino because it's open source, and there are a lot of libraries. CCS PICC compiler for PIC micro-controller also has a library to controller this GLCD. Mikroelectronika have many compilers targeting different type of micro-controllers including PIC, AVR, 8051, ARM, etc. It has many libraries for LCD and graphical LCD including this type. However either CCS PICC and Mikroelectronika's compilers, the user needs to pay for license to get non-restricted access to their compilers.

Microchip Studio (formerly Atmel Studio) is a free IDE from Microchip Technology. We can write the program for Microchip AVR or ARM micro-controller without restriction. For AVR type we can use AVR LIBC that's an open source tool chain. 

RT12864J-1 128x64 Graphical LCD

I bought this GLCD module from Ebay many years ago. At that time it was expensive. However it's a low cost now.


ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Front Panel

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Back Panel

Inside this LCD module there are two KS0108 display driver chip, and one KS0107 common driver chip. Since they are chip-on-board we can not see these chips.

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
RT12864J-1 Datasheet
This module works at +5VDC. It uses the 6800 parallel interface. Command and data are latched into the controller chip at positive transition of Enabel pin (E) between CSA and CSB selection. 

Chip Select A (CSA) and Chip Select B (CSB) are active high. we can select one of them, or both. 

Data/Instruction (DI) or RS (Register Select pin) is use for choosing between data or instruction code. Data refers to any graphic displaying on screen.

Read or Write (R/W) pin is use for reading or writing data/instruction to LCD controller. We just need to write data to controller. So we must connect it to ground.

Reset (RST) pin is active low signal that will restart the module. We will need to connect it to VCC via a current cutting resistor allowing this module to operate.

Command and sending this LCD is very simple, as it's shown in device data sheet.

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
The origin of data on screen starts from the upper left corner. 

Hardware Interfacing and Programming

I draw and simulate this example program using Proteus 8. This software can simulate the circuit. However the footprint for RT12864J-1 Graphical LCD doesn't exist in this simulator.


ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Schematic Diagram

I use Microchip Studio IDE to write C codes for this program.

  1. /*
  2.  * RT12864Test4.c
  3.  *
  4.  * Created: 7/15/2023 11:57:20 AM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include "font5x8.h"
  10. #include "diy-logo.h"
  11.  
  12. #define F_CPU 8000000UL
  13. #include <util/delay.h>
  14.  
  15. #define LCD_DATA PORTC
  16. #define LCD_CONT PORTD
  17.  
  18. #define LCD_EN 0
  19. #define LCD_DI 1
  20. #define LCD_CS1 2
  21. #define LCD_CS2 3
  22. #define LCD_RST 4
  23.  
  24. unsigned char dotCount=0;
  25.  
  26. void lcdCommand(unsigned char cmd,char cs){
  27. if(cs==1) LCD_CONT|=(1<<LCD_CS1);
  28. else if(cs==2) LCD_CONT|=(1<<LCD_CS2);
  29. LCD_CONT&=~(1<<LCD_DI);
  30. LCD_CONT|=(1<<LCD_EN);
  31. LCD_DATA=cmd;
  32. LCD_CONT&=~(1<<LCD_EN);
  33. _delay_us(10);
  34. if(cs==1) LCD_CONT&=~(1<<LCD_CS1);
  35. else if(cs==2) LCD_CONT&=~(1<<LCD_CS2);
  36. }
  37.  
  38. void lcdData(char data,char cs){
  39. if(cs==1) LCD_CONT|=(1<<LCD_CS1);
  40. else if(cs==2) LCD_CONT|=(1<<LCD_CS2);
  41. LCD_CONT|=(1<<LCD_DI);
  42. LCD_CONT|=(1<<LCD_EN);
  43. LCD_DATA=data;
  44. LCD_CONT&=~(1<<LCD_EN);
  45. _delay_us(10);
  46. if(cs==1) LCD_CONT&=~(1<<LCD_CS1);
  47. else if (cs==2) LCD_CONT&=~(1<<LCD_CS2);
  48. }
  49.  
  50. void lcdHorizontal(unsigned char horizontal){
  51. lcdCommand(0x40+horizontal,1);
  52. lcdCommand(0x40+horizontal,2);
  53.  
  54. }
  55.  
  56. void lcdVerticalLine(unsigned char line){
  57. lcdCommand(0xB8+line,1);
  58. lcdCommand(0xB8+line,2);
  59. dotCount=0;
  60. }
  61.  
  62. void lcdSetZ(unsigned char zAxis){
  63. lcdCommand(0xC0+zAxis,1);
  64. lcdCommand(0xC0+zAxis,2);
  65. }
  66.  
  67. void lcdXy(unsigned char x,unsigned char y){
  68. if(x<64){
  69. lcdCommand(0x40+x,1);
  70. lcdCommand(0x40+0,2);
  71. }
  72. else{
  73. lcdCommand(0x40+0,1);
  74. lcdCommand(0x40+x-63,2);
  75. }
  76.  
  77. lcdCommand(0xB8+y,1);
  78. lcdCommand(0xB8+y,2);
  79. dotCount=x;
  80. }
  81.  
  82. void writeChar(unsigned char aph){
  83. for (int i=0;i<5;i++)
  84. {
  85. if(dotCount<64) lcdData(font5x8[((aph-32)*5)+i],1);
  86. else lcdData(font5x8[((aph-32)*5)+i],2);
  87. dotCount++;
  88. }
  89. lcdData(0,(dotCount<64)?1:2);
  90. dotCount++;
  91. if (dotCount>127)
  92. {
  93. dotCount=0;
  94. }
  95. }
  96.  
  97. void writeCharDelay(unsigned char aph,unsigned int dT){
  98. for (int i=0;i<5;i++)
  99. {
  100. if(dotCount<64) lcdData(font5x8[((aph-32)*5)+i],1);
  101. else lcdData(font5x8[((aph-32)*5)+i],2);
  102. if(dT!=0) for(int i=0;i<dT;i++) _delay_us(1000);
  103. dotCount++;
  104. }
  105. lcdData(0,(dotCount<64)?1:2);
  106. dotCount++;
  107. if (dotCount>127)
  108. {
  109. dotCount=0;
  110. }
  111. }
  112.  
  113. void lcdString(char *str){
  114. while(*str)
  115. writeChar(*str++);
  116. }
  117.  
  118. void writeStringDelay(char *str,unsigned int dT){
  119. while(*str){
  120. if(dT!=0) writeCharDelay(*str++,dT);
  121. else writeChar(*str++);
  122. }
  123. }
  124.  
  125. void lcdInit(void){
  126. DDRC=0xFF;
  127. DDRD=0xFF;
  128. /*Display On*/
  129. lcdCommand(0x3F,1);
  130. lcdCommand(0x3F,2);
  131. dotCount = 0;
  132. }
  133.  
  134. void lcdClearScreen(){
  135.  
  136. for (int i=0;i<8;i++)
  137. {
  138. lcdXy(0,i);
  139. for(int j=0;j<128;j++){
  140. lcdData(0,1);
  141. lcdData(0,2);
  142. }
  143. }
  144. dotCount=0;
  145. lcdXy(0,0);
  146. }
  147.  
  148. void lcdClearSection(unsigned char x,unsigned char y,unsigned char dot){
  149. lcdXy(x,y);
  150. dotCount=x;
  151. for (int i=0;i<dot;i++)
  152. {
  153. if(dotCount<64) lcdData(0,1);
  154. else lcdData(0,2);
  155. dotCount++;
  156. }
  157.  
  158. if (dotCount>127)
  159. {
  160. dotCount=0;
  161. }
  162. }
  163.  
  164. void showGraphic(void){
  165. unsigned int kCount=0;
  166. for (int i=0;i<8;i++)
  167. {
  168.  
  169. dotCount=0;
  170. lcdXy(0,i);
  171. for (int j=kCount;j<kCount+128;j++)
  172. {
  173. if(dotCount<64) lcdData(logo[j],1);
  174. else lcdData(logo[j],2);
  175. dotCount++;
  176. }
  177. kCount+=128;
  178. }
  179. }
  180.  
  181. int main(void)
  182. {
  183. lcdInit();
  184. lcdClearScreen();
  185. while (1)
  186. {
  187. lcdXy(25,0);
  188. writeStringDelay("HELLO WORLD!",10);
  189. lcdXy(0,1);
  190. writeStringDelay("ATMEGA32 Graphic LCD ",10);
  191. lcdXy(0,2);
  192. writeStringDelay("Programming in C With",10);
  193. lcdXy(0,3);
  194. writeStringDelay("Microchip Studio",10);
  195. lcdXy(0,4);
  196. writeStringDelay("DIY-Instruments",10);
  197. lcdXy(0,5);
  198. writeStringDelay("KS0108 LCD Controller",10);
  199. lcdXy(0,6);
  200. writeStringDelay("RT12864J-1 128x64",10);
  201. lcdXy(0,7);
  202. writeStringDelay("Graphical LCD Module",10);
  203. _delay_ms(5000);
  204. lcdClearScreen();
  205.  
  206. lcdXy(0,0);
  207. showGraphic();
  208. _delay_ms(5000);
  209. lcdClearScreen();
  210. }
  211. }
  212.  

 

Click here to download its source file.

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Hardware Testing 1

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD

Hardware Testing 2

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Atmega32 Test Board With PICKit2 ICSP

ATMega32 Simple Graphical LCD Interfacing Using A 128x64 GLCD
Atmega32 Test Board With PICKit2 ICSP


No comments:

Post a Comment