Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Rounding Off float value in PIC C

Status
Not open for further replies.

scorrpeio

Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
I am facing a problem while converting the received Temperature count into its equivalent temperature.

I have used LM35 as temp sensor and PIC18F4520 as controller.

The logic goes as...

Code:
Temperature = LM35Value * 0.48;

Now, I have declared Temperature as unsigned int. So, the above instruction doesnt serve the purpose.

I have also tried to multiply by 100 and during display divide by 100.

But, since the variable is int, the divide instruction creates an unidentifiable bug. I cant see any output, not even led blinking. 8O

Is it not possible to do above calculation using int only?:idea: Ought I use float?
If I use float how can I convert the hex( 234.546) into its equivalent decimal? :|
 

What exaclty you want to do?
If you wanna display the floating number then why you are going to change it into integer?
Clear us about your problem.
 

thank you for reply!!!

Sorry to say but I was also bewildered at that time about what to do and how to display temperature in the format 23.34 C.
I have got the solution as to multiply by 100 and display each digit separately and insert decimal point in between.
 

Are you displaying temp on LCD or 7segment?
If you are displaying on LCD then no need to seperate the digits,you can print the value directly
But if you are using 7 segment then you have to break the digits.
 
???
I didnt know that I can print it directly.
This is what I did...

1.I got the temp value in hex as 49 in a register.
2.I converted it to decimal using Hex to Decimal conversion
3.Each decimal digit is then converted into its ASCII
4.These ascii value are stored in array and passed to LCD through SendData routine.

Please let me know, if there is any simple method to achieve this.

---------- Post added at 10:58 ---------- Previous post was at 10:48 ----------

also, how can I print ºC? What is the ASCII for º. (I tried ASCII 0xA7...but didnt work)
 

Which compiler you are using.I am using CCS Compiler,it has an LCD driver which supports the printing function.

In my program i have done like that
Code:
printf(lcd_putc,"%3.2f",on_time);
and it is printing well.
if you are including LCD driver file then see that driver file for printing floating point number,definately it was described in the driver file.

I dont know if there is any ascii for º. you can make your own charater as there are options in lcd that you can create 8 custom characters.

From where you are getting the value in hex format?
 

The printf is the easy solution but will uses many resources,
i would suggest the multiplication method to avoid float numbers for this simple task.
Float version of sprintf is just a line but will create more code than the multiplication method,
you can still try it for experience with the method or you can use it if you don't have any other tasks in the micro controller and you don't care about the compiled program size.

Check this project, it uses a very nice integer to ascii method (using multiplication)
A Digital temperature meter using an LM35 temperature sensor :Embedded Lab

Alex
 

Yes...printf function would work surely. But, as a learning process in embedded programming, I am avoiding the ready made functions like printf, sprintf etc.

Well, what I have done is multiplied the value by 100 and then converted the whole into 5 digits. After the first three digit I put decimal point and then the next 2 numbers. In this way, without using float datatype, I did this :)

I have got the ascii value for the degree symbol from the above link :)

Thank you for replies!!!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top