fm_ccjy/Board/Src/ms5803.c

322 lines
6.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "MS5803.h"
PromVar PROMData;
volatile int32_t CurrentTemp = 0,CurrentPress = 0;
/* SPI handler declaration */
/* Buffer used for transmission */
uint8_t aTxBuffer[4];
uint8_t aRxBuffer[4];
uint8_t NSS_Select = 1;
//拉高所有片选脚
void SetCS(void)
{
LL_GPIO_SetOutputPin(SPI1_NSS1_GPIO_Port,SPI1_NSS1_Pin);
LL_GPIO_SetOutputPin(SPI1_NSS2_GPIO_Port,SPI1_NSS2_Pin);
LL_GPIO_SetOutputPin(SPI1_NSS3_GPIO_Port,SPI1_NSS3_Pin);
LL_GPIO_ResetOutputPin(SPI1_NSS4_GPIO_Port,SPI1_NSS4_Pin);
LL_GPIO_SetOutputPin(SPI1_NSS5_GPIO_Port,SPI1_NSS5_Pin);
LL_GPIO_SetOutputPin(SPI1_NSS6_GPIO_Port,SPI1_NSS6_Pin);
LL_GPIO_SetOutputPin(SPI1_NSS7_GPIO_Port,SPI1_NSS7_Pin);
LL_GPIO_ResetOutputPin(SPI1_NSS8_GPIO_Port,SPI1_NSS8_Pin);
}
void ClrCS(void)
{
switch(NSS_Select)
{
case 1 :
LL_GPIO_ResetOutputPin(SPI1_NSS1_GPIO_Port,SPI1_NSS1_Pin);
break;
case 2 :
LL_GPIO_ResetOutputPin(SPI1_NSS2_GPIO_Port,SPI1_NSS2_Pin);
break;
case 3 :
LL_GPIO_ResetOutputPin(SPI1_NSS3_GPIO_Port,SPI1_NSS3_Pin);
break;
case 4 :
LL_GPIO_SetOutputPin(SPI1_NSS4_GPIO_Port,SPI1_NSS4_Pin);
break;
case 5 :
LL_GPIO_ResetOutputPin(SPI1_NSS5_GPIO_Port,SPI1_NSS5_Pin);
break;
case 6 :
LL_GPIO_ResetOutputPin(SPI1_NSS6_GPIO_Port,SPI1_NSS6_Pin);
break;
case 7 :
LL_GPIO_ResetOutputPin(SPI1_NSS7_GPIO_Port,SPI1_NSS7_Pin);
break;
case 8 :
LL_GPIO_SetOutputPin(SPI1_NSS8_GPIO_Port,SPI1_NSS8_Pin);
break;
}
}
//****************************************
//*功能向MS5803写入命令
//*参数CMD
//*返回True/False
static int WriteCmd(uint8_t CMD)
{
memset(aTxBuffer,0,sizeof(aTxBuffer));
aTxBuffer[0] = CMD;
ClrCS();
LL_mDelay(1); // pull CSB low to start the command
if(HAL_SPI_Transmit(&hspi1,(uint8_t*)aTxBuffer,1,10))
{
LL_mDelay(10);
SetCS();
LL_mDelay(1);
return 1;
}
else
{
LL_mDelay(10);
SetCS();
LL_mDelay(1);
return 0;
}
// if(HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, 1, 100))
// {
// LL_mDelay(10);
// SetCS();
// LL_mDelay(1);
// return 1;
// }
// else
// {
// LL_mDelay(10);
// SetCS();
// LL_mDelay(1);
// return 0;
// }
//return 1;
}
//****************************************
//*功能根据指令从MS5803读相应数据
//*参数CMD,Count
//*返回True/False
static int ReadCmdData(uint8_t CMD,uint8_t Count)
{
memset(aTxBuffer,0,sizeof(aTxBuffer));
aTxBuffer[0] = CMD;
ClrCS();
LL_mDelay(1);
// if(HAL_SPI_Transmit(&hspi1,(uint8_t*)aTxBuffer,1,10)&&HAL_SPI_Receive(&hspi1,(uint8_t *)aRxBuffer,Count+1,100))
// {
// SetCS(); // pull CSB high to finish the command
// LL_mDelay(1);
// return 1;
// }
// else
// {
// SetCS(); // pull CSB high to finish the command
// LL_mDelay(1);
// return 0;
// }
if(HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, Count+1, 100))
{
SetCS();
LL_mDelay(1);
return 1;
}
else
{
SetCS();
LL_mDelay(1);
return 0;
}
//memmove(aRxBuffer,aRxBuffer+1,Count);//
//return 1;
}
//****************************************
//*功能复位MS5803
//*参数:
//*返回:
static void ResetDevice()
{
WriteCmd(RESET);
LL_mDelay(3); //必须延时3ms
}
//****************************************
//*功能获取PROM校准参数,只需要获取一次
//*参数:
//*返回:
static void GetPromData()
{
memset(aRxBuffer,0,sizeof(aRxBuffer));
ReadCmdData(Cof1,2);
PROMData.C1[NSS_Select] = (aRxBuffer[1]<<8) + aRxBuffer[2];
memset(aRxBuffer,0,sizeof(aRxBuffer));
ReadCmdData(Cof2,2);
PROMData.C2[NSS_Select] = (aRxBuffer[1]<<8) + aRxBuffer[2];
memset(aRxBuffer,0,sizeof(aRxBuffer));
ReadCmdData(Cof3,2);
PROMData.C3[NSS_Select] = (aRxBuffer[1]<<8) + aRxBuffer[2];
memset(aRxBuffer,0,sizeof(aRxBuffer));
ReadCmdData(Cof4,2);
PROMData.C4[NSS_Select] = (aRxBuffer[1]<<8) + aRxBuffer[2];
memset(aRxBuffer,0,sizeof(aRxBuffer));
ReadCmdData(Cof5,2);
PROMData.C5[NSS_Select] = (aRxBuffer[1]<<8) + aRxBuffer[2];
memset(aRxBuffer,0,sizeof(aRxBuffer));
ReadCmdData(Cof6,2);
PROMData.C6[NSS_Select] = (aRxBuffer[1]<<8) + aRxBuffer[2];
}
//****************************************
//*功能MS5803初始化
//*参数:
//*返回:
void MS5803Init()
{
// LL_SPI_Enable(SPI1);
//SPI_Init();
LL_GPIO_ResetOutputPin(SENSOR_SELECT_GPIO_Port,SENSOR_SELECT_Pin);
SetCS();
NSS_Select = 1;
//初始化代码
ResetDevice();
GetPromData();
NSS_Select = 2;
//初始化代码
ResetDevice();
GetPromData();
NSS_Select = 3;
//初始化代码
ResetDevice();
GetPromData();
//
// NSS_Select = 4;
// //初始化代码
// ResetDevice();
// GetPromData();
}
//****************************************
//*功能:获取温度和压力 MS5803-14BA
//*参数:
//*返回:
void StartCalculation()
{
uint32_t D1,D2;
int32_t dT,TEMP;
int64_t OFF,SENS;
//转换压力
WriteCmd(CD1_4096);
LL_mDelay(10); //必须延时10ms
ReadCmdData(ADC_Read,3);
D1 = (aRxBuffer[1]*pow(2,16)) + (aRxBuffer[2]*pow(2,8)) + aRxBuffer[3];
//转换温度
WriteCmd(CD2_4096);
LL_mDelay(10); //必须延时10ms
ReadCmdData(ADC_Read,3);
D2 = (aRxBuffer[1]*pow(2,16)) + (aRxBuffer[2]*pow(2,8)) + aRxBuffer[3];
//计算温度
dT = D2 - (PROMData.C5[NSS_Select]*pow(2,8));
if(dT > 16777216)
{
dT = 16777216;
}
else if(dT < -16776960)
{
dT = -16776960;
}
TEMP = 2000 + ((((long long)dT)*PROMData.C6[NSS_Select])/pow(2,23));
//计算压力
OFF = (PROMData.C2[NSS_Select]*pow(2,16)) + ((((long long)dT)*PROMData.C4[NSS_Select])/pow(2,7));
if(OFF > 51538821120)
{
OFF = 51538821120;
}
else if(OFF < -34358689800)
{
OFF = -34358689800;
}
SENS = (((long long)PROMData.C1[NSS_Select])*pow(2,15)) + ((((long long)dT)*PROMData.C3[NSS_Select])/pow(2,8));
if(SENS > 17179607040)
{
SENS = 17179607040;
}
else if(SENS < -8589672450)
{
SENS = -8589672450;
}
__nop();
//Second order compensation
long T2 = 0,OFF2 = 0,SENS2 = 0;
if(TEMP < 2000)
{
T2 = (3*pow(dT,2))/pow(2,33);
OFF2 = (3*pow((TEMP-2000),2))/pow(2,1);
SENS2 = (5*pow((TEMP-2000),2))/pow(2,3);
if(TEMP < -1500)
{
OFF2 += 7 * pow(TEMP+1500,2);
SENS2 += 4 * pow((TEMP+1500),2);
}
}
else
{
T2 = (7*pow(dT,2))/pow(2,37);
OFF2 = pow((TEMP-2000),2)/pow(2,4);
}
CurrentTemp = TEMP - T2;
OFF -= OFF2;
SENS -= SENS2;
CurrentPress = (((((long long)SENS)*D1)/pow(2,21)) - OFF)/pow(2,15);
__nop();
}
void ms5803_task(void)
{
if(NSS_Select > 3)
NSS_Select = 1;
StartCalculation();
InputReg[NSS_Select + 6] = CurrentPress/10;//压力数据存入
NSS_Select++;
}