加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MS5611.c 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
一个杀手 提交于 2020-08-01 10:24 . init ms5611
#include "MS5611.h"
/*
* 参数MS5611结构体
*/
ms5611_init()
{
//指针必须初始化
ms5611_handle = (struct MS5611_t *)malloc(sizeof(struct MS5611_t));
ms5611_handle->reserve = (unsigned short *)malloc(sizeof(unsigned short));
ms5611_handle->CRC = (unsigned short *)malloc(sizeof(unsigned short));
ms5611_handle->C[0] = 0;
ms5611_handle->C[1] = 0;
ms5611_handle->C[2] = 0;
ms5611_handle->C[3] = 0;
ms5611_handle->C[4] = 0;
ms5611_handle->C[5] = 0;
ms5611_handle->D[0] = 0;
ms5611_handle->D[1] = 0;
ms5611_handle->TEMP = 0;
ms5611_handle->P = 0;
ms5611_handle->SENS = 0;
ms5611_handle->OFF = 0;
}
/*
* 重启ms5611
*/
void MS5611_Rest()
{
I2C_WriteByte(I2C1,MS5611_SLAVE_ADDR,MS5611_CMD_REST);
delay_ms(4);
}
/*
* 读取prom的内容
*/
int MS5611_init()
{
ms5611_init();
Ms5611_Rest();
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_0,2,(unsigned char *)(ms5611_handle->reserve),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_1,2,(unsigned char *)(ms5611_handle->C),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_2,2,(unsigned char *)((ms5611_handle->C)+1),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_3,2,(unsigned char *)((ms5611_handle->C)+2),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_4,2,(unsigned char *)((ms5611_handle->C)+3),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_5,2,(unsigned char *)((ms5611_handle->C)+4),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_6,2,(unsigned char *)((ms5611_handle->C)+5),MSB);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS5611_PROM_READ_7,2,(unsigned char *)(ms5611_handle->CRC),MSB);
}
/*
* 读取温度 转换精度4096
*/
int MS5611_read_temp()
{
I2C_WriteByte(I2C1,MS5611_SLAVE_ADDR,MS5611_CMD_CONVERT_D2_4096);//发送转换命令
delay_ms(9);//等待转换
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS6511_ADC_READ,3,(unsigned char *)((ms5611_handle->D)+1),MSB);//读取三个字节的温度
}
/*
* 读取气压 转换精度4096
*/
int MS5611_read_press()
{
I2C_WriteByte(I2C1,MS5611_SLAVE_ADDR,MS5611_CMD_CONVERT_D1_4096);//发送转换命令
delay_ms(9);
I2C_ReadBytes(I2C1,MS5611_SLAVE_ADDR,MS6511_ADC_READ,3,(unsigned char *)(ms5611_handle->D),MSB);//读取三个字节的气压
}
/*
* 修正气压和温度
*/
int MS5611_calculate()
{
signed long long dT = 0,TEMP = 0,T2 = 0,OFF = 0,OFF2 = 0,SENS2 = 0,SENS = 0;
dT = ms5611_handle->D[1] - ((signed long long) (ms5611_handle->C[4])<<8);
TEMP = 2000 + ((signed long long) (dT*(ms5611_handle->C[5]))>>23);
//低于20°时:
if(TEMP < 2000 && TEMP > -1500)
{
T2 = ( dT*dT )>>31;
OFF2 = 5 * (TEMP - 2000) * (TEMP - 2000) / 2;
SENS2 = 5 * (TEMP - 2000) * (TEMP - 2000) / 4;
}
OFF = (((int64_t)(ms5611_handle->C[1])) << 16) + (((ms5611_handle->C[3]) * dT) >> 7);
SENS = (((int64_t)(ms5611_handle->C[0])) << 15) + (((ms5611_handle->C[2]) * dT) >> 8);
ms5611_handle->dT = dT;
ms5611_handle->OFF -= OFF2;
ms5611_handle->TEMP -= T2;
ms5611_handle->SENS -= SENS2;
ms5611_handle->P = ((((ms5611_handle->D[0]) * (ms5611_handle->SENS))>>21) - (ms5611_handle->OFF))>>15;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化