PDA

View Full Version : MCP3202 & LM34 - missing something in the convert from DAC to mV display



Stempile
11-23-2010, 07:46 AM
I am very new to spin.

I have tried several of the objects download for MCP3202 and appear to be working. I have CH0 hooked to an LM34 and I am measuring the output of the LM34 with a volt meter.

Specs for MCP3202 indicate the following:


Digital Output Code = 4096 * Vin / Vdd

My values:
Vin = .74 (output per volt meter for the LM34)
Vdd = 3.3

As I display the DEC value of the ADC output to my debug term, I get something close to 918 (as calculated using above). I am not sure how to process decimals properly in spin, nor how to convert that to a mV value that would be equal to the LM34's 'F output. LM34 specs call for the mV output to be the same as the temp in 'F. I will likely be puzzle by signed values too...

Looking for help with spin number converting. Thanks!

kwinn
11-23-2010, 02:29 PM
@Stempile

0.74 / 3.3 * 4096 = 918.4969….. so 918 or something close to that is the raw reading you should get from the MCP3202.

Since you are using 3.3V or 3300mV as the reference the output represents 3300 / 4096 = 0.80566…. mV per bit. To get the output to represent 1mV per bit you need to multiply 918 by 0.80566… which equals 739.6mV. Since the output of the LM34 is 10mV per degree F we divide the result by 10 to get 73.96 degrees.

Simplified, the calculation would be 918 * 0.080566.

Stempile
11-24-2010, 01:57 AM
@Stempile

Simplified, the calculation would be 918 * 0.080566.


The part I am stuck on is within SPIN and how to represent a calculation as you have represented (with a decimal). Next is how to display that as a string for use on an LCD, console or SD file. I am not clear on the the display of the a "." in the result. I have tried multiple variations of shifting things around and I am just not getting it. I suspect I continue to look past something obvious.

Thanks for the follow up!
ms

kwinn
11-24-2010, 05:06 AM
The simplest way would be to use the floating point package for the calculations and fullduplexserial or one of the other serial drivers for I/O.
The calculations could be done in 32 bit binary and converted to fixed point decimal as well, but that is a slight bit more complicated.

Stempile
11-24-2010, 06:01 AM
Thanks for the tip, I hadn't looked at the float libs yet, will do that next.

I am still figuring out what the object exchange is all about. I finally started looking there for string stuff shortly after posting. I still have a pbasic mindset, though I have worked with C, just not enough... Messing around with 'FloatString v2' now; haven't got it working yet.

ms

Ariba
11-24-2010, 07:41 AM
Don't use Floatingpoint for such simple calculations !

If you calculate in milliVolts then integer will be good enough:

mV := ADCvalue * 3300 / 4096

It is important that you first multiply and then divide, otherwise the intermediate result can be lower than 1.0 (and gets truncated to 0).
The variables must be longs (32bit).

To display the value on a LCD:
(I assume the lcd object has the methodes dec, out and str, like the TV_Text and many other display objects)

lcd.dec(mV) 'show in milliVolt
lcd.str(string("mV "))

lcd.dec(mV/1000) 'show in Volt, first integer part
lcd.out(".")
lcd.dec(mV/100//10) 'then decimal places
lcd.dec(mV/10//10)
lcd.out("V")


Andy

Stempile
12-04-2010, 06:24 AM
Thanks Andy. I did not have time to get back to this until recently. Your suggestion was what I was looking for.