Continue to Site

# Pic interfacing with LCD problem?

Status
Not open for further replies.

#### engr.waqas

##### Full Member level 3
pic lcd problem

I have taken data into PIc controller from LM35 temp sensor.Now i want to display the reading of temp sensor to LCD.
Should i convert that value to ASCII before sending to LCD or it is already in ASCII.
If i have to convert to ASCII then how to do that in C in MPLAB.

pic lcd problems

Hi,
Not only do you need to convert your “data” to ASCII, but you also need to convert that “data” to degrees, because the data you talk about is just a numeric representation (provided you did use PIC’s ADC) of the voltage at the output of your temperature sensor.
Since you didn’t say much about what you’re doing, I will assume that you do actually sample this voltage regularly and quantify the samples (express them numerically) using an ADC. This gives you an indication of how big this voltage is as compared with the ADC’s reference voltage (whatever you chose that to be).
Having figured the absolute value of this voltage, you then cross-reference it with the datasheet that will give you a numerical value representing a temperature in degrees Celsius. (I think the datasheet mentions 10mV/°C, so, if your voltage is, say 230mV, the temperature is 230/10 = 23°C.)
This is now just a number, no text or ASCII or whatever.
To convert it to text, you either use
Code:
sprintf(receiving_text_buffer, "%d", numeric_temp)
or you do a custom algorithm that splices your number into its component digits (by using a combination of repetitive division and modulus with/of 10, for instance) and then adding the ASCII value of character ‘0’ to each of them to get their equivalent ASCII values.
I hope this makes sense to you...

Arthur

1 to 5 digit numbers ascii

Thanks a lot dear
Actually i have done that "degree conversion option" u said but now the prob is that i am getting right value on LCD for temp 0 to9 but as i set temp in sensor to 10 or other 2 digit i get
: for 10
; for 11,
< for 12
= for 13
> for 14
? for 15
and again
0 for 16
1 for 17 and so on
i.e not getting corrrect indication on LCD for 2 digit values.
MY C coding in MPLAB is
#include <P18f452.h>
#define idata PORTD
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
void lcdcmd(unsigned char);
void MSDelay(unsigned int);
void main (void)
{
int lbyte,hbyte,bin;
unsigned char x,y,z;

//A to D

TRISD=0;
TRISAbits.TRISA0=1;
TRISAbits.TRISA2=1;
while(1)
{
MSDelay(1); // give A to D time to sample
lbyte>>=2;
lbyte&=0x3F;
hbyte<<=6;
hbyte&=0xC0;
bin=lbyte|hbyte;

//display on LCD
TRISD=0;
TRISB=0;
en=0;
MSDelay(1);
lcdcmd(0x38);
MSDelay(1);
lcdcmd(0x0E);
MSDelay(1);
lcdcmd(0x01);
MSDelay(1);
lcdcmd(0x06);
MSDelay(1);
lcdcmd(0x86);
MSDelay(1);
//now display
x=bin & 0x0F;// mask upper 4 bits
y=x|0x30; //make it ASCII
PORTD=y;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
y=bin & 0xF0; // mask lower 4 bits
y=y>>4; //shift it to lower 4 bits
MSDelay(50);
PORTD=y|0x30;//make it ascii

}
}
void lcdcmd(unsigned char value)
{
idata=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
}

void MSDelay(unsigned int itime)
{
unsigned int i;unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}

pic lcd display int to ascii c

You didn't pay attention to what I wrote above: you need to split your numbers into their component digits!
For instance, the number 15 you'll want as 1, 5.
For 2 digit numbers, you can do that like this:
Code:
digit0 = number % 10;   /* step 1: digit0 = 5 */
digit1 = number / 10;   /* step 2: digit1 = 1 */

If you have more digits, you replace *step 2* with
number = number / 10
and repeat *step 1* of the code above as neccessary.
(Note that you get the digits in reverse order!)
Hope it's clearer now.

Arthur

converting to ascii value in lcd

I think the spliting part and converting part is going in this part of coding
The bin contains data from temp sensor.
x=bin & 0x0F;// mask upper 4 bits
y=x|0x30; //make it ASCII and display
PORTD=y;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
y=bin & 0xF0; // mask lower 4 bits
y=y>>4; //shift it to lower 4 bits
MSDelay(50);
PORTD=y|0x30;//make it ascii and display

convertir entero a caracter lcd pic

As Arthur wrote previously you have to convert result of ADC in "Degree" form then print to the LCD...

In above program u are directly printing 'bin' variable which is merge result of lower and higher bits of ADC registers...

-Vipul

I think the spliting part and converting part is going in this part of coding
No! What you do in that part of the code is just a bit masking in which you isolate a symbol in the hexadecimal numeration system (0h - Fh, equivalent to 0 - 15 in the decimal system). A digit is, per definition, a number from 0 to 9 in the decimal numeration system.

I was afraid this would boil down to a discussion involving the words “binary”, “hex”, “decimal”, “number”, “ASCII” etc., because their meanings are best explained in school and understood via [hard] work. There’s no good substitution for that.
I can easily write your code for you, but we’ll both lose: me valuable time, and you the point with all of it!
So let me show it to you one more time instead (for a maximum of 3-digit number, say 137):
Code:
char d0, d1, d2;
d2 = bin % 10;                  /* d2 <-- 7 */
d1 = (bin / 10) % 10;           /* d1 <-- 3 */
d0 = ((bin / 10) / 10) % 10;    /* d0 <-- 1 */
/* continue like this for even more digits */

/* convert digit (numbers) to ASCII (text characters) */
d0 += ‘0’;   /* same as d0 = d0 + ‘0’ same as d0 = d0 | 0x30 */
d1 += ‘0’;
d2 += ‘0’;

/* send ASCII chars to port */
/* ... */
You could do this nicer, you could do it a little more efficiently, but for you to understand I wanted to show all the little steps that you’ve missed and also to suggest how the digits can be extracted recursively, but that’s your homework.

So this will do the conversion for you, but I’m sill not convinced that what you’ll see is temperature since there’s nothing in your program that resemble a “volt to degree” conversion, unless you’ve chosen such a voltage reference that your ADC will actually do it for you.

Arthur

Status
Not open for further replies.