View previous topic :: View next topic |
Author |
Message |
capella
Joined: 07 Feb 2009 Posts: 38
|
math problem? |
Posted: Mon Jun 21, 2010 8:40 am |
|
|
my problem is when I send data from pc to pic as like code
Code: |
if (kbhit())
a=getch();
////////////////////////////////////////////////
if (a=='9')
{
delay_ms(200);
b = a;
c=(b*4);
printf(" %d ",c);
} |
printf result is - 28 - 28 -28 -28 (why not result is 36)
second code as
Code: |
if (kbhit())
a=getch();
////////////////////////////////////////////////
if (a=='9')
{
delay_ms(200);
b = a;
c=(b*4);
printf(" %d ",c);
} |
printf result is
ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä ä (why not result is 36)
what is problem?????
best regards |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Jun 21, 2010 8:45 am |
|
|
because a = 57
'9' 9 surrounded by single quotes is the character 9 not the decimal value 9. to convert this to the value 9 you need to
a = getch();
b = a - '0';
This will only work for single values though. For larger numbers you will need to do it differently.
I can't actually see any difference between your first and second code.
Must be something else you have done to give a different output, I think I can guess
The first code you were using signed int for c, the second you weren't. |
|
|
capella
Joined: 07 Feb 2009 Posts: 38
|
|
Posted: Mon Jun 21, 2010 9:50 am |
|
|
Thanks for your reply. Problem solved. How can I send three-digit numbers for example 120,132 number? When I try if (a=='100')
compiler give me an error like this....
Character constant constructed incorrectly.
How can I solve this problem?
best regards |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jun 21, 2010 11:18 am |
|
|
If you want to test if a equals one hundred, just get rid of the quotes:
if (a==100)
Is this what you are looking for? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
capella
Joined: 07 Feb 2009 Posts: 38
|
|
Posted: Mon Jun 21, 2010 12:34 pm |
|
|
When I change as your suggestion no result again. |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Mon Jun 21, 2010 4:34 pm |
|
|
Our brains are not wired to handle numbers well. Sure we can at a glance and tell if there are one two or three items but any number above ten and we are often unable to see the number directly. That's why we invented notation.
Now 10 is notation but it could be binary or decimal or another base like hex or maybe the two ascii chars representing '1' and '0'
With acii we have numbers that represent characters. 'A' in ascii is 65 decimal.
Even numbers have ascii representation '1' is decimal 49. The CCS compiler as to make sense of all of this since it's number notation is going to be based on registers and RAM ROM storage that are electrical signals representing 1 an 0 in binary notation. The compiler needs to know what the external notation will be so it can convert form this external notation and display the results in this external notation. The syntax for ascii ' ' is used and " " for decimal n..n is used for hex 0xhhhhh
for binary 0b00000000.
Knowing this certain techniques are possible. The ascii '9' is 57 internally to a pic so if we subtract 48 we get the correct internal notation for the integer 9. Further we can notate numbers as float and perform mathematics on them using registers and renotate them as ascii values when we use a printf statement. |
|
|
capella
Joined: 07 Feb 2009 Posts: 38
|
|
Posted: Tue Jun 22, 2010 1:18 am |
|
|
Yes you are right, but pc program just send string as 100, 120 , 130 bla bla ...so pic convert string as integer and It has to use as a integer value.
Is it possible to create any code...? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Jun 22, 2010 1:51 am |
|
|
Of course it is 'possible'. Look at any number of the millions of programs around....
There are functions to help _you_ do this. Look at 'atol' in the manual. - the 'a' stands for 'ASCII' - the code that text is being sent using, and the 'l' for 'long integer'.
Also get_string, in 'input.c' (safer than 'gets').
Some searches on the forum, will find dozens of past examples, and give some of the problems, and how to deal with them (hint, remember that people may not type what you expect....).
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 22, 2010 2:07 am |
|
|
Ttelmah is correct, off cause
What you need to do to read in a value greater than 9 is to read in a string. There are many ways to do this BUT remember a string is null terminated!
As Ttelmah mentioned atoi and atol will convert a numerical ascii STRING in to the value it represents:-
char str[] = "123";
int a = atoi(str);
So you just need to work out how to get the 123 in to the array and null terminate it. When you have written the code to do this, post it here and we will help you fix it.
If you want values above 255 then you need atol. But this also has a restriction so if you need to go above that then you will have to write your own routine, and it is very easy to do so. |
|
|
capella
Joined: 07 Feb 2009 Posts: 38
|
|
Posted: Tue Jun 22, 2010 2:54 am |
|
|
thanks your help, I try what you say and I searched,
but
Code: | if (kbhit())
a=getch();
b=atoi(a);
////////////////////////////////////////////////
if (a > '4' )
{
delay_ms(200);
c =(b*4);
printf("%d",c);
}
//ilk_pot_deger = read_adc();
ikinci_deger = ilk_pot_deger+100;
ucuncu_deger = ilk_pot_deger+200;
dorduncu_deger = ilk_pot_deger+300;
if (a=='1')
{
// set_pwm2_duty(0);
delay_ms(100);
kendi_degeri = read_adc(); |
and I tried
Code: |
if (kbhit())
a=getch();
b=atoi(a);
////////////////////////////////////////////////
if (b > 4 )
{
delay_ms(200);
c =(b*4);
printf("%d",c);
}
if (a=='1')
{
// set_pwm2_duty(0);
delay_ms(100);
kendi_degeri = read_adc();
|
but first code printf is 00000
second code printf is nothing
cause of my code create is that : pc program send this value not my hands and I have to take one character like this '1' an I also have to take like this 120,130. I think my codes tell everything what I want.
what is wrong?
Last edited by capella on Tue Jun 22, 2010 2:57 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Jun 22, 2010 4:04 am |
|
|
You are 'missing the point'.
kbhit, tells you there is one character waiting. You are reading a single character. Is the number '100', one character?. Type it. How many times do you have to press a key?.
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 22, 2010 4:42 am |
|
|
Hi,
You have not shown it but a is proberbly defined as int :-
int a;
a = getch();
a is NOT a string so atoi will not work properly on it.
here is an example of what you need to do:-
For 1 digit
Code: |
int buf[80];
int b;
if (kbhit())
{
buf[0] = getch();
buf[1] = '\0'; // Make it a null terminated string
b = atoi(buf);
}
|
For numbers larger than 9 e.g. 10, 123 you need to keep reading the values in to a buffer until <CR> is pressed, you then either put a '\0' at the end or replace the <CR> with a '\0' depending on how you read the chars in.
You can use gets or if you don't want your code to stop whilst waiting for the user to finish inputting the value you can use an interrupt driven routine for getting the data. |
|
|
capella
Joined: 07 Feb 2009 Posts: 38
|
|
Posted: Tue Jun 22, 2010 6:39 am |
|
|
Wayne_ wrote: | Hi,
You have not shown it but a is proberbly defined as int :-
int a;
a = getch();
a is NOT a string so atoi will not work properly on it.
here is an example of what you need to do:-
For 1 digit
Code: |
int buf[80];
int b;
if (kbhit())
{
buf[0] = getch();
buf[1] = '\0'; // Make it a null terminated string
b = atoi(buf);
}
|
For numbers larger than 9 e.g. 10, 123 you need to keep reading the values in to a buffer until <CR> is pressed, you then either put a '\0' at the end or replace the <CR> with a '\0' depending on how you read the chars in.
You can use gets or if you don't want your code to stop whilst waiting for the user to finish inputting the value you can use an interrupt driven routine for getting the data. |
thanks wayne_ but your program is working that:
when I send 180 value;
pic is taking 8 because pics answer 32
when I send 150 value;
pic is taking 5 because pics answer 20
I think pic just takes middle value what is wrong wayne_? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue Jun 22, 2010 6:58 am |
|
|
It will do.
Read what Wayne is saying.
It is _not_ the program needed.
He is giving you a big hint, and showing how to deal with a _one character_ 'string', as opposed to a single text character. _You_ need to write your program to accept _multi character_ strings.
He has given the 'clue' for what you next need to do, under the program.
We are not going to write the program for you. You need to learn what to do, and how to do it. We will _help_ once you start actually writing something, but _you_ need to think about what you have to do, and start doing it....
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jun 22, 2010 6:59 am |
|
|
Well this will depend on how fast you enter the data.
I was assuming you were entering the data by hand (typing in the value)
What happens if you just press one number ?
As stated in the post, this is for one (1) digit only. For more you need to read in the string of digits and convert it.
Thank you Ttelmah for trying to explain |
|
|
|