GLCD 128x64 - main() looping

Discussion in 'Embedded Systems and Microcontrollers' started by onlyvinod56, Dec 22, 2013.

  1. onlyvinod56

    Thread Starter Active Member

    Oct 14, 2008
    362
    1
    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


    Code ( (Unknown Language)):
    1. #include<reg51.h>
    2. #define dport P2
    3. #define DAT 1
    4. #define CMD 0
    5. #define DISPLAY_ON 0x3F  
    6. #define DISPLAY_OFF 0x3E
    7. #define LEFT 1
    8. #define RIGHT 0
    9. #define BOTH 2
    10. #define NONE 3
    11. #define MAX_ROW_PIXEL       64      //MAX_ROW_PIXEL the physical matrix length (y direction)                                     
    12. #define MAX_COL_PIXEL       128     //MAX_COL_PIXEL the physical matrix width (x direction)
    13. #define BLACK               1
    14. #define WHITE               0
    15. #define PAGE_OFFSET             0xB8  
    16. #define Y_OFFSET                0x40
    17.  
    18. sbit rs=P1^0;
    19. sbit rw=P1^1;
    20. sbit en=P1^2;
    21. sbit cs1=P1^3;
    22. sbit cs2=P1^4;
    23. sbit LCD_BL=P1^5;
    24. sbit LCD_RST=P1^6;
    25.  
    26.  
    27.  
    28. void delay()
    29. {
    30. int i,j;
    31. for(i=0;i<1;i++)
    32. for(j=0;j<1;j++);
    33. }
    34.  
    35. void GDispChipSel(unsigned char wing)  
    36. {  
    37.     if(wing==LEFT)  
    38.         {cs1=1;cs2=0;}  
    39.     else if(wing==RIGHT)  
    40.         {cs1=0;cs2=1;}  
    41.     else if (wing==BOTH)  
    42.         {cs1=1;cs2=1;}  
    43.     else if (wing==NONE)  
    44.         {cs1=0;cs2=0;}  
    45. }
    46.  
    47.                  
    48. void GDispWr(bit type, unsigned char dat)  
    49. {  
    50.     (type==DAT)?(rs=1):(rs=0);  
    51.     rw = 0;  
    52.     en = 1;    
    53.     dport = dat;  
    54.     delay();  
    55.     en = 0;  
    56. }
    57.  
    58. void GDispWrByte(unsigned char col, unsigned char page, unsigned char c)  
    59. {  
    60.     col     = col%MAX_COL_PIXEL;  
    61.     page    = page%8;  
    62.    
    63.     if(col<(MAX_COL_PIXEL/2))  
    64.         {GDispChipSel(LEFT);}  
    65.     else  
    66.         {GDispChipSel(RIGHT);col=col-(MAX_COL_PIXEL/2);}  
    67.    
    68.     GDispWr(CMD,PAGE_OFFSET|page);   GDispWr(CMD,Y_OFFSET|col); //set page and column position  
    69.     GDispWr(DAT, c);                                            //output data to D0-D7  
    70.     GDispChipSel(NONE);  
    71. }
    72.  
    73.  
    74. void GDispSwitch(bit sw)              // display switch
    75. {  
    76.     GDispChipSel(2);  
    77.     (sw==1)?(GDispWr(CMD,DISPLAY_ON)):(GDispWr(CMD,DISPLAY_OFF));  
    78.     GDispChipSel(0);  
    79. }  
    80.  
    81. void GDispBackLite(bit ctrl)              // back light
    82. {  
    83.     (ctrl==1)?(LCD_BL=0):(LCD_BL=1);  
    84. }  
    85.  
    86. void GDispReset(void)                  // reset
    87. {  
    88.     unsigned char i;  
    89.     LCD_RST=0;  
    90.     for(i=0;i<0xFF;i++);  
    91.     LCD_RST=1;  
    92.     for(i=0;i<0xFF;i++);  
    93. }
    94.  
    95. void GDispClr(unsigned char pat)  
    96. {  
    97.  unsigned char col, page;  
    98.    
    99.  for(col=0;col<128;col++)  
    100.     {
    101.      for(page=0;page<8;page++)
    102.      GDispWrByte(col,page,pat);  
    103.     }  
    104. }
    105.  
    106.  
    107.  
    108. void GDispInit(void)  
    109. {  
    110.     GDispBackLite(1);  
    111.     GDispReset();  
    112.     GDispSwitch(0);  
    113.     GDispWr(CMD,0xC0);//set display start line at the top of the LCD  
    114.     GDispClr(0x00);                 //clear the display  
    115.     GDispSwitch(1);  
    116. }
    117.  
    118. void main()
    119. {
    120. int i,j=0;
    121. GDispInit();
    122.  
    123.  GDispBackLite(1);  
    124.  GDispReset();  
    125.  GDispSwitch(1);
    126. for(j=0;j<8;j++)  
    127. {for(i=0;i<128;i++)  // column
    128. GDispWrByte(i,j,1);}
    129.  
    130.    
    131.      
    132. }
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.

    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!
     
  3. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
  4. onlyvinod56

    Thread Starter Active Member

    Oct 14, 2008
    362
    1
    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.
     
  5. onlyvinod56

    Thread Starter Active Member

    Oct 14, 2008
    362
    1
    Its keil compiler. Simulator is proteus.
     
    Last edited: Dec 28, 2013
  6. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    Insert a while(1); statement at the end of the program and see what happens.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  8. onlyvinod56

    Thread Starter Active Member

    Oct 14, 2008
    362
    1
    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.
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    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: Dec 26, 2013
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
Loading...