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

If you look at button increment routine you have two tests for button == 1,
so both incrementing sections increment when a button is pressed, and
both times the cursor position is changed, independent of the "value" of
what is going on in the digit cursor is pointing to.

This is a classic case where you should do a decision flow chart so you can
visualize the code and its decision paths.

Also your braces in the code are not right. Your second section of code under
and "if(temp > 57) temp = 48; " missing in second section ?

So should look like this -

Code:
void incrementbutton(void) {
    
  if (button == 1) { 
              
      set_cursor_position (1, 2);

       if (temp >= 47 && 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 >= 47 && temp1 < 57) {
             temp1 = temp1 +1;
       }

       if(temp > 57) temp = 48;                   
            set_cursor_position(1,3);   << What should you do here ?
            upf = 0;             
        }
    }     
}

Aboive still not right because when button is pressed both sections of code
always execute, which I do not think you want. In other words there is no variable
telling you what cursor digit you are working on so you only apply the button
press to that digit, and when it hits 9 and you want to inc then you test for digit overflow,
reset digit, AND then move to next cursor position and inc that digit.....

So I think you have two decision tree, inc current digit at cursor location, then test if
it was 9 and now needs to be 0 and move to next cursor position and force an inc to
that position. If initial digit was < 9 before inc, then inc that digit and leave cursor position
unchanged ? Right.....?

Flow charts are great for revealing these problems.


Regards, Dana.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top