CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

display integer variable on LCD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Sensor



Joined: 04 Nov 2004
Posts: 33

View user's profile Send private message Send e-mail

display integer variable on LCD
PostPosted: Thu Oct 06, 2005 8:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 06, 2005 8:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 06, 2005 8:43 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 06, 2005 8:46 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 06, 2005 9:24 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 06, 2005 9:27 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 06, 2005 9:37 am     Reply with quote

Thanks Mark!!!

sorry for all the "beginner" questions, just learning.

Thanks again
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Oct 06, 2005 9:38 am     Reply with quote

Thanks why I didn't give you the answer. Just pushed you in the right direction.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group