View previous topic :: View next topic |
Author |
Message |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
help:convert char into int |
Posted: Thu Aug 16, 2007 12:55 pm |
|
|
Dear all, I communicate my LCD module with my PC in RS232. When I type two number such as to run the rutine of Code: | lcd_gotoxy(Byte x, Byte y) | the cursor would not point to the exact position where I wanna it go.
I thought because I used Code: | x=getch() and y=getch(), | maybe the signal type was character so,can not understand those message.
But i still can not fix this problem, please help me if anyone of you know the answer.
I do thank you very much. _________________ Enjoy our EEE |
|
|
Miniman
Joined: 30 Apr 2007 Posts: 44
|
|
Posted: Thu Aug 16, 2007 1:11 pm |
|
|
if you use getch() you get the character from the pc. that means that a '1' has a number of 0x31, that is 49 decimal. if you want to move the cursor to the first line that would be a number of 1 (or 0 if first line is 0..) since you then pas a character '1' which is a number 49 you are trying to move the cursor to line 49... see what I'm trying to say? I'm not that good in explaining..
you can use the function atoi() in stdlib.h, like this:
Code: |
#include <stdlib.h>
...
c=getch();
x=atoi(c);
lcd_gotoxy(x,y);
...
|
atoi() "converts" a string to its number, like this:
Code: |
strcpy(string,"123");
x = atoi(string);
// x is now 123
|
hope it helps.
Best regards
miniman |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
help again |
Posted: Thu Aug 16, 2007 1:56 pm |
|
|
My codes were rectified as follows:
Code: | lcd_putc("\f Input x: \n");
x=atoi(getch());
lcd_putc(x);
delay_ms(500);
lcd_putc("\f Input y: \n");
y=atoi(getch());
lcd_putc(y);
delay_ms(500);
lcd_putc("\f");
lcd_gotoxy(x,y); |
The compiling was ok, but it still not work.
The result was that when I type 2 number , the cursor just go to second line first position.
please give me more suggestion if you can.
thanks _________________ Enjoy our EEE |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Aug 16, 2007 4:07 pm |
|
|
You need to understand the difference between the value of a number and the notation of a number. Take for example the decimal (base ten) number that has the value one (1). The notation we use is "1" but this is coded in ascii so what you see is an encoded form of the value. Another encoded notational form for the value one is "0x01" this is hexadecimal notation that is encoded as 4 ascii characters. So say your PC sends the character "1" ascii over the rs232 interface then your getc also receives the ascii notation "1" which is encoded as 8 bits binary representing a value of decimal 49 to the PIC. To have the char "1" represent the value one inside the PIC you would subtract decimal 48 from it. The function atoi is doing this for single char strings but its real power is that it is doing it for multiple char strings so "12" ascii notation for twelve will be converted to the value 12 (first char "1" =49 -48)*10+(second char"2"=50-48). You need to look at your LCD documentation and see via what notation it expects to receive.ex is it raw binary or ascii encoded or a combination Ex raw for commands and ascii for text display. Remember ascii is not always the encoding used by LCD's but it often is. Research atoi in the compiler manual maybe you might spot something about how you are using it that is giving you grief. Also look at char and string definitions. |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
cool |
Posted: Thu Aug 16, 2007 4:23 pm |
|
|
thanks millian about your help.
it works.
By the way, if I wanna input number of x which was large than 10, how can I do that? _________________ Enjoy our EEE |
|
|
|