NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Tempsensor Project and Decimal Places in C
March 21, 2012 by bsucher |
Hi everyone, I just finished the tempsensor project and I had a question. Basically, I finished the project and my LCD readout was 0.00 degrees F. I then compared my code to the sample code and the only difference was that for the conversion equation of: sample * (5000.0/1024.0/10.0) I actually wrote: sample * (5000/1024/10). After changing the code to include the .0 in the numbers, the project works just fine. What is it in C programming and in this code specifically that makes that happen? Does it have to do with how the numbers are printed and the %.2f formatting line? |
---|---|
March 21, 2012 by pcbolt |
Hi bsucher - When the compiler sees fixed numbers in the source code like 5000, 1024, 1024.0 etc, it converts them into binary numbers and "hard-codes" them into the final machine code. In other words it doesn't store them away somewhere to be used later. The way it converts them into binary numbers is different depending on what type of numbers they are (integers, characters, floating point numbers etc). It can figure out whether to use 8-bits (for integers less than 256) or 16-bits or 32-bits just by the size of the number. What it can't figure out on its own is whether or not the number is an integer or a floating point number. 1024 and 1024.0 are the same to you and I, but to the compiler the binary representation of them are vastly different. So by putting the .0 at the end you are telling the compiler "I want this number to be a floating point number". After that the compiler knows to treat all operations with this number as floating point operations. If you just use 1024 for example, the compiler will treat it as an integer and use "integer" math in all calculations. So in your example...
It will first divide 5000 by 1024 with NO remainder taken into account and you'll get 4. Then it will divide 4 by 10 to give 0 always. 0 times "sample" will always be 0. By using...
It knows now all numbers are floating point numbers and this time it will keep the remainders in all division operations. So, 5000.0/1024.0 = 4.883 and 4.883/10 = 0.4883. Now it can give the correct answer when multiplying by "sample". The %.2f syntax tells the compiler to treat the variable associated with this print statement as a floating point number (the "f" tag) and to represent it with 2 decimal places (.2 tag). Hope this helps. |
March 21, 2012 by bsucher |
pcbolt, Thank you so much for your reply. You clarified everything perfectly. The reason I purchased the NerdKit is because I wanted to learn C programming through fun projects. I'm going to be graduating with an Electrical Engineering degree in a year and I figured it'd be good to know how to do some basic coding since it seems most employers want that. Prior to this the only "coding" experience I have is using MATLAB and writing MATLAB programs/functions. I'm fairly familiar with the basics of coding because of this, but I lack a lot of major foundations. I'd expect a whole lot more dumb coding questions in the future =). Thanks again, Brandon |
March 21, 2012 by bsucher |
pic test |
March 21, 2012 by pcbolt |
Brandon - Glad to help. The nice thing about the Nerdkit is that it does exactly what you describe, namely fun projects and learning how using "C" code you can interact with micro controllers. I don't think it was an accident Humberto and Mike picked "C" to base their product on. Most "high level" languages are based on "C" and believe it or not, most of the Windows, MAC and Linux operating systems are written in "C" mainly because it is closely related to the way hardware interacts. After I did some of the projects in the NK guide, I started doing some of the tutorial projects and it was onward and upward from there. I just wish I had an EE's background to expand even further. No need to worry about dumb questions, I'll have a few of my own for you. |
March 22, 2012 by bsucher |
Ah gotcha! That makes a lot of sense then why every job I see says "Knowledge/Experience in C/C++/embedded C preferred." Yeah it's interesting going to school for electrical engineering. At my school you do so much theory and barely anything "hands on." I'm in all my senior electives and I've barely ever touched any electronics (analog or digital). Then I heard about this kit a couple weeks ago and I just had to get it and start learning. It's weird...I know a lot of the math, physics, and systems design that goes into designing some of these types of digital circuits...but in my 3 years of the EE program I've never actually physically done anything with them. I go to a California state school though, so needless to say the school is beyond bankrupt and has no money for labs. Anyways, I think the NK will provide everything I'm looking for and more. My inevitable goal is to be able to do this project from scratch after learning everything I can through the various NK guides and a book I bought on embedded C programming for atmel chips. |
March 22, 2012 by Rick_S |
pcbolt, You taught me something there as well. I never dropped the decimals in the tempsensor project so I never came across that. I would have been frustrated as could be had I done it though. I wouldn't have even thought about the compiler defaulting to integer math based on the numbers being an integer. Makes sense though. Thanks Brandon for bringing this up. Rick |
March 22, 2012 by Ralphxyz |
Really thanks pcbolt that is a classic lesson. Now Brandon get ready for a real education with lots of hands on, sweat, worry and hair pulling (something one does not get from theory). You should tell your fellow students about Nerdkits. Ralph |
March 22, 2012 by bsucher |
Ralph, Since my purchase of the NK 2 weeks ago, I've already gotten 4 others to buy it as well. Get ready for an influx of newbies! |
March 22, 2012 by JKITSON |
PCBOLT I was having a small error in my program. Thought it was hardware & decided to live with it. I changed my constants as you show. WOW no more errors.....Thanks Jim |
March 22, 2012 by pcbolt |
@ Rick, Ralph, JKITSON - Glad to hear it helps. If I can spare anyone some of the "hair-pulling, sweat and worry", it makes it well worthwhile. @ Brandon - I knew the Cal State schools were strapped for $$$, but no labs is a shame. Hopefully Nerdkits will give you a leg up on the competition. Oh and if you can pull off that 3-D cube project, that would be awesome. Can't wait for the video :D |
Please log in to post a reply.
Did you know that sound travels via pressure waves in the air, and you can make these with a piezoelectric buzzer? Learn more...
|