How to Increment count on 16x2LCD display?

Status
Not open for further replies.

buzzed

Newbie level 6
Joined
Oct 8, 2021
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
50
I want to increment a counter for Line2 and display it on LCD when the pushbutton is pressed. It displays correctly, but when I press the button it shows different characters like this(||). When I place a cursor to the 9 of Line2 and pressed the button it displays symbol(||).

Code to display the LCD as:

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

extern unsigned char LIne1[32] = "Hello, World";
extern unsigned char Line2[32] = "9567   ";

void dispset_titl(void)
{
    unsigned char n;
    unsigned char dsp_buf;
   
            lcd_l1(0x00);                                                //for  postion of line1
            for (n=0 ;n<16 ;n++) {
                 dsp_buf = Line1[n];        
                 lcd_dout(dsp_buf);        
    }   
            lcd_l2(0x40);                                                //for  postion of line2
            for (n=0 ;n<16 ;n++) {
                 dsp_buf = LIne2[n];         
                  lcd_dout(dsp_buf);        
      }
}

void lcd_cout(unsigned char ccod)
{
    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)
{
    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);    
}

code for Pushbutton is as:
Code:
int counter = 0;
int temp;

void Increment_button(void)
{        
      if (button == 1) {                        //when button is on      
          temp=counter++;         
          lcd_dout(temp);          
         }
  }

main function as:
Code:
#include <stdio.h>
#include <string.h>

unsigned char button= 0;                  
void main(void)
    {       
        while (1)
          {            
            panel();
       
           WDT_Reset();
           
          Increment_button();
    }
 
Last edited:

Hi,

In the shown code "button" is set to 0, it never becomes 1.

"Temp" is defined as integer. A binary values. It starts with 0 and gets incremented.
But "lcdout" expects a char, usually treated as ASCII character. (As it's name says) I expect a compiler warning on this.

If you want to display a "0" you need to send the ASCII value for "0", which is 0x30 = 48.
If you want to display "123" you need to send three characters "1", "2", "3", which are {0x31, 0x32, 0x33}.
If you want to display " 3" you need to send three characters " ", " ", "3", which are {0x20, 0x20, 0x33}.

Klaus
 

@KlausST in the code pushbutton, the button is set to 1.
and I have already displayed the character by converting each to ASCII and now, I want to increase those numbers.
 

Hi,

No.
There's just a "compare".
"button" never becomes modified.

Klaus
 

Hello!

A few issues.
1. You show code, but you don't display everything.
2. Let's consider your main program. Your program will do panel(), but you don't define
the panel() function is not defined, so we don't know what it does and we can't help you.
3. You define button = 0, you test button in increment_button, but button is never set
anywhere, so it will stay at 0. Therefore count will never be incremented.
And even if you set it somewhere, you have to reset it to 0 somewhere else, otherwise
the loop will keep incrementing the count about one million times a second.
4. dsp_buf = [n]; ->what do you think it will do?
5. dispset_title(void). I'm not sure what it will do, but TITL1[n] is not defined.
Beside this, if you just want to display the title, why not first making a function that writes
a string, what ever it is?
for example:
Code:
void write_string(uint8 line, uint8 col, const char * str) {
     [implementation]
}
and then call your function:
write_string(0, 2, Line1]; // Write "hello, world" centered on first line.
6. What do you want to do with 2 similar functions lcd_cout and lcd_dout?
You never use lcd_cout. Why do you leave it in your code?

Etc, etc...

Dora
 

@doraemon I displayed only the relevant part to the push button. As I already mention the button is set in another function, and sorry for the part of [dsp_buf]=[n], also TITL[], I have edited just.
lcd_dout()function is for converting characters to the ASCII part.
The DIsplay is displaying correctly but how can I increment the counter.
 

Hi,

I have to agree with doraemon: You hide relevant code. It makes it almost impossible to help.

We can see that counter gets incremented correctly.
But only when:
* function increment_button is called
* and when variable button = 1, this we don't see.

--> Provide the whole code and we are able to give better assistance.

Klaus
 

@KlausST when the variable button is called as :
Code:
if (button == 1) {                        //when button is on     
          temp=counter++;                //counter value is incremented
          lcd_dout(temp);                     //data is send to display
          button = 0;                             //button is of
         }
 

Hi,

Why do you make it so hard to help ... and lengthy and demotivating?
You don't do yourself a favour.

--> We still don't see where, why and how button is set to 1.

Klaus
 

@KlausST
button is set to 1 as:

Code:
unsigned char before_opsw_id;
void switich_in(void)
{
    unsigned char    opsw_id;   
    opsw_id = op_swin_1d();     

    if (opsw_id == before_opsw_id){
        if ((opsw_id & 0x01) == 0) button = 1;
                }   
    before_opsw_id = opsw_id;       
}

unsigned char op_swin_1d(void)
{
    unsigned char swid;                  //switch_id
   
    P0 = P0 | 0x20;                    //op_switch common on
    delay_micro(100);   
    swid = P2 & 0x3F;
    P0 = P0 & 0xDF;                   //op_switch common off
   
    return swid;
}
 

Hi,

now you hide when/how/why switich_in() is called.

I´m not motivated to ask again.

Klaus
 

@KlausST you said where the where, why and how button is set to 1 and I showed that portion and now u are saying ....
 

In the code you give us ...

never switich_in() is called. Thus never button is set to 1.
No wonder it does not work.

That´s all I can see.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…