View previous topic :: View next topic |
Author |
Message |
Sensor
Joined: 04 Nov 2004 Posts: 33
|
display integer variable on LCD |
Posted: Thu Oct 06, 2005 8:35 am |
|
|
I'm having trouble displaying an integer variable on an LCD. I have my own LCD driver written and it works great when I send it a single char or a string. Now, I want to take an integer variable and display it but can't. It display junk. For example, if my integer variable I want to display is NUM, and NUM = 123, I want my LCD to display 123, but intead it displays junk.
I am using PCW compiler, IDE version 3.30, PCB version 3.155, PCM version 3.155, PCH version 3.155.
Here is a simple program:
Code: |
void main() {
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
port_b_pullups(TRUE);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_timer1(0);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8);
lcd_init();
lcd_putch("El Angle: 00.0"); //works fine
el_angle = 2000; //normally I will have this would be a counter
//but for now I'm just using a constant
while(1)
{
temp = el_angle * 750; //these convert my counter to the
converted_el = temp / 8000; //corresponding elevation angle
LCD_XY(1,12); //places the cursor on line 1, position 12 of the LCD
lcd_putch(converted_el);
delay_ms(4000);
el_angle=el_angle+10;
}
}
|
The line "lcd_putch(converted_el);" does not work correctly. |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Thu Oct 06, 2005 8:39 am |
|
|
Need to see: lcd_putch()
Without that, I don't think anyone will be able to help. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Oct 06, 2005 8:43 am |
|
|
Try puting your variable into a Char variable first.
char text[17];
sprintf(text, "%d", converted_el);
lcd_putch(text);
Give that a shot.
Ronald |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Oct 06, 2005 8:46 am |
|
|
NUM is a number! Not a string. You must first convert 123 to '1' '2' '3' and print each of these characters.
printf(lch_putch,"%lu",converted_el); |
|
|
Sensor
Joined: 04 Nov 2004 Posts: 33
|
|
Posted: Thu Oct 06, 2005 9:24 am |
|
|
Thanks,
The printf worked. I had tried the sprintf yesterday but had no luck.
Ok so now I can output my number but... what I am trying to display is an elevation angle between 0 and 90 degrees. So in my code I showed el_angle as a constant equal to 2000. So the converted_el is equal to 187.5, but since I'm usint integers it equals 187.
My LCD is actually displaying 1870, why not 187?
What I want to display is actually 18.7. Since a value of 187 is actually 18.7. I'm doing it this way so I don't have to use floating points.
How can I display 18.7 instead of 187? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Oct 06, 2005 9:27 am |
|
|
Sensor wrote: | Thanks,
The printf worked. I had tried the sprintf yesterday but had no luck.
Ok so now I can output my number but... what I am trying to display is an elevation angle between 0 and 90 degrees. So in my code I showed el_angle as a constant equal to 2000. So the converted_el is equal to 187.5, but since I'm usint integers it equals 187.
My LCD is actually displaying 1870, why not 187?
What I want to display is actually 18.7. Since a value of 187 is actually 18.7. I'm doing it this way so I don't have to use floating points.
How can I display 18.7 instead of 187? |
Hint... What does 187/10 equal?
Hint... What does 187%10 equal?
Hint... "%u.%u" |
|
|
Sensor
Joined: 04 Nov 2004 Posts: 33
|
|
Posted: Thu Oct 06, 2005 9:37 am |
|
|
Thanks Mark!!!
sorry for all the "beginner" questions, just learning.
Thanks again |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Oct 06, 2005 9:38 am |
|
|
Thanks why I didn't give you the answer. Just pushed you in the right direction. |
|
|
|