📄 vaccum.c
字号:
MiddleData4 = MiddleData3/MiddleData1;
X = MiddleData4-OFF;
//calculate the Press value,have two decimal fraction
//Press = X*100/Get2_x(5)+C7*10;
MiddleData1 = X*100;
MiddleData2 = Get2_x(5);
MiddleData3 = MiddleData1/MiddleData2;
MiddleData4 = C7*10;
Press = MiddleData3+MiddleData4;
//calculate the Temperature value
if (btesttemp)
Temp = 250+DUT*C6/Get2_x(16)-DUT/Get2_x(DD);
else
Temp = 0;
}
//**************************************************
//function:calculate altitude
//input :Press value
//output :Altitude, unit: 0.1m
//*************************
void CalculateAltitude(void)
{
char ucCount;
unsigned int uiBasicPress;
unsigned int uiBiasTotal;
unsigned int uiBiasPress;
unsigned int uiBiasAltitude;
for ( ucCount=0; ; ucCount++ ) {
uiBasicPress = MaxPress-(ucCount*10);
if (uiBasicPress < (int)(Press/100)) break;
}
uiBiasTotal = Tab_BasicAltitude[ucCount] - Tab_BasicAltitude[ucCount-1];
uiBiasPress = Press - (long)(uiBasicPress*100);
uiBiasAltitude = (long)uiBiasTotal * uiBiasPress /1000;
Altitude = Tab_BasicAltitude[ucCount] - uiBiasAltitude;
ucCount = abs(Altitude % 10); //four lose and five up
if (Altitude <0) {
if (ucCount >4)
Altitude -= 10 - ucCount;
else
Altitude += ucCount;
} else {
if (ucCount > 4)
Altitude += 10 - ucCount;
else
Altitude -= ucCount;
}
}
//================================================
void IIC_ReadCalData(void)
{
unsigned char ucValue;
//read C1
IIC_Start();
IIC_WriteByte(0xa0);
IIC_WriteByte(16); //C1 address:16~17 (2 bytes)
IIC_Start();
IIC_WriteByte(0xa1); //EEPROM chip address = 0xa1
ucValue = IIC_ReadByte();
IIC_ACK();
C1=ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C1 <<= 8;
C1 |= ucValue;
//Read C2
ucValue = IIC_ReadByte(); //C2 address: 18~19 (2 Bytes)
IIC_ACK();
C2 = ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C2 <<= 8;
C2 |= ucValue;
//Read C3
ucValue = IIC_ReadByte(); //C3 address: 20~21(2 Bytes)
IIC_ACK();
C3 = ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C3 <<= 8;
C3 |= ucValue;
//Read C4
ucValue = IIC_ReadByte(); //C4 address: 22~23(2 Bytes)
IIC_ACK();
C4=ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C4 <<= 8;
C4 |= ucValue;
//Read C5
ucValue = IIC_ReadByte(); //C3 address: 24~25(2 Bytes)
IIC_ACK();
C5=ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C5 <<= 8;
C5 |= ucValue;
//Read C6
ucValue = IIC_ReadByte(); //C6 address: 26~27(2 Bytes)
IIC_ACK();
C6 = ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C6 <<= 8;
C6 |= ucValue;
//Read C7
ucValue = IIC_ReadByte(); //C7 address: 28~29(2 Bytes)
IIC_ACK();
C7 = ucValue;
ucValue = IIC_ReadByte();
IIC_ACK();
C7 <<= 8;
C7 |= ucValue;
//Read A
ucValue = IIC_ReadByte(); //A address: 30, 1byte
IIC_ACK();
AAA= ucValue;
//Read B
ucValue = IIC_ReadByte(); //B address: 31, 1 byte
IIC_ACK();
BB= ucValue;
//Read C
ucValue = IIC_ReadByte(); //C address: 32, 1 byte
IIC_ACK();
CC = ucValue;
//Read D
ucValue = IIC_ReadByte(); //D address: 33, 1byte
IIC_NoAck();
IIC_Stop();
DD= ucValue;
}
//============================================
void MCLKOn(void)
{
bMCLKOn = 1;
}
//===============================
void MCLKOff(void)
{
bMCLKOn = 0;
MCLK = 0;
}
//================================
void ReadTemperaturePreesureAD(void)
{
long uiSumADValue;
//Initial
uiSumADValue = 0;
IIC_XCLR_HIGH();
MCLKOn();
SysDelay2ms(1);
D1 = IIC_ReadPressureAD();
uiSumADValue += D1;
D1 = IIC_ReadPressureAD();
uiSumADValue += D1;
D1 = IIC_ReadPressureAD();
uiSumADValue += D1;
D1 = IIC_ReadPressureAD();
uiSumADValue += D1;
//total read 4 times, average it
D1 = uiSumADValue >> 2;
D2 = IIC_ReadTempretureAD();
SDA=0;
SCL=0;
MCLKOff();
IIC_XCLR_LOW();
}
//=====================================
void vect_Timer0(void) interrupt 1 using 1 //porduct 32k Hz signal
{
TF0 = 0;
if (bMCLKOn)
MCLK = ~MCLK;
else
MCLK = 0;
if (systimer1)
systimer1 --;
}
//===============================================
unsigned int IIC_ReadTempretureAD(void)
{
unsigned char ucData;
unsigned int wADT;
IIC_Start();
IIC_WriteByte(0xEE);
IIC_WriteByte(0xff);
IIC_WriteByte(0xE8);
IIC_Stop();
SysDelay2ms(20);
IIC_Start();
IIC_WriteByte(0xEE);
IIC_WriteByte(0xFD);
IIC_Start();
IIC_WriteByte(0xEF);
ucData = IIC_ReadByte();
IIC_ACK();
// UartSend(CCC);
wADT = ucData;
wADT <<= 8;
ucData = IIC_ReadByte();
IIC_NoAck();
IIC_Stop();
wADT |= ucData;
// UartSend(DD);
return wADT;
}
//============================================
unsigned int IIC_ReadPressureAD(void)
{
unsigned char ucData;
unsigned int wADp;
IIC_Start(); //start condition
IIC_WriteByte(0xEE); //AD Chip address = 0xEE (device write address)
IIC_WriteByte(0xff); //
IIC_WriteByte(0xF0);
IIC_Stop();
SysDelay2ms(20);
IIC_Start();
IIC_WriteByte(0xEE);
IIC_WriteByte(0xFD);
IIC_Start();
IIC_WriteByte(0xEF);
ucData = IIC_ReadByte();
IIC_ACK();
wADp = ucData;
wADp <<=8;
// UartSend(AA);
ucData = IIC_ReadByte();
IIC_NoAck();
IIC_Stop();
wADp |= ucData;
// UartSend(BB);
return wADp;
}
//==============================
unsigned char IIC_ReadByte(void)
{
unsigned char ucValue;
unsigned char ucIndex;
IIC_SDA_HIGH();
SysDelay20us();
for ( ucIndex = 0; ucIndex < 8; ucIndex++) {
ucValue <<= 1;
IIC_SCL_LOW();
SysDelay20us();
IIC_SCL_HIGH();
SysDelay20us();
if (SDA)
ucValue |= 1;
SysDelay20us();
IIC_SCL_LOW();
SysDelay20us();
}
return ucValue;
}
void IIC_WriteByte(unsigned char ucData)
{
unsigned char i;
for ( i = 0; i < 8; i++ ) {
IIC_SCL_LOW();
SysDelay20us();
if ((ucData &0x80) == 0x80) {
IIC_SDA_HIGH();
SysDelay20us();
} else {
IIC_SDA_LOW();
SysDelay20us();
}
IIC_SCL_HIGH();
SysDelay20us();
ucData <<= 1;
IIC_SCL_LOW();
}
IIC_SDA_HIGH();
SysDelay20us();
IIC_SCL_LOW();
SysDelay20us();
IIC_SCL_HIGH();
SysDelay20us();
IIC_SCL_LOW();
SysDelay20us();
}
void IIC_Start(void)
{
IIC_SDA_HIGH();
SysDelay20us();
IIC_SCL_HIGH();
SysDelay20us();
IIC_SDA_LOW();
SysDelay20us();
IIC_SCL_LOW();
SysDelay20us();
}
void IIC_Stop(void)
{
IIC_SCL_LOW();
SysDelay20us();
IIC_SDA_LOW();
SysDelay20us();
IIC_SCL_HIGH();
SysDelay20us();
IIC_SDA_HIGH();
SysDelay20us();
}
void IIC_ACK(void)
{
IIC_SDA_LOW();
SysDelay20us();
IIC_SCL_HIGH();
SysDelay20us();
IIC_SCL_LOW();
SysDelay20us();
}
void IIC_NoAck(void)
{
IIC_SDA_HIGH();
SysDelay20us();
IIC_SCL_HIGH();
SysDelay20us();
IIC_SCL_LOW();
SysDelay20us();
}
void IIC_SDA_HIGH(void)
{
SDA=1;
}
void IIC_SDA_LOW(void)
{
SDA=0;
}
void IIC_SCL_HIGH(void)
{
SCL=1;
}
void IIC_SCL_LOW(void)
{
SCL=0;
}
void IIC_XCLR_LOW(void)
{
XCLR=0;
}
void IIC_XCLR_HIGH(void)
{
XCLR=1;
}
void SysDelay2ms( unsigned int t)
{
unsigned int i,j;
while (t--) {
for (i=0;i<100;i++)
for (j=0;j<100;j++);
}
}
void SysDelay20us()
{
unsigned int i;
i = 200;
while (i--);
}
void ClockDelay2ms( unsigned int t)
{
while (t--) {
systimer1 = 32; // = 2ms/t, t = 1/(32768/2)
while (systimer1);
}
}
/*********************
void ClockDelay20us()
{
systimer1 = 3;
while(systimer1);
}
********************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -