Char to function transfer

Status
Not open for further replies.

Tiwana6330

Member level 1
Joined
Feb 3, 2011
Messages
36
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,536
Hi Everyone

I finally got my LCD working and now I can write anything to the LCD.

For next step, I am trying to put the value of ADC0 to LCD

I an able to get the ADC running and have verified it by putting the output to LED's on Port B. The LCD is connected on PORT D (data) and Port A (Control 1-3)

Now problem is that I am not able to tranfer ADC value to LCD. I think I am having some problem in Char to function tranfer. The code compiles fine, so I cannot debug it.


My MAIN code is

unsigned char val0;

while(1)
{
val0 = ReadAdcChannel(0);
lcd_goto(0); // select first line
lcd_puts(val0);
PORTB=val0;
}

LCD Puts function is below

void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}


What am I doing wrong. Pl help. Thanks
 

hi

convert the char to ascii because lcd displays only ascii.in a char you will get 3 ascii numbers.
then display the same on lcd

ml
 

If you need to format an integer or float value to send to a display, you can use the sprintf function that is defined in stdio.h.
Example:

Code:
#include <stdio.h>

void display_value(void)
  {
  unsigned int value = 22;
  char buffer[16];  /* size of line on display */
  
  sprintf(buffer, "Value is %d", value);  /* Same formatting options as printf */
  display_line(buffer);                   /* Your call to display function */
  }
 
hi

convert the char to ascii because lcd displays only ascii.in a char you will get 3 ascii numbers.
then display the same on lcd

ml

Thanks for your reply

if I do

lcd_puts("THIS IS TEXT");


I get
THIS IS TEXT
on the lcd. Hence with this code, I do not need to do ASCII conversion. Its a matter of conversion & transferring value to lcd_puts function
 

If you look at the character map of your display, you will find that 'T' is represented as the 8 bit number 0x54. This is the ascii code for 'T'.
The ascii code for the number 0 is 0x30 and for 1 it is 0x31. So if you take a digit and add 0x30 to it you get the ascii code for the digit.

a = 0x30 + 1;
lcd_puts(a); will display 1.

You need to convert your ADC reading value to ascii codes to display it on the lcd.
sprintf does this for you with formatting options.
 
Last edited:

Thanks. I found your reply very useful

So let me rewrite the code

unsigned char val0;
char buffer[16];

while(1)
{
val0 = ReadAdcChannel(0);
lcd_goto(0); // select first line
sprintf(buffer, "Value is %c", val0);
lcd_puts(buffer);
PORTB=val0;
}

is this code ok? Basically I am unsure if sprintf will also work on unsigned char.

Once again thanks for your help
 
Last edited:

Don't forget to #include <stdio.h>

Look at printf for the formatting options, sprintf has the same.
 
Don't forget to #include <stdio.h>

Look at printf for the formatting options, sprintf has the same.

Yes I compiled my code in MPLAB and it compiled fine. I need to test the code on micro-controller and then will know. Thanks
 

It depends on what compiler you are using.
But they should all support the basic options.

d, i Print an int as a signed decimal number.
u Print decimal unsigned int.
f, F Print a double in normal (fixed-point) notation.
x, X Print an unsigned int as a hexadecimal number. 'x' uses lower-case letters and 'X' uses upper-case.
o Print an unsigned int in octal.
s Print a character string.
c Print a char (character).

You are asking it to print a char? %c
Ask it to print a unsigned int. %u
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…