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.

[SOLVED] how to convert a number to display in lcd?

Status
Not open for further replies.

ps_arunkumar

Member level 1
Joined
Nov 24, 2011
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,545
Hi all,
I am trying to write a simple C program to display the no. of times the switch is pressed in the lcd i.e.,when a switch is pressed, the variable gets update and i am displaying it on the lcd. When i do that, it counts upto 9 and from 10 to 15, it displays as symbols and then displays from 20 to 29 and 30 to 35 as symbols .....I dont how to convert the variable so that i properly display from 0 to ....

kindly help me.

thanks,
Arun
//my program

#include<at89x52.h>
#include<lcd.h>
#include<serial.h>
#include<stdio.h>
sbit switch_pin=P1^0;
#define switch_not_pressed(bit) 0
#define switch_pressed (bit) 1

void display_countupdate(unsigned char count)
{
unsigned char temp;
temp=(count);
printf("%d\n", temp);
LCD_clear();
LCD_row1();
send2lcd(temp);
}

bit switch_getinput()
{
unsigned int i;
bit return_value = 0;
if(switch_pin==0)
{
for(i=0;i<=20;i++);
if(switch_pin==0)
{
while(switch_pin==0);
return_value=switch_pressed;
}
}
return return_value;
}

void main(void)
{
unsigned char switch_presses = 0;
switch_pin=0;
PowerOn();
InitSerial();
while(1)
{
if(switch_getinput()==switch_pressed)
{
switch_presses++;
}
display_countupdate(switch_presses);
}
}
 

Code:
void lcd_print_value(u32 val)
{
	u8 buffer[10] = {0};
	u8* head = buffer;
	u8 cnt=0;
	if (val!=0)
	{
		while( val )
		{
    		*head++ = (val % 10)["0123456789"];
    		val /= 10;
			cnt++;
		}
		while (cnt!=0){ cnt--; lcd_print_char(buffer[cnt]); }
	}
	else
	{
		lcd_print_char('0');
	}
}
 

another integer to string routine


string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}
 

I can't imagine how it works even from 0 to 9. I suppose that send2lcd() function ORs 0x30 ('0') to its argument. If this is the case then you should see after 9 the symbols : ; < = > ?. In a case like that try this one:

Code:
void display_countupdate(unsigned char count)
{
unsigned char temp;
temp=(count);
printf("%d\n", temp);  //What this line is supposed to do anyway???
LCD_clear();
LCD_row1();
send2lcd(temp/10);
send2lcd(temp%10);
}

If 0x30 is not ORed inside send2lcd(), then you should write:

Code:
send2lcd((temp/10)|'0');
send2lcd((temp%10)|'0');

Please note that this code works for values from 0 to 99.

Hope this solves the problem.

---------- Post added at 19:11 ---------- Previous post was at 19:03 ----------

another integer to string routine


string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}

I am not so sure that ps_arunkumar has written his program in c++.

PS: I am not a c++ expert, but doesn't c++ has built in functions for int to string convertions?
 

I am getting wrong output when i try
send2lcd(temp/10);
send2lcd(temp%10);
as send2lcd() function ORs 0x30 ('0') to its argument.

I have attached the output which i got.

please look at the output and guide me.
 

Attachments

  • files.rar
    146.1 KB · Views: 82

ps_arunkumar said:
I am getting wrong output when i try
send2lcd(temp/10);
send2lcd(temp%10);
as send2lcd() function ORs 0x30 ('0') to its argument.

Function send2lcd() seems to work for bcd numbers. Keep your display_countupdate() routine as it is in post #1 and change only the send2lcd() function:

Code:
void send2lcd(unsigned char value)  //value range 0-99
{
  unsigned char decs = value/10;
  unsigned char units = value%10;
  decs |= '0';
  units |= '0';
  LCD_putc(decs);
  LCD_putc(units);
}

Try this and post back for the simulation results.

PS: Don't put executable code inside header files, unless you are using function inlining. I suggest you move the code from lcd.h to a .c file.
 
I changed the lcd.h to lcd.c and updates the send2lcd function. It worked and output displays as expected from 0 to 99.

Thanks a lot Alexxx and all team members who helped me.

Kindly say me how should i change to display upto 999, so that I can learn.
 

Attachments

  • output.jpg
    output.jpg
    184 KB · Views: 110

ps_arunkumar said:
Kindly say me how should i change to display upto 999, so that I can learn.

Code:
void send2lcd(unsigned int value)  //value range 0-999
{
  unsigned char hunds = value/100;
  unsigned char decs = (value/10)%10;
  unsigned char units = value%10;
  hunds |= '0';
  decs |= '0';
  units |= '0';
  LCD_putc(hunds);
  LCD_putc(decs);
  LCD_putc(units);
}

If you need from 0 to 9999
Code:
void send2lcd(unsigned int value)  //value range 0-9999
{
  unsigned char thous = value/1000;
  unsigned char hunds = (value/100)%10;
  unsigned char decs = (value/10)%10;
  unsigned char units = value%10;
  thous |= '0';
  hunds |= '0';
  decs |= '0';
  units |= '0';
  LCD_putc(thous);
  LCD_putc(hunds);
  LCD_putc(decs);
  LCD_putc(units);
}


And so on...:smile:

Just to get an idea of the algorithm, imagine we have a bigger number like 98765 (you will need a 32-bit unsigned for this one).

(98765/10000)%10 = 9
(98765/1000)%10 = 8
(98765/100)%10 = 7
(98765/10)%10 = 6
(98765/1)%10 = 5

So I am happy a solution is found. If there are no further questions on this subject, please mark this thread as "solved".

Cheers!
 

I am so happy..
Thanks a lot Alexxx and all team members.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top