• AVR Freaks

Hot!PIC16F887 BME280 sensor API

Author
SpikeHawk
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2020/10/06 15:57:16
  • Location: Thessaloniki, Greece
  • Status: offline
2021/01/23 03:58:22 (permalink)
0

PIC16F887 BME280 sensor API

Hi everyone,
 
 I would like to ask regarding the user_i2c_read and user_i2c_write functions as written in the BME280 Sensor API. In the datasheet is recommended to start a burst read from address 0xF7-FE so Do I have to implement an I2C read inside the function from starting address as the 0xF7?. Secondly, regarding the I2C write as per datasheet the write registers are config(0xF5), ctrl_meas(0xF4) and the ctrl_hum(0xF2) so in this case which is the sequence of writing to the particular registers?
 
Regards,
 Andrew Georgousis.

Attached Image(s)

#1

3 Replies Related Threads

    Kabak
    Super Member
    • Total Posts : 250
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC16F887 BME280 sensor API 2021/01/23 14:06:13 (permalink)
    +1 (1)
    Do I have to implement an I2C read inside the function from starting address as the 0xF7?
    Yes. Better read 6 bytes in a row
    Secondly, regarding the I2C write as per datasheet the write registers are config(0xF5), ctrl_meas(0xF4) and the ctrl_hum(0xF2) so in this case which is the sequence of writing to the particular registers?
    It seems to me - does not matter.  I use only : ctrl_meas(0xF4), config(0xF5)
    #2
    SpikeHawk
    New Member
    • Total Posts : 11
    • Reward points : 0
    • Joined: 2020/10/06 15:57:16
    • Location: Thessaloniki, Greece
    • Status: offline
    Re: PIC16F887 BME280 sensor API 2021/01/23 15:24:26 (permalink)
    0
    Hi Kabak,
    example.txt
      do you have any application example for the implementation of the i2c write function for the BME280. Actually, I am confused on how to implement it. In the attached file you can find an example that I've created on how to implement the function.
     
    Regards,
     Andrew.
    #3
    Kabak
    Super Member
    • Total Posts : 250
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC16F887 BME280 sensor API 2021/01/24 01:34:25 (permalink)
    +1 (1)
    SpikeHawk
    Hi Kabak,
    example.txt
      do you have any application example for the implementation of the i2c write function for the BME280. Actually, I am confused on how to implement it. In the attached file you can find an example that I've created on how to implement the function.
     
    Regards,
     Andrew.


    I cant provide whole project ( The lib is not stand alone) , but i can provide all functions i used to read and write BMP280 registers.
     
    functions for BMP280 , not BME280.     BMP280 has no humidity sensor.  actually you should add storage for humidity, and read 8 bytes in the row
     
    its for pic18f26k22 SPI module number 1

     
    #define I2C_1_START SSP1CON2bits.SEN = 1; while ( SSP1CON2bits.SEN );
    #define I2C_2_START SSP2CON2bits.SEN = 1; while ( SSP2CON2bits.SEN );

    #define I2C_1_STOP SSP1CON2bits.PEN = 1;
    #define I2C_2_STOP SSP2CON2bits.PEN = 1;

    #define I2C_1_READ SSP1CON2bits.RCEN = 1; while ( SSP1CON2bits.RCEN );
    #define I2C_2_READ SSP2CON2bits.RCEN = 1; while ( SSP2CON2bits.RCEN );

    #define I2C_1_ACK SSP1CON2bits.ACKDT = 0; SSP1CON2bits.ACKEN = 1; while ( SSP1CON2bits.ACKEN );
    #define I2C_2_ACK SSP2CON2bits.ACKDT = 0; SSP2CON2bits.ACKEN = 1; while ( SSP2CON2bits.ACKEN );

    #define I2C_1_NOACK SSP1CON2bits.ACKDT = 1; SSP1CON2bits.ACKEN = 1; while ( SSP1CON2bits.ACKEN );
    #define I2C_2_NOACK SSP2CON2bits.ACKDT = 1; SSP2CON2bits.ACKEN = 1; while ( SSP2CON2bits.ACKEN );

    #define I2C_1_IDLE while ( ( SSP1CON2 & 0x1F ) | ( SSP1STATbits.R_W ) );
    #define I2C_2_IDLE while ( ( SSP2CON2 & 0x1F ) | ( SSP2STATbits.R_W ) );

    #define I2C_1_WRITE_DONE while ( SSP1STATbits.R_W );
    #define I2C_2_WRITE_DONE while ( SSP2STATbits.R_W );


    #define Delay_4_FF uchar i = 4; do { Delay_Byte( 0xff ); --i; } while ( i != 0 );

    typedef uint24_t _3bytes;
    typedef uint16_t word;
    typedef int16_t  sword;
     
     
     
     
     
     
     

    typedef struct _BMP280_Data
    {
        union _Pressure
        {
         _3bytes Pressure;
         uchar Pressure_Bytes[3];
        } Pressure;

        union _Temperature
        {
         _3bytes Temperature;
         char Temperature_Bytes[3];
        } Temperature;
        
        
    } BMP280_Data;


    typedef struct _BMP280_Const_Data
    {
         word dig_T1;
        sword dig_T2;
        sword dig_T3;
        
         word dig_P1;
        sword dig_P2;
        sword dig_P3;
        sword dig_P4;
        sword dig_P5;
        sword dig_P6;
        sword dig_P7;
        sword dig_P8;
        sword dig_P9;
    } BMP280_Const_Data;


    void Delay_Byte ( uchar Amount )
    {
       for ( uchar A = 0; A < Amount; ++A )
       {

       }
    }


    void Write_I2C_Device_Reg_OneByte ( uchar DeviceAddress, uchar Reg_Address, uchar Value )
    {
        
        I2C_1_IDLE
        I2C_1_START // Auto checking BUS for IDLE state

        SSP1BUF = DeviceAddress;
        I2C_1_WRITE_DONE
        
        SSP1BUF = Reg_Address;
        I2C_1_WRITE_DONE
     
        SSP1BUF = Value;
        I2C_1_WRITE_DONE

        I2C_1_STOP
    }


    void Read_I2C_Device_Reg ( uchar DeviceAddress, uchar Reg_Address, uchar*Destination, uchar Amount )
    {
        uchar byte;
        
        I2C_1_IDLE
        I2C_1_START
        
        byte = DeviceAddress;
        SSP1BUF = byte; // Write sequence begin
        I2C_1_WRITE_DONE
        
        SSP1BUF = Reg_Address;
        I2C_1_WRITE_DONE

        I2C_1_START

        byte = 0x01 | byte;
        SSP1BUF = byte; // Read sequence begin
        I2C_1_WRITE_DONE
                
        for (uchar i = 0; i < Amount; ++i )
        {
            I2C_1_READ
            
           *Destination = SSP1BUF;
          ++Destination;

        if ( (i + 1) == Amount )
        {
            I2C_1_NOACK // If last byte to read we should generate NOACK
        }
        else
        {
            I2C_1_ACK
        }
        }
        
        I2C_1_STOP
    }



    void BMP280_Init ( BMP280_Const_Data* Data )
    {

        Read_I2C_Device_Reg ( 0xEC, 0x88, Data, 24 );
       
        // PAUSE
        Delay_4_FF
        // Force mode
        Write_I2C_Device_Reg_OneByte ( 0xEC, 0xF4, 0b11111110 );
        Write_I2C_Device_Reg_OneByte ( 0xEC, 0xF5, 0b11111110 );

    }


    void BMP280_Get_Data ( BMP280_Data* Data, BMP280_Const_Data* BMP280_Const )
    {
        // Read once and stop readings for power save
        // Force mode
        Write_I2C_Device_Reg_OneByte ( 0xEC, 0xF4, 0b11111110 );
         
        Read_I2C_Device_Reg ( 0xEC, 0xF7, Data, 6 );

    }

    post edited by Kabak - 2021/01/24 04:46:22
    #4
    Jump to:
    © 2021 APG vNext Commercial Version 4.5