高级会员

- 积分
- 566
- 金钱
- 566
- 注册时间
- 2016-9-28
- 在线时间
- 158 小时
|

楼主 |
发表于 2017-4-22 11:06:20
|
显示全部楼层
本帖最后由 mftang2016 于 2017-4-22 11:08 编辑
全部代码如下:
[mw_shl_code=c,true]/*******************************************************************************
** File name: Bmp085.c
** Created by: Mingfei Tang
** Created date: 2017/4/19
** Version: V1.00
** Descriptions: for I2C driver
** Details:
**------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
** 安富莱STM32-V5开发板 i2c总线GPIO:
PH4 -- I2C2_SCL
PH5 -- I2C2_SDA
*******************************************************************************/
#include "includes.h"
#define GPIO_PORT_I2C GPIOH /* GPIO端口 */
#define RCC_I2C_PORT RCC_AHB1Periph_GPIOH /* GPIO端口时钟 */
#define I2C_SCL_PIN GPIO_Pin_4 /* 连接到SCL时钟线的GPIO */
#define I2C_SDA_PIN GPIO_Pin_5 /* 连接到SDA数据线的GPIO */
/* 定义读写SCL和SDA的宏 */
#define I2C_SCL_1() GPIO_PORT_I2C->BSRRL = I2C_SCL_PIN /* SCL = 1 */
#define I2C_SCL_0() GPIO_PORT_I2C->BSRRH = I2C_SCL_PIN /* SCL = 0 */
#define I2C_SDA_1() GPIO_PORT_I2C->BSRRL = I2C_SDA_PIN /* SDA = 1 */
#define I2C_SDA_0() GPIO_PORT_I2C->BSRRH = I2C_SDA_PIN /* SDA = 0 */
#define I2C_SDA_READ() ((GPIO_PORT_I2C->IDR & I2C_SDA_PIN) != 0) /* 读SDA口线状态 */
#define I2C_SCL_READ() ((GPIO_PORT_I2C->IDR & I2C_SCL_PIN) != 0) /* 读SCL口线状态 */
I2C I2C_Bmp085;
BMP085_T g_tBMP085;
static INT8U bsp_i2cRead( void )
{
return I2C_SDA_READ();
}
static INT8U bsp_i2cReadSCL( void )
{
return I2C_SCL_READ();
}
static void bsp_i2c_SclHigh( void )
{
I2C_SCL_1();
}
static void bsp_i2c_SclLow( void )
{
I2C_SCL_0();
}
static void bsp_i2c_SdaHigh( void )
{
I2C_SDA_1();
}
static void bsp_i2c_SdaLow( void )
{
I2C_SDA_0();
}
static void bsp_InitI2C(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_I2C_PORT, ENABLE); /* 打开GPIO时钟 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; /* 设为输出口 */
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; /* 设为开漏模式 */
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* 上下拉电阻不使能 */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; /* IO口最大速度 */
GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
GPIO_Init(GPIO_PORT_I2C, &GPIO_InitStructure);
}
void Bmp085_Init( void )
{
I2C *pI2C;
pI2C = &I2C_Bmp085;
pI2C->IIC_InitPort = bsp_InitI2C;
pI2C->IIC_READ = bsp_i2cRead;
pI2C->IIC_READ_SCL = bsp_i2cReadSCL;
pI2C->IIC_SCL_H = bsp_i2c_SclHigh;
pI2C->IIC_SCL_L = bsp_i2c_SclLow;
pI2C->IIC_SDA_H = bsp_i2c_SdaHigh;
pI2C->IIC_SDA_L = bsp_i2c_SdaLow;
i2c_Stop( pI2C ); /* 给一个停止信号, 复位I2C总线上的所有设备到待机模式 */
bsp_InitI2C(); //初始化IO
}
static void BMP085_WriteReg(uint8_t _ucRegAddr, uint8_t _ucRegValue)
{
I2C *pI2C;
pI2C = &I2C_Bmp085;
i2c_Start( pI2C ); /* 总线开始信号 */
i2c_SendByte( pI2C, BMP085_SLAVE_ADDRESS); /* 发送设备地址+写信号 */
i2c_WaitAck( pI2C );
i2c_SendByte(pI2C, _ucRegAddr); /* 发送寄存器地址 */
i2c_WaitAck( pI2C );
i2c_SendByte( pI2C, _ucRegValue); /* 发送寄存器数值 */
i2c_WaitAck( pI2C );
i2c_Stop( pI2C ); /* 总线停止信号 */
}
static uint16_t BMP085_Read2Bytes(uint8_t _ucRegAddr)
{
I2C *pI2C;
pI2C = &I2C_Bmp085;
uint8_t ucData1;
uint8_t ucData2;
uint16_t usRegValue;
i2c_Start( pI2C ); /* 总线开始信号 */
i2c_SendByte( pI2C, BMP085_SLAVE_ADDRESS); /* 发送设备地址+写信号 */
i2c_WaitAck( pI2C );
i2c_SendByte( pI2C, _ucRegAddr); /* 发送地址 */
i2c_WaitAck( pI2C );
i2c_Start( pI2C ); /* 总线开始信号 */
i2c_SendByte( pI2C, BMP085_SLAVE_ADDRESS + 1); /* 发送设备地址+读信号 */
i2c_WaitAck( pI2C );
ucData1 = i2c_ReadByte( pI2C ); /* 读出高字节数据 */
i2c_Ack( pI2C );
ucData2 = i2c_ReadByte( pI2C ); /* 读出低字节数据 */
i2c_NAck( pI2C );
i2c_Stop( pI2C ); /* 总线停止信号 */
usRegValue = (ucData1 << 8) + ucData2;
return usRegValue;
}
static uint32_t BMP085_Read3Bytes(uint8_t _ucRegAddr)
{
uint8_t ucData1;
uint8_t ucData2;
uint8_t ucData3;
uint32_t uiRegValue;
I2C *pI2C;
pI2C = &I2C_Bmp085;
i2c_Start( pI2C ); /* 总线开始信号 */
i2c_SendByte( pI2C, BMP085_SLAVE_ADDRESS); /* 发送设备地址+写信号 */
i2c_WaitAck( pI2C );
i2c_SendByte( pI2C, _ucRegAddr); /* 发送地址 */
i2c_WaitAck( pI2C );
i2c_Start( pI2C ); /* 总线开始信号 */
i2c_SendByte( pI2C, BMP085_SLAVE_ADDRESS + 1);/* 发送设备地址+读信号 */
i2c_WaitAck( pI2C );
ucData1 = i2c_ReadByte( pI2C ); /* 读出高字节数据 */
i2c_Ack( pI2C );
ucData2 = i2c_ReadByte( pI2C ); /* 读出中间字节数据 */
i2c_Ack( pI2C );
ucData3 = i2c_ReadByte( pI2C ); /* 读出最低节数据 */
i2c_NAck( pI2C );
i2c_Stop( pI2C ); /* 总线停止信号 */
uiRegValue = (ucData1 << 16) + (ucData2 << 8) + ucData3;
return uiRegValue;
}
static void BMP085_WaitConvert(void)
{
if (g_tBMP085.OSS == 0)
{
TimeActDelayMs(6); /* 4.5ms 7.5ms 13.5ms 25.5ms */
}
else if (g_tBMP085.OSS == 1)
{
TimeActDelayMs(9); /* 4.5ms 7.5ms 13.5ms 25.5ms */
}
else if (g_tBMP085.OSS == 2)
{
TimeActDelayMs(15); /* 4.5ms 7.5ms 13.5ms 25.5ms */
}
else if (g_tBMP085.OSS == 3)
{
TimeActDelayMs(27); /* 4.5ms 7.5ms 13.5ms 25.5ms */
}
}
void bsp_InitBMP085(void)
{
Bmp085_Init();
/* 读出芯片内部的校准参数(每个芯片不同,这是BOSCH出厂前校准好的数据) */
g_tBMP085.AC1 = (int16_t)BMP085_Read2Bytes(0xAA);
g_tBMP085.AC2 = (int16_t)BMP085_Read2Bytes(0xAC);
g_tBMP085.AC3 = (int16_t)BMP085_Read2Bytes(0xAE);
g_tBMP085.AC4 = (uint16_t)BMP085_Read2Bytes(0xB0);
g_tBMP085.AC5 = (uint16_t)BMP085_Read2Bytes(0xB2);
g_tBMP085.AC6 = (uint16_t)BMP085_Read2Bytes(0xB4);
g_tBMP085.B1 = (int16_t)BMP085_Read2Bytes(0xB6);
g_tBMP085.B2 = (int16_t)BMP085_Read2Bytes(0xB8);
g_tBMP085.MB = (int16_t)BMP085_Read2Bytes(0xBA);
g_tBMP085.MC = (int16_t)BMP085_Read2Bytes(0xBC);
g_tBMP085.MD = (int16_t)BMP085_Read2Bytes(0xBE);
g_tBMP085.OSS = 0; /* 过采样参数,0-3 */
}
void BMP085_ReadTempPress(void)
{
long UT, X1, X2, B5, T;
long UP, X3, B3, B6, B7, p;
unsigned long B4;
/* 流程见 pdf page 12 */
/* 读温度原始值 */
BMP085_WriteReg(0xF4, 0x2E);
BMP085_WaitConvert(); /* 等待转换结束 */
UT = BMP085_Read2Bytes(0xF6);
/* 读压力原始值 */
BMP085_WriteReg(0xF4, 0x34 + (g_tBMP085.OSS << 6));
BMP085_WaitConvert(); /* 等待转换结束 */
UP = BMP085_Read3Bytes(0xF6) >> (8 - g_tBMP085.OSS);
/* 计算真实温度(单位 0.1摄氏度) */
X1 = ((long)(UT - g_tBMP085.AC6) * g_tBMP085.AC5) >> 15;
X2 = ((long)g_tBMP085.MC << 11) / (X1 + g_tBMP085.MD);
B5 = X1 + X2; /* 该系数将用于压力的温度补偿计算 */
T = (B5 + 8) >> 4;
g_tBMP085.Temp = T; /* 将计算结果保存在全局变量 */
/* 计算真实压力值(单位 Pa) */
B6 = B5 - 4000;
X1 = (g_tBMP085.B2 * (B6 * B6) >> 12) >> 11;
X2 = (g_tBMP085.AC2 * B6) >> 11;
X3 = X1 + X2;
B3 = (((((long)g_tBMP085.AC1) * 4 + X3) << g_tBMP085.OSS) + 2) >> 2;
X1 = (g_tBMP085.AC3 * B6) >> 13;
X2 = (g_tBMP085.B1 * ((B6 * B6) >> 12)) >> 16;
X3 = ((X1 + X2) + 2) >> 2;
B4 = (g_tBMP085.AC4 * (unsigned long)(X3 + 32768)) >> 15;
B7 = ((unsigned long)(UP - B3) * (50000 >> g_tBMP085.OSS));
if ( B7 < 0x80000000 )
{
p = (B7 << 1) / B4;
}
else
{
p = (B7 / B4) << 1;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7357 * p) >> 16;
p = p + ((X1 + X2 + 3791) >> 4);
g_tBMP085.Press = p; /* 将计算结果保存在全局变量 */
}
/* End of this file */
[/mw_shl_code]
Bmp085.zip
(2.89 KB, 下载次数: 384)
|
|