Math for 32-bit numbers
I have a pressure sensor that outputs pressure in Pascal represented as 4-byte integers (long). I am able to trigger this output and receive the response, but need to do some conversion to present real-time data to a 4-digit LED. This displayed data will be psi value with 2 decimals. Otherwise the 4-byte data are being written to storage in the BS2pe.
An example would be 0x00 0x01 0x92 0x27. Converting this to decimal value would be: 102951 Pascal or 14.9279 psi (Pascal*0.000145).
How might I do the conversion on the fly with this 8-bit microcontroller? Be advised I am capturing at 10Hz for 10 seconds.
An example would be 0x00 0x01 0x92 0x27. Converting this to decimal value would be: 102951 Pascal or 14.9279 psi (Pascal*0.000145).
How might I do the conversion on the fly with this 8-bit microcontroller? Be advised I am capturing at 10Hz for 10 seconds.
Comments
where temp is a word and dividend is a 4 byte array containing the pressure sensor value. Each time getDigit is called, it produces another digit (right to left) as a character.
Thank you for the help. here is what I have from your code:
This is an impementation of the getDigit subroutine. While this still triggers the sensor and returns data, I'm thinking I have an incomplete understanding of inserting your code. For instance; why the "0"? And, I get nothing from the DEBUG statement.
This will display the digits backwards, but that's ok for debugging.
If the maximum is 99.99 psi, that is 689586 Pacal. That is a lot more digits of precision than you need for the display. It should be possible to drop the least significant digits. That is, get 6896 into a word variable and then multiply times 1.45 to get 9999. Something like that.
I was wondering, though, are the values returned by the sensor really ascii hex, or are they ascii BCD? I didn't see any "0x0A" to "0x0F"s in your example. The example conversion you gave was indeed HEX not BCD.
If it is ascii HEX, then the third byte will be no higher than hex "0x0A" at 100 psi. The fourth byte (msb) will always be zero.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Your assumptions are correct on all of your points, I expound;
1. I do not need full precision on the display, just on the write to OWL2pe memory. 2 digits of precision is more than enough.
2. The Keller Druck 33x pressure transmitters output 8-bit + 1 start + 1 stop for a 10-bit byte. Effectively, the data byte response is always a 32-bit binary series with 4 start/stop pairs. I am converting it for my own edification and to promote my own understanding. In full, the response is as follows: Device#,Command,B3,B2,B1,B0,Status,Checksum_H,Checksum_L.
3. A point not previously made is that the units I am using are rated to 700 fsw, but I will only be using them to SCUBA depths, so the MSB will probably not ever be used as the sensor will not be going to those depths. However, I would like to make the display and memory write fully scaled.
which is 689586 Pascal. Use the most significant nibbles, A85B and drop the least significant nibble (or round off):
total = xb2.nib0 * 16 + xb1.nib1 * 16 + xb1.nib0 * 16 + xb0.nib1
The calculation is cumulative left to right per PBASIC, not standard precedence of operators.
Then
psi = total ** 15205.
That will give psi = 9999, when the pascal value is 689586.
It works like this.
0xA85B is 43099 decimal. That is about 1/16 of the correct value due to dropping the final nibble.
The multiplier 15205 comes from
15204.352 = 0.000145 * 16 * 100 * 65536
Round up to 15205 to counter the tendency of the Stamp to truncate.
The factor of 100 is so that you get two digits past the decimal point.
The factor 65536 sets it up for the ** operator.
With a little more finessing and rounding, you could squeeze a little more precision out of it. If it has to go to higher pressures, to the next nibble digit up, it will have to be modified. But the resolution should still keep to about one part in 10000 for your display.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com