搜索
bottom↓
回复: 31

BMP180气压传感器读出来的值

[复制链接]

出0入0汤圆

发表于 2014-10-6 17:09:43 | 显示全部楼层 |阅读模式
温度值是:0x333   =819是啥意思啊
气压只是:0xBBC4 =48068
求大神指点啊
怎么搞得对不上呀



源代码


//-----------------------------------------------------------------------------
// F31x_SMBus_EEPROM.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This example demonstrates how the C8051F31x SMBus interface can communicate
// with a 256 byte I2C Serial EEPROM (Microchip 24LC02B).
// - Interrupt-driven SMBus implementation
// - Only master states defined (no slave or arbitration)
// - Timer1 used as SMBus clock source
// - Timer3 used by SMBus for SCL low timeout detection
// - SCL frequency defined by <SMB_FREQUENCY> constant
// - Pinout:
//    P0.0 -> SDA (SMBus)
//    P0.1 -> SCL (SMBus)
//
//    P3.0 -> C2D (debugging interface)
//
//    P3.3 -> LED (on the 'F310 TB)
//
//    all other port pins unused
//
// How To Test:
//
// 1) Download code to a 'F31x device that is connected to a 24LC02B serial
//    EEPROM (see the EEPROM datasheet for the pinout information).
// 2) Run the code:
//         a) the test will indicate proper communication with the EEPROM by
//            turning on the LED at the end the end of the test
//         b) the test can also be verified by running to the if statements
//            in main and checking the sent and received values by adding
//            the variables to the Watch Window
//
//
// FID:            31X000008
// Target:         C8051F31x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (TP)
//    -02 MAR 2006
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <C8051F310.h>

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------


#define  SYSCLK         24500000       // System clock frequency in Hz

#define  SMB_FREQUENCY  50000          // Target SCL clock rate
                                       // This example supports between 10kHz
                                       // and 100kHz

#define  WRITE          0x00           // SMBus WRITE command
#define  READ           0x01           // SMBus READ command

// Device addresses (7 bits, lsb is a don't care)
#define  BMP180_ADDR    0xEE           // Device address for slave target
                                       // Note: This address is specified
                                       // in the Microchip 24LC02B
                                       // datasheet.
// SMBus Buffer Size
#define  SMB_BUFF_SIZE  0x08           // Defines the maximum number of bytes
                                       // that can be sent or received in a
                                       // single transfer

// Status vector - top 4 bits only
#define  SMB_MTSTA      0xE0           // (MT) start transmitted
#define  SMB_MTDB       0xC0           // (MT) data byte transmitted
#define  SMB_MRDB       0x80           // (MR) data byte received
// End status vector definition


const unsigned char OSS = 0;  // Oversampling Setting

// Calibration values
int idata ac1;
int idata ac2;
int idata ac3;
unsigned int idata ac4;
unsigned int idata ac5;
unsigned int idata ac6;
int idata b1;
int idata b2;
int idata mb;
int idata mc;
int idata md;
long   idata bmp180_presure; //bmp180
unsigned int idata bmp180_temp;

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
unsigned char* pSMB_DATA_IN;           // Global pointer for SMBus data
                                       // All receive data is written here

unsigned char SMB_SINGLEBYTE_OUT;      // Global holder for single byte writes.

unsigned char* pSMB_DATA_OUT;          // Global pointer for SMBus data.
                                       // All transmit data is read from here

unsigned char SMB_DATA_LEN;            // Global holder for number of bytes
                                       // to send or receive in the current
                                       // SMBus transfer.

unsigned char WORD_ADDR;               // Global holder for the EEPROM word
                                       // address that will be accessed in
                                       // the next transfer

unsigned char TARGET;                  // Target SMBus slave address

bit SMB_BUSY = 0;                      // Software flag to indicate when the
                                       // EEPROM_ByteRead() or
                                       // EEPROM_ByteWrite()
                                       // functions have claimed the SMBus

bit SMB_RW;                            // Software flag to indicate the
                                       // direction of the current transfer

bit SMB_SENDWORDADDR;                  // When set, this flag causes the ISR
                                       // to send the 8-bit <WORD_ADDR>
                                       // after sending the slave address.


bit SMB_RANDOMREAD;                    // When set, this flag causes the ISR
                                       // to send a START signal after sending
                                       // the word address.
                                       // For the 24LC02B EEPROM, a random read
                                       // (a read from a particular address in
                                       // memory) starts as a write then
                                       // changes to a read after the repeated
                                       // start is sent. The ISR handles this
                                       // switchover if the <SMB_RANDOMREAD>
                                       // bit is set.

bit SMB_ACKPOLL;                       // When set, this flag causes the ISR
                                       // to send a repeated START until the
                                       // slave has acknowledged its address

sfr16    TMR3RL   = 0x92;              // Timer3 reload registers
sfr16    TMR3     = 0x94;              // Timer3 counter registers

sbit     LED      = P3^3;              // LED on port P3.3

sbit SDA = P0^0;                       // SMBus on P0.0
sbit SCL = P0^1;                       // and P0.1

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void                   SMBus_Init(void);
void                   Timer1_Init(void);
void          Timer3_Init(void);
void          Port_Init(void);

void          SMBus_ISR(void);
void          Timer3_ISR(void);

void          BMP180_ByteWrite(unsigned char addr, unsigned char dat);
void          BMP180_WriteArray(unsigned char dest_addr, unsigned char* src_addr,
                       unsigned char len);
unsigned char BMP180_ByteRead(unsigned char addr);
void          BMP180_ReadArray(unsigned char* dest_addr, unsigned char src_addr,
                      unsigned char len);

void temp_calibration(void);
void  Delay(unsigned int nCount);
void test_pressure_main(void);

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
//
// Main routine performs all configuration tasks, then loops forever sending
// and receiving SMBus data to the slave EEPROM.

void main (void)
{
//   char in_buff[8] = {0};              // Incoming data buffer
//   char out_buff[8] = "ABCDEFG";       // Outgoing data buffer

//   bit error_flag = 0;                 // Flag for checking EEPROM contents
   unsigned char i;                    // Temporary counter variable


   PCA0MD &= ~0x40;                    // WDTE = 0 (disable watchdog timer)


   // Set internal oscillator to highest
   // setting of 24500000
   OSCICN |= 0x03;

   // If slave is holding SDA low because of an improper SMBus reset or error
   while(!SDA)
   {
      // Provide clock pulses to allow the slave to advance out
      // of its current state. This will allow it to release SDA.
      XBR1 = 0x40;                     // Enable Crossbar
      SCL = 0;                         // Drive the clock low
      for(i = 0; i < 255; i++);        // Hold the clock low
      SCL = 1;                         // Release the clock
      while(!SCL);                     // Wait for open-drain
                                       // clock output to rise
      for(i = 0; i < 10; i++);         // Hold the clock high
      XBR1 = 0x00;                     // Disable Crossbar
   }

   Port_Init ();                       // Initialize Crossbar and GPIO

   LED = 0;                            // Turn off the LED before the test
                                       // starts


   Timer1_Init ();                     // Configure Timer1 for use as SMBus
                                       // clock source

   Timer3_Init ();                     // Configure Timer3 for use with SMBus
                                       // low timeout detect

   SMBus_Init ();                      // Configure and enable SMBus

   EIE1 |= 0x01;                       // Enable the SMBus interrupt

   EA = 1;                             // Global interrupt enable



   // Read and write some bytes to the EEPROM and check for proper
   // communication

   // Write the value 0xAA to location 0x25 in the EEPROM
//   EEPROM_ByteWrite(0x25, 0xAA);

   // Read the value at location 0x25 in the EEPROM
//   temp_char = EEPROM_ByteRead(0x25);

   // Check that the data was read properly
//   if (temp_char != 0xAA)
//   {
//      error_flag = 1;
//   }

   // Write the value 0xBB to location 0x25 in the EEPROM
//   EEPROM_ByteWrite(0x25, 0xBB);

   // Write the value 0xCC to location 0x38 in the EEPROM
//   EEPROM_ByteWrite(0x38, 0xCC);

   // Read the value at location 0x25 in the EEPROM
//   temp_char = EEPROM_ByteRead(0x25);

   // Check that the data was read properly
//   if (temp_char != 0xBB)
//   {
//      error_flag = 1;
//   }

   // Read the value at location 0x38 in the EEPROM
//   temp_char = EEPROM_ByteRead(0x38);

   // Check that the data was read properly
//   if (temp_char != 0xCC)
//   {
//      error_flag = 1;
//   }

   // Store the outgoing data buffer at EEPROM address 0x50
//   EEPROM_WriteArray(0x50, out_buff, sizeof(out_buff));

   // Fill the incoming data buffer with data starting at EEPROM address 0x50
//   EEPROM_ReadArray(in_buff, 0x50, sizeof(in_buff));

   // Check that the data that came from the EEPROM is the same as what was
   // sent
//   for (i = 0; i < sizeof(in_buff); i++)
//   {
//      if (in_buff != out_buff)
//      {
//         error_flag = 1;
//      }
//   }

   // Indicate communication is good
//   if (error_flag == 0)
//   {
//      // LED = ON indicates that the test passed
//      LED = 1;
//   }

   while(1)
   {
                   test_pressure_main();
   }

}

//-----------------------------------------------------------------------------
// Initialization Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SMBus_Init()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// The SMBus peripheral is configured as follows:
// - SMBus enabled
// - Slave mode disabled
// - Timer1 used as clock source. The maximum SCL frequency will be
//   approximately 1/3 the Timer1 overflow rate
// - Setup and hold time extensions enabled
// - Free and SCL low timeout detection enabled
//
void SMBus_Init (void)
{
   SMB0CF = 0x5D;                      // Use Timer1 overflows as SMBus clock
                                       // source;
                                       // Disable slave mode;
                                       // Enable setup & hold time extensions;
                                       // Enable SMBus Free timeout detect;
                                       // Enable SCL low timeout detect;

   SMB0CF |= 0x80;                     // Enable SMBus;
}

//-----------------------------------------------------------------------------
// Timer1_Init()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Timer1 is configured as the SMBus clock source as follows:
// - Timer1 in 8-bit auto-reload mode
// - SYSCLK / 12 as Timer1 clock source
// - Timer1 overflow rate => 3 * SMB_FREQUENCY
// - The maximum SCL clock rate will be ~1/3 the Timer1 overflow rate
// - Timer1 enabled
//
void Timer1_Init (void)
{
// Make sure the Timer can produce the appropriate frequency in 8-bit mode
// Supported SMBus Frequencies range from 10kHz to 100kHz.  The CKCON register
// settings may need to change for frequencies outside this range.
#if ((SYSCLK/SMB_FREQUENCY/3) < 255)
   #define SCALE 1
      CKCON |= 0x08;                   // Timer1 clock source = SYSCLK
#elif ((SYSCLK/SMB_FREQUENCY/4/3) < 255)
   #define SCALE 4
      CKCON |= 0x01;
      CKCON &= ~0x0A;                  // Timer1 clock source = SYSCLK / 4
#endif

   TMOD = 0x20;                        // Timer1 in 8-bit auto-reload mode

   TH1 = -(SYSCLK/SMB_FREQUENCY/12/3); // Timer1 configured to overflow at 1/3
                                       // the rate defined by SMB_FREQUENCY

   TL1 = TH1;                          // Init Timer1

   TR1 = 1;                            // Timer1 enabled
}


//-----------------------------------------------------------------------------
// Timer3_Init()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Timer3 configured for use by the SMBus low timeout detect feature as
// follows:
// - Timer3 in 16-bit auto-reload mode
// - SYSCLK/12 as Timer3 clock source
// - Timer3 reload registers loaded for a 25ms overflow period
// - Timer3 pre-loaded to overflow after 25ms
// - Timer3 enabled
//
void Timer3_Init (void)
{
   TMR3CN = 0x00;                      // Timer3 configured for 16-bit auto-
                                       // reload, low-byte interrupt disabled

   CKCON &= ~0x40;                     // Timer3 uses SYSCLK/12

   TMR3RL = -(SYSCLK/12/40);           // Timer3 configured to overflow after
   TMR3 = TMR3RL;                      // ~25ms (for SMBus low timeout detect)

   EIE1 |= 0x80;                       // Timer3 interrupt enable
   TMR3CN |= 0x04;                     // Start Timer3
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the Crossbar and GPIO ports.
//
// P0.0   digital   open-drain    SMBus SDA
// P0.1   digital   open-drain    SMBus SCL
//
// P3.3   digital   push-pull     LED
//
// all other port pins unused
//
// Note: If the SMBus is moved, the SCL and SDA sbit declarations must also
// be adjusted.
//
void PORT_Init (void)
{
   P0MDOUT = 0x00;                     // All P0 pins open-drain output

   P3MDOUT |= 0x08;                    // Make the LED (P3.3) a push-pull
                                       // output

   XBR0 = 0x04;                        // Enable SMBus pins
   XBR1 = 0x40;                        // Enable crossbar and weak pull-ups

   P0 = 0xFF;
}

//-----------------------------------------------------------------------------
// SMBus Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
//
// SMBus ISR state machine
// - Master only implementation - no slave or arbitration states defined
// - All incoming data is written starting at the global pointer <pSMB_DATA_IN>
// - All outgoing data is read from the global pointer <pSMB_DATA_OUT>
//
void SMBus_ISR (void) interrupt 7
{
   bit FAIL = 0;                       // Used by the ISR to flag failed
                                       // transfers

   static char i;                      // Used by the ISR to count the
                                       // number of data bytes sent or
                                       // received

   static bit SEND_START = 0;          // Send a start

   switch (SMB0CN & 0xF0)              // Status vector
   {
      // Master Transmitter/Receiver: START condition transmitted.
      case SMB_MTSTA:
         SMB0DAT = TARGET;             // Load address of the target slave
         SMB0DAT &= 0xFE;              // Clear the LSB of the address for the
                                       // R/W bit
         SMB0DAT |= SMB_RW;            // Load R/W bit
         STA = 0;                      // Manually clear START bit
         i = 0;                        // Reset data byte counter
         break;

      // Master Transmitter: Data byte (or Slave Address) transmitted
      case SMB_MTDB:
         if (ACK)                      // Slave Address or Data Byte
         {                             // Acknowledged?
            if (SEND_START)
            {
               STA = 1;
               SEND_START = 0;
               break;
            }
            if(SMB_SENDWORDADDR)       // Are we sending the word address?
            {
               SMB_SENDWORDADDR = 0;   // Clear flag
               SMB0DAT = WORD_ADDR;    // Send word address

               if (SMB_RANDOMREAD)
               {
                  SEND_START = 1;      // Send a START after the next ACK cycle
                  SMB_RW = READ;
               }

               break;
            }

            if (SMB_RW==WRITE)         // Is this transfer a WRITE?
            {

               if (i < SMB_DATA_LEN)   // Is there data to send?
               {
                  // send data byte
                  SMB0DAT = *pSMB_DATA_OUT;

                  // increment data out pointer
                  pSMB_DATA_OUT++;

                  // increment number of bytes sent
                  i++;
               }
               else
               {
                 STO = 1;              // Set STO to terminte transfer
                 SMB_BUSY = 0;         // Clear software busy flag
               }
            }
            else {}                    // If this transfer is a READ,
                                       // then take no action. Slave
                                       // address was transmitted. A
                                       // separate 'case' is defined
                                       // for data byte recieved.
         }
         else                          // If slave NACK,
         {
            if(SMB_ACKPOLL)
            {
               STA = 1;                // Restart transfer
            }
            else
            {
               FAIL = 1;               // Indicate failed transfer
            }                          // and handle at end of ISR
         }
         break;

      // Master Receiver: byte received
      case SMB_MRDB:
         if ( i < SMB_DATA_LEN )       // Is there any data remaining?
         {
            *pSMB_DATA_IN = SMB0DAT;   // Store received byte
            pSMB_DATA_IN++;            // Increment data in pointer
            i++;                       // Increment number of bytes received
            ACK = 1;                   // Set ACK bit (may be cleared later
                                       // in the code)

         }

         if (i == SMB_DATA_LEN)        // This is the last byte
         {
            SMB_BUSY = 0;              // Free SMBus interface
            ACK = 0;                   // Send NACK to indicate last byte
                                       // of this transfer
            STO = 1;                   // Send STOP to terminate transfer
         }

         break;

      default:
         FAIL = 1;                     // Indicate failed transfer
                                       // and handle at end of ISR
         break;
   }

   if (FAIL)                           // If the transfer failed,
   {
      SMB0CF &= ~0x80;                 // Reset communication
      SMB0CF |= 0x80;
      STA = 0;
      STO = 0;
      ACK = 0;

      SMB_BUSY = 0;                    // Free SMBus

      FAIL = 0;
   }

   SI = 0;                             // Clear interrupt flag
}

//-----------------------------------------------------------------------------
// Timer3 Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
//
// A Timer3 interrupt indicates an SMBus SCL low timeout.
// The SMBus is disabled and re-enabled if a timeout occurs.
//
void Timer3_ISR (void) interrupt 14
{
   SMB0CF &= ~0x80;                    // Disable SMBus
   SMB0CF |= 0x80;                     // Re-enable SMBus
   TMR3CN &= ~0x80;                    // Clear Timer3 interrupt-pending flag
   SMB_BUSY = 0;                       // Free bus
}

//-----------------------------------------------------------------------------
// Support Functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// EEPROM_ByteWrite ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) unsigned char addr - address to write in the EEPROM
//                        range is full range of character: 0 to 255
//
//   2) unsigned char dat - data to write to the address <addr> in the EEPROM
//                        range is full range of character: 0 to 255
//
// This function writes the value in <dat> to location <addr> in the EEPROM
// then polls the EEPROM until the write is complete.
//
void BMP180_ByteWrite(unsigned char addr, unsigned char dat)
{
   while (SMB_BUSY);                   // Wait for SMBus to be free.
   SMB_BUSY = 1;                       // Claim SMBus (set to busy)

   // Set SMBus ISR parameters
   TARGET = BMP180_ADDR;               // Set target slave address
   SMB_RW = WRITE;                     // Mark next transfer as a write
   SMB_SENDWORDADDR = 1;               // Send Word Address after Slave Address
   SMB_RANDOMREAD = 0;                 // Do not send a START signal after
                                       // the word address
   SMB_ACKPOLL = 1;                    // Enable Acknowledge Polling (The ISR
                                       // will automatically restart the
                                       // transfer if the slave does not
                                       // acknoledge its address.

   // Specify the Outgoing Data
   WORD_ADDR = addr;                   // Set the target address in the
                                       // EEPROM's internal memory space

   SMB_SINGLEBYTE_OUT = dat;           // Store <dat> (local variable) in a
                                       // global variable so the ISR can read
                                       // it after this function exits

   // The outgoing data pointer points to the <dat> variable
   pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;

   SMB_DATA_LEN = 1;                   // Specify to ISR that the next transfer
                                       // will contain one data byte

   // Initiate SMBus Transfer
   STA = 1;

}

//-----------------------------------------------------------------------------
// EEPROM_WriteArray ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) unsigned char dest_addr - beginning address to write to in the EEPROM
//                        range is full range of character: 0 to 255
//
//   2) unsigned char* src_addr - pointer to the array of data to be written
//                        range is full range of character: 0 to 255
//
//   3) unsigned char len - length of the array to be written to the EEPROM
//                        range is full range of character: 0 to 255
//
// Writes <len> data bytes to the EEPROM slave specified by the <EEPROM_ADDR>
// constant.
//
void BMP180_WriteArray(unsigned char dest_addr, unsigned char* src_addr,
                       unsigned char len)
{
   unsigned char i;
   unsigned char* pData = (unsigned char*) src_addr;

   for( i = 0; i < len; i++ ){
      BMP180_ByteWrite(dest_addr++, *pData++);
   }

}

//-----------------------------------------------------------------------------
// EEPROM_ByteRead ()
//-----------------------------------------------------------------------------
//
// Return Value :
//   1) unsigned char data - data read from address <addr> in the EEPROM
//                        range is full range of character: 0 to 255
//
// Parameters   :
//   1) unsigned char addr - address to read data from the EEPROM
//                        range is full range of character: 0 to 255
//
// This function returns a single byte from location <addr> in the EEPROM then
// polls the <SMB_BUSY> flag until the read is complete.
//
unsigned char BMP180_ByteRead(unsigned char addr)
{
   unsigned char retval;               // Holds the return value

   while (SMB_BUSY);                   // Wait for SMBus to be free.
   SMB_BUSY = 1;                       // Claim SMBus (set to busy)

   // Set SMBus ISR parameters
   TARGET = BMP180_ADDR;               // Set target slave address
   SMB_RW = WRITE;                     // A random read starts as a write
                                       // then changes to a read after
                                       // the repeated start is sent. The
                                       // ISR handles this switchover if
                                       // the <SMB_RANDOMREAD> bit is set.
   SMB_SENDWORDADDR = 1;               // Send Word Address after Slave Address
   SMB_RANDOMREAD = 1;                 // Send a START after the word address
   SMB_ACKPOLL = 1;                    // Enable Acknowledge Polling


   // Specify the Incoming Data
   WORD_ADDR = addr;                   // Set the target address in the
                                       // EEPROM's internal memory space

   pSMB_DATA_IN = &retval;             // The incoming data pointer points to
                                       // the <retval> variable.

   SMB_DATA_LEN = 1;                   // Specify to ISR that the next transfer
                                       // will contain one data byte

   // Initiate SMBus Transfer
   STA = 1;
   while(SMB_BUSY);                    // Wait until data is read

   return retval;

}

//-----------------------------------------------------------------------------
// EEPROM_ReadArray ()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) unsigned char* dest_addr - pointer to the array that will be filled
//                                 with the data from the EEPROM
//                        range is full range of character: 0 to 255
//
//   2) unsigned char src_addr - beginning address to read data from the EEPROM
//                        range is full range of character: 0 to 255
//
//   3) unsigned char len - length of the array to be read from the EEPROM
//                        range is full range of character: 0 to 255
//
// Reads up to 256 data bytes from the EEPROM slave specified by the
// <EEPROM_ADDR> constant.
//
void BMP180_ReadArray (unsigned char* dest_addr, unsigned char src_addr, unsigned char len)
{
   while (SMB_BUSY);                   // Wait for SMBus to be free.
   SMB_BUSY = 1;                       // Claim SMBus (set to busy)

   // Set SMBus ISR parameters
   TARGET = BMP180_ADDR;               // Set target slave address
   SMB_RW = WRITE;                     // A random read starts as a write
                                       // then changes to a read after
                                       // the repeated start is sent. The
                                       // ISR handles this switchover if
                                       // the <SMB_RANDOMREAD> bit is set.
   SMB_SENDWORDADDR = 1;               // Send Word Address after Slave Address
   SMB_RANDOMREAD = 1;                 // Send a START after the word address
   SMB_ACKPOLL = 1;                    // Enable Acknowledge Polling

   // Specify the Incoming Data
   WORD_ADDR = src_addr;               // Set the target address in the
                                       // EEPROM's internal memory space

   // Set the the incoming data pointer
   pSMB_DATA_IN = (unsigned char*) dest_addr;


   SMB_DATA_LEN = len;                 // Specify to ISR that the next transfer
                                       // will contain <len> data bytes


   // Initiate SMBus Transfer
   STA = 1;
   while(SMB_BUSY);                    // Wait until data is read

}


void test_pressure_main(void)
{
       
  long x1,x2,x3,b3,b5,b6,b7,press_reg,pressure,temp_reg,temp;
        //long x1,x2,x3,b3,b5,b6,b7,press_reg,pressure,temp_reg;
  unsigned long b4;
  //int ret,i;

  unsigned char ReadBuffer[10];
        //unsigned short ;
  //unsigned int ;
  char oss = 0;  //这个值在读气压时可以置进寄存器
        //GPIO_Configuration();
        ////USART_Configuration();
//  I2C_Configuration();
       
        ////printf("\r\n****************************************************************\r\n");
        //while (1)
        //{       
        ////printf("\r\n Read start \r\n");

  temp_calibration();
  
  //read uncompensated temperature
//  I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,0x2e);
  BMP180_ByteWrite(0xf4, 0x2e);
  Delay(5000);//delay 4.5ms
  //ret = I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,2);
//  I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,2);
  BMP180_ReadArray (ReadBuffer, 0xf6, 2);
  temp_reg = ReadBuffer[0] << 8 | ReadBuffer[1];
  ////printf("temp_reg %d \r\n",temp_reg);
  
  //calculate true temperature
  x1 = ((temp_reg - ac6) * ac5) >> 15;
  x2 = ((long) mc << 11) / (x1 + md);
  b5 = x1 + x2;
  temp = (b5 + 8) >> 4;
  bmp180_temp=temp;
  ////printf("x1:%d, x2:%d, b5:%d, temp(*0.1):%d \r\n",x1,x2,b5,temp);
  
  //read uncompensated pressure
  
  //////write 0x34+(oss<<6) into reg 0xF4, wait 4.5ms
//  I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,(0x34 +(oss<<6)));
   BMP180_ByteWrite(0xf4, (0x34 +(oss<<6)));

  
  //I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,(0x3e + oss<<6));
  //I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,0x3e);
  Delay(4500);//delay 4.5ms
  
  //read reg 0xF6 (MSB), 0xF7 (LSB), 0xF8 (XLSB)
//  I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,3);
  BMP180_ReadArray (ReadBuffer, 0xf6, 3);
  
  //I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,2);

  //////UP = (MSB<<16 + LSB<<8 + XLSB) >> (8-oss)
  press_reg = ((ReadBuffer[0] << 16)+(ReadBuffer[1] << 8)+ReadBuffer[2]) >> (8 - oss);

  //press_reg = ((ReadBuffer[0] << 16) | (ReadBuffer[1] << 8) | ReadBuffer[2]) >> (8 - oss);
  //press_reg = ReadBuffer[0] << 8 | ReadBuffer[1];
  //press_reg &= 0x00FFFF;
  ////printf("press_reg %d \r\n",press_reg);
  
  b6 = b5 - 4000;
  
  //X1 = (B2 * (B6 * B6 / 2^12)) / 2^11
  x1 = (b2 * ((b6 * b6) >> 12)) >> 11;
  
  //X2 = AC2 * B6 / 2^11
  x2 = (ac2 * b6) >> 11;
  
  x3 = x1 + x2;
  
  //B3 = ((AC1*4+X3) << oss + 2) / 4
  b3 = ((((long)ac1 * 4 + x3) << oss) + 2) / 4;
  //b3 = (((long)ac1 * 4 + x3) + 2) / 4;
  
  //X1 = AC3 * B6 / 2^13
  x1 = (ac3 * b6) >> 13;

  //X2 = (B1 * (B6 * B6 / 2^12 )) / 2^16
  x2 = (b1 * ((b6 * b6)>> 12)) >> 16;

  //X3 = ((X1 + X2) + 2) / 2^2
  x3 = ((x1 + x2 )+ 2) >> 2;

  //B4 = AC4 * (unsigend long)(X3 + 32768) / 2^15
  b4 = (ac4 * (unsigned long)(x3 + 32768)) >> 15;

  //B7 = ((unsigned long)UP - B3) * (50000 >> oss)
  b7 = ((unsigned long)press_reg - b3) * (50000 >> oss);
  //b7 = ((unsigned long)press_reg - b3) * (50000);
  
//  if(b7 < 0x80000000)
//  {
//    pressure = (b7 * 2) / b4;
//  }
//  else
//  {
//    pressure = (b7 / b4) * 2;
//  }

  //X1 = (p / 2^8 ) * (p / 2^8 )
  x1 = (pressure >> 8) * (pressure >> 8);

  //X1 = (X1 * 3038) / 2^16
  x1 = (x1 * 3038) >> 16;

  //X2 = (-7357 * p) / 2^16
  x2 = (-7357 * pressure) >> 16;

  //p = p + (X1 + X2 + 3791) / 2^4
  pressure = pressure + ((x1 + x2 + 3791) >> 4);

  bmp180_presure  = pressure;
//  bmp180_presure  /= 10;
  //for(i=0; i<200*3; i++)
  //I2C_delay(2000);
        //}
}


void temp_calibration(void)
{
  unsigned char buf[2];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xaa,buf, 2);
  BMP180_ReadArray (buf, 0xaa, 2);
  ac1 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xac,buf, 2);
  BMP180_ReadArray (buf, 0xac, 2);
  ac2 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xae,buf, 2);
  BMP180_ReadArray (buf, 0xae, 2);
  ac3 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xb0,buf, 2);
  BMP180_ReadArray (buf, 0xb0, 2);
  ac4 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xb2,buf, 2);
  BMP180_ReadArray (buf, 0xb2, 2);
  ac5 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xb4,buf, 2);
  BMP180_ReadArray (buf, 0xb4, 2);
  ac6 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xb6,buf, 2);
  BMP180_ReadArray (buf, 0xb6, 2);
  b1 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xb8,buf, 2);
  BMP180_ReadArray (buf, 0xb8, 2);
  b2 = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xba,buf, 2);
  BMP180_ReadArray (buf, 0xba, 2);
  mb = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xbc,buf, 2);
  BMP180_ReadArray (buf, 0xbc, 2);
  mc = buf[0] << 8 |buf[1];
//  I2C_Read(Open_I2C,ADDR_24LC02,0xbe,buf, 2);
  BMP180_ReadArray (buf, 0xbe, 2);
  md = buf[0] << 8 |buf[1];
  ////printf("ac1=%d,ac2=%d,ac3=%d,ac4=%d,ac5=%d,ac6=%d,b1=%d,b2=%d,mb=%d,mc=%d,md=%d\r\n",ac1,ac2,ac3,ac4,ac5,ac6,b1,b2,mb,mc,md);
}



/*******************************************************************************
* Function Name  : Delay
* Description    : Delay Time
* Input          : - nCount: Delay Time
* Output         : None
* Return         : None
* Attention                 : None
/*    http://www.rdbuy.cn  */
/*******************************************************************************/
void  Delay(unsigned int nCount)
{
        unsigned char i;
        for(i=25;i>0;i--)
       for(; nCount != 0; nCount--);
}


//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

出0入0汤圆

 楼主| 发表于 2014-10-6 17:12:41 | 显示全部楼层
本帖最后由 396221798 于 2014-10-6 17:16 编辑

// Value returned will be pressure in units of Pa.
// Value returned will be in units of 0.1 deg C


函数中显示计算出来的气压是单位是帕斯卡,温度值是0.1摄氏度

也就是说气压为:48068pa
温度值为:81.9℃


严重不对啊
用手捏住气压传感器温度有上升
气压没法测

大神们求关照

出0入8汤圆

发表于 2014-10-6 18:13:40 | 显示全部楼层
有可能是I2C时序不对

出0入0汤圆

发表于 2014-10-6 18:15:56 | 显示全部楼层
同意有可能是时序有问题

出0入0汤圆

 楼主| 发表于 2014-10-6 18:38:33 | 显示全部楼层
C8051f310自带的I2C 函数库啊

出0入0汤圆

 楼主| 发表于 2014-10-6 18:40:55 | 显示全部楼层
时序不对怎么解释呢?

出0入0汤圆

发表于 2014-10-6 19:17:02 | 显示全部楼层
如果数据读出来了,但是你的结果不对的话。

那你就要注意你数据的类型。因为要做乘除法,所以基本上都要用有符号数。

出0入0汤圆

发表于 2014-10-6 19:17:52 | 显示全部楼层
对了,还有就是传感器要放在无风的地方哈。。

温度和气压测得误差还是比较小的、。

出0入0汤圆

 楼主| 发表于 2014-10-6 20:01:06 | 显示全部楼层
好的好的

出0入0汤圆

 楼主| 发表于 2014-10-6 20:02:32 | 显示全部楼层
两个嘴巴笑110 发表于 2014-10-6 19:17
对了,还有就是传感器要放在无风的地方哈。。

温度和气压测得误差还是比较小的、。 ...

你用过这个传感器吗

出0入0汤圆

发表于 2014-10-6 22:17:17 | 显示全部楼层
396221798 发表于 2014-10-6 20:02
你用过这个传感器吗

用过,使用STM32驱动的。

出0入0汤圆

 楼主| 发表于 2014-10-7 01:00:42 来自手机 | 显示全部楼层
嘿嘿!供应商给的就是stm32的我移植到51

出0入0汤圆

 楼主| 发表于 2014-10-7 08:51:48 | 显示全部楼层
两个嘴巴笑110 发表于 2014-10-6 22:17
用过,使用STM32驱动的。

照你说的把变量改成有符号也一样啊

出0入0汤圆

发表于 2014-10-7 11:54:03 | 显示全部楼层
396221798 发表于 2014-10-7 08:51
照你说的把变量改成有符号也一样啊

你要看资料,有一部分的是有符号数,有一部分是无符号数,但是运算的时候要用浮点型。

出0入0汤圆

 楼主| 发表于 2014-10-7 14:35:32 | 显示全部楼层
两个嘴巴笑110 发表于 2014-10-7 11:54
你要看资料,有一部分的是有符号数,有一部分是无符号数,但是运算的时候要用浮点型。 ...

变量声明都一样的

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2014-10-7 16:38:31 | 显示全部楼层
396221798 发表于 2014-10-7 14:35
变量声明都一样的

我的部分程序。
我给你看看。。
如果你的时序没有大问题的话,应该就不会出现你说的问题,可能你哪里太粗心。

细细的检查一下哈。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2014-10-8 14:59:51 | 显示全部楼层
两个嘴巴笑110 发表于 2014-10-7 16:38
我的部分程序。
我给你看看。。
如果你的时序没有大问题的话,应该就不会出现你说的问题,可能你哪里太粗 ...

大哥啊还是不对呀
温度81°

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2014-10-8 16:26:47 来自手机 | 显示全部楼层
那这样的话,我也不是很清楚了。我也就只是玩了玩,没有深入的研究!

出0入0汤圆

 楼主| 发表于 2014-10-8 17:37:49 | 显示全部楼层
两个嘴巴笑110 发表于 2014-10-8 16:26
那这样的话,我也不是很清楚了。我也就只是玩了玩,没有深入的研究!

多谢你一直关注

出0入0汤圆

发表于 2014-10-8 23:26:07 | 显示全部楼层
又是一个代码党,有谁会耐心看完这程序啊……

出0入0汤圆

 楼主| 发表于 2014-10-9 11:19:16 | 显示全部楼层
这破传感器搞不懂呀!

出0入0汤圆

 楼主| 发表于 2014-10-10 10:44:49 | 显示全部楼层
两个嘴巴笑110 发表于 2014-10-7 16:38
我的部分程序。
我给你看看。。
如果你的时序没有大问题的话,应该就不会出现你说的问题,可能你哪里太粗 ...

报告一下我查出来是什么问题了:

我调试了这么多天原来我错误地把SMbus总线和I2C认为是一种通信方式了所以传感器数据读出错误的值。

原因是我以为C8051单片机的I2C通信就是SMbus,
其实8051是没有I2C通信的

今天刚检查出来的错误
下一步就是自己创建一个I2C通信协议的函数

请各位大神不要像我一样愚蠢啊犯如此低级错误!

出0入0汤圆

发表于 2014-10-10 17:45:48 | 显示全部楼层
396221798 发表于 2014-10-10 10:44
报告一下我查出来是什么问题了:

我调试了这么多天原来我错误地把SMbus总线和I2C认为是一种通信方式了所 ...

多谢分享。

出0入0汤圆

 楼主| 发表于 2014-10-14 14:54:38 | 显示全部楼层

哈哈哈!!!

出0入8汤圆

发表于 2014-10-14 14:57:49 | 显示全部楼层
你手上的是啥 电机?

出0入0汤圆

 楼主| 发表于 2014-10-15 15:07:45 | 显示全部楼层
justdomyself 发表于 2014-10-14 14:57
你手上的是啥 电机?

金属合金记忆舵机

出0入0汤圆

发表于 2014-11-2 22:40:00 | 显示全部楼层
差点看吐了~~~
第一步,先读取器件ID,读取正确后说明通讯正常,然后再干别的

出0入0汤圆

发表于 2014-11-2 22:42:59 | 显示全部楼层
差点看吐了~~~
第一步,先读取器件ID,读取正确后说明通讯正常,然后再干别的

出0入0汤圆

发表于 2014-11-6 10:54:21 | 显示全部楼层
楼主哪里买的传感器.可否把32的驱动共享一下。

出0入0汤圆

 楼主| 发表于 2014-11-7 16:42:02 | 显示全部楼层
woshixiaozhou 发表于 2014-11-6 10:54
楼主哪里买的传感器.可否把32的驱动共享一下。

分享一下下吧!实在不好意思!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2014-11-10 10:13:04 | 显示全部楼层
谢谢楼主分享!

出0入0汤圆

发表于 2015-2-2 02:03:33 | 显示全部楼层
谢谢楼主的分享,正在研究这个BMP180,用汇编读数据没问题,校正数据搞死人了。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-19 20:14

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表