当前位置:   article > 正文

SPL06_001气压计驱动例程_spl06-001 程序

spl06-001 程序

spl06.h

#ifndef __SPL06_H
#define __SPL06_H

#include "stm32f10x.h"

/**< \brief 寄存器地址 */
#define SP06_PSR_B2     0x00
#define SP06_PSR_B1     0x01
#define SP06_PSR_B0     0x02
#define SP06_TMP_B2     0x03
#define SP06_TMP_B1     0x04
#define SP06_TMP_B0     0x05

#define SP06_PSR_CFG    0x06
#define SP06_TMP_CFG    0x07
#define SP06_MEAS_CFG   0x08

#define SP06_CFG_REG    0x09
#define SP06_INT_STS    0x0A
#define SP06_FIFO_STS   0x0B

#define SP06_RESET      0x0C
#define SP06_ID         0x0D

#define SP06_COEF       0x10
#define SP06_COEF_SRCE  0x28

/**< \brief 气压测量速率(sample/sec),Background 模式使用 */
#define PM_RATE_1           (0<<4)      /* 1 measurements pr. sec.   */
#define PM_RATE_2           (1<<4)      /* 2 measurements pr. sec.   */
#define PM_RATE_4           (2<<4)      /* 4 measurements pr. sec.   */      
#define PM_RATE_8           (3<<4)      /* 8 measurements pr. sec.   */
#define PM_RATE_16          (4<<4)      /* 16 measurements pr. sec.  */
#define PM_RATE_32          (5<<4)      /* 32 measurements pr. sec.  */
#define PM_RATE_64          (6<<4)      /* 64 measurements pr. sec.  */
#define PM_RATE_128         (7<<4)      /* 128 measurements pr. sec. */

/**< \brief 气压重采样速率(times),Background 模式使用 */
#define PM_PRC_1            0           /* Sigle        kP=524288,  3.6ms   */
#define PM_PRC_2            1           /* 2 times      kP=1572864, 5.2ms   */
#define PM_PRC_4            2           /* 4 times      kP=3670016, 8.4ms   */
#define PM_PRC_8            3           /* 8 times      kP=7864320, 14.8ms  */
#define PM_PRC_16           4           /* 16 times     kP=253952,  27.6ms  */
#define PM_PRC_32           5           /* 32 times     kP=516096,  53.2ms  */
#define PM_PRC_64           6           /* 64 times     kP=1040384, 104.4ms */
#define PM_PRC_128          7           /* 128 times    kP=2088960, 206.8ms */

/**< \brief 温度测量速率(sample/sec),Background 模式使用 */
#define TMP_RATE_1          (0<<4)      /* 1 measurements pr. sec.   */
#define TMP_RATE_2          (1<<4)      /* 2 measurements pr. sec.   */
#define TMP_RATE_4          (2<<4)      /* 4 measurements pr. sec.   */        
#define TMP_RATE_8          (3<<4)      /* 8 measurements pr. sec.   */
#define TMP_RATE_16         (4<<4)      /* 16 measurements pr. sec.  */
#define TMP_RATE_32         (5<<4)      /* 32 measurements pr. sec.  */
#define TMP_RATE_64         (6<<4)      /* 64 measurements pr. sec.  */
#define TMP_RATE_128        (7<<4)      /* 128 measurements pr. sec. */

/**< \brief 温度重采样速率(times),Background 模式使用 */
#define TMP_PRC_1           0           /* Sigle     */
#define TMP_PRC_2           1           /* 2 times   */
#define TMP_PRC_4           2           /* 4 times   */
#define TMP_PRC_8           3           /* 8 times   */
#define TMP_PRC_16          4           /* 16 times  */
#define TMP_PRC_32          5           /* 32 times  */
#define TMP_PRC_64          6           /* 64 times  */
#define TMP_PRC_128         7           /* 128 times */

/**< \brief SPL06_MEAS_CFG */
#define MEAS_COEF_RDY                   0x80
#define MEAS_SENSOR_RDY                 0x40    /* 传感器初始化完成 */
#define MEAS_TMP_RDY                    0x20    /* 有新的温度数据   */
#define MEAS_PRS_RDY                    0x10    /* 有新的气压数据   */

#define MEAS_CTRL_Standby               0x00    /* 空闲模式         */
#define MEAS_CTRL_PressMeasure          0x01    /* 单次气压测量     */
#define MEAS_CTRL_TempMeasure           0x02    /* 单次温度测量     */
#define MEAS_CTRL_ContinuousPress       0x05    /* 连续气压测量     */
#define MEAS_CTRL_ContinuousTemp        0x06    /* 连续温度测量     */
#define MEAS_CTRL_ContinuousPressTemp   0x07    /* 连续气压温度测量 */

/**< \brief FIFO_STS */
#define SPL06_FIFO_FULL                 0x02
#define SPL06_FIFO_EMPTY                0x01

/**< \brief INT_STS */
#define SPL06_INT_FIFO_FULL             0x04
#define SPL06_INT_TMP                   0x02
#define SPL06_INT_PRS                   0x01

/**< \brief CFG_REG */
#define SPL06_CFG_T_SHIFT               0x08    /* oversampling times>8时必须使用 */
#define SPL06_CFG_P_SHIFT               0x04    /* oversampling times>8时必须使用 */

typedef int (*pfn_i2c_read) (uint8_t slave_addr,
                             uint8_t reg_addr,
                             uint32_t nbytes,
                             uint8_t *p_data);
                                        
typedef int (*pfn_i2c_write) (uint8_t slave_addr,
                              uint8_t reg_addr,
                              uint32_t nbytes,
                              uint8_t *p_data);

#define SPL06_ADDR  0x76

typedef struct
{
    int32_t Praw;   /* 气压原始值 */
    int32_t Traw;   /* 温度原始值 */
    float   Pcomp;  /* 计算后的气压值 */
    float   Tcomp;  /* 计算后的温度值 */
} spl06_result_t;

int spl06_init(pfn_i2c_read read, pfn_i2c_write write);
void spl06_get_raw_data(spl06_result_t *p_res);
void spl06_get_result(spl06_result_t *p_res);

#endif

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

spl06.c

#include "spl06.h"
#include "bsp_systick.h"	/* 提供延时函数 */

static pfn_i2c_read  i2c_read;
static pfn_i2c_write i2c_write;

static float _kT, _kP;
static int16_t _c0, _c1, _c01,_c11, _c20, _c21, _c30;
static int32_t _c10, _c00;

static void _spl06_pressure_config(uint8_t rate, uint8_t oversampling)
{
    uint8_t regval = 0;

    switch(oversampling)
    {
        case PM_PRC_1:   _kP = 524288;  break;
        case PM_PRC_2:   _kP = 1572864; break;
        case PM_PRC_4:   _kP = 3670016; break;
        case PM_PRC_8:   _kP = 7864320; break;
        case PM_PRC_16:  _kP = 253952;  break;
        case PM_PRC_32:  _kP = 516096;  break;
        case PM_PRC_64:  _kP = 1040384; break;
        case PM_PRC_128: _kP = 2088960; break;
        default: break;
    }

    regval = rate | oversampling;
    i2c_write(SPL06_ADDR, SP06_PSR_CFG, 1, &regval);

    if(oversampling > PM_PRC_8)
    {
        i2c_read(SPL06_ADDR, SP06_CFG_REG, 1, &regval);
        regval |= SPL06_CFG_P_SHIFT;
        i2c_write(SPL06_ADDR, SP06_CFG_REG, 1, &regval);
    }
}

static void _spl06_temperature_config(uint8_t rate, uint8_t oversampling)
{
    uint8_t regval = 0;
    
    switch(oversampling)
    {
        case TMP_PRC_1:   _kT = 524288;  break;
        case TMP_PRC_2:   _kT = 1572864; break;
        case TMP_PRC_4:   _kT = 3670016; break;
        case TMP_PRC_8:   _kT = 7864320; break;
        case TMP_PRC_16:  _kT = 253952;  break;
        case TMP_PRC_32:  _kT = 516096;  break;
        case TMP_PRC_64:  _kT = 1040384; break;
        case TMP_PRC_128: _kT = 2088960; break;
        default: break;
    }
    
    regval = rate | oversampling | 0x80;
    i2c_write(SPL06_ADDR, SP06_TMP_CFG, 1, &regval);    /* External sensor */
    
    if(oversampling > PM_PRC_8)
    {
        i2c_read(SPL06_ADDR, SP06_CFG_REG, 1, &regval);
        regval |= SPL06_CFG_T_SHIFT;
        i2c_write(SPL06_ADDR, SP06_CFG_REG, 1, &regval);
    }
}

int spl06_init(pfn_i2c_read read, pfn_i2c_write write)
{
    uint8_t regval = 0;
    uint8_t coef[18] = {0};
    
    /* 移植I2C通信接口函数 */
    if ((read == 0) || (write == 0)) {
        return -1;
    }
    i2c_read  = read;
    i2c_write = write;
    
    regval = 0x89;
    i2c_write(SPL06_ADDR, SP06_RESET, 1, &regval);
    
    i2c_read(SPL06_ADDR, SP06_ID, 1, &regval);
    if (0x10 != regval)
    {
        return -1;
    }
    
    delay_ms(50);
    
    i2c_read(SPL06_ADDR, SP06_COEF, 18, coef);
    _c0 = ((int16_t)coef[0] << 4) | ((coef[1] & 0xF0) >> 4);
    _c0 = (_c0 & 0x0800) ? (0xF000 | _c0) : _c0;
    _c1 = ((int16_t)(coef[1] & 0x0F) << 8) | coef[2];
    _c1 = (_c1 & 0x0800) ? (0xF000 | _c1) : _c1;
    _c00 = ((int32_t)coef[3] << 12) | ((uint32_t)coef[4] << 4 ) | (coef[5] >> 4);
    _c00 = (_c00 & 0x080000) ? (0xFFF00000 | _c00) : _c00;
    _c10 = ((int32_t)(coef[5] & 0x0F) << 16) | ((uint32_t)coef[6] << 8) | coef[7];
    _c10 = (_c10 & 0x080000) ? (0xFFF00000 | _c10) : _c10;
    _c01 = ((int16_t)coef[8] << 8)  | coef[9];
    _c11 = ((int16_t)coef[10] << 8) | coef[11];
    _c20 = ((int16_t)coef[12] << 8) | coef[13];
    _c21 = ((int16_t)coef[14] << 8) | coef[15];
    _c30 = ((int16_t)coef[16] << 8) | coef[17];
    
    _spl06_pressure_config(PM_RATE_8, PM_PRC_64);
    _spl06_temperature_config(TMP_RATE_1, TMP_PRC_1);
    
    /* 启动测量 */
    regval = MEAS_CTRL_ContinuousPressTemp;
    i2c_write(SPL06_ADDR, SP06_MEAS_CFG, 1, &regval);
    
    return 0;
}

void spl06_get_raw_data(spl06_result_t *p_res)
{
    uint8_t regval[3] = {0};
    
    i2c_read(SPL06_ADDR, SP06_PSR_B2, 3, regval);
    p_res->Praw = (int32_t)regval[0] << 16 | (int32_t)regval[1] << 8 | regval[2];
    p_res->Praw = (p_res->Praw & 0x00800000) ? (0xFF000000 | p_res->Praw) : p_res->Praw;
    
    i2c_read(SPL06_ADDR, SP06_TMP_B2, 3, regval);
    p_res->Traw = (int32_t)regval[0] << 16 | (int32_t)regval[1] << 8 | regval[2];
    p_res->Traw = (p_res->Traw & 0x00800000) ? (0xFF000000 | p_res->Traw) : p_res->Traw;
}

void spl06_get_result(spl06_result_t *p_res)
{
    spl06_get_raw_data(p_res);

    float Praw_sc = p_res->Praw / _kP;
    float Traw_sc = p_res->Traw / _kT;
    
    p_res->Pcomp = _c00 
                 + Praw_sc * (_c10 + Praw_sc * (_c20 + Praw_sc * _c30))
                 + Traw_sc * _c01
                 + Traw_sc * Praw_sc * (_c11 + Praw_sc * _c21);
    
    p_res->Tcomp = _c0 * 0.5 + _c1 * Traw_sc;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143

main.c伪代码

#include "bsp_i2c.h"
#include "bsp_systick.h"	/* 提供延时函数 */
#include "spl06.h"

int main(void)
{
	spl06_result_t spl06_result;
	spl06_init(i2c_read, i2c_write);
	while (1)
	{
		spl06_get_result(&spl06_result);
		delay_ms(125);	/* 配置了气压计检测频率为8次/秒 */
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/402967
推荐阅读
相关标签
  

闽ICP备14008679号