GLCD 128x64 - main() looping

Thread Starter

onlyvinod56

Joined Oct 14, 2008
369
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


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);}

	
	 
}
 

ErnieM

Joined Apr 24, 2011
8,415
1. The lines are disappearing after the display. I cant hold the display.
I can only assume when you "cant hold the display" your unit drops to the floor and breaks. Keep a better grip on your device, or place it on your bench.

Or maybe give us a hint what "display" means.

2. I didn't use "while(1)" , but the main function is running infinite.
main() is all there is to run. It runs infinitely since you did not use a while(1): after main() exits, what else is the code supposed to do but run main() again and again?

And welcome to the forums!
 

THE_RB

Joined Feb 11, 2008
5,438
If the compiler is MikroC, it inserts a while(1) statement at the end of main. So main runs only once then the program hangs.
 

Thread Starter

onlyvinod56

Joined Oct 14, 2008
369
I can only assume when you "cant hold the display" your unit drops to the floor and breaks. Keep a better grip on your device, or place it on your bench Or maybe give us a hint what "display" means.
I cant hold means i am unable to stop the data on the display. it keeps on running. If i use while(1); at the end, then it is displaying the steady data.


main() is all there is to run. It runs infinitely since you did not use a while(1): after main() exits, what else is the code supposed to do but run main() again and again?
might be this is the reason.
 

ErnieM

Joined Apr 24, 2011
8,415
I cant hold means i am unable to stop the data on the display. it keeps on running. If i use while(1); at the end, then it is displaying the steady data.
might be this is the reason.
It looks like you already confirmed that.

while(1); is a popular C code snippet to get the program to stop and hold in one place till the end of time. It works since the "while(1) part forms a loop and the ";" part does nothing... so it loops endlessly doing nothing each loop.

Occasionally that is just what you need to do.
 

Thread Starter

onlyvinod56

Joined Oct 14, 2008
369
Occasionally that is just what you need to do.
Yes ofcourse. While(1); makes the program to stop. Upto this its ok.

Now suppose if the microcontroller is recieving external pulses. For first pulse i need to display a dot in a page(for ex page 3) and column 0.

The dot should stay on the screen until next pulse hits the controller. For the next pulse the column should be incremented in the same page.
The dot should not blink off.

THE PIXEL SHOULD REMAIN ON UNTIL THE NEXT PULSE HITS. WHEN THE NEXT PULSE HITS THE CURRENT PIXEL SHOULD GO OFF ANS THE NEXT COLUMN PIXEL SHOULD ON AND IT SHOULS REMAIN ON UNTIL THE NEXT PULSE.
 

MrChips

Joined Oct 2, 2009
35,168
You are incorrect on two counts.

1) A microcontroller requires a while (1) at the end of the program. Without this, the processor continues to execute random instructions which causes the program to "crash". The behaviour of the program can be totally random at that point. It can also be dependent on the compiler since some compilers may automatically include the while(1) statement while others may automatically restart the main( ) program.

2) LCD graphics displays with controller do not behave in the manner you have described. The image that appears on the screen is refreshed by the controller not the microprocessor. The microprocessor sends commands to the controller in order to set/reset individual pixels. These are commands not pulses.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,415
THE PIXEL SHOULD REMAIN ON UNTIL THE NEXT PULSE HITS. WHEN THE NEXT PULSE HITS THE CURRENT PIXEL SHOULD GO OFF ANS THE NEXT COLUMN PIXEL SHOULD ON AND IT SHOULS REMAIN ON UNTIL THE NEXT PULSE.
That is simple enough. You start with taking that "while(1);" statement and adding some tasks between the ")" and the ";" that being looking for the input pulse. IF you see a pulse, then do some drawing.

The simplest way is to just clear the screen then send in the new pixel. A better way is to remember where that last pixel got set, write to just that location to clear one pixel, then write the new pixel. You may even see one pixel fade out as the next fades in.

It's "better" because it's faster; clearing the entire screen takes some time.
 
Top