C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 1   


C51 COMPILER V7.09, COMPILATION OF MODULE KS0108
OBJECT MODULE PLACED IN ks0108.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ks0108.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /*
   2          ***********************************************************************************************
   3          *                                       JHD12864G/J Graphical LCD Display Driver (KS0107/8 controller)
   4          *
   5          * File name             : ks0108.c
   6          * Programmer    : John Leung, TechToys. Hong Kong
   7          * Web presence  : www.TechToys.com.hk
   8          * Note                  : 
   9          * Language              : Keil C 7.09 2K limited version in uVision2 IDE version 2.40
  10          * Hardware              : AT89S52-gLCD-STK1 demo board
  11          * Date                  : Version 1.0 (8th May 2006) - (4th July 2006)
  12          ***********************************************************************************************
  13          *                                                                               DESCRIPTION
  14          *
  15          * This module provides an interface to JHD12864J Graphical LCD module of size 128x64 dots
  16          * Display controller chip is KS0107/8
  17          * LCD pinout function summarized as below
  18          * --------- LCD         MCU -----------------
  19          * pin 1         Vss     - 0V
  20          * pin 2         Vdd     - 5.0V
  21          * pin 3         Vo      - LCD Drive Voltage (adjust contrast, trimmer to Vee in our case)
  22          * pin 4         D/I             - LCD_DI (H:Data, L:Command (data or command control))
  23          * pin 5         R/W     - LCD_RW (H:Read, L:Write (read status or write data/command))
  24          * pin 6         E       - LCD_EN (H/H->L (Strobe signal, H->L effective))
  25          * pin 7:14      DB0:DB7 - LCD_DATA
  26          * pin 15        CS1             - LCD_CS1 (left, high effective)
  27          * pin 16        CS2             - LCD_CS2 (right, high effective)
  28          * pin 17        RST             - LCD_RST (reset pin, low effective)
  29          * pin 18        Vee             - -10.0V, LCD driving voltage. There is a Vee generator built-in LCD
  30          *                                         module, thus there is no need to use external generator IC
  31          * pin 19        EL1 / A - LCD_BL (Depends on the backlight option.
  32          *                                               If it's a LED backlight, it's an anode.
  33          *                                               If it is EL type, this is EL controller input)
  34          * pin 20        EL2 / K - GND (Cathode for LED-backlight. EL input (OR NC) for EL-backlight model)
  35          ***********************************************************************************************
  36          */
  37          #include <regx52.h>                                     
  38          #include <intrins.h>                            
  39          #include "ks0108.h"             
  40          #if PIXEL_FONT_EN                                                                               
              #include "sysfont.h"
              #endif          
  43          
  44          /*
  45          *********************************************************************************************************
  46          *                                                                                       LOCAL DEFINITIONS
  47          *********************************************************************************************************
  48          */
  49          
  50          #define LCD_STAT_RST            0x10                                                                                                    
  51          #define LCD_STAT_BUSY           0x80
  52          #define LCD_STAT_OFF            0x20
  53          #define DISPLAY_ON                      0x3F
  54          #define DISPLAY_OFF             0x3E
  55          #define DISPLAY_START_LINE      0xC0
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 2   

  56          #define PAGE_OFFSET                     0xB8
  57          #define Y_OFFSET                        0x40
  58          #define STATUS_RD                       0
  59          #define DATA_RD                         1
  60          #define CMD                                     0
  61          #define DAT                                     1
  62          #define HIGH                            1
  63          #define LOW                                     0
  64          #define RIGHT                           0
  65          #define LEFT                            1
  66          #define BOTH                            2
  67          #define NONE                            3
  68          
  69          /*
  70          *********************************************************************************************************
  71          *                                                       DISPLAY INITIALIZATION
  72          *
  73          * Description : This function initializes the LCD module by the following steps:
  74          *                                       1. Enable the backlight
  75          *                                       2. Reset the LCD module
  76          *                                       3. Disable the LCD
  77          *                                       4. Set display start line at the top of the screen
  78          *                                       5. clear the display by writing 0 to the whole screen
  79          *                                       6. re-enable the display                
  80          * Arguments   : none
  81          *                       
  82          * Returns     : none
  83          * Note            : This function should be called once before any of the other functions
  84          *********************************************************************************************************
  85          */
  86          void GDispInit(void)
  87          {
  88   1              GDispBackLite(ENABLE);
  89   1              GDispReset();
  90   1              GDispSwitch(DISABLE);
  91   1              GDispWr(CMD,DISPLAY_START_LINE);//set display start line at the top of the LCD
  92   1              GDispClr(0x00);                                 //clear the display
  93   1              GDispSwitch(ENABLE);
  94   1      }
  95          
  96          
  97          /*
  98          *********************************************************************************************************
  99          *                                                       DISPLAY ON/OFF SWITCH
 100          *
 101          * Description : This function turns display ON/OFF by writing the command DISPLAY_ON / DISPLAY_OFF
 102          * Arguments   : (bit) 'sw'              ENABLE for turning ON defined under ks0108.h
 103          *                                                               DISABLE for turning OFF defined under ks0108.h
 104          *                               
 105          * Returns     : none
 106          * Notes           : Internal status and display RAM data not changed by switching between ON and OFF
 107          *                               Data type 'bit' is Keil C specific
 108          *********************************************************************************************************
 109          */
 110          void GDispSwitch(bit sw)
 111          {
 112   1              GDispChipSel(BOTH);
 113   1              (sw==ENABLE)? (GDispWr(CMD,DISPLAY_ON)):(GDispWr(CMD,DISPLAY_OFF));
 114   1              GDispChipSel(NONE);
 115   1      }
 116          
 117          /*
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 3   

 118          *********************************************************************************************************
 119          *                                       WRITE A SINGLE BYTE AT A COLUMN# AND PAGE#
 120          *
 121          * Description : This function writes a single byte 'c' to a column position defined by 'col' and 
 122          *                               to PAGE# indicated by the variable 'page'
 123          * Arguments   : 'col'           the column position (0-127) of the LCD, 0<=col<128 (MAX_COL_PIXEL in ks0108.h)
 124          *                               'page'          the PAGE number (0-7) of the LCD. It is NOT the y coordinate as our usual 
 125          *                                                       Cartesian coordinate system. 
 126          *                                                       It is the PAGE number defined in JHD12864J data sheet
 127          *                               'c'                     byte to write
 128          * Returns     : none
 129          * Notes           : This function should has been declared as a local function because it is rarely
 130          *                               called in main. However, if we want to make a different version of string display with
 131          *                               higher efficiency, we may make use of this function to write byte in vertical manner.
 132          *********************************************************************************************************
 133          */
 134          void GDispWrByte(unsigned char col, unsigned char page, unsigned char c)
 135          {
 136   1              col     = col%MAX_COL_PIXEL;
 137   1              page    = page%8;
 138   1      
 139   1              if(col<(MAX_COL_PIXEL/2))
 140   1                      {GDispChipSel(LEFT);}
 141   1              else
 142   1                      {GDispChipSel(RIGHT);col=col-(MAX_COL_PIXEL/2);}
 143   1      
 144   1              GDispWr(CMD,PAGE_OFFSET|page);   GDispWr(CMD,Y_OFFSET|col);     //set page and column position
 145   1              GDispWr(DAT, c);                                                                                        //output data to D0-D7
 146   1              GDispChipSel(NONE);
 147   1      }
 148          
 149          /*
 150          *********************************************************************************************************
 151          *                                                               CLEAR DISPLAY
 152          *
 153          * Description : This function writes a fixed byte to all pages (0-7) of both half. If the byte is 0x00,
 154          *                               the action is equivalent to clearing the display (turning all pixels OFF)
 155          * Arguments   : 'pat'   is the pattern to write to the display in a repetitive manner. Writing 0xFF
 156          *                                               for example, will turn all pixels BLACK.
 157          *                               
 158          * Returns     : none
 159          * Notes           : This function makes use of GdispWrByte() thus the speed of clearing is fast!
 160          *********************************************************************************************************
 161          */
 162          void GDispClr(unsigned char pat)
 163          {
 164   1              unsigned char col, page;
 165   1      
 166   1              for(col=0;col<128;col++)
 167   1              {
 168   2                      for(page=0;page<8;page++) GDispWrByte(col,page,pat);
 169   2              }
 170   1      }
 171          
 172          
 173          /*
 174          *********************************************************************************************************
 175          *                                                       SET PIXEL AT x, y POSITION
 176          *
 177          * Description : This function sets a pixel with color = BLACK / WHITE (defined in ks0108.h) at a position
 178          *                               defined by (x,y) following Cartesian coordinate system. Coordinates (0,0) at the 
 179          *                               top left corner, coordinates (127,63) at the lower right corner of the LCD screen.
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 4   

 180          *
 181          * Arguments   : 'x'    0....MAX_COL_PIXEL-1 is matrix position in horizontal direction
 182          *                               'y'    0....MAX_ROW_PIXEL-1 is matrix position in vertical direction 
 183          *                               'color'  sets BLACK / WHITE standing for pixel ON/OFF
 184          * Returns     : none
 185          * Notes           : The action GDispRd() has an effect of auto-increment on the x-direction (Y address)
 186          *                               Thus, GDispWr(CMD,Y_OFFSET|x) has to be immediate above data write code line
 187          *                               (color==BLACK)? GDispWr(DAT,pre_data|(1<<y%8)):GDispWr(DAT,pre_data&~(1<<y%8))
 188          *                               Data type 'bit' is Keil C specific
 189          *********************************************************************************************************
 190          */
 191          void GDispSetPixel(unsigned char x, unsigned char y, bit color)
 192          {       
 193   1              unsigned char pre_data;
 194   1      
 195   1              x = x%MAX_COL_PIXEL;
 196   1              y = y%MAX_ROW_PIXEL;
 197   1      
 198   1              if(x<(MAX_COL_PIXEL/2))
 199   1                      {GDispChipSel(LEFT);}
 200   1              else
 201   1                      {GDispChipSel(RIGHT);x=x-(MAX_COL_PIXEL/2);}
 202   1      
 203   1              GDispWr(CMD,PAGE_OFFSET|(y/8)); //set pixel address in vertical direction (page address)
 204   1              GDispWr(CMD,Y_OFFSET|x);                //set pixel address in horizontal direction
 205   1              pre_data=GDispRd(DATA_RD);              //dummy read required
 206   1              pre_data=GDispRd(DATA_RD);              //address pointer auto increment by one
 207   1              GDispWr(CMD,Y_OFFSET|x);                //set pixel address in horizontal direction again to offset 
 208   1                                                                              //the address pointer shift from GDispRd(DATA_RD) above
 209   1              (color==BLACK)? GDispWr(DAT,pre_data|(1<<(y%8))):GDispWr(DAT,pre_data&~(1<<(y%8)));
 210   1      
 211   1              GDispChipSel(NONE);
 212   1      }
 213          
 214          /*
 215          *********************************************************************************************************
 216          *                                                       WRITE A PATTERN AT X, Y COORDINATES
 217          *
 218          * Description : This function writes a particular pattern of size patWidth and patHeight defined under 
 219          *                               the pattern structure in ks0108.h at a position defined by (x,y) following Cartesian 
 220          *                               coordinate system. Pixel set in horizontal direction.
 221          *                               *** Pixels displayed in horizontal manner ***
 222          * Arguments   : 'x'             0....MAX_COL_PIXEL-1 is matrix position in horizontal direction
 223          *                                                       top left corner of the pattern to write
 224          *                               'y'             0....MAX_ROW_PIXEL-1 is matrix position in vertical direction
 225          *                                                       top left corner of the pattern to write
 226          *                               '*pPat'         pointer to pattern structure 
 227          *                               'color'         sets BLACK / WHITE standing for pixel ON/OFF
 228          * Returns     : none
 229          * Notes           : Data type 'bit' is Keil C specific
 230          *********************************************************************************************************
 231          */
 232          #if PIXEL_PAT_EN
 233          void GDispPixPatAt(unsigned char x, unsigned char y, pattern* pPat, bit color)
 234          {
 235   1              unsigned char i, j, bit_idx, pixelByte, patYsize, patByte;
 236   1      
 237   1              patYsize  = pPat->patHeight;
 238   1              patByte   = (pPat->patWidth)>>3;        //number of byte = width of icon/8
 239   1      
 240   1              for(i=0;i<patYsize;i++)
 241   1              {
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 5   

 242   2                      for(j=0;j<patByte;j++)
 243   2                      {
 244   3                              pixelByte = pPat->patBody[i*patByte+j];
 245   3                              for(bit_idx=0;bit_idx<8;bit_idx++)
 246   3                                      {
 247   4                                       (pixelByte&(0x80>>bit_idx))? GDispSetPixel(x+bit_idx+8*j, y+i, color):\
 248   4                                        GDispSetPixel(x+bit_idx+8*j, y+i, !color);
 249   4                                      }
 250   3                      }
 251   2              }
 252   1       
 253   1      }
 254          #endif
 255          
 256          /*
 257          *********************************************************************************************************
 258          *                                       CONVERT A STRING TO PIXEL DATA AND DISPLAY AT x,y
 259          *
 260          * Description : This function outputs a string in graphic mode (pixel-by-pixel) at a position defined 
 261          *                               by (x,y) following Cartesian coordinate system with variable font spacing defined by 
 262          *                               fontSpace. Character wrapping is allowed.
 263          *                               Enable switch PIXEL_FONT_EN under ks0108.h to use this function
 264          *                               *** REQUIRE AT LEAST ONE FONT SET (sysfont.h) ****
 265          *
 266          * Arguments   : 'x'             0....MAX_COL_PIXEL-1 is matrix position in horizontal direction,
 267          *                                                       top left corner of each character to write
 268          *                               'y'             0....MAX_ROW_PIXEL-1 is matrix position in vertical direction,
 269          *                                                       top left corner of each character to write
 270          *                               '*pStr'         pointer to an ASCII string 
 271          *                               'fontSpace' font spacing from 1 to 127
 272          *                               'color'         WHITE / BLACK
 273          * Returns     : none
 274          * Notes           : Example 
 275          *                               ....
 276          *                               main() 
 277          *                               { 
 278          *                                       GDispPixFontAt(0,20,"Hello!",1,BLACK);  //Display the ASCII string "Hello!"
 279          *                                                                                                                       //at (x,y)=(0,20), font space = 1
 280          *                                                                                                                       //color in BLACK
 281          *                               }
 282          *
 283          *                               Data type 'bit' is Keil C specific
 284          *********************************************************************************************************
 285          */
 286          #if PIXEL_FONT_EN
              void GDispPixStrAt(unsigned char x, unsigned char y, unsigned char *pStr, unsigned char fontSpace, bit col
             -or)
              {
                      unsigned char pixelByte,bit_idx, i;                                     //loop counters 
                      unsigned char c,fwidth, fheight;                                        //character, font width & height store
              
                      while (*pStr)
                      {
                              c = *pStr++ - 32;
                                              
                              fwidth = SYS_FNT[c].fontWidth;
                              fheight = 8;
              
                              if((x+fwidth)>MAX_COL_PIXEL)                                    //character wrapping here                               
                              {
                                      x=0;
                                      y+=fheight;
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 6   

                              }
              
                              if((y+fheight)>MAX_ROW_PIXEL)                                   //check for y boundary                          
                              {
                                      y=0;
                              }
              
                              for(i=0;i<fheight;i++)
                              {
                                      pixelByte = SYS_FNT[c].fontBody[i];
                                      for(bit_idx=0; bit_idx<fwidth; bit_idx++)
                                              {
                                                      if((pixelByte)&(0x80>>bit_idx)) GDispSetPixel(x+bit_idx, y+i, color);
                                              }
                              }
                              
                              x = x+fwidth+fontSpace;
                      }       
              }
              #endif
 323          
 324          /*
 325          *********************************************************************************************************
 326          *                                                               LOW LEVEL RESET                         
 327          *
 328          * Description : This function performs a low level reset for the LCD module
 329          * Arguments   : none
 330          *                       
 331          * Returns     : none
 332          * Notes           : Hardware specific.
 333          *********************************************************************************************************
 334          */
 335          void GDispReset(void)
 336          {
 337   1              unsigned char i;
 338   1      
 339   1              LCD_RST = LOW;
 340   1              for(i=0;i<0xFF;i++);            //software delay
 341   1              LCD_RST = HIGH;
 342   1              for(i=0;i<0xFF;i++);            //software delay
 343   1      }
 344          
 345          /*
 346          *********************************************************************************************************
 347          *                                                       CHIP SELECT FOR CS1 OR CS2                              
 348          *
 349          * Description : This function performs a "chip select" for the left, right, both, or none LCD portion
 350          * Arguments   : 'wing' can be   LEFT
 351          *                                                               RIGHT
 352          *                                                               BOTH
 353          *                                                               NONE    defined under ks0108.c
 354          *                       
 355          * Returns     : none
 356          * Notes           : Hardware specific.
 357          *********************************************************************************************************
 358          */
 359          void GDispChipSel(unsigned char wing)
 360          {
 361   1              if(wing==LEFT)
 362   1                      {LCD_CS1=HIGH;LCD_CS2=LOW;}
 363   1              else if(wing==RIGHT)
 364   1                      {LCD_CS1=LOW;LCD_CS2=HIGH;}
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 7   

 365   1              else if (wing==BOTH)
 366   1                      {LCD_CS1=HIGH;LCD_CS2=HIGH;}
 367   1              else if (wing==NONE)
 368   1                      {LCD_CS1=LOW;LCD_CS2=LOW;}
 369   1      } 
 370          
 371          /*
 372          *********************************************************************************************************
 373          *                                       LOW LEVEL DATA OR COMMAND WRITE TO LCD
 374          *
 375          * Description : This function performs low level data/command write to LCD
 376          * Arguments   : (bit)'type'                     is the data type, DAT or CMD as defined under ks0108.c
 377          *                                                                               DAT stands for data, CMD stands for command
 378          *                               (unsigned char) 'dat'   is the data written to the LCD module, defined by the data bus
 379          *                                                                               connection defined under ks0108.h
 380          * Returns     : none
 381          * Notes           : Hardware specific. The type bit is specific to Keil C complier.
 382          *********************************************************************************************************
 383          */
 384          void GDispWr(bit type, unsigned char dat)
 385          {
 386   1              (type==DAT)?(LCD_DI=HIGH):(LCD_DI=LOW);
 387   1              LCD_RW = LOW;
 388   1              LCD_EN = HIGH; 
 389   1              LCD_DATA = dat;
 390   1              _nop_();
 391   1              LCD_EN = LOW;
 392   1      }
 393          
 394          
 395          /*
 396          *********************************************************************************************************
 397          *                                                               GDispStatRd
 398          *
 399          * Description : This function reads status of the LCD
 400          * Arguments   : 'type'  STATUS  for a status read
 401          *                                               DAT     for a data read         defined under ks0108.c
 402          *                       
 403          * Returns     : status byte / data byte return from the LCD data port
 404          * Notes           : Hardware specific. The type bit is specific to Keil C complier.
 405          *********************************************************************************************************
 406          */
 407          unsigned char GDispRd(bit type)
 408          {
 409   1              unsigned char lcd_status;
 410   1              
 411   1              LCD_DATA = 0xFF;                                                //set data port as input to read LCD status
 412   1              (type==DATA_RD)?(LCD_DI=HIGH):(LCD_DI=LOW);
 413   1              LCD_RW = HIGH;
 414   1              LCD_EN = HIGH; 
 415   1              _nop_();
 416   1              lcd_status = LCD_DATA;
 417   1              LCD_EN = LOW;
 418   1      
 419   1              return (lcd_status);
 420   1      }
 421          
 422          /*
 423          *********************************************************************************************************
 424          *                                                               GDispBackLite
 425          *
 426          * Description : This function control the LCD backlight ENABLE -OR- DISABLE
C51 COMPILER V7.09   KS0108                                                                07/06/2006 15:48:34 PAGE 8   

 427          * Arguments   : 'bit'   ENABLE  for backlight ON
 428          *                                               DISABLE for backlight OFF
 429          *                       
 430          * Returns     : none
 431          * Notes           : Hardware specific. The type bit is specific to Keil C complier.
 432          *********************************************************************************************************
 433          */
 434          void GDispBackLite(bit ctrl)
 435          {
 436   1              (ctrl==ENABLE)?(LCD_BL=LOW):(LCD_BL=HIGH);
 437   1      }
 438          
 439          
 440          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    583    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      21
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       6
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
