DELAY
#include “delay.h”
static u8 fac_us=0; //us延時倍乘數
static u16 fac_ms=0; //ms延時倍乘數,在ucos下,代表每個節拍的ms數
//初始化延遲函數
//SYSTICK的時鐘固定為HCLK時鐘的1/8
//SYSCLK:系統時鐘
void delay_init()
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //選擇外部時鐘 HCLK/8
fac_us=SystemCoreClock/8000000; //為系統時鐘的1/8
fac_ms=(u16)fac_us*1000; //非OS下,代表每個ms需要的systick時鐘數
}
//延時nus
//nus為要延時的us數。
void delay_us(u32 nus)
{
u32 temp;
SysTick-》LOAD=nus*fac_us; //時間加載
SysTick-》VAL=0x00; //清空計數器
SysTick-》CTRL|=SysTick_CTRL_ENABLE_Msk ; //開始倒數
do
{
temp=SysTick-》CTRL;
}while((temp&0x01)&&!(temp&(1《《16))); //等待時間到達
SysTick-》CTRL&=~SysTick_CTRL_ENABLE_Msk; //關閉計數器
SysTick-》VAL =0X00; //清空計數器
}
//延時nms
//注意nms的范圍
//SysTick-》LOAD為24位寄存器,所以,最大延時為:
//nms《=0xffffff*8*1000/SYSCLK
//SYSCLK單位為Hz,nms單位為ms
//對72M條件下,nms《=1864
void delay_ms(u16 nms)
{
u32 temp;
SysTick-》LOAD=(u32)nms*fac_ms; //時間加載(SysTick-》LOAD為24bit)
SysTick-》VAL =0x00; //清空計數器
SysTick-》CTRL|=SysTick_CTRL_ENABLE_Msk ; //開始倒數
do
{
temp=SysTick-》CTRL;
}while((temp&0x01)&&!(temp&(1《《16))); //等待時間到達
SysTick-》CTRL&=~SysTick_CTRL_ENABLE_Msk; //關閉計數器
SysTick-》VAL =0X00; //清空計數器
}
MS5611
#include “MS5611.h”
/*宏定義------------------------------------------------------------------*/
//定義器件在IIC總線中的從地址,根據CSB引腳不同修改
//#define MS561101BA_ADDR 0xec //CBR=1 0x76 I2C address when CSB is connected to HIGH (VCC)
#define MS561101BA_ADDR 0xee //CBR=0 0x77 I2C address when CSB is connected to LOW (GND)
// 定義MS561101BA內部地址
// registers of the device
#define MS561101BA_D1 0x40
#define MS561101BA_D2 0x50
#define MS561101BA_RESET 0x1E
// D1 and D2 result size (bytes)
#define MS561101BA_D1D2_SIZE 3
// OSR (Over Sampling Ratio) constants
#define MS561101BA_OSR_256 0x00
#define MS561101BA_OSR_512 0x02
#define MS561101BA_OSR_1024 0x04
#define MS561101BA_OSR_2048 0x06
#define MS561101BA_OSR_4096 0x08
//#define MS561101BA_D1_OSR_256 0x40
//#define MS561101BA_D1_OSR_512 0x42
//#define MS561101BA_D1_OSR_1024 0x44
//#define MS561101BA_D1_OSR_2048 0x46
#define MS561101BA_D1_OSR_4096 0x48
//#define MS561101BA_D2_OSR_256 0x50
//#define MS561101BA_D2_OSR_512 0x52
//#define MS561101BA_D2_OSR_1024 0x54
//#define MS561101BA_D2_OSR_2048 0x56
#define MS561101BA_D2_OSR_4096 0x58
#define MS561101BA_PROM_BASE_ADDR 0xA0 // by adding ints from 0 to 6 we can read all the prom configuration values.
// C1 will be at 0xA2 and all the subsequent are multiples of 2
#define MS561101BA_PROM_REG_COUNT 6 // number of registers in the PROM
#define MS561101BA_PROM_REG_SIZE 2 // size in bytes of a prom registry.
/*變量聲明----------------------------------------------------------------*/
uint16_t Cal_C[7]; //用于存放PROM中的6組數據
uint32_t D1_Pres,D2_Temp; // 存放數字壓力和溫度
float Pressure; //溫度補償大氣壓
float dT,Temperature,Temperature2;//實際和參考溫度之間的差異,實際溫度,中間值
double OFF,SENS; //實際溫度抵消,實際溫度靈敏度
float Aux,OFF2,SENS2; //溫度校驗值
uint32_t ex_Pressure; //串口讀數轉換值
uint8_t exchange_num[8];
/*函數聲明----------------------------------------------------------------*/
void MS561101BA_Reset(void);
void MS561101BA_readPROM(void);
uint32_t MS561101BA_DO_CONVERSION(u8 command);
void MS561101BA_GetTemperature(u8 OSR_Temp);
void MS561101BA_GetPressure(u8 OSR_Pres);
void MS561101BA_Init(void);
void SampleANDExchange(void);
/************************************************************
* 函數名:MS561101BA_Reset
* 描述 : 復位
* 輸入 :無
* 輸出 :無
*/
void MS561101BA_Reset(void)
{
I2C_NoAddr_WriteByte(MS561101BA_ADDR,MS561101BA_RESET);
}
/************************************************************
* 函數名:MS561101BA_readPROM
* 描述 : 從PROM讀取出廠校準數據
* 輸入 :無
* 輸出 :無
*/
void MS561101BA_readPROM(void)
{ uint16_t value=0;u8 temp1[2]={0};
u8 i;
for (i=0;i《=MS561101BA_PROM_REG_COUNT;i++)
{
// I2C_Read_MultiBytes(MS561101BA_ADDR,MS561101BA_PROM_BASE_ADDR + (i * MS561101BA_PROM_REG_SIZE),2,temp1);
//value=temp1[0]《《8|temp1[1];
//Cal_C[i]=value;
Cal_C[i]=I2C_Read_2Bytes(MS561101BA_ADDR,MS561101BA_PROM_BASE_ADDR + (i * MS561101BA_PROM_REG_SIZE));
}
printf(“\n The MS561101BA is reading PROM : \r\n”);
printf(“\r\nC1 = %d\r\nC2 = %d\r\nC3 = %d\r\nC4 = %d\r\nC5 = %d\r\nC6 = %d\r\n”,Cal_C[1],Cal_C[2],Cal_C[3],Cal_C[4],Cal_C[5],Cal_C[6]);
}
/************************************************************
* 函數名:MS561101BA_DO_CONVERSION
* 描述 :
* 輸入 :無
* 輸出 :無
*/
uint32_t MS561101BA_DO_CONVERSION(uint8_t command)
{
uint32_t conversion;
I2C_NoAddr_WriteByte(MS561101BA_ADDR,command);
delay_ms(10);//延時,去掉數據錯誤
conversion=I2C_Read_3Bytes(MS561101BA_ADDR,0);
return conversion;
}
/************************************************************
* 函數名:MS561101BA_GetTemperature
* 描述 : 讀取數字溫度
* 輸入 :過采樣率
* 輸出 :無
*/
void MS561101BA_GetTemperature(u8 OSR_Temp)
{
D2_Temp= MS561101BA_DO_CONVERSION(OSR_Temp);
delay_ms(100);
dT=D2_Temp - (((uint32_t)Cal_C[5])《《8);
Temperature=2000+dT*((uint32_t)Cal_C[6])/8388608; //算出溫度值的100倍,2001表示20.01°
}
/************************************************************
* 函數名:MS561101BA_GetPressure
* 描述 : 讀取數字氣壓
* 輸入 :過采樣率
* 輸出 :無
*/
void MS561101BA_GetPressure(u8 OSR_Pres)
{
D1_Pres= MS561101BA_DO_CONVERSION(OSR_Pres);
delay_ms(100);
OFF=(uint32_t)(Cal_C[2]《《16)+((uint32_t)Cal_C[4]*dT)/128.0;
SENS=(uint32_t)(Cal_C[1]《《15)+((uint32_t)Cal_C[3]*dT)/256.0;
//溫度補償
if(Temperature 《 2000)// second order temperature compensation when under 20 degrees C
{
Temperature2 = (dT*dT) / 0x80000000;
Aux = (Temperature-2000)*(Temperature-2000);
OFF2 = 2.5*Aux;
SENS2 = 1.25*Aux;
if(Temperature 《 -1500)
{
Aux = (Temperature+1500)*(Temperature+1500);
OFF2 = OFF2 + 7*Aux;
SENS2 = SENS + 5.5*Aux;
}
}else //(Temperature 》 2000)
{
Temperature2 = 0;
OFF2 = 0;
SENS2 = 0;
}
Temperature = Temperature - Temperature2;
OFF = OFF - OFF2;
SENS = SENS - SENS2;
Pressure=(D1_Pres*SENS/2097152.0-OFF)/32768.0;
}
/************************************************************
* 函數名:MS561101BA_Init
* 描述 : MS561101BA初始化
* 輸入 :無
* 輸出 :無
*/
void MS561101BA_Init(void)
{
MS561101BA_Reset();
delay_ms(100);
MS561101BA_readPROM();
delay_ms(100);
}
/************************************************************
* 函數名:SampleANDExchange
* 描述 : 讀取數據并轉換串口發送
* 輸入 :無
* 輸出 :無
*/
void SampleANDExchange(void)
{
uint8_t i=0;
MS561101BA_GetTemperature(MS561101BA_D2_OSR_4096);//0x58
MS561101BA_GetPressure(MS561101BA_D1_OSR_4096); //0x48
ex_Pressure=(long)(Pressure);
if(Pressure《0)
{
ex_Pressure=-ex_Pressure;
exchange_num[0]=‘-’;
}
else exchange_num[0]=‘\0’;
exchange_num[1]=ex_Pressure/100000+0x30;
ex_Pressure=ex_Pressure%100000;
exchange_num[2]=ex_Pressure/10000+0x30;
ex_Pressure=ex_Pressure%10000;
exchange_num[3]=ex_Pressure/1000+0x30;
ex_Pressure=ex_Pressure%1000;
exchange_num[4]=ex_Pressure/100+0x30;
ex_Pressure=ex_Pressure%100;
exchange_num[5]=‘。’;
exchange_num[6]=ex_Pressure/10+0x30;
ex_Pressure=ex_Pressure%10;
exchange_num[7]=ex_Pressure+0x30;
printf(“\nP : %c%c%c%c%c%c%c%c mbar \r\n”,exchange_num[0],exchange_num[1],exchange_num[2],exchange_num[3],exchange_num[4],exchange_num[5],exchange_num[6],exchange_num[7]);
// for(i=0;i《8;i++)
// {
// printf(“%c”,exchange_num[i]);
// }
// printf(“ mbar \r\n”);
printf(“T : %4.3f °C\r\n ”,Temperature/100);
}
評論
查看更多