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

MS5607B Calculations

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



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

MS5607B Calculations
PostPosted: Tue May 17, 2011 5:35 am     Reply with quote

Hi All

I am trying to write a driver for a pressure sensor MS5607B
Datasheet Here http://www.intersema.ch/products/guide/calibrated/ms5607b/

I have managed to aquire all the variables from the device over I2C but I am having trouble with the final stage of the calculations.

On the datasheet Pg 7 under "Calculate temperature compensated pressure"
The calculation uses signed int 64 variables and states that "Maximal size of intermediate result during evaluation of variable" is upto between 41 and 58 bits

Is there a way/what is the best way, to do this on PIC18 CCS Compiler ?

Any help would be much appreciated

Thanks
Jeff
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 17, 2011 5:43 am     Reply with quote

Surely the mfr. has an example either in the datasheet or some application note ? I find it hard to believe that they don't supply such important information !
Perhaps 'googling' will help find existing code for this product, even if it's written in another language, it'd be simple to convert to CCS C.
That being said, perhaps if you post your relevent code and the algorithm we can help...
JeffLewcock



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

PostPosted: Tue May 17, 2011 5:55 am     Reply with quote

Hi Temtronic

They do indeed have an example in "C" but its for a system that can use 64 bit floats.

The longest float in CCS for PIC18's is 32 bit

The code is not really the problem its the 64 bit maths

I have searched for code but can find noe that is helpfull

Jeff
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Tue May 17, 2011 6:21 am     Reply with quote

Quantum Mechanics is the theory that underpins electronics. It is accurate to 12 places and is the most accurate of any theories to date. Now 64bit well exceeds this for precision. This 64bit stuff is done often to paper over the mathematically correct issues that occur when changing notation from the decimal system to binary and vice versa. Unless you are using whole numbers or the fixed point equivalent notational conversion issues will arise.
64 bit moves the conversion issue many digits right of the decimal point so that after truncation and conversion to decimal notation calculations appear as if they were done in exclusively in decimal notation. It is rare sensors are more accurate than 32 bit binary float ( 6 or 7 significant decimal digits). I'd write the code using 32 bit it should be 99.99% as accurate as 64bit.
JeffLewcock



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

PostPosted: Tue May 17, 2011 7:14 am     Reply with quote

HI Douglas

Thanks for that

Can you see an easy way to reduce the formulae on Pg 7 to a lower resolution ?

Thanks

Jeff
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 17, 2011 7:18 am     Reply with quote

I have to giggle at ANYTHING over 16 bits. Real World gotchas include noise ,EMI, magnetic deviation, solar flares, PCB layout,etc.etc,etc.
Sure in THEORY 64 bits are better than 12 or 16 or 32 BUT unless you spend hundreds of hours making 'ultra quiet' PCBs,tri-coaxial shielded cables,etc. it really is a futile effort.
Heck if 64 bit is great, 128 has to be better !Hmm..how about 256 bits?

Do a realty check, you can get to the moon and back on less then 16 bits.

Brings up a point, what is the specs of the sensor? It can't be cheap if you require 64 bit math !

Also beaware that float math is a hog on time and if your application is time sensitive, then the sensor data may have signifigantly changed during the match calculations.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Tue May 17, 2011 7:24 am     Reply with quote

Have you just tried the code as it is?.
You can use the keyword 'double' in CCS, it just does nothing (except on the PCD compiler), with the basic accuracy remaining at 24bit plus exponent. The sensor itself 'only' has a 24bit data return, and the last couple of bits of this will be 'doubtful' in most circumstances. But realistically it should work pretty well with the maths 'as is'.
They are using double, because it is available, and to reduce the risk of rounding errors in the intermediate stages (which may be a problem), but I suspect it'll work quite well with single precision floats....

Best Wishes
JeffLewcock



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

PostPosted: Tue May 17, 2011 7:56 am     Reply with quote

Hi Telmah

I did try this very quickly whilst I was coding the i2c but got garbage.

I will try again with a bit more attention to detail !

Thanks

Jeff
JeffLewcock



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

PostPosted: Wed May 18, 2011 1:59 pm     Reply with quote

Hi All

Temtronic, 16 bits may be good enough for a lot of things but there are a lot of things its not good for ie if you earn more than 65536 then the 16 Bit calculations computer that pays you has a problem ! also The moon is 238,357 miles away (an 18 bit integer) I dont fancy trying to land a spacecraft with a altimeter that has 3.6mile resolution.

In answer to your question the sensor costs about $10

Ttelmah, Thanks for the suggestion however the calculation falls at the first part of the first equations on Pg7
C2 *2^17 where C2 is 43981 but using CCS the max multiplier is 2^16 that gives a correct answer

Code:

int16 C[6];
int32 D1,D2;
signed int32 dT;
float Pressure_Sensor_Temp;
int32 Offset;

void MS5670_Read_Coefficients (int8 Address)
 {       
         C[0] = 46372;
         C[1] = 43981;
         C[2] = 29509;
         C[3] = 27842;
         C[4] = 31553;
         C[5] = 28165;
 }
 
 void MS5670_Power_On_Reset (int8 Address)
 {
         i2c_start();                                 // Start condition
         i2c_write(Address*2);                        // (Slave Address * 2) For R/W bit low for a write
         i2c_write(0x1E);                             // Command POR
         i2c_Stop();
         
 }
 
 void MS5670_Start_Conversion_and_Read_Pressure (int8 Address)
 {
          D1 = 6465444;
 }
 
 void MS5670_Start_Conversion_and_Read_Temperature (int8 Address)
 {
         D2 = 8077636;
 }
 
 void MS5670_Calculate_Temperature_and_Pressure()
 {
 
         dT = D2-((float)C[4]*256);
         Pressure_Sensor_Temp = (2000 + (dT*((float)C[5]/8388608)))/100;
         
         Offset = ((float)C[1] * 65536); //+(((float)C[4]*dT)/64);
         
         
 }



The Display Routine (To RS232)

Code:

#include <18f67j60_Trial_RS232.h>
#include <Math.h>
#include <MS5607B_Trial.c>


void main()
{

 MS5670_Read_Coefficients(0x77);



    while(true){
   
      Delay_ms (1000);
      output_low(LED);
         
         
      MS5670_Start_Conversion_and_Read_Pressure (0x77);
      MS5670_Start_Conversion_and_Read_Temperature (0x77);
      MS5670_Calculate_Temperature_and_Pressure();
         
output_high(LED);

printf("\033[2J");
printf ("Pressure Sensor C1  = %Lu\n\r",C[0]);
printf ("Pressure Sensor C2  = %Lu\n\r",C[1]);
printf ("Pressure Sensor C3  = %Lu\n\r",C[2]);
printf ("Pressure Sensor C4  = %Lu\n\r",C[3]);
printf ("Pressure Sensor C5  = %Lu\n\r",C[4]);
printf ("Pressure Sensor C6  = %Lu\n\r",C[5]);
printf ("Pressure Sensor D1  = %Lu\n\r",D1);
printf ("Pressure Sensor D2  = %Lu\n\r",D2);
printf ("Dt = %Ld\n\r",dT);
printf ("Pressure_Sensor_Temp = %3.3f\n\r",Pressure_Sensor_Temp);
printf ("Pressure_Sensor_Offset = %Lu\n\r",offset);

    }}
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed May 18, 2011 3:05 pm     Reply with quote

Your multiplier is working as an int16, not a float. You are gong to have to be scrupulously careful about casting, since CCS doesn't 'propagate upwards', and if a numeric type overflows you will just get garbage.
I think the 2^x construct only works with int16. You will need to use the fp 'pow' operation instead.

Best Wishes
JeffLewcock



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

PostPosted: Wed May 18, 2011 3:19 pm     Reply with quote

HI Ttelmah

I have tried casting the multiplier as int32 and float

but both ways the answer is incorrect the correct answer is 5764677632

Code:

Offset = ((float)C[1] * (float)131072); answer = 1469710337
and
Offset = ((float)C[1] * (int32)131072); answer = 1469710337


as you point out I think there is an overflow

I am really a hardware man and far from an expert in maths (especially in CCS C) is there a good reference you can suggest ?

I can reduce the resolution (I only need 12 bits really) but working out WHERE to do it is not obvious to me

many thanks for your attention

Jeff


Last edited by JeffLewcock on Wed May 18, 2011 3:36 pm; edited 1 time in total
JeffLewcock



Joined: 10 Apr 2007
Posts: 29

View user's profile Send private message

PostPosted: Wed May 18, 2011 3:36 pm     Reply with quote

Hi Ttelmah

I will look into the "pow" operator !

Thanks
Jeff
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