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

code for MPL115 I2C MPL115A2

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
carloshernangarrido



Joined: 02 Feb 2011
Posts: 1

View user's profile Send private message

code for MPL115 I2C MPL115A2
PostPosted: Wed Feb 02, 2011 12:53 pm     Reply with quote

Code:

/*
El presente es un programa de ejemplo con librerías para utilizar el barómetro
digital MPL115A2 con interface Soft_I2C por software.

Compiler: ccs
Author: Hernan Garrido
*/

#include <16F883.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

//CONSTANT DEFINITIONS
const int8 ACK = 1;  // ACKNOWLEDGE
const int8 NACK = 0;  // NOT ACKNOWLEDGE
const int8 ADDRESS_W = 0xC0;// 0x60 ADDRESS PLUS WRITE BIT
const int8 ADDRESS_R = 0xC1;// 0x60 ADDRESS PLUS READ  BIT
const int8 POUTH  = 0x00;// 10-bit pressure output value MSB 8 Yes Yes Yes
const int8 TOUTH  = 0x02;// 10-bit temperature output value MSB 8 Yes Yes Yes
const int8 BOTH   = 0x12;// Start both Conversions




//FUNCTION DECLARATIONS
void MPL115A2_init();
void MPL115A2_meas_proc();
int16 MPL115A2_read_pressure();
int16 MPL115A2_read_temperature();
int32 Presion_compensada(int16 PressCntdec, int16 TempCntdec);


void main(){
int16 P, T;


//printf("\n\rArranque");
MPL115A2_init();
//printf("\n\rInicializacion");
   
 
while (1){
   MPL115A2_meas_proc();
   //printf("\n\rMedicion");

   P = MPL115A2_read_pressure();
   //printf("\n\rLectura de presion");
   
   T = MPL115A2_read_temperature();
   //printf("\n\rLectura de temperatura");

   //printf("\n\rPresion: %Lu; Temperatura: %Lu", P, T);
   
   printf("\n\r Presion compensada %Lu Pa", Presion_compensada(P, T));
   
   delay_ms(1000);

}



}






//FUNCTION DEFINITIONS
void MPL115A2_init(){//INITIALIZES THE SOFT Soft_I2C PORT
     #use I2C(master, sda=PIN_C4, scl=PIN_C3, SLOW)
}

void MPL115A2_meas_proc(){//EXECUTE A MEASURE OF PRESSURE AND TEMPERATURE
     I2C_Start();//START BIT
     I2C_Write(ADDRESS_W);//SENDS THE ADDRESS OF THE DEVICE + WRITE
     I2C_Write(BOTH);//START BOTH CONVERSIONS
     I2C_Write(0x01);//no se porque carajo va esto, pero parece que es necesario
     I2C_Stop();
     delay_ms(10);
}

int16 MPL115A2_read_pressure(){//ask for the pressure measurement
     int16   P_MSB, P_LSB;// MS and LS bytes of pressure (8 bits variables)
     int16   P;// 10 bits pressure mesurement (16 bits variable)
     I2C_Start();//START BIT
     I2C_Write(ADDRESS_W);//IT SENDS THE ADDRESS OF THE DEVICE + WRITE
     I2C_Write(POUTH);//IT SENDS READ MSB PRESURE COMMAND
     I2C_Start();//RESTART
     I2C_Write(ADDRESS_R);//IT SENDS THE ADDRESS OF THE DEVICE + READ
     P_MSB = I2C_Read(ACK); //IT READS THE MSB AND SENDS AN ACK
     P_LSB = I2C_Read(NACK);//IT READS THE MSB AND DOES'T SEND ACK
     I2C_Stop();//IT SENDS A STOP BIT

     P = P_MSB*256 + P_LSB;
     
     //printf("\n\r P_MSB:%X; P_LSB:%X;", P_MSB, P_LSB);
     
     P=P/64;//porque los 10 bits de la medición estan amontonados del lado mas significativo
     
     DELAY_MS(10);
     return P;
}

int16 MPL115A2_read_temperature(){//ask for the pressure measurement
     int16   T_MSB, T_LSB;// MS and LS bytes of pressure (8 bits variables)
     int16   T;// 10 bits pressure mesurement (16 bits variable)
     I2C_Start();//START BIT
     I2C_Write(ADDRESS_W);//IT SENDS THE ADDRESS OF THE DEVICE + WRITE
     I2C_Write(TOUTH);//IT SENDS READ MSB TEMPERATURE COMMAND
     delay_ms(10);
     I2C_Start();//RESTART
     I2C_Write(ADDRESS_R);//IT SENDS THE ADDRESS OF THE DEVICE + READ
     delay_ms(10);
     T_MSB = I2C_Read(ACK); //IT READS THE MSB AND SENDS AN ACK
     T_LSB = I2C_Read(NACK);//IT READS THE MSB AND DOES'T SEND ACK
     I2C_Stop();//IT SENDS A STOP BIT

     T = T_MSB*256 + T_LSB;
     
     //printf("\n\r T_MSB:%X; T_LSB:%X;", T_MSB, T_LSB);
     
     
     T=T/64;//porque los 10 bits de la medición estan amontonados del lado mas significativo
     
     DELAY_MS(10);
     return T;
}



int32 Presion_compensada(unsigned int16 P, unsigned int16 T){
   // VARIABLE DEFINITIONS

   float          a0, b1, b2, c12;
   int16          w_a0, w_b1, w_b2, w_c12;//words (16bits)
   signed int16   s_w_a0, s_w_b1, s_w_b2, s_w_c12;//signed words (15bits + sign bit)

   float Pcomp;//resultado
   int32 intPcomp;
   
   float c12porTf;
   float c12porTfmasb1;
   float c12porTfmasb1porPf;
   float b2porTf;
   
   int16 I2CCoeff[12];
   
   int i=0;

   float Pf;
   float Tf;
   
   Pf = P;
   Tf = T;
   
   I2C_Start();//START BIT
   I2C_Write(ADDRESS_W);//IT SENDS THE ADDRESS OF THE DEVICE + WRITE
   I2C_Write(0x04);//IT SENDS READ MSB TEMPERATURE COMMAND
   delay_ms(10);   
   I2C_Start();//RESTART
   I2C_Write(ADDRESS_R);//IT SENDS THE ADDRESS OF THE DEVICE + READ
   delay_ms(10);
     
   for (i=0; i<12; i++){
      if (i==11){
         I2CCoeff[i] = I2C_Read(NACK); //IT READS THE MSB AND SENDS AN ACK
      }
      else{
         I2CCoeff[i] = I2C_Read(ACK); //IT READS THE MSB AND SENDS AN ACK
      }
      DELAY_MS(10);
   }

   I2C_Stop();//IT SENDS A STOP BIT

   /*printf("\n\rCoeficientes:" );
   for (i=0; i<12; i++){
      printf("%X", I2CCoeff[i]);
   }*/

   //construccion de w_a0, w_b1, w_b2, w_c12;
   w_a0 = I2CCoeff[0]*256 + I2CCoeff[1];
   w_b1 = I2CCoeff[2]*256 + I2CCoeff[3];
   w_b2 = I2CCoeff[4]*256 + I2CCoeff[5];
   w_c12= I2CCoeff[6]*256 + I2CCoeff[7];
   //c12 es de 14 bits por lo cual los dos bits menos significativos no van
   w_c12 = w_c12/4;


   //contruccion de a0
   /*1 bit de signo
   12 bits parte entera
   3bits parte fraccionaria*/
   s_w_a0 = w_a0;//para respetar el signo
   a0 = (signed int16)s_w_a0;//para pasar a flotante
   a0 = a0/8;//para ubicar la coma donde va

   //printf("\n\rCoeficiente a0:\n\r w_a0: %LX %Lu \n\r s_w_a0: %LX %Ld \n\r a0: %LX %.5f", w_a0, w_a0, s_w_a0, s_w_a0, a0, a0);
   
   
   //contruccion de b1
   /*1 bit de signo
   2 bits parte entera
   13bits parte fraccionaria*/
   s_w_b1 = w_b1;//para respetar el signo
   b1 = (signed int16)s_w_b1;//para pasar a flotante
   b1=b1/8192;//para ubicar la coma donde va

   //printf("\n\rCoeficiente b1:\n\r w_b1: %LX %Lu \n\r s_w_b1: %LX %Ld \n\r b1: %LX %.5f", w_b1, w_b1, s_w_b1, s_w_b1, b1, b1);

   //contruccion de b2
   /*1 bit de signo
   1 bits parte entera
   14bits parte fraccionaria*/
   s_w_b2 = w_b2;//para respetar el signo
   b2 = (signed int16)s_w_b2;//para pasar a flotante
   b2=b2/16384;//para ubicar la coma donde va

   //printf("\n\rCoeficiente b2:\n\r w_b2: %LX %Lu \n\r s_w_b2: %LX %Ld \n\r b2: %LX %.5f", w_b2, w_b2, s_w_b2, s_w_b2, b2, b2);
   
   
   //contruccion de c12
   /*1 bit de signo
   0 bits parte entera
   13bits parte fraccionaria
   9 ceros entre la coma y el primero de los 13 bits fraccionales
   */
   s_w_c12 = w_c12;//para respetar el signo
   c12 = (signed int16)s_w_c12;//para pasar a flotante
   c12=c12/4194304;//para ubicar la coma donde va

   //printf("\n\rCoeficiente c12:\n\r w_c12: %LX %Lu \n\r s_w_c12: %LX %Ld \n\r c12: %LX %.5f", w_c12, w_c12, s_w_c12, s_w_c12, c12, c12); 

   c12porTf = c12*Tf;
   c12porTfmasb1 = c12porTf + b1;
   c12porTfmasb1porPf = c12porTfmasb1 * Pf;
   b2porTf = b2 * Tf;
   
   Pcomp = a0 + c12porTfmasb1porPf + b2porTf;

   Pcomp = (Pcomp * 63.538611925708699902248289345064)+50000.0;

   intPcomp = Pcomp;

   return intPcomp;

}



I HOPE IT HELPS YOU!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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