基于STM32的MS5540c压强温度传感器的应用
2018-10-15 来源:eefocus
3.5的库函数,stm32f10x系列的mcu,网上这个芯片的实例较少,而官方的代码an510这个文档找不到下载,datasheet写的也不甚明了。下面贴上我的基本程序给大家一个参考:
#include 'main.h'
#include 'Flash_LED.h'
#include 'SPI_MS5540C.h'
#include 'SysTick.h'
vu32 led_timer = 0;
extern vu32 global_timer;
uint8_t detect_timer=0;
vu16 var_D1=0,var_D2=0;
int main()
{
SystemInit();
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); //8 preemption priority
Init_Flash_LED();
Init_SPI_Device_Clk();
Init_SPI_Device();
Compute_Para();
while(1)
{
if(((global_timer > led_timer) && (global_timer - led_timer >= 200))||((global_timer < led_timer) && (0xffffffff - led_timer + global_timer >= 200)))
{
toggle_led();
led_timer=global_timer;
detect_timer++;
}
SPI_Control_Byte(0);
Delay_ms(20);
SPI_Control_Byte(1);
Delay_ms(100);
var_D1=SPI_Read();
Delay_ms(20);
SPI_Control_Byte(2);
Delay_ms(100);
var_D2=SPI_Read();
Compute_value(var_D1,var_D2);
//while(1);
// */
}
}
时间来不及仔细整理,spi_ms5540c.c 如下,头文件里面就是些声明就不写了 另外main函数里面用到的延时是随便写的,官方datasheet是33ms的转换时间
#include 'SPI_MS5540C.h'
uint16_t Control_D1=0x0f40;//0x03d0 // pressure conversion control_words
uint16_t Control_D2=0x0f20;//0x03c8 // temperature conversion control_words
uint16_t Control_read_word1=0x1d50;//0x0ea8;//
uint16_t Control_read_word2=0x1d60;//0x0eb0;//
uint16_t Control_read_word3=0x1d90;//0x0ec8;//
uint16_t Control_read_word4=0x1da0;//0x0ed0;//
uint16_t Control_Reset1=0x0015; //the funtion 'SPI_I2S_SendData' only could supply 8bit or 16bit send,and the chip ms5540c could start up with three high bits
uint16_t Control_Reset2=0x5540;
uint16_t Blank=0x9249;//1001 0010 0100 1001 send it to read the coversion result and avoid the wrong operation
vu16 pressure_value=0,temp_value=0;
vu16 para_C1=0,para_C2=0,para_C3=0,para_C4=0,para_C5=0,para_C6=0;
vu16 data1=0,data2=0,data3=0,data4=0;
void Init_SPI_Device(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1|RCC_APB2Periph_GPIOA,ENABLE);//??SP1I?GPIOA??
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//MOSI SCK??
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//??????
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;//SPI_CS
//GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//????
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_Init(GPIOA,&GPIO_InitStructure);
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;//??SPI???????????:SPI??????????
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;//??SPI????:????SPI
SPI_InitStructure.SPI_DataSize=SPI_DataSize_16b;//??SPI?????:SPI????8????
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low; //??????????:?????
SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;//???????????
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;//SPI_NSS_Soft;//NSS?????(NSS??)????(??SSI?)??:??NSS???SSI???
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256; //??????????:????????256
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //???????MSB???LSB???:?????MSB???
//SPI_InitStructure.SPI_CRCPolynomial=7;//CRC???????
SPI_Init(SPI1,&SPI_InitStructure);//??SPI_InitStruct???????????SPIx???
SPI_Cmd(SPI1,ENABLE);//??SPI??
}
void SPI_Control_Byte(u8 xx)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_Cmd(SPI1,DISABLE);
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;//??SPI???????????:SPI??????????
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;//??SPI????:????SPI
SPI_InitStructure.SPI_DataSize=SPI_DataSize_16b;//??SPI?????:SPI????8????
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low; //??????????:?????
//SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;//???????????
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;//SPI_NSS_Soft;//NSS?????(NSS??)????(??SSI?)??:??NSS???SSI???
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256; //??????????:????????256
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //?
SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;//???????????
SPI_Init(SPI1,&SPI_InitStructure);//??SPI_InitStruct???????????SPIx???
SPI_Cmd(SPI1,ENABLE);//??SPI??
if(xx==0)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Control_Reset1);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Control_Reset2);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
if(xx==1)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1, Control_D1);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
if(xx==2)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Control_D2);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
if(xx==11)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,Control_read_word1);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
if(xx==12)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,Control_read_word2);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
if(xx==13)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Control_read_word3);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
if(xx==14)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Control_read_word4);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(SPI1);
}
}
uint16_t SPI_Read(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_Cmd(SPI1,DISABLE);
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;//??SPI???????????:SPI??????????
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;//??SPI????:????SPI
SPI_InitStructure.SPI_DataSize=SPI_DataSize_16b;//??SPI?????:SPI????8????
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low; //??????????:?????
//SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;//???????????
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;//SPI_NSS_Soft;//NSS?????(NSS??)????(??SSI?)??:??NSS???SSI???
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256; //??????????:????????256
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB; //?
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;//???????????
SPI_Init(SPI1,&SPI_InitStructure);//??SPI_InitStruct???????????SPIx???
SPI_Cmd(SPI1,ENABLE);//??SPI??
// SPI_InitTypeDef SPI_InitStructure;
// SPI_Cmd(SPI1,DISABLE);
// SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;//???????????
// SPI_Init(SPI1,&SPI_InitStructure);//??SPI_InitStruct???????????SPIx???
// SPI_Cmd(SPI1,ENABLE);//??SPI??
// vu8 data1 = 0;
// vu8 data2 = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Blank);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
u16 Read_Factory_words(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_Cmd(SPI1,DISABLE);
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;//???????????
SPI_Init(SPI1,&SPI_InitStructure);//??SPI_InitStruct???????????SPIx???
SPI_Cmd(SPI1,ENABLE);//??SPI??
// vu8 data1 = 0;
// vu8 data2 = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Blank);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
void TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //enable the TIM2 CLK
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE); // enable the GPIOa CLK
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; //TIM2 CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM4, ENABLE); // if use the remap ,afio is need
//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
//GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM2_Mode_Config(u16 CCR4_Val)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 2197;
TIM_TimeBaseStructure.TIM_Prescaler = 0; //the PWM period is 20Hz
TIM_TimeBaseStructure.TIM_ClockDivision =0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// PWM1 Mode configuration: Channel4
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //????4??????,??????????PWM
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //????4
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE); // ??TIM4?????ARR
// TIM4 enable counter
TIM_Cmd(TIM2, ENABLE); //?????4
}
void Init_SPI_Device_Clk(void)
{
TIM2_GPIO_Config();
TIM2_Mode_Config(1099);
}
void Compute_Para(void)
{
u16 tmp=0;
SPI_Control_Byte(0);
//SPI_Control_Byte(0);
//SPI_Control_Byte(0);
// Delay_ms(100);
//Temperature_Value=SPI_Read();
SPI_Control_Byte(11);
//Delay_ms(1000);
data1=SPI_Read();
SPI_Control_Byte(12);
//Delay_ms(1000);
data2=SPI_Read();
SPI_Control_Byte(13);
//Delay_ms(1000);
data3=SPI_Read();
SPI_Control_Byte(14);
//Delay_ms(1000);
data4=SPI_Read();
Delay_ms(20);
SPI_Control_Byte(2);
tmp=data1&0xfffe;
para_C1=tmp>>1;
tmp=data3&0x003f;
para_C2=tmp<<6;
tmp=data4&0x003f;
para_C2+=tmp;
tmp=data4&0xffc0;
para_C3=tmp>>6;
tmp=data3&0xffc0;
para_C4=tmp>>6;
tmp=data1&0x0001;
para_C5=tmp<<10;
tmp=data2&0xffc0;
para_C5+=tmp>>6;
para_C6=data2&0x003f;
}
void Compute_value(u16 var_d1,u16 var_d2)
{
double var_dt,var_x,var_off,var_sense,temp_value1,pressure_value1,var_t2,var_p2;
var_dt=var_d2-20224-8*para_C5;
var_off=para_C2*4+(para_C4-512)*var_dt/4096;
var_sense=para_C1+para_C3*var_dt/1024+24576;
var_x=var_sense*(var_d1-7168)/16384-var_off;
temp_value1=(var_dt*(para_C6+50)/1024+200);
pressure_value1=(var_x*10/32+2500);
if(temp_value1<200)
{
var_t2=11*(para_C6+24)*(200-temp_value1)*(200-temp_value1)/1048576;
var_p2=3*var_t2*(pressure_value1-3500)/16384;
}
else if(temp_value1>450)
{
var_t2=3*(para_C6+24)*(450-temp_value1)*(450-temp_value1)/1048576;
var_p2=2*var_t2*(pressure_value1-10000)/16384;
}
else
{
var_t2=0;
var_p2=0;
}
temp_value=(int)(temp_value1-var_t2);
pressure_value=(int)(pressure_value1-var_p2);
}
上一篇:STM32的延时程序,us级延时