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

Ascii to float or int....

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



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

Ascii to float or int....
PostPosted: Fri Mar 23, 2012 12:47 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 23, 2012 12:55 pm     Reply with quote

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.
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

worked
PostPosted: Sun Mar 25, 2012 8:09 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Sun Mar 25, 2012 8:31 am     Reply with quote

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
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

i tried but not working
PostPosted: Sun Mar 25, 2012 9:26 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Sun Mar 25, 2012 9:40 am     Reply with quote

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:
Code:

   } while(c!=13);

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:
Code:

   } while(c!=10);

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
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

still no good results
PostPosted: Sun Mar 25, 2012 10:40 am     Reply with quote

Hello.... thanks for your courage.... but I am still failing in doing with my code.

I changed as you mentioned from 13 - 10 but same hanging..... here is my data sheet shows

http://www.rhydolabz.com/index.php?main_page=product_info&cPath=137_144&products_id=308

I don't understand at all why this problem come.... past 1 day i am cracking my head.
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

after a long run i figured
PostPosted: Sun Mar 25, 2012 11:25 am     Reply with quote

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.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 25, 2012 12:45 pm     Reply with quote

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.
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