Hello.
I am from electrical backgound and i know only basic programming skills in 'C'. I know how to generate pulses and display data on 16x2 LCD. First time i am trying on GLCD (KS0108).
As for practice, first i want to draw horizontal lines on every page of the glcd. I got it but with two problems.
1. The lines are disappearing after the display. I cant hold the display.
2. I didn't use "while(1)" , but the main function is running infinite.
My code
I am from electrical backgound and i know only basic programming skills in 'C'. I know how to generate pulses and display data on 16x2 LCD. First time i am trying on GLCD (KS0108).
As for practice, first i want to draw horizontal lines on every page of the glcd. I got it but with two problems.
1. The lines are disappearing after the display. I cant hold the display.
2. I didn't use "while(1)" , but the main function is running infinite.
My code
Rich (BB code):
#include<reg51.h>
#define dport P2
#define DAT 1
#define CMD 0
#define DISPLAY_ON 0x3F
#define DISPLAY_OFF 0x3E
#define LEFT 1
#define RIGHT 0
#define BOTH 2
#define NONE 3
#define MAX_ROW_PIXEL 64 //MAX_ROW_PIXEL the physical matrix length (y direction)
#define MAX_COL_PIXEL 128 //MAX_COL_PIXEL the physical matrix width (x direction)
#define BLACK 1
#define WHITE 0
#define PAGE_OFFSET 0xB8
#define Y_OFFSET 0x40
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P1^2;
sbit cs1=P1^3;
sbit cs2=P1^4;
sbit LCD_BL=P1^5;
sbit LCD_RST=P1^6;
void delay()
{
int i,j;
for(i=0;i<1;i++)
for(j=0;j<1;j++);
}
void GDispChipSel(unsigned char wing)
{
if(wing==LEFT)
{cs1=1;cs2=0;}
else if(wing==RIGHT)
{cs1=0;cs2=1;}
else if (wing==BOTH)
{cs1=1;cs2=1;}
else if (wing==NONE)
{cs1=0;cs2=0;}
}
void GDispWr(bit type, unsigned char dat)
{
(type==DAT)?(rs=1):(rs=0);
rw = 0;
en = 1;
dport = dat;
delay();
en = 0;
}
void GDispWrByte(unsigned char col, unsigned char page, unsigned char c)
{
col = col%MAX_COL_PIXEL;
page = page%8;
if(col<(MAX_COL_PIXEL/2))
{GDispChipSel(LEFT);}
else
{GDispChipSel(RIGHT);col=col-(MAX_COL_PIXEL/2);}
GDispWr(CMD,PAGE_OFFSET|page); GDispWr(CMD,Y_OFFSET|col); //set page and column position
GDispWr(DAT, c); //output data to D0-D7
GDispChipSel(NONE);
}
void GDispSwitch(bit sw) // display switch
{
GDispChipSel(2);
(sw==1)?(GDispWr(CMD,DISPLAY_ON)):(GDispWr(CMD,DISPLAY_OFF));
GDispChipSel(0);
}
void GDispBackLite(bit ctrl) // back light
{
(ctrl==1)?(LCD_BL=0):(LCD_BL=1);
}
void GDispReset(void) // reset
{
unsigned char i;
LCD_RST=0;
for(i=0;i<0xFF;i++);
LCD_RST=1;
for(i=0;i<0xFF;i++);
}
void GDispClr(unsigned char pat)
{
unsigned char col, page;
for(col=0;col<128;col++)
{
for(page=0;page<8;page++)
GDispWrByte(col,page,pat);
}
}
void GDispInit(void)
{
GDispBackLite(1);
GDispReset();
GDispSwitch(0);
GDispWr(CMD,0xC0);//set display start line at the top of the LCD
GDispClr(0x00); //clear the display
GDispSwitch(1);
}
void main()
{
int i,j=0;
GDispInit();
GDispBackLite(1);
GDispReset();
GDispSwitch(1);
for(j=0;j<8;j++)
{for(i=0;i<128;i++) // column
GDispWrByte(i,j,1);}
}