程序代码如下:按照此程序输出的传感器(HP03)内存的几个常数不对,不知道为什么?调试了一周了,郁闷 //Function:模拟IIC与MCU通信,压力传感器的MCLK接MCU的ACLK(32768Hz输出) // Vss--> GND // VDD-->VCC // MCLK(32768HZ)-->P2.0 // XCLK-->P6.1 // SDA-->P6.0 // SCL-->P6.2 //*******************head files********************************** #include<msp430x14x.h> //#include"io430.h" #include<math.h> //*********************symbol define***************************** #define SDA_STATE P6IN & BIT0 //读入SDA状态
unsigned int C1=0; unsigned int C2=0; unsigned int C3=0; unsigned int C4=0; unsigned int C5=0; unsigned int C6=0; unsigned int C7=0; unsigned char AA=0; unsigned char BB=0; unsigned char CC=0; unsigned char DD=0;
unsigned int D1=0; unsigned int D2=0; long float DUT=0; long float OFF=0; long float SENS=0; long float X=0; long float Press=0; long float Temp=0; //*******************function declaration************************ void IIC_start(void); void IIC_stop(void); void IIC_SDA_HIGH(void); void IIC_SDA_LOW(void); void IIC_SCL_HIGH(void); void IIC_SCL_LOW(void); void IIC_ACK(void); void IIC_NOACK(void); void MCLKON(void); void MCLKOFF(void); void XCLR_LOW(void); void XCLR_HIGH(void); void IIC_ReadCalDate(void); unsigned char IIC_readbyte(void); void IIC_writebyte(unsigned char); void ReadTemperaturePressureAD(void); void CalculatepressTemp(void); unsigned int ReadPressureAD(); unsigned int ReadTemperatureAD(); void SysDelay1ms(unsigned int); void Delay16us(void); void CPU_Init(void); //初始化时钟 void Port_Init(void); //初始化I/O //******************************************************** //function:read calcute data and pressure,temp data //******************************************************** void main( void ) { //SysDelay1ms(100); CPU_Init(); Port_Init(); //SysDelay2ms(50); IIC_ReadCalDate(); while(1) { ReadTemperaturePressureAD(); CalculatepressTemp(); SysDelay1ms(100); } } //******************************************************** void CPU_Init(void) { unsigned char i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer to prevent time out reset // Configure internal clocks DCOCTL = DCO1 + DCO2; // Set digitally controlled oscillator to ~1MHz BCSCTL1 = XT2OFF + RSEL2; BCSCTL2 = SELM0; // MCLK and SMCLK use this oscillator do { IFG1 &= ~OFIFG; //clear OSCFault flag_breathe for(i=0xFF; i>0; i--); //delay wait } while((IFG1&OFIFG)!=0);
} //******************** Port Initialization**************** // Vss GND // //P6.3 VDD // P2.0 MCLK(32768HZ) // P6.1 XCLK // P6.0 SDA // P6.2 SCL //******************************************************** void Port_Init(void) { //P6SEL &= ~0x08; //P6.3普通IO //P6DIR |= 0x08; //P6.3输出1 //P6OUT |= 0x08; P2DIR |= 0x01; P2SEL |= 0x01; //P2.0输出ACLK P6SEL &= ~0x02; //P6.1普通IO P6DIR |= 0x02; //P6.1输出 P6SEL &= ~0x01; //P6.0普通IO P6SEL &= ~0x04; //P6.2普通IO P6DIR |= 0x04; //P6.2输出 } //********************************************************* void IIC_ReadCalDate(void) { unsigned int ucValue=0; // P6OUT &= ~0x02; //P6.1输出0 IIC_start(); IIC_writebyte(0xa0); //contain stop signal IIC_writebyte(16); IIC_start(); IIC_writebyte(0xa1); ucValue=IIC_readbyte(); IIC_ACK(); C1=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C1<<=8; C1|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C2=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C2<<=8; C2|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C3=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C3<<=8; C3|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C4=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C4<<=8; C4|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C5=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C5<<=8; C5|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C6=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C6<<=8; C6|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C7=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); C7<<=8; C7|=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); AA=ucValue; ucValue=IIC_readbyte(); IIC_ACK(); BB=ucValue; //UartSend(AA); //UartSend(BB);
ucValue=IIC_readbyte(); IIC_ACK(); CC=ucValue; ucValue=IIC_readbyte(); IIC_NOACK(); IIC_stop(); DD=ucValue; } //********************************************************* void ReadTemperaturePressureAD(void) { long sumADvalue=0; XCLR_HIGH(); MCLKON(); SysDelay1ms(2); D1=ReadPressureAD(); sumADvalue+=D1; D1=ReadPressureAD(); sumADvalue+=D1; D1=ReadPressureAD(); sumADvalue+=D1; D1=ReadPressureAD(); sumADvalue+=D1; D1=sumADvalue>>2; D2=ReadTemperatureAD(); P6SEL &= ~0x01; //P6.0普通IO P6DIR |= 0x01; //P6.0输出 P6OUT &= ~0x01; P6SEL &= ~0x04; //P6.2普通IO P6DIR |= 0x04; //P6.2输出 P6OUT &= ~0x04; MCLKOFF(); XCLR_LOW(); } //********************************************************* unsigned int ReadTemperatureAD(void) { unsigned char ucData=0; unsigned int ADT=0; IIC_start(); IIC_writebyte(0xee); IIC_writebyte(0xff); IIC_writebyte(0xe8); IIC_stop(); SysDelay1ms(40); IIC_start(); IIC_writebyte(0xee); IIC_writebyte(0xfd); IIC_start(); IIC_writebyte(0xef); ucData=IIC_readbyte(); IIC_ACK(); //UartSend(CC); ADT=ucData; ADT<<=8; ucData=IIC_readbyte(); IIC_NOACK(); IIC_stop(); ADT|=ucData; //UartSend(DD); return (unsigned int)(ADT); } unsigned int ReadPressureAD(void) { unsigned char ucData=0; unsigned int ADP=0; IIC_start(); IIC_writebyte(0xee); IIC_writebyte(0xff); IIC_writebyte(0xf0); IIC_stop(); SysDelay1ms(40); IIC_start(); IIC_writebyte(0xee); IIC_writebyte(0xfd); IIC_start(); IIC_writebyte(0xef); ucData=IIC_readbyte(); IIC_ACK(); ADP=ucData; ADP<<=8; ucData=IIC_readbyte(); IIC_NOACK(); IIC_stop(); ADP|=ucData; //UartSend(BB); return (unsigned int)(ADP); } //*************Calculate press and temp******************* void CalculatepressTemp(void) { if (D2 >= C5) { DUT = D2 - C5 - (D2 - C5) * (D2 - C5) * AA / 16384 / (1 << CC); } else // (D2 < C5) { DUT = D2 - C5 - (D2 - C5) * (D2 - C5) * BB / 16384 / (1 << CC); } OFF = (C2 * 4) + ((C4 - 1024) * DUT * 4 / 16384); SENS = (C1 * 1024 + C3 * DUT) / 1024; X = (SENS * (D1 - 7168) - OFF * 16384) / 16384; Press = (X * 10 + C7 * 32) / 32; // Final pressure value (in millibars) Temp = 250 + ( DUT * C6 / 256 * (1 << DD) - DUT * 256) / (1 << DD) / 256; // Final temperature value Press = Press * 98312 / 131072; // Convert to mmHg }
//********************1ms定时程序************************ void SysDelay1ms(unsigned int ms) { int i,j; for(i=0;i<ms;i++) { for(j=0;j<200;j++); } } //********************// 20us定时程序*********************** //void Delay20us(void) //{ // _NOP(); // _NOP(); // _NOP(); // _NOP(); // _NOP(); // _NOP(); //} //**********************// 16us定时程序******************** void Delay16us(void) { _NOP(); } //********************************************************* void IIC_writebyte(unsigned char ucData) { unsigned char i; for(i=0;i<8;i++) { IIC_SCL_LOW(); Delay16us(); if((ucData & 0x80)==0x80) { IIC_SDA_HIGH(); Delay16us(); } else { IIC_SDA_LOW(); Delay16us(); } IIC_SCL_HIGH(); Delay16us(); ucData<<=1; IIC_SCL_LOW(); } //stop signal IIC_SDA_HIGH(); Delay16us(); IIC_SCL_LOW(); Delay16us(); IIC_SCL_HIGH(); Delay16us(); IIC_SCL_LOW(); Delay16us(); } unsigned char IIC_readbyte(void) { unsigned char ucValue=0; unsigned char ucindex=0; IIC_SDA_HIGH(); //output high level,get ready for accept Delay16us(); for(ucindex=0;ucindex<8;ucindex++) { ucValue<<=1; IIC_SCL_LOW(); Delay16us(); IIC_SCL_HIGH(); Delay16us(); P6SEL &= ~0x01; //P6.0普通IO P6DIR &= ~0x01; if(SDA_STATE) ucValue|=1; Delay16us(); IIC_SCL_LOW(); Delay16us(); } return (unsigned int)(ucValue); } //********************************************************* void IIC_start(void) { IIC_SDA_HIGH(); Delay16us();
IIC_SCL_HIGH(); Delay16us();
IIC_SDA_LOW(); Delay16us();
IIC_SCL_LOW(); Delay16us(); } void IIC_stop(void) { IIC_SCL_LOW(); Delay16us();
IIC_SDA_LOW(); Delay16us();
IIC_SCL_HIGH(); Delay16us();
IIC_SDA_HIGH(); Delay16us(); } //********************************************************* void IIC_ACK(void) { IIC_SDA_LOW(); Delay16us(); IIC_SCL_HIGH(); Delay16us(); IIC_SCL_LOW(); Delay16us(); } void IIC_NOACK(void) { IIC_SDA_HIGH(); Delay16us(); IIC_SCL_HIGH(); Delay16us(); IIC_SCL_LOW(); Delay16us(); } //********************************************************* void MCLKON(void) { P2SEL |= 0x01; // set p2.0 as ACLK Output for sensor MCLK P2DIR |= 0x01; //P2.0输出 } void MCLKOFF(void) // shutdown p2.0 as ACLK Output { P2SEL &= 0x00; P2DIR |= 0x01; //P2.0输出 P2OUT &= ~0x01; //P2.0 = 0 } void XCLR_LOW(void) { P6SEL &= ~0x02; //P6.1普通IO P6DIR |= 0x02; //P6.1输出0 P6OUT &= ~0x02; } void XCLR_HIGH(void) { P6SEL &= ~0x02; //P6.1普通IO P6DIR |= 0x02; //P6.1输出1 P6OUT |= 0x02; } //********************************************************* void IIC_SDA_HIGH(void) { P6SEL &= ~0x01; //P6.0普通IO P6DIR |= 0x01; //P6.0输出 P6OUT |= 0x01; //P6.0输出1 }
void IIC_SDA_LOW(void) { P6SEL &= ~0x01; //P6.0普通IO P6DIR |= 0x01; //P6.0输出 P6OUT &= ~0x01; //P6.0输出0
} //********************************************************* void IIC_SCL_HIGH(void) { P6SEL &= ~0x04; //P6.2普通IO P6DIR |= 0x04; P6OUT |= 0x04; //P6.2输出1 } void IIC_SCL_LOW(void) { P6SEL &= ~0x04; //P6.2普通IO P6DIR |= 0x04; P6OUT &= ~0x04; //P6.2输出0 }
|