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.

Button count in LCD

Status
Not open for further replies.

jack1998

Junior Member level 2
Joined
Oct 17, 2021
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
136
I have connected the 8051 microcontrollers to the 16x2 LCD. I have connected the button so that I can increment every digit. When I pressed the button, it increments for single digits when I use it this way.

Code:
unsigned char number[2] = "3";
int temp = 0;

void display1(void) {
    write_string(1, 0, number);
    delay_msec(100);
}

int write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char  *str) {                          
    if (row)
    {
        col |= 0x40;                  
    }
    col |= 0x80;
    lcd_cout(col);                
    while (*str)
        temp = lcd_dout(*str++);
    return temp;                      
}

void incrementbutton(void) {
    if (button == 1) {            
        temp = temp + 1;        
        button = 0;
    }    
}
But when I use it as below it does not get incremented. I want to increment more than one digits in the LCD and tried the below way so that I can increment that. How can I do that? Any other way? Or below where I am wrong?

Code:
int write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char  *str) {                          
    if (row)
    {
        col |= 0x40;                  
    }
    col |= 0x80;
    lcd_cout(col);

    while (*str)
        lcd_dout(*str++);
}

void display1(void) {
    temp = write_string(1, 0, number);
    delay_msec(100);
}

void incrementbutton(void) {  
    if (button == 1) {                     //button is pressed        
        temp = temp + 1;     
        button = 0;                       
    }
}
 

Hello!

(sigh!!)
As usual, we don't know what you're up to. At least I don't know.
I suppose it's related to your last post, but clearly, I don't know what you are
trying to do. If the problem is clearly explained, it's half solved. But there
are no other explanations than the fact that there is one button and that you
want increment something.
So first:
1. What do you want to do? Increment a counter and display it?
2. What is a "uint_fast8_t"? Do you really need to define a type? There are
already plenty of type, for example uint8_t. What is the difference between a
regular uint8_t and a fast one?
3. Your string length is 2. If you have 2 digits in it, there are chances
that your write_string will not end cleanly. You should reserve one extra
character for the 0 value because you don't know what is in the RAM after the
string.
4. Beside this, I don't know where these functions are called, how you detect
the button press, etc, etc...

So, please, start with my question 1 above, and it will be easier.

Dora.
 
If you are trying to increase the rate of digit increase proportional
to time button pressed then you have to keep track of time and
make the increment proportional to time button has been pressed.

Make a flow chart , it will become obvious what you need to do.


Regards, Dana.
 
@doraemon It's related to the last post. Yeah, I want to increment the count and display it and it is incrementing for one digit as I show the code above, but now I have displayed two digits in the LCD display and tried the below second way but it does not increment the count. It is not necessary to define the different types, for the time being, I have just used them, I will correct them. I will correct the string length too.
All functions are called in the main function. What is the problem in the second code, can you notice where I am wrong?
 

Hello!

It's related to the last post.

So basically you are expecting me to take your new code snippet, open an editor,
replace what has changed before debugging it? You ask me to compare 2 codes,
but did you try to compare it yourself? (i.e. remove the common parts and see what's left?
By the way there is a good tool to know: the diff tool, that will highlight all the differences
between to source files.

I don't have access to your full code and therefore cannot help. If we keep telling
you that we need the WHOLE code (from the includes down to the last function, custom
include files if any), it means that we do need it.

In your second code, for example, the variable you call "number" is not declared
anywhere. Maybe it is in your code, maybe not. How can I know?
The variable temp is not declared either. So from this point of view, this code
cannot compile.

Before doing a complex system, what about first trying to write a string on your LCD,
AND ONLY THAT?
Apparently you don't master that kind of code, sorry to put it that way, but you should
try simple.
Beside this, you are talking about 2 digits. Where are they? Supposing the string
"number" is actually defined, it CANNOT CONTAIN 2 DIGITS because it needs a trailing 0
as all C strings do need a termaination, and as your writ_sring function also needs
a termination. One possibility is that if you have 2 digits, the code jumps to nowhere
because it's outside of an allocated area/

So my advice would be:
1. Start with some string, and try to write it anywhere on your LCD.
Example:
char * hellostring[] = "Hello!";

write_string(0, 0, hellostring);

// Check what would happen if you don't have enough space. The implementation
// is up to you, you may decide that it will write "He" at the end of the first
// line, but you may also decide that it will write "llo!" on the next line.
write_string(0, 14, hellostring);

2. Try to write a function that converts a number to a string.
int32 val;

int32_to_str(char * str, int32 val);
Test the this function for all the possible widths.
Same as above, you may decide to write the beginning of the number only, write the
end on the second line, write "error", etc if you have no space.
Write numbers of 2, 3, 4, 5, 6, 7 .. etc... digits to test your functions.

Then when you master the LCD, add your button stuff and we can talk again.

Dora.
 
@doraemon I have declared all the variables at the top of the program. Since that is the same for both I don't declare that in second.
As I already said, I have made an increment of one digit and in my last post, I have code that prints in the LCD.
Of course, I will try the way u said. But for the time being, would u please help me why assigning the same function to some variable temp gives the counter increment, but why not in another same function. Thank you.
 

Hi,

it´s you who causes the delays and makes it unable to help.

We like to help, we repeatedly ask for complete informations ...


Klaus
 
A feature of C language is that we can use libraries and don't need to write anything, e.g. formatted number output, from the scratch. Unless specified otherwise in your exercise problem, I would expect usage of sprintf() or itoa() for multi digit number to string conversion.
 
Hi,

it´s you who causes the delays and makes it unable to help.

We like to help, we repeatedly ask for complete informations ...


Klaus
@KlausST till the code and relevant information for the problem is all I have provided.
--- Updated ---

A feature of C language is that we can use libraries and don't need to write anything, e.g. formatted number output, from the scratch. Unless specified otherwise in your exercise problem, I would expect usage of sprintf() or itoa() for multi digit number to string conversion.
@FvM I want to increment the two or more than 2 digits in the LCD when the button is pressed and usage of sprintf() and.....how it works...
--- Updated ---

@KlausST how can i make count value to zero after it reach 9, at present its incrementing between 0 to 9.
Code:
void incrementbutton(void) {  
    if (button == 1) {                     //button is pressed              
       if (temp >= 48 && temp < 57) {                         //to check the counter value between 0-9
          temp = temp+1;
          if (temp >57) {                                                   //to make count value to 0 when count is more than 9
              temp = 48;
          }
        button = 0;                       
    }
}
 
Last edited:

till the code and relevant information for the problem is all I have provided.
Hi,

You say so, but we can´t agree. And we told you this many times.

Why do yo make it so difficult?
For us it´s not the big problem, it´s just demotivating.

Now you spend time to write you are in hurry ... but at the same you don´t spend time to give useful information.
I don´t understand this.

One by one of helpful people will leave this thread.

Klaus
 
Hello!

@KlausST till the code and relevant information for the problem is all I have provided.

I'm not sure about what you are trying to say, but no, you haven't provided the code.
Supposing somebody want to spend some more time on your exercises (not me, sorry,
whatever I tell you, it seems that you don't listen), you have to provide the
whole code, not in pieces, in such a case, a single file will do. Do you understand
what we keep repeating? The WHOLE code, be it main.c or whatever.

In that case the guy who want to spend more time assisting you could make a
new project in his MPLab, cut and paste the whole code, compile it and see
what happens.

You are asking us to do your homework, and you apparently didn't do much in
the last 3 days. Thinking about my past experience, a long time ago, I think
maybe you don't know where to start, so I'm usually not against giving some
of my time to help, but you have to understand that if you want help, you have
to make it easy for the people who may help you.

At present, we just have a fuzzy description of one program that prints only
one digit in this case and nothing in that case. I had to ask so that you
finally told me you want to make a counter. I'm guessing you want a counter
from 0 to 99 since you want 2 digits, but even this is not clearly said.
I've just given you hints about what I guess you want to do, but I may have
lost my time.

So, together with the WHOLE code:

- Describe EXACTLY what you want to make (ok, I guessed it's a counter), from
which value until which value, what does it when it reaches the upper value?
Roll down to 0? to 1?

- Describe EXACTLY what happens. It counts right from 0 to 9? Then what does
it do at the next step? Print 1 because the 0 for 10 is missing? Print garbage?
Does it print at the right place?

By the way, why did you open a new topic if it was the same program?

Just a warning: if you keep ignoring our advices (which by the way are taken
on our time, and are free), there are chances that this thread will be closed.

Etc, etc...

Dora
 
@KlausST till the code and relevant information for the problem is all I have provided.
--- Updated ---


@FvM I want to increment the two or more than 2 digits in the LCD when the button is pressed and usage of sprintf() and.....how it works...
--- Updated ---

@KlausST how can i make count value to zero after it reach 9, at present its incrementing between 0 to 9.
Code:
void incrementbutton(void) {
    if (button == 1) {                     //button is pressed        
       if (temp >= 48 && temp < 57) {                         //to check the counter value between 0-9
          temp = temp+1;
          if (temp >57) {                                                   //to make count value to 0 when count is more than 9
              temp = 48;
          }
        button = 0;                 
    }
}
The button press followed by release (you should debounce it, in and out), should unconditionally
increment a total variable. Then convert that variable to a printable string. Of course you probably
want some criteria to reset the variable, another button, or a press for extended time or.....unless
you want it to count to infinity and beyond.....

When you print variable you only want to print it when total changes. If you print in a tight
loop, with no key presses, ad infinitum, you can get undesired display artifacts. So to print
test total variable has changed, otherwise do not print, its already on display. This of course
if your LCD has its own internal controller. Which is likely given its a 16 x 2 module.

Regards, Dana.
 
Last edited:
@doraemon @KlausST @FvM @danadakk here is all the details I have coded till.
I have connected the LCD with the microcontroller 8051. I have used 16x2 LCD where D4_D8 are used for I/O and LCD connected to the microcontroller port P7. I have connected two buttons to the PORT2 one for the moving cursor and another for the increment. For the time being, I have printed the two numbers in a specified position (1,0) and (1,1) respectively.

I want these two numbers increments when the button is pressed. When I move the cursor in the printed location and press the button I want to increase the number i.e 3 and when I move the cursor to the specified location of the number1 variable I want to increase the number1 i.e 2.
I want to increment the count between 0 to 9 and when more than 9 it count back to 0.

For the time being, the count for the number i.e 3 is incrementing 0 to 9 but does not return back to 0 after 9 when the button is pressed, it remains in 9 but I want the count to roll down to 0.
And this is all the code I am using till and increments only a single count.
How can I increment the temp and temp1 count with the same increment button?
Thank you.

Code:
#include <stdio.h>
#include <string.h>
#include <stdint.h>

extern unsigned char number[4] = "3 ";
extern unsigned char number1[4] = "2 ";
int temp = 0;
int temp1 = 0;
int place1 = 1;
int place2 = 0;
unsigned char  button    = 0;        
unsigned char  button1  = 0;

void Countdisplay(void);
void lcd_init(void);
void lcd_cout(unsigned char ccod);
void lcd_dout(unsigned char dcod);
void display1(void);
void set_cursor_position (uint_fast8_t row, uint_fast8_t col);
void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char  *str);
void incrementbutton(void);
void op_switich_in(void)  ;
unsigned char op_swin_1d(void);
void move_cursor(void); 
void CountdisplayToLCD(void);

void main()
              {
                          lcd_init();
                          display1();
             while (1)
                {
                  incrementbutton();
                  move_cursor();
                }
            }

void lcd_init(void)
            {
              P7 = 0x00;                         //E=0,RS=0

              P7 = 0x03;
              P7 = 0x23;                           //E=1,RS=0
              delay_micro(100);
              P7 = 0x03;                           //E=0,RS=0
              delay_msec(5);
              P7 = 0x23;                            //E=1,RS=0
              delay_micro(100);
              P7 = 0x03;                               //E=0,RS=0
              delay_msec(5);
              P7 = 0x23;                                 //E=1,RS=0
              delay_micro(100);
              P7 = 0x03;                                //E=0,RS=0
              lcd_cout(0x02);                   
              delay_msec(5);
              lcd_cout(0x28);    
              delay_msec(5);
              lcd_cout(0x0C);
              delay_msec(5);                 
          }

        void lcd_cout(unsigned char ccod)           //for Lcd command
          {
              unsigned char ccod_msb;
              unsigned char ccod_lsb;

              ccod_msb = ccod / 0x10;
              ccod_lsb = ccod & 0x0F;
              P7 = ccod_msb;             

              P7 = ccod_msb | 0x20;       
              delay_micro(2);
              P7 = ccod_msb;     
              delay_micro(2);
              P7 = ccod_lsb; 
              P7 = ccod_lsb | 0x20;
              delay_micro(2);
              P7 = ccod_lsb;     
              delay_micro(50);           
               }

              void lcd_dout(unsigned char dcod)                    //for lcd_Data
                     {
               unsigned char dcod_msb;
               unsigned char dcod_lsb;

               dcod_msb = dcod / 0x10;
               dcod_lsb = dcod & 0x0F;

               P7 = dcod_msb | 0x10; 

               P7 = dcod_msb | 0x30; 
               delay_micro(2);
               P7 = dcod_msb | 0x10; 
               delay_micro(2);
               P7 = dcod_lsb | 0x10;     
               P7 = dcod_lsb | 0x30; 
               delay_micro(2);
               P7 = dcod_lsb | 0x10; 
               delay_micro(50);  
           }

       void display1(void)
          {                      
          temp =  write_string(1, 0, number);
            delay_msec(100);
          temp1 =    write_string(1, 1, number1);
            delay_msec(100);         
         }

void set_cursor_position (uint_fast8_t row, uint_fast8_t col)                //for moving cursor                              
           {           
          if (row)
                        {
                        col |= 0x40;                 
                    }
                    col |= 0x80;
                    lcd_cout (col);
           }          

void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char  *str) {                                                
             if (row)
                    {
                    col |= 0x40;                 
                }
                col |= 0x80;
                lcd_cout (col);            
               while (*str)
               lcd_dout(*str++);                
           }

Code for the button:

void op_switich_in(void)                                           //reading the button
{
    unsigned char    opsw_id; 
    opsw_id = op_swin_1d();   
    if (opsw_id == before_opsw_id){
        if ((opsw_id & 0x01) == 0) button = 1;                //increment button
         if ((opsw_id & 0x01) == 0) button1 = 1;             //cursor moving button
    } 
    before_opsw_id = opsw_id;     
}

unsigned char op_swin_1d(void)
{
    unsigned char swid; 
    P0 = P0 | 0x20;                                                         
    delay_micro(100);     
    swid = P2 & 0x3F;
    P0 = P0 & 0xDF;         
    return swid;
}

void incrementbutton(void) {
    if (button == 1) {                                                 //button pressed
      if (temp >= 48 && temp < 57) {                         //to check if the counter value between 0-9
          temp = temp+1;
          if (temp >57) {                                                   //to roll count value to 0 when count is more than 9
              temp = 48;                                                      //But not rolling
          }
        set_cursor_position (1, 0);                                    //sets the cursor to this position when incrementing count
        button = 0;
    } 
}

void move_cursor(void)                                            
{ 
    if (button1 == 1) {
        set_cursor_position (place1, place2); 
        place1 = 1;
        place2 = place1+1;
        button1 = 0;     
          } 
      }
}

void CountdisplayToLCD(void) {
lcd_dout(temp);
}
 
Last edited:

I do not see a variable assigned to each button, for incrementing, just one common
variable.....?


Regards, Dana.
 

@danadakk I tried using two variable for each digits this way:
Code:
void op_switich_upf(void){                  
        if (upf == 1) {               
                  set_cursor_position (1, 2);
             if (temp >= 47 && temp < 57) {                
                     temp = temp+1;         
                     lcd_dout(temp);
                    set_cursor_position (1, 2);
                    upf = 0;     
          }  
          else {
               set_cursor_position(1,3);
          if (upf ==1) {
          if (temp1 >= 47 && temp1 < 57) {
                  temp1 = temp1 +1;
                  lcd_dout(temp1);
                  set_cursor_position(1,3);
                  upf = 0;
            }
          }
        }     
    }               
}
 

I do not see where you make a call to op_switich_upf, post all the code.

What happens now with the code ?

As an aside you virtually have no commented code. Thats very poor practice
for all concerned, either in a product thats has to be revised or you the coder
who has to revisit code base at a later date. I would add that brace alignment
visually also helps see flow of code correctly. Like this -

void op_switich_upf(void) {

if (upf == 1) {

set_cursor_position (1, 2);

if (temp >= 47 && temp < 57) {

temp = temp+1;
lcd_dout(temp);
set_cursor_position (1, 2);
upf = 0;

} else {

set_cursor_position(1,3);

if (upf == 1) {

if (temp1 >= 47 && temp1 < 57) {

temp1 = temp1 +1;
lcd_dout(temp1);
set_cursor_position(1,3);
upf = 0;
}
}
}
}
}
Notice in code above you never process temp1.....section because you
test for if (upf == 1) and if its 0, because you test for it = 1 again in
second section of code you never execute second section of code. You
would be better code as below or elim the second if (upf == 1) test.
void op_switich_upf(void) {

if (upf == 1) {

set_cursor_position (1, 2);

if (temp >= 47 && temp < 57) {

temp = temp+1;
lcd_dout(temp);
set_cursor_position (1, 2);
upf = 0;

}

if (upf == 0 ) {

set_cursor_position(1,3);

if (temp1 >= 47 && temp1 < 57) {

temp1 = temp1 +1;
lcd_dout(temp1);
set_cursor_position(1,3);
upf = 0;
}
}
}


Regards, Dana.
--- Updated ---

Looks like my alkignment, becuse I could not finde code tags, sucks.

some images -

1634901922018.png


Corrected code -

1634901967657.png



Regards, Dana.
--- Updated ---

Note, in second block of corrected code should upf be set to 1 ?
To work on other digit next pass thru code ?

Regards, Dana.
 
Last edited:
Can you post total code again, I do not see where upf variable declared or
controlled when calling

void op_switich_upf(void) {


Regards, Dana.
 

@danadakk
void op_switich_upf(void) is replaced by void incrementbutton(void) so that it will be easy to understand for all.
and ups is replaced by button.
Now, the code looks like this, now both the digits are incrementing but when I move the cursor and press the button to the first digits it increments up to 9 and when I move the cursor to the second digits, the second digits get increments only after incrementing the first digits to 9 and then second digits get incremented.
The digits should be incremented wherever I put the cursor as per their original place value. But now it is incremented second digits only after incrementing first digits to 9.
Thank you!
Code:
#include <stdio.h>
#include <string.h>
#include <stdint.h>

extern unsigned char number[4] = "3 ";
extern unsigned char number1[4] = "2 ";
int temp = 0;
int temp1 = 0;
int place1 = 1;
int place2 = 0;
unsigned char  button    = 0;     
unsigned char  button1  = 0;

void Countdisplay(void);
void lcd_init(void);
void lcd_cout(unsigned char ccod);
void lcd_dout(unsigned char dcod);
void display1(void);
void set_cursor_position (uint_fast8_t row, uint_fast8_t col);
void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char  *str);
void incrementbutton(void);
void op_switich_in(void)  ;
unsigned char op_swin_1d(void);
void move_cursor(void);
void CountdisplayToLCD(void);

void main()
              {
                          lcd_init();
                          display1();
             while (1)
                {
                  incrementbutton();
                  move_cursor();
                }
            }

void lcd_init(void)
            {
              P7 = 0x00;                         //E=0,RS=0

              P7 = 0x03;
              P7 = 0x23;                           //E=1,RS=0
              delay_micro(100);
              P7 = 0x03;                           //E=0,RS=0
              delay_msec(5);
              P7 = 0x23;                            //E=1,RS=0
              delay_micro(100);
              P7 = 0x03;                               //E=0,RS=0
              delay_msec(5);
              P7 = 0x23;                                 //E=1,RS=0
              delay_micro(100);
              P7 = 0x03;                                //E=0,RS=0
              lcd_cout(0x02);                
              delay_msec(5);
              lcd_cout(0x28); 
              delay_msec(5);
              lcd_cout(0x0C);
              delay_msec(5);              
          }

        void lcd_cout(unsigned char ccod)           //for Lcd command
          {
              unsigned char ccod_msb;
              unsigned char ccod_lsb;

              ccod_msb = ccod / 0x10;
              ccod_lsb = ccod & 0x0F;
              P7 = ccod_msb;          

              P7 = ccod_msb | 0x20;    
              delay_micro(2);
              P7 = ccod_msb;  
              delay_micro(2);
              P7 = ccod_lsb;
              P7 = ccod_lsb | 0x20;
              delay_micro(2);
              P7 = ccod_lsb;  
              delay_micro(50);        
               }

              void lcd_dout(unsigned char dcod)                    //for lcd_Data
                     {
               unsigned char dcod_msb;
               unsigned char dcod_lsb;

               dcod_msb = dcod / 0x10;
               dcod_lsb = dcod & 0x0F;

               P7 = dcod_msb | 0x10;

               P7 = dcod_msb | 0x30;
               delay_micro(2);
               P7 = dcod_msb | 0x10;
               delay_micro(2);
               P7 = dcod_lsb | 0x10;  
               P7 = dcod_lsb | 0x30;
               delay_micro(2);
               P7 = dcod_lsb | 0x10;
               delay_micro(50);
           }

       void display1(void)
          {                   
          temp =  write_string(1, 0, number);
            delay_msec(100);
          temp1 =    write_string(1, 1, number1);
            delay_msec(100);      
         }

void set_cursor_position (uint_fast8_t row, uint_fast8_t col)                //for moving cursor                           
           {        
          if (row)
                        {
                        col |= 0x40;              
                    }
                    col |= 0x80;
                    lcd_cout (col);
           }       

void write_string(uint_fast8_t row, uint_fast8_t col, unsigned const char  *str) {                                             
             if (row)
                    {
                    col |= 0x40;              
                }
                col |= 0x80;
                lcd_cout (col);         
               while (*str)
               lcd_dout(*str++);             
           }

Code for the button:

void op_switich_in(void)                                           //reading the button
{
    unsigned char    opsw_id;
    opsw_id = op_swin_1d();
    if (opsw_id == before_opsw_id){
        if ((opsw_id & 0x01) == 0) button = 1;                //increment button
         if ((opsw_id & 0x01) == 0) button1 = 1;             //cursor moving button
    }
    before_opsw_id = opsw_id;  
}

unsigned char op_swin_1d(void)
{
    unsigned char swid;
    P0 = P0 | 0x20;                                                      
    delay_micro(100);  
    swid = P2 & 0x3F;
    P0 = P0 & 0xDF;      
    return swid;
}

void incrementbutton(void) {
 
  if (button == 1) {            
                    set_cursor_position (1, 2);
                     if (temp >= 48 && temp < 57) {             
                     temp = temp+1;
                     if(temp > 57) temp = 48;                 
                     set_cursor_position (1, 2);
                     upf = 0;  
          }
     if(button == 1) {
                  set_cursor_position(1,3); 
                  if (temp1 >= 48 && temp1 < 57) {
                  temp1 = temp1 +1;             
                  set_cursor_position(1,3);
                  upf = 0;           
            }
         }  
    }           
}

void move_cursor(void)                                         
{
    if (button1 == 1) {
        set_cursor_position (place1, place2);
        place1 = 1;
        place2 = place1+1;
        button1 = 0;  
          }
      }
}

void CountdisplayToLCD(void) {
lcd_dout(temp);
delay_msec(5);
lcd_dout(temp1);
}
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top