View previous topic :: View next topic |
Author |
Message |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
Ascii to float or int.... |
Posted: Fri Mar 23, 2012 12:47 pm |
|
|
Hello
I bought a distance sensor which gives me distance in ascii via serial port
I need help in converting that ascii to float so that i will do some math for my project
(fyi : my project needs distance measurement for control of pwm of a dc motor)
I searched in the forum before i post using these key words
1. Ascii to float
2. String to integer
3. Char to decimal or integer
Etc but no results forum returned
Any helps welcome
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 23, 2012 12:55 pm |
|
|
See the get_float() function in this CCS driver file:
Quote: |
c:\program files\picc\drivers\input.c
|
If that doesn't help, then post the manufacturer and part number of your
sensor, and post a link to the webpage for it. |
|
![](templates/subSilver/images/spacer.gif) |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
worked |
Posted: Sun Mar 25, 2012 8:09 am |
|
|
hello thanks a lot for your useful info
finally it worked very well...... the function is (ATOF)...... thanks again
------------------------------END----------------------------
new problem....
my PIC 18F4550 gets hang if i use
GETS(XXX);
where XXX = char XXX[10];
If I use getc then it is not hanging. Can you plz help me. The GETS reads data as 222.2 (and carry return)... this is a simple fault but I don't know why it is not working.
thanks |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Sun Mar 25, 2012 8:31 am |
|
|
Much safer to use get_string, in input.c
Key difference with this is you can set a _maximum_ size to avoid the buffer overflowing in line feed is not seen.
Most likely reason for gets failing, is that the code at the other end, is sending line feed, not carriage return (code 10, rather than 13). If so, you can change this in get_string (two lines before the end of the function).
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
i tried but not working |
Posted: Sun Mar 25, 2012 9:26 am |
|
|
heres my code
Code: |
if(kbhit()){ // distance calc is already done here and output is float distance variable
get_string(mtr, 4);
distance = atof(mtr);
//distance = distance;
sprintf(mtr,"%f",distance);
}
|
and in place of gets(mtr) it same problem..... mtr = char mtr[6];
can u plz help me from getting out from this...... ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Sun Mar 25, 2012 9:40 am |
|
|
As I said. Look at the get_string function - key is that the whole code is in input.c.
Two lines before the end, you have:
Which is waiting for the '/r' code (carriage return - 13). I suspect your program at the other end, is sending '/n' (line feed' - code 10) instead, so the end of input is never seen.
So change the line in get_string, to:
which will accept line feed instead.
Four is not enough for the limit. The string you show has five characters - remember the decimal point.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
|
![](templates/subSilver/images/spacer.gif) |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
after a long run i figured |
Posted: Sun Mar 25, 2012 11:25 am |
|
|
hello....
So far I used kbhit and GETS function in MAIN loop where i used delay 500ms for LCD update.
When I use Kbhit and gets function in interrupt i.e TIMER0 used every 1ms in my code finally that worked good.
I can see the value with out hand now. Now i have a little confusion (will this kbhit and gets implementation make any trouble for every 1ms timer function???)
Or how else can I implement to get the accurate result without losing the interrupt stability ?
Thanks a lot for your time. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 25, 2012 12:45 pm |
|
|
Quote: | I changed as you mentioned from 13 - 10
|
Read your data sheet. On page 4 it clearly tells you what the terminator
byte value is.
Here is a link to the data sheet for your ultrasonic unit:
http://www.rhydolabz.com/documents/sensors/Ultrasonic%20Distance%20Sensor%20Serial%20Output-User%20Manual.pdf
Quote: | get_string(mtr, 4);
|
Also stop trying to cut everything very close. There is no reason to try
to set the buffer exactly. Just make it large compared to the incoming
string size. Set it to 10 or more. Then you will have no problems.
In other words, you are making your life hard by not reading the manual
and by trying to set incoming buffer sizes too exactly, and then making
a mistake on the size. |
|
![](templates/subSilver/images/spacer.gif) |
|