代码如下
#include "i2c.h"
#include "gpio.h"
#include "cqi2c.h"
#include "systick.h"
#include "math.h"
#define I2C_DEVADDR (0x56)
uint8_t u8Senddata[10] = {0};
uint8_t u8Recdata[10]={0};
uint8_t u8SendLen=0;
uint8_t u8RecvLen=0;
uint8_t SendFlg = 0,Comm_flg = 0;
uint8_t u8recvflg=0;
uint8_t u8State = 0;
uint8_t u8Addr = 0x00;//地址字节
unsigned char cods[COD_SIZE]={0};
int32_t i2c0Ini(void)
{
int i=0;
stc_gpio_config_t stcGpioCfg;
stc_i2c_config_t stcI2cCfg;
stc_sysctrl_clk_config_t stcCfg;
DDL_ZERO_STRUCT(stcCfg);
DDL_ZERO_STRUCT(stcI2cCfg);
DDL_ZERO_STRUCT(stcGpioCfg);
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);
Sysctrl_SetPeripheralGate(SysctrlPeripheralI2c0,TRUE);
stcGpioCfg.enDir = GpioDirOut;
stcGpioCfg.enOD = GpioOdEnable;
stcGpioCfg.enPuPd = GpioPu;
Gpio_Init(GpioPortA,GpioPin9,&stcGpioCfg);
Gpio_Init(GpioPortA,GpioPin10,&stcGpioCfg);
Gpio_Init(GpioPortD,GpioPin7,&stcGpioCfg); //复位脚
Gpio_SetAfMode(GpioPortD,GpioPin7,GpioAf0);
Gpio_ClrIO(GpioPortD, GpioPin7);
Gpio_SetAfMode(GpioPortA,GpioPin9,GpioAf4);
Gpio_SetAfMode(GpioPortA,GpioPin10,GpioAf4);
stcI2cCfg.enFunc = I2cBaud_En;
//stcI2cCfg.u8Tm = 0x04;//400kbps=(16000000/(8*(4+1))//实际测试:HCLK:1000000 Hz PCLK:1000000 Hz
stcI2cCfg.u8Tm = 0x02;//1M=(24000000/(8*(2+1)) //41kHz
stcI2cCfg.pfnI2c0Cb = NULL;
stcI2cCfg.bTouchNvic = FALSE;
I2C_DeInit(I2C0);
I2C_Init(I2C0,&stcI2cCfg);
I2C_SetFunc(I2C0,I2cMode_En);
I2C_SetFunc(I2C0,I2cHlm_En);
I2C_SetFunc(I2C0,I2cStart_En);
Gpio_SetIO(GpioPortB, GpioPin13); //open sensor power GpioPin14 -> GpioPin13
delay1ms(500);
Gpio_ClrIO(GpioPortB, GpioPin13); //open sensor power GpioPin14 -> GpioPin13
delay1ms(15);
Gpio_SetIO(GpioPortD, GpioPin7);
delay1ms(15);
Gpio_ClrIO(GpioPortD, GpioPin7);
//读取校准参数
delay1ms(200);
// I2C_MasterReadData(I2C0,I2C_DEVADDR<<1,cods,0xa0,COD_SIZE);
// for(i=0;i<COD_SIZE;i++) printf(" %.2x",cods[i]);
// printf("\n");
I2C_MasterReadData(I2C0,I2C_DEVADDR<<1,cods,0xa0,COD_SIZE);
for(i=0;i<COD_SIZE;i++) printf(" %.2x",cods[i]);
printf("\n");
I2C_MasterReadData(I2C0,I2C_DEVADDR<<1,cods,0xa0,COD_SIZE);
for(i=0;i<COD_SIZE;i++) printf(" %.2x",cods[i]);
printf("\n");
//设置采样次数
// u8Senddata[0]=0xfd;//0x19;
// u8Addr=0xf4;
// I2C_MasterWriteData(I2C0,I2C_DEVADDR<<1,u8Addr,u8Senddata,1);
//设置FIR滤波器
u8Senddata[0]=0x3;
u8Addr=0xf1;
I2C_MasterWriteData(I2C0,I2C_DEVADDR<<1,u8Addr,u8Senddata,1);
return 0;
}
void ReadPrs()
{
unsigned char *pData=u8Recdata;
int dqprs,dqtemp;
int i=0;
double dVal=0,Tr,Pr;//,Dp;
int Dp;
int c_a0,c_b00;
short c_a1,c_a2,c_bt1,c_bp1,c_b11,c_bt2,c_bp2,c_b12,c_b21,c_bp3;
double a0,a1,a2,b00,bt1,bp1,b11,bt2,bp2,b12,b21,bp3;
//设置采样次数
u8Senddata[0]=0xfd;//0x19;
u8Addr=0xf4;
I2C_MasterWriteData(I2C0,I2C_DEVADDR<<1,u8Addr,u8Senddata,1);
//设置FIR滤波器
// u8Senddata[0]=0x7;
// u8Addr=0xf1;
// I2C_MasterWriteData(I2C0,I2C_DEVADDR<<1,u8Addr,u8Senddata,1);
//读取采集数据
u8Addr=0xf7;
memset(u8Recdata,8,0);
I2C_MasterReadData(I2C0,I2C_DEVADDR<<1,u8Recdata,u8Addr,6);
pData=u8Recdata;
printf(" acc \n ");
for(i=0;i<6;i++) printf(" %.2x",u8Recdata[i]);
printf(" \n ");
dqprs=(pData[0]<<16)+ (pData[1]<<8)+pData[2]-pow(2,23);
Dp=dqprs;
dqtemp=(pData[3]<<16)+ (pData[4]<<8)+pData[5]-pow(2,23);
printf("-raw prs:%d temp:%d-\n" ,dqprs,dqtemp);
//01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8
//4c c1 02 1c 10 15 fa 6d fb 4e 13 3c 00 2c 07 ba f1 f9 f8 71 0e 40 0b 27 5f
c_a0=(cods[0xb8-0xa0]&0xf)+(cods[0xb3-0xa0]<<4)+(cods[0xb2-0xa0]<<12);
c_a0=-(c_a0&0x80000)|(c_a0&0x7ffff);
c_a1=cods[0xb5-0xa0]+(cods[0xb4-0xa0]<<8);
c_a2=cods[0xb7-0xa0]+(cods[0xb6-0xa0]<<8);
a0=c_a0/16;
a1=-6.3E-03+((4.3E-04*c_a1)/32767);
a2= -1.9E-11+((1.2E-10*c_a2)/32767);
c_b00=(cods[0xb8-0xa0]>>4)+(cods[0xa1-0xa0]<<4)+(cods[0xa0-0xa0]<<12);
c_b00=-(c_b00&0x80000)|(c_b00&0x7ffff);
c_bt1=(cods[0xa2-0xa0]<<8)+(cods[0xa3-0xa0]);
c_b11=(cods[0xa8-0xa0]<<8)+(cods[0xa9-0xa0]);
c_bp1=(cods[0xa6-0xa0]<<8)+(cods[0xa7-0xa0]);
c_bt2=(cods[0xa4-0xa0]<<8)+(cods[0xa5-0xa0]);
c_bp2=(cods[0xaa-0xa0]<<8)+(cods[0xab-0xa0]);
c_b12=(cods[0xac-0xa0]<<8)+(cods[0xad-0xa0]);
c_b21=(cods[0xae-0xa0]<<8)+(cods[0xaf-0xa0]);
c_bp3=(cods[0xb0-0xa0]<<8)+(cods[0xb1-0xa0]);
b00=c_b00/16;
bt1=1.0E-01+((9.1E-02*c_bt1)/32767);
bt2=1.2E-08+((1.2E-06*c_bt2)/32767);
bp1=3.3E-02+((1.9E-02*c_bp1)/32767);
b11=2.1E-07+((1.4E-07*c_b11)/32767);
bp2=-6.3E-10+((3.5E-10*c_bp2)/32767);
b12=2.9E-13+((7.6E-13*c_b12)/32767);
b21=2.1E-15+((1.2E-14*c_b21)/32767);
bp3=1.3E-16+((7.9E-17*c_bp3)/32767);
dVal=a0+(a1*dqtemp)+((a2*dqtemp)*dqtemp); //计算温度
dqtemp=dVal/256;
Tr=dVal;
printf("-jz temp:%d-\n" ,dqtemp); //温度数据 摄氏度
Pr=b00+bt1*Tr+bp1*Dp+b11*Dp*Tr+bt2*Tr*Tr+bp2*Dp*Dp+b12*Dp*Tr*Tr+((b21*Dp)*Dp)*Tr+((bp3*Dp)*Dp)*Dp;//计算压力
dqprs=Pr;//Pr;
printf("-jz prs:%d-\n" ,dqprs); //压力数据 Pa
return;
}
以上程序的在华大单片机H32L136运行通过
对比手机的大气压力值和环境温度,读出的温度和大气压力比较准确的
关键点:
是几个系数的计算,变量定义类型