Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Display randomly characters

Status
Not open for further replies.

sumonpic10

Newbie level 5
Newbie level 5
Joined
Jul 30, 2016
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
99
Dear All,
Recently I facing some problems with LCD display characters, here I used next & previous button for change the program line in the LCD display. But there some random characters came when I pressed the previous button. And my while loop working not good with display. But I was used some led high/low command in that while loop. That's led working according my code. At that time display is random characters. Pls help me, below my code & screenshot of display


Code:
  // LCD module connections  /////////////////////////////////////////////
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections //////////////////////////////////////////////

// SWITCH CONNECTIONS       ///////////////////////////////////////////////

#define prog      porta.f3        // used for INPUT
#define rev       porta.f7        // used for INPUT
#define forw      portb.f0        // used for INPUT

//END SWITCH CONNECTIONS    ///////////////////////////////////////////////



const char menu_1[] = "MAIN MENU";
const char part1[] = "1 PROGRAM";
const char part2[] = "2 PROGRAM";
const char part3[] = "3 PROGRAM";
const char part4[] = "4 PROGRAM";
const char part5[] = "5 PROGRAM";
const char part6[] = "6 PROGRAM";

const char arrow[] = "<==";





unsigned int number = 1;


char msg[20]; //declare array

// copy const to ram string
char * CopyConst2Ram(char * dest, const char * src){
  char * d ;
      d = dest;
      for(;*dest++ = *src++;)
      ;
  return d;
}



void main()
{


  ANSEL  = 0x01;
  ANSELH = 0;                 // Configure other AN pins as digital I/O
  C1ON_bit = 0;               // Disable comparators
  C2ON_bit = 0;
  adcon1=0x80;                // Analog A0 is Active
  trisa =0xff;
  porta =0x00;
  trise =0xff;
  porte =0x00;
  trisb =0xff;
  portb =0x00;
  trisd =0x00;
  portd =0x00;

    lcd_init();                     // initialazied the LCD
    lcd_cmd(_LCD_CLEAR);             // Clean thje LCD
    lcd_cmd(_LCD_CURSOR_OFF);        // LCD cursor off

  while(1)
   {


      while(number == 1)          // while number = 1 ,then print Arrow on the 1no program
      {

        if(rev == 1)              // check rev button is 1 or 0
        {
         number = 2;              // if button is press number value will save 2
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         delay_ms(200);             // Delay 200 mili second
        }

        Lcd_Out(1,6,CopyConst2Ram(msg,menu_1));     // print    "MAIN MENU";
        Lcd_Out(2,1,CopyConst2Ram(msg,part1));      // print    "1 PROGRAM"; <==
        Lcd_Out(3,1,CopyConst2Ram(msg,part2));      // print    "2 PROGRAM";
        Lcd_Out(4,1,CopyConst2Ram(msg,part3));      // print    "3 PROGRAM";
        Lcd_Out(2,11,CopyConst2Ram(msg,arrow));     //    <==


      }

      while(number == 2)           // when number = 2
      {


        if(rev == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 3;
         delay_ms(200);
        }

        if(forw == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 1;
         delay_ms(200);
        }

        Lcd_Out(1,6,CopyConst2Ram(msg,menu_1));     // print    "MAIN MENU";
        Lcd_Out(2,1,CopyConst2Ram(msg,part1));      // print    "1 PROGRAM";
        Lcd_Out(3,1,CopyConst2Ram(msg,part2));      // print    "2 PROGRAM"; <==
        Lcd_Out(4,1,CopyConst2Ram(msg,part3));      // print    "3 PROGRAM";
        Lcd_Out(3,11,CopyConst2Ram(msg,arrow));     //    <==


      }
      
      
      
      while(number == 3)           // when number = 2
      {


        if(rev == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 4;
         delay_ms(200);
        }

        if(forw == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 2;
         delay_ms(200);
        }

        Lcd_Out(1,6,CopyConst2Ram(msg,menu_1));     // print    "MAIN MENU";
        Lcd_Out(2,1,CopyConst2Ram(msg,part1));      // print    "1 PROGRAM";
        Lcd_Out(3,1,CopyConst2Ram(msg,part2));      // print    "2 PROGRAM"; <==
        Lcd_Out(4,1,CopyConst2Ram(msg,part3));      // print    "3 PROGRAM";
        Lcd_Out(4,11,CopyConst2Ram(msg,arrow));     //    <==


      }

      
      
      while(number == 4)           // when number = 2
      {


        if(rev == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 5;
         delay_ms(200);
        }

        if(forw == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 3;
         delay_ms(200);
        }

        Lcd_Out(1,6,CopyConst2Ram(msg,menu_1));     // print    "MAIN MENU";
        Lcd_Out(2,1,CopyConst2Ram(msg,part4));      // print    "1 PROGRAM";
        Lcd_Out(3,1,CopyConst2Ram(msg,part5));      // print    "2 PROGRAM"; <==
        Lcd_Out(4,1,CopyConst2Ram(msg,part6));      // print    "3 PROGRAM";
        Lcd_Out(2,11,CopyConst2Ram(msg,arrow));     //    <==


      }

      
      
      while(number == 5)           // when number = 2
      {


        if(rev == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 6;
         delay_ms(200);
        }

        if(forw == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 4;
         delay_ms(200);
        }

        Lcd_Out(1,6,CopyConst2Ram(msg,menu_1));     // print    "MAIN MENU";
        Lcd_Out(2,1,CopyConst2Ram(msg,part4));      // print    "1 PROGRAM";
        Lcd_Out(3,1,CopyConst2Ram(msg,part5));      // print    "2 PROGRAM"; <==
        Lcd_Out(4,1,CopyConst2Ram(msg,part6));      // print    "3 PROGRAM";
        Lcd_Out(3,11,CopyConst2Ram(msg,arrow));     //    <==


      }

      
      
      
      while(number == 6)           // when number = 2
      {




        if(forw == 1)
        {
         lcd_cmd(_LCD_CLEAR);
         lcd_cmd(_LCD_CURSOR_OFF);
         number = 5;
         delay_ms(200);
        }

        Lcd_Out(1,6,CopyConst2Ram(msg,menu_1));     // print    "MAIN MENU";
        Lcd_Out(2,1,CopyConst2Ram(msg,part4));      // print    "1 PROGRAM";
        Lcd_Out(3,1,CopyConst2Ram(msg,part5));      // print    "2 PROGRAM"; <==
        Lcd_Out(4,1,CopyConst2Ram(msg,part6));      // print    "3 PROGRAM";
        Lcd_Out(4,11,CopyConst2Ram(msg,arrow));     //    <==


      }
      
      


   }
}


IMG_20181126_135906.png
after rev.PNG
after forw.PNG
 

Just wonder, where is this sh***t is comming from?
Code:
// copy const to ram string
char * CopyConst2Ram(char * dest, const char * src){
  char * d ;
      d = dest;
      for(;*dest++ = *src++;)
      ;
  return d;
}
;
 

If you are talking about the display leaving the 'ghost' arrow behind then that is a direct consequence of the way you have coded this. I suggest that you learn about 'state transition diagrams' (or 'finite state machines' - same thing) and how they can easily solve this type of problem.
To demonstrate what is happening, lets take 'number as begin 1. Therefore the first 'while' loop will continually write the 4 lines to the LCD with the arrow pointing to the first menu entry. Note that this path does not try to erase the display each time - it just writes the characters over the top of whatever is there. In this case it over-writes with the same characters and arrow position.
How the button gets pushed (and I assume the 200mSec delay is your attempt at a debounce!) so the display is erased, 'number' becomes 2 *BUT* you still fall into the part where you write the menu with the arrow pointing to the first entry.
However, as 'number' is not 2, the first while loop will exist and the 2nd while loop is entered. If 'rev' is 0 by now, and 'fwd' is also 0, then the LCD is over-written by the menu text but with the arrow pointing to the 2nd entry.
You will note that we have *NOT* erased the LCD between the last run through the first 'while' loop and this run through the 2nd 'while' loop.
Because you are not writing to end of each LCD line, you are not removing the arrow pointing to the first menu option.
Pencil and paper would have shown you this fault in the logic before you even came near the keyboard.
Susan
 
Sir, I used this code for reduce the used RAM space. Now RAM space grab 9% but before use this code RAM space was assume 23%. Thanks

- - - Updated - - -

Sir, I used this code for reduce the used RAM space. Now RAM space grab 9% but before use this code RAM space was assume 23%. Thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top