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.

LCD Character error!

Status
Not open for further replies.

Mithun_K_Das

Advanced Member level 3
Joined
Apr 24, 2010
Messages
899
Helped
24
Reputation
48
Reaction score
26
Trophy points
1,318
Location
Dhaka, Bangladesh, Bangladesh
Activity points
8,254
I don't know where I did the mistake. Kindly watch the video first for the result.
Code:
Code:
// LCD module connections
sbit LCD_RS at RD7_bit;
sbit LCD_EN at RD6_bit;
sbit LCD_D4 at RC5_bit;
sbit LCD_D5 at RC4_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD2_bit;

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

#define   buzzer    RD1_bit
#define   led       RD5_bit
char  keypadPort at PORTB;

void lcd_rst()
{
    Lcd_Init();                              // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR);                     // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Delay_ms(50);
}


void beep()
{
   buzzer = 1;Delay_ms(50);
   buzzer = 0;Delay_ms(50);
}

unsigned short kp, cnt, oldstate = 0;
char txt[6];
int n=0,k=0;
char number[12];
char phone[12];

void main()
{

  TRISA = 0xFF;//all input
  TRISD1_bit = 0;//output
  TRISD5_bit = 0;//output
  Lcd_Init();                              // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  Lcd_Out(1, 1, "SET NUMBER_");
  Keypad_Init();                           // Initialize Keypad
  beep();
  while(1)
  {


          kp = 0;
          kp = Keypad_Key_Click();
          if(kp!=0)
          {
             n++;
             beep();
          }
          // Prepare value for output, transform key to it's ASCII value
          switch (kp)
          {
              case  1: kp = 49; break; // 1        // Uncomment this block for keypad4x4
              case  2: kp = 50; break; // 2
              case  3: kp = 51; break; // 3
              case  4: kp = 65; break; // A
              case  5: kp = 52; break; // 4
              case  6: kp = 53; break; // 5
              case  7: kp = 54; break; // 6
              case  8: kp = 66; break; // B
              case  9: kp = 55; break; // 7
              case 10: kp = 56; break; // 8
              case 11: kp = 57; break; // 9
              case 12: kp = 67; break; // C
              case 13: kp = 42; break; // *
              case 14: kp = 48; break; // 0
              case 15: kp = 35; break; // #
              case 16: kp = 68; break; // D
          }
         
          if(n<12)
          {
             number[n-1] = kp;
             EEPROM_Write(n-1,number[n-1]);
             while(WR_bit);//save number in eeprom
             Delay_ms(60);
             phone[n-1] = 0;
             phone[n-1] = EEPROM_Read(n-1);
             if(kp!=0)Lcd_Chr(2, n,  phone[n-1]);
          }
             
          if(kp==42)// * pressed
          {
            lcd_rst();beep();beep();
            Lcd_Out(1, 1, "CANCELED! "); Delay_ms(2000);lcd_rst();
            Lcd_Out(1, 1, "SET NUMBER_");
            for(k=0;k<12;k++)
            {
               EEPROM_Write(k,0); while(WR_bit);//save number in eeprom
               Delay_ms(60);
            }
            EEPROM_Write(20,0);
            while(WR_bit);//save number in eeprom
            Delay_ms(60);
            n = 0;
          }
         
          if(kp==35) // # pressed
          {
            lcd_rst();beep();beep();
            Lcd_Out(1, 1, "NUMBER SAVED!");

           
            for(k=0;k<12;k++)
            {
              phone[k] = EEPROM_Read(k);
              Lcd_Chr(2, k,  phone[k]);
            }

           
            Delay_ms(2000);
            EEPROM_Write(20,1);
            while(WR_bit);//save number in eeprom
            Delay_ms(60);
            lcd_rst();
            n = 0;
          }
         
         if(EEPROM_Read(20)==1)
         {
            Lcd_Out(1, 1, "Device ready! ");
            Lcd_Out(2, 1, "Num:");
           
            for(k=0;k<12;k++)
            {
              phone[k] = EEPROM_Read(k);
              Lcd_Chr(2, 5+k,  phone[k]);
            }
         }
         else
         {
            Lcd_Out(1, 1, "SET NUMBER_");
         }
         

  }
}//void main
 

Attachments

  • Code.txt
    4.1 KB · Views: 72
Last edited by a moderator:

Solution
Why not simply pressing the CODE button in the editor window?

Klaus
Not working here.
--- Updated ---

The problem is solved !!!

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

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

#define   buzzer    RD1_bit
#define   led       RD5_bit
char  keypadPort at PORTB;

void lcd_rst()
{
    Lcd_Init();                              // Initialize LCD...

--- Updated ---

Sorry, I'm having trouble posting in edaboard. Anyway,
Problem is, in my code, I'm reading a keypad and trying to store the values in EEPROM. In the loop:

Code:
if(n<12)
          {
             number[n-1] = kp;
             EEPROM_Write(n-1,number[n-1]);
             while(WR_bit);//save number in eeprom
             Delay_ms(60);
             phone[n-1] = 0;
             phone[n-1] = EEPROM_Read(n-1);
             if(kp!=0)Lcd_Chr(2, n,  phone[n-1]);
          }

I'm simply reading the key values and saving in EEPROM. Then reading that value from EEPROM again to confirm if the number is written ok. And if you check the video, the MCU is doing ok.

But, after that, in the next loop:

Code:
if(kp==35) // # pressed
          {
            lcd_rst();beep();beep();
            Lcd_Out(1, 1, "NUMBER SAVED!");

           
            for(k=0;k<12;k++)
            {
              phone[k] = EEPROM_Read(k);
              Lcd_Chr(2, k,  phone[k]);
            }

           
            Delay_ms(2000);
            EEPROM_Write(20,1);
            while(WR_bit);//save number in eeprom
            Delay_ms(60);
            lcd_rst();
            n = 0;
          }

I'm checking if the process is ok and the number was saved in EEPROM. Here, the value is not ok. I'm trying to figure it out what is the mistake I made here.
 
Last edited by a moderator:

I've edited your code tags again, they should look like this without the spaces: [ C O D E ] at the beginning and [ / C O D E ] at the end.

That compiler doesn't work under Linux so I can't help with the code except general comments:
1. why the delay after "while(WR_bit);//save number in eeprom" ? The WR bit is telling you it has finished its task.
2. using "[n-1]" as an array index is very inefficient because it forces a calculation each time it is used.
3. if "lcd_rst();" is resetting the LCD to clear it, use the clear command instead, it will be quicker.
4. make the code more readable by #defining names for the keys, for example "#define Key_9 57".
5. does pressing the '#' key actually store the number at all?

Brian.
 

I've edited your code tags again, they should look like this without the spaces: [ C O D E ] at the beginning and [ / C O D E ] at the end.

That compiler doesn't work under Linux so I can't help with the code except general comments:
1. why the delay after "while(WR_bit);//save number in eeprom" ? The WR bit is telling you it has finished its task.
2. using "[n-1]" as an array index is very inefficient because it forces a calculation each time it is used.
3. if "lcd_rst();" is resetting the LCD to clear it, use the clear command instead, it will be quicker.
4. make the code more readable by #defining names for the keys, for example "#define Key_9 57".
5. does pressing the '#' key actually store the number at all?

Brian.


Thanks for the hint. [code...]

1. While(WR_bit); it works normally, as I'm getting error, I added extra delay after that if it can be solved.
2. as the n is increased before that is why I'm taking n-1 array address, this can be modified to reduce calculation
3. ok, it is corrected
4. ok, I'll do that
5. after # button, I'm just trying to display the saved number, although the number is being saved before.
--- Updated ---



Code:
test
test
test
 

You are processing for no button pressed (kp = 0) the same as if a button were pressed, so EEPROM gets filled with zeros.

Here is my attempt at a horrible fix. Still messy, still buggy, but better formatted.
Requires valid entry of 11 numbers, else garbage is saved - but will leave that for you to fix.

Code:
// random mikroC code found on forum
// PIC16F877A with 20MHz 2-pin crystal
// 4x4 keypad on PORTB
// 16x2 LCD module on ports C/D as defined below

// LCD module connections
sbit LCD_RS at RD7_bit;
sbit LCD_EN at RD6_bit;
sbit LCD_D4 at RC5_bit;
sbit LCD_D5 at RC4_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD2_bit;

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

#define   buzzer    RD1_bit
#define   led       RD5_bit
char  keypadPort at PORTB;

void lcd_rst()
{
    Lcd_Init();                                                                 // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR);                                                        // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Delay_ms(50);
}

void beep()
{
    buzzer = 1;Delay_ms(50);
    buzzer = 0;Delay_ms(50);
}

unsigned short kp;
unsigned short cnt;
unsigned short oldstate = 0;
char txt[6];
int n = 0;
int k = 0;
char number[12];
char phone[12];

void main()
{
    TRISA = 0xFF;                                                               // all input
    TRISD1_bit = 0;                                                             // output
    TRISD5_bit = 0;//output
    Lcd_Init();                                                                 // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR);                                                        // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);                                                   // Cursor off
    Lcd_Out(1, 1, "SET NUMBER_");
    Keypad_Init();                                                              // Initialize Keypad
    beep();
    
    while(1)
    {
        kp = 0;
        kp = Keypad_Key_Click();
        if(kp != 0)
        {
            n++;
            beep();
            // Prepare value for output, transform key to it's ASCII value
            switch (kp)
            {
                case  1: kp = '1'; break; // 1
                case  2: kp = '2'; break; // 2
                case  3: kp = '3'; break; // 3
                case  4: kp = 'A'; break; // A
                case  5: kp = '4'; break; // 4
                case  6: kp = '5'; break; // 5
                case  7: kp = '6'; break; // 6
                case  8: kp = 'B'; break; // B
                case  9: kp = '7'; break; // 7
                case 10: kp = '8'; break; // 8
                case 11: kp = '9'; break; // 9
                case 12: kp = 'C'; break; // C
                case 13: kp = '*'; break; // *
                case 14: kp = '0'; break; // 0
                case 15: kp = '#'; break; // #
                case 16: kp = 'D'; break; // D
            }

            if(n < 12)
            {
                number[n] = kp;
                EEPROM_Write(n, number[n]);
                while(WR_bit);                                                      // save number in eeprom
                Delay_ms(60);
                phone[n] = 0;
                phone[n] = EEPROM_Read(n);
                if(kp != 0){
                    Lcd_Chr(2, n,  phone[n]);
                }
            }

            if(kp == '*')                                                           // * pressed
            {
                lcd_rst();beep();beep();
                Lcd_Out(1, 1, "CANCELED! "); Delay_ms(2000);lcd_rst();
                Lcd_Out(1, 1, "SET NUMBER_");
                for(k = 0; k < 12; k++)
                {
                    EEPROM_Write(k, 0);
                    while(WR_bit);                                                  // save number in eeprom
                    Delay_ms(60);
                }
                EEPROM_Write(20,0);
                while(WR_bit);                                                      // save number in eeprom
                Delay_ms(60);
                n = 0;
            }

            if(kp == '#') // # pressed
            {
                lcd_rst();beep();beep();
                Lcd_Out(1, 1, "NUMBER SAVED!");

                for(k = 0; k < 12; k++)
                {
                    phone[k] = EEPROM_Read(k);
                    Lcd_Chr(2, k,  phone[k]);
                }

                Delay_ms(2000);
                EEPROM_Write(20,1);
                while(WR_bit);                                                      // save number in eeprom
                Delay_ms(60);
                lcd_rst();
                n = 0;
            }

            if(EEPROM_Read(20) == 1)
            {
                Lcd_Out(1, 1, "Device ready! ");
                Lcd_Out(2, 1, "Num:");

                for(k = 0; k < 12; k++)
                {
                    phone[k] = EEPROM_Read(k);
                    Lcd_Chr(2, 5 + k,  phone[k]);
                }
            }
            else
            {
                Lcd_Out(1, 1, "SET NUMBER_");
            }
        }
    }
}//void main
--- Updated ---

OOPS, sorry

After further checking, My attempted fix works really badly too.

May put more effort in tomorrow (but no promises).

Ignore this post
 
Last edited:

Why not simply pressing the CODE button in the editor window?

Klaus
Not working here.
--- Updated ---

The problem is solved !!!

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

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

#define   buzzer    RD1_bit
#define   led       RD5_bit
char  keypadPort at PORTB;

void lcd_rst()
{
    Lcd_Init();                              // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR);                     // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);
    Delay_ms(50);
}


void beep()
{
   buzzer = 1;Delay_ms(50);
   buzzer = 0;Delay_ms(50);
}

unsigned short kp, cnt, oldstate = 0;
char txt[6];
int n=0,k=0;
char number[12];
char phone[12];

void main() 
{

  TRISA = 0xFF;//all input
  TRISD1_bit = 0;//output
  TRISD5_bit = 0;//output
  Lcd_Init();                              // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  Lcd_Out(1, 1, "SET NUMBER_");
  Keypad_Init();                           // Initialize Keypad
  beep();
  while(1)
  {


          kp = 0;
          kp = Keypad_Key_Click();
          if(kp!=0)
          {
              n++;
              beep();

              // Prepare value for output, transform key to it's ASCII value
              switch (kp)
              {
                  case  1: kp = 49; break; // 1        // Uncomment this block for keypad4x4
                  case  2: kp = 50; break; // 2
                  case  3: kp = 51; break; // 3
                  case  4: kp = 65; break; // A
                  case  5: kp = 52; break; // 4
                  case  6: kp = 53; break; // 5
                  case  7: kp = 54; break; // 6
                  case  8: kp = 66; break; // B
                  case  9: kp = 55; break; // 7
                  case 10: kp = 56; break; // 8
                  case 11: kp = 57; break; // 9
                  case 12: kp = 67; break; // C
                  case 13: kp = 42; break; // *
                  case 14: kp = 48; break; // 0
                  case 15: kp = 35; break; // #
                  case 16: kp = 68; break; // D
              }

              if(n<12)
              {
                 number[n-1] = kp;
                 EEPROM_Write(n-1,number[n-1]);
                 while(WR_bit);//save number in eeprom
                 Delay_ms(60);
                 phone[n-1] = 0;
                 phone[n-1] = EEPROM_Read(n-1);
                 if(kp!=0)Lcd_Chr(2, n,  phone[n-1]);
              }

              if(kp==42)// * pressed
              {
                lcd_rst();beep();beep();
                Lcd_Out(1, 1, "CANCELED! "); Delay_ms(2000);lcd_rst();
                Lcd_Out(1, 1, "SET NUMBER_");
                for(k=0;k<12;k++)
                {
                   EEPROM_Write(k,0); while(WR_bit);//save number in eeprom
                   Delay_ms(60);
                }
                EEPROM_Write(20,0);
                while(WR_bit);//save number in eeprom
                Delay_ms(60);
                n = 0;
              }

              if(kp==35) // # pressed
              {
                lcd_rst();beep();beep();
                Lcd_Out(1, 1, "NUMBER SAVED!");


                for(k=0;k<11;k++)
                {
                  phone[k] = EEPROM_Read(k);
                  Lcd_Chr(2, k+1,  phone[k]);
                }


                Delay_ms(2000);
                EEPROM_Write(20,1);
                while(WR_bit);//save number in eeprom
                Delay_ms(60);
                lcd_rst();
                n = 0;
              }
         }
          
         if(EEPROM_Read(20)==1)
         {
            Lcd_Out(1, 1, "Device ready! ");
            Lcd_Out(2, 1, "Num:");
            
            for(k=0;k<11;k++)
            {
              phone[k] = EEPROM_Read(k);
              Lcd_Chr(2, 5+k,  phone[k]);
            }
         }
         else
         {
            Lcd_Out(1, 1, "SET NUMBER_");
         }
          

  }
}//void main
 
Last edited:

Solution
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top