本帖最后由 feifeivictor 于 2019-7-15 23:16 编辑
#include "sys.h"
#define SCLK_PORT GPIOD
#define SCLK_PIN GPIO_Pin_4
#define DOUT_PORT GPIOD
#define DOUT_PIN GPIO_Pin_3
#define DIN_PORT GPIOD
#define DIN_PIN GPIO_Pin_6
#define MCLK_PORT GPIOB
#define MCLK_PIN GPIO_Pin_5
#define SCLK_high() GPIO_SetBits(SCLK_PORT,SCLK_PIN)
#define SCLK_low() GPIO_ResetBits(SCLK_PORT,SCLK_PIN)
#define DIN_high() GPIO_SetBits(DIN_PORT,DIN_PIN)
#define DIN_low() GPIO_ResetBits(DIN_PORT,DIN_PIN)
#define DOUT PDin(3)
typedef struct {
unsigned int SENST1;
unsigned int OFFT1;
unsigned int TCS;
unsigned int TCO;
unsigned int Tref;
unsigned int TEMPSENS;
} coeff_t;
coeff_t c={0};
unsigned int DT;
void MS5541C_IO_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE); //ê1ÄüPB,PD¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = MCLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MCLK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SCLK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SCLK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DIN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DIN_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DOUT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(DOUT_PORT, &GPIO_InitStructure);
}
u16 GetWord(char str[7])
{
int i;
u16 ret=0;
u16 temp=0;
SCLK_low();
delay_us(10);
//3bit start bits
for(i=0;i<3;i++)
{
DIN_high();
delay_us(10);
SCLK_high();
delay_us(10);
SCLK_low();
}
delay_us(10);
//6bit word address
for(i=0;i<6;i++)
{
if(str=='1')
{
DIN_high();
}
else if(str=='0')
{
DIN_low();
}
else
{
printf("GetWord error
");
}
delay_us(10);
SCLK_high();
delay_us(10);
SCLK_low();
}
//3bit stop bits
for(i=0;i<3;i++)
{
DIN_low();
delay_us(10);
SCLK_high();
delay_us(10);
SCLK_low();
}
for(i=0;i<1;i++)
{
DIN_low();
delay_us(10);
SCLK_high();
delay_us(10);
SCLK_low();
}
delay_us(10);
SCLK_high();
delay_us(10);
for(i=0;i<16;i++)
{
SCLK_low();
delay_us(10);
temp=DOUT;
ret=ret|(DOUT<<(15-i));
SCLK_high();
delay_us(10);
}
SCLK_low();
delay_us(10);
return ret;
}
void MS5541C_Reset()
{
int i;
SCLK_low();
delay_us(10);
for(i=0;i<16;i++)
{
SCLK_low();
delay_us(10);
if(i%2==0)
{
DIN_high();
}
else
{
DIN_low();
}
SCLK_high();
delay_us(10);
}
for(i=0;i<5;i++)
{
SCLK_low();
delay_us(10);
DIN_low();
SCLK_high();
delay_us(10);
}
SCLK_low();
delay_us(10);
}
u16 GetWord1()
{
u16 temp=GetWord("010101");
printf("GetWord1=%d
",temp);
return temp;
}
u16 GetWord2()
{
u16 temp=GetWord("010110");
printf("GetWord2=%d
",temp);
return temp;
}
u16 GetWord3()
{
u16 temp=GetWord("011001");
printf("GetWord3=%d
",temp);
return temp;
}
u16 GetWord4()
{
u16 temp=GetWord("011010");
printf("GetWord4=%d
",temp);
return temp;
}
u16 GetD1()
{
int i;
u8 cmd[12]={1,1,1,1,0,1,0,0,0,0,0,0};
u16 ret=0;
u16 temp;
SCLK_low();
delay_us(10);
for(i=0;i<12;i++)
{
if(cmd==1)
{
DIN_high();
}
else
{
DIN_low();
}
SCLK_high();
delay_us(10);
SCLK_low();
delay_us(10);
}
delay_ms(50);
for(i=0;i<16;i++)
{
SCLK_high();
delay_us(10);
SCLK_low();
temp=DOUT;
ret=ret|(temp<<(15-i));
delay_us(10);
}
SCLK_high();
delay_us(10);
SCLK_low();
delay_us(10);
printf("GetD1=%d
",ret);
return ret;
}
u16 GetD2()
{
int i;
u8 cmd[12]={1,1,1,1,0,0,1,0,0,0,0,0};
u16 ret=0;
u16 temp;
SCLK_low();
delay_us(10);
for(i=0;i<12;i++)
{
if(cmd==1)
{
DIN_high();
}
else
{
DIN_low();
}
SCLK_high();
delay_us(10);
SCLK_low();
delay_us(10);
}
delay_ms(50);
for(i=0;i<16;i++)
{
SCLK_high();
delay_us(10);
SCLK_low();
temp=DOUT;
ret=ret|(temp<<(15-i));
delay_us(10);
}
SCLK_high();
delay_us(10);
SCLK_low();
delay_us(10);
printf("GetD2=%d
",ret);
return ret;
}
void get_coeffs(coeff_t* coefficients)
{
unsigned int w1, w2, w3, w4;
w1 = GetWord1();
w2 = GetWord2();
w3 = GetWord3();
w4 = GetWord4();
// print("w1 = %u w2 = %u w3 = %u w4 = %u
", w1, w2, w3, w4);
coefficients->SENST1 = w1 >> 3;
coefficients->OFFT1 = (w1 & 0x07) << 10; //0b111
coefficients->OFFT1 |= w2 >> 6;
coefficients->TCS = w3 >> 6;
coefficients->TCO = w4 >> 7;
coefficients->Tref = (w2 & 0x3f) << 6; //0b111111
coefficients->Tref |= w3 & 0x3f; //0b111111
coefficients->TEMPSENS = w4 & 0x7f; //0b1111111
}
float GetTemperature()
{
int c1,c2,c3,c4,c5,c6;
int d1,d2;
int ut1;
int dt;
int off,sens,p;
float tem;
get_coeffs(&c);
c1=c.SENST1;
c2=c.OFFT1;
c3=c.TCS;
c4=c.TCO;
c5=c.Tref;
c6=c.TEMPSENS;
d1=GetD1();
d2=GetD2();
ut1=8*c5+10000;
dt=d2-ut1;
//temp 0.1 degree
tem=(float)(200+dt*(c6+100)/(1<<11))*0.1;
printf("temp=%f
",tem);
off = c2 + ((c4-250)*dt)/(1<<12) + 10000;
sens = c1/2 + ((c3+200)*dt)/(1<<13) + 3000;
//pressure,mbar
p= (sens * (d1-off))/(1<<12) + 1000;
printf("pressure=%d mbar
",p);
return tem;
}
int GetPressure()
{
int c1,c2,c3,c4,c5,c6;
int d1,d2;
int ut1;
int dt;
int off,sens,p;
float tem;
get_coeffs(&c);
c1=c.SENST1;
c2=c.OFFT1;
c3=c.TCS;
c4=c.TCO;
c5=c.Tref;
c6=c.TEMPSENS;
d1=GetD1();
d2=GetD2();
ut1=8*c5+10000;
dt=d2-ut1;
//temp 0.1 degree
tem=(float)(200+dt*(c6+100)/(1<<11))*0.1;
printf("temp=%f
",tem);
off = c2 + ((c4-250)*dt)/(1<<12) + 10000;
sens = c1/2 + ((c3+200)*dt)/(1<<13) + 3000;
//pressure,mbar
p= (sens * (d1-off))/(1<<12) + 1000;
printf("pressure=%d mbar
",p);
return p;
}
一周热门 更多>