BPM180_STM32F103系列驅動代碼,加入了移動平均值濾波算法

barometer.c文件中的代碼

#include "barometer.h"
#include "usart.h"    
#include "delay.h"

#define    BMP085_SlaveAddress   0xee      //¶¨ÒåÆ÷¼þÔÚIIC×ÜÏßÖеĴӵØÖ·                               

#define OSS 0    // Oversampling Setting (note: code is not set up to use other OSS values)


#define   uchar unsigned char
#define   uint unsigned int    

void I2c_GPIO_Init()
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE );
    
    GPIO_InitStructure.GPIO_Pin =  I2C_Pin_SCL | I2C_Pin_SDA;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(ANO_GPIO_I2C, &GPIO_InitStructure);        
}                               
     
//-----------------------------------
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;

/*******************************/
//void Delay5us()
//{
//    u16 i=0;
//    u8 time=5;
//    while(time--)
//    {
//        i=10;
//        while(i--)
//            ;
//    }

//}

//void Delay_ms(float time)
//{
//     u16 i=0;
// time=time*1000;
//    while(time--)
//    {
//        i=10;
//        while(i--)
//            ;
//    }
//}
void Delay5us()
{
    delay_us(5);

}

void Delay_ms(float time)
{

    delay_ms(time);
}


void BMP085_Start()
{
    SDA_H;                   
    SCL_H;                   
    Delay5us();               
    SDA_L ;                  
    Delay5us();                 
    SCL_L;                   
}

///**************************************
//????
//**************************************/
void BMP085_Stop()
{
    SDA_L;                    //?????
    SCL_H;                    //?????
    Delay5us();                 //??
    SDA_H;                    //?????
    Delay5us();                 //??
}

///**************************************
//??????
//????:ack (0:ACK 1:NAK)
//**************************************/
void I2c_Soft_Ask()
{
    
    SDA_L;
    Delay5us();
    SCL_H;
    Delay5us();
    SCL_L;
    Delay5us();
}

void I2c_Soft_NoAsk()
{

    SDA_H;
    Delay5us();
    SCL_H;
    Delay5us();
    SCL_L;
    Delay5us();
}
void BMP085_SendACK(u8 ack)
{
    if(ack==0)
    {
        I2c_Soft_Ask();
    }
    else
    {
        I2c_Soft_NoAsk();
    }
}
///**************************************
//??????
//**************************************/
u8 BMP085_RecvACK()
{
    u8 ErrTime = 0;
    SCL_H;
    Delay5us();
    while(SDA_read)
    {
            ErrTime++;
            if(ErrTime>50)
            {
                BMP085_Stop();
                return 1;
            }
    }
    SCL_L;
    Delay5us();
    return 0;
}

/**************************************
?IIC??????????
**************************************/
void BMP085_SendByte(u8 SendByte)
{
    u8 i=8;
    while(i--)
    {
      SCL_L;
      Delay5us();
      if(SendByte&0x80)
      {
        SDA_H;  
      }
      else
      {
        SDA_L;
      }        
      SendByte<<=1;
      Delay5us();
      SCL_H;
      Delay5us();
      SCL_L;
      Delay5us();
    }
    BMP085_RecvACK();
    
}

/**************************************
?IIC??????????
**************************************/
unsigned char BMP085_RecvByte()
{
    u8 i;
    u8 dat = 0;

    SDA_H;                   //??????,??????,
    for (i=0; i<8; i++)         //8????
    {
        dat <<= 1;
        SCL_H;                //?????
        Delay5us();             //??
        if(SDA_read)
        {
          dat|=0x01;
        }
        SCL_L;                //?????
        Delay5us();             //??
    }
    return dat;
}

void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
    BMP085_Start();                  //ÆðʼÐźÅ
    BMP085_SendByte(SlaveAddress);   //·¢ËÍÉ豸µØÖ·+дÐźÅ
    BMP085_SendByte(REG_Address);    //ÄÚ²¿¼Ä´æÆ÷µØÖ·
    BMP085_SendByte(REG_data);       //ÄÚ²¿¼Ä´æÆ÷Êý¾Ý
    BMP085_Stop();                   //·¢ËÍÍ£Ö¹ÐźÅ
}

uchar Single_Read(uchar REG_Address)
{  uchar REG_data;
    BMP085_Start();                          //ÆðʼÐźÅ
    BMP085_SendByte(BMP085_SlaveAddress);           //·¢ËÍÉ豸µØÖ·+дÐźÅ
    BMP085_SendByte(REG_Address);            //·¢ËÍ´æ´¢µ¥ÔªµØÖ·    
    BMP085_Start();                          //ÆðʼÐźÅ
    BMP085_SendByte(BMP085_SlaveAddress+1);         //·¢ËÍÉ豸µØÖ·+¶ÁÐźÅ
    REG_data=BMP085_RecvByte();              //¶Á³ö¼Ä´æÆ÷Êý¾Ý
    BMP085_SendACK(1);   
    BMP085_Stop();                           //Í£Ö¹ÐźÅ
    return REG_data;
}
short Multiple_read(uchar ST_Address)
{   
    uchar msb, lsb;
    short _data;
    BMP085_Start();                          //ÆðʼÐźÅ
    BMP085_SendByte(BMP085_SlaveAddress);    //·¢ËÍÉ豸µØÖ·+дÐźÅ
    BMP085_SendByte(ST_Address);             //·¢ËÍ´æ´¢µ¥ÔªµØÖ·
    BMP085_Start();                          //ÆðʼÐźÅ
    BMP085_SendByte(BMP085_SlaveAddress+1);         //·¢ËÍÉ豸µØÖ·+¶ÁÐźÅ

    msb = BMP085_RecvByte();                 //BUF[0]´æ´¢
    BMP085_SendACK(0);                       //»ØÓ¦ACK
    lsb = BMP085_RecvByte();     
    BMP085_SendACK(1);                       //×îºóÒ»¸öÊý¾ÝÐèÒª»ØNOACK

    BMP085_Stop();                           //Í£Ö¹ÐźÅ
    Delay_ms(5);
    _data = msb << 8;
    _data |= lsb;    
    return _data;
}
//********************************************************************
long bmp085ReadTemp(void)
{

    BMP085_Start();                  //ÆðʼÐźÅ
    BMP085_SendByte(BMP085_SlaveAddress);   //·¢ËÍÉ豸µØÖ·+дÐźÅ
    BMP085_SendByte(0xF4);              // write register address
    BMP085_SendByte(0x2E);           // write register data for temp
    BMP085_Stop();                   //·¢ËÍÍ£Ö¹ÐźÅ
      Delay_ms(4.5);    // max time is 4.5ms
    
    return (long) Multiple_read(0xF6);
}
//*************************************************************
long bmp085ReadPressure(void)
{
    long pressure = 0;

    BMP085_Start();                   //ÆðʼÐźÅ
    BMP085_SendByte(BMP085_SlaveAddress);   //·¢ËÍÉ豸µØÖ·+дÐźÅ
    BMP085_SendByte(0xF4);              // write register address
    BMP085_SendByte(0x34);             // write register data for pressure
    BMP085_Stop();                    //·¢ËÍÍ£Ö¹ÐźÅ
      Delay_ms(25.5);                          // max time is 4.5ms
    
    pressure = Multiple_read(0xF6);
    pressure &= 0x0000FFFF;
    
    return pressure;    
    //return (long) bmp085ReadShort(0xF6);
}
//³õʼ»¯BMP085£¬¸ù¾ÝÐèÒªÇë²Î¿¼pdf½øÐÐÐÞ¸Ä**************
void Init_BMP085()
{
    ac1 = Multiple_read(0xAA);
    ac2 = Multiple_read(0xAC);
    ac3 = Multiple_read(0xAE);
    ac4 = Multiple_read(0xB0);
    ac5 = Multiple_read(0xB2);
    ac6 = Multiple_read(0xB4);
    b1 =  Multiple_read(0xB6);
    b2 =  Multiple_read(0xB8);
    mb =  Multiple_read(0xBA);
    mc =  Multiple_read(0xBC);
    md =  Multiple_read(0xBE);
    
    printf("AC1:%d\r\n",ac1);
    printf("AC2:%d\r\n",ac2);
    printf("AC3:%d\r\n",ac3);
    
    printf("AC4:%d\r\n",ac4);
    printf("AC5:%d\r\n",ac5);
    printf("AC6:%d\r\n",ac6);
    
    printf("b1:%d\r\n",b1);
    printf("b2:%d\r\n",b2);
    
    printf("mb:%d\r\n",mb);
    printf("mc:%d\r\n",mc);
    printf("md:%d\r\n",md);
}

void barometerInit()
{
    I2c_GPIO_Init();
    Init_BMP085();
}

    

float arry[20];
 float mathArry(float * Arrydata,u8 num);
u8 i=0;
float data_filter(long measure_pressure)
{
    u8 j=0,k;
    float dataFlaterResult;
    while(i<=19)
    {
        arry[i]=measure_pressure;
        i++;
        printf("i%d\r\n",i);
    }
    //printf("i%d\r\n",i);
    for (;j<=18;j++)
    {
        arry[j]=arry[j+1];
    //    printf("j%d\r\n",j);
    
    }
    //printf("j%d\r\n",j);
    arry[j]=measure_pressure;
    j=0;
    dataFlaterResult=mathArry(arry,20);
    return dataFlaterResult;

}
float mathArry(float * Arrydata,u8 num)
{
    u8 i=0;
    float caculate=0.0;
    for (i=0;i<num;i++)
    {
        caculate=caculate+ *(Arrydata+i)/num;
    }
    return caculate;

}
long  temperature;
long  pressure;
float filterPressure;
void bmp085ConvertMeasure()
{
    long ut;
    long up;
    long x1, x2, b5, b6, x3, b3, p;
    unsigned long b4, b7;


    
    ut = bmp085ReadTemp();
    up = bmp085ReadPressure();  
    //printf("ut:%d\r\n",ut);
//    printf("up:%d\r\n",up);
    
    x1 = ((long)ut - ac6) * ac5 >> 15;
    x2 = ((long) mc << 11) / (x1 + md);
    b5 = x1 + x2;
    temperature = (b5 + 8) >> 4;
    
    b6 = b5 - 4000;
    x1 = (b2 * (b6 * b6 >> 12)) >> 11;
    x2 = ac2 * b6 >> 11;
    x3 = x1 + x2;
    b3 = (((long)ac1 * 4 + x3) + 2)/4;
    x1 = ac3 * b6 >> 13;
    x2 = (b1 * (b6 * b6 >> 12)) >> 16;
    x3 = ((x1 + x2) + 2) >> 2;
    b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
    b7 = ((unsigned long) up - b3) * (50000 >> OSS);
    if( b7 < 0x80000000)
         p = (b7 * 2) / b4 ;
           else  
            p = (b7 / b4) * 2;
    x1 = (p >> 8) * (p >> 8);
    x1 = (x1 * 3038) >> 16;
    x2 = (-7357 * p) >> 16;
    pressure = p + ((x1 + x2 + 3791) >> 4);
                    
    filterPressure=data_filter(pressure);

    //printf("**********************************************pressure:%d\r\n",pressure);
    printf("%f\r\n",filterPressure);
 }

barometer.h文件中的代碼

#ifndef __BAROMETER_H
#define __BAROMETER_H    
#include "sys.h"

#define SCL_H                    GPIO_SetBits(ANO_GPIO_I2C, GPIO_Pin_6)        /* SCL = 1 */
#define SCL_L                     GPIO_ResetBits(ANO_GPIO_I2C, GPIO_Pin_6)        /* SCL = 0 */
#define SDA_H                    GPIO_SetBits(ANO_GPIO_I2C, GPIO_Pin_7)        /* SDA = 1 */
#define SDA_L                    GPIO_ResetBits(ANO_GPIO_I2C, GPIO_Pin_7)        /* SDA = 0 */
#define SDA_read                      GPIO_ReadInputDataBit(ANO_GPIO_I2C, GPIO_Pin_7)    /* ?SDA???? */


/***************I2C GPIO??******************/
#define ANO_GPIO_I2C    GPIOB
#define I2C_Pin_SCL        GPIO_Pin_6
#define I2C_Pin_SDA        GPIO_Pin_7
#define ANO_RCC_I2C        RCC_APB2Periph_GPIOB
/*********************************************/


void barometerInit(void);
void bmp085ConvertMeasure(void);
#endif

main函數中的代碼

    barometerInit();
    while(1)
    {
        bmp085ConvertMeasure();
        delay_ms(50);
    }   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章