TDC-GP2与MSP430F413通讯C程序 下载本文

TDC-GP2与MSP430F413通讯C程序 .......................................................... 1 ........................................................................................... 12

TDC-GP2与MSP430F413通讯C程序

//======================================== #include #include

//#include \

//========================================

#define Init_GP2 0x70 //初始化TDC #define Start_Cycle 0x01 //脉冲发送

#define Start_Temp 0x02 //开始温度测量 #define Start_Cal_Resonator 0x03 //校准晶振时钟 #define Start_Cal_TDC 0x04 //校准TDC

//定义各管脚工作状态名称

#define PVCCOn P4OUT |= BIT0 #define PVCCOff P4OUT = 0;

#define SSNEn P3OUT &=~BIT0 // GP2 R/W enable #define SSNDs P3OUT |= BIT0 // GP2 R/W disable #define RSTNHigh P3OUT |= BIT4 #define RSTNLow P3OUT &=~BIT4

#define StartEn P1OUT |= BIT6 // GP2 En_Start #define StartDs P1OUT &=~BIT6

#define StopEn P1OUT |= BIT7 // GP2 En_Stop #define StopDs P1OUT &=~BIT7

#define Up P1OUT |= BIT2 #define Down P1OUT |= BIT3

#define Close P1OUT &=~(BIT2 + BIT3) //配置GP2流量测量

//const unsigned char ConfigGP2[23] =

{0x50,0x80,0x37,0x8A,0x68,0x81,0x21,0x44,0x00,0x82,0xE0,0x32,0x00,0x8

1

3,0x08,0x33,0x00,0x84,0x20,0x34,0x00,0x70,0x01};

const unsigned char ReadGP2STAT[3] = {0xB4,0x55,0x55};

const unsigned char ReadRes0[5] = {0xB0,0x55,0x55,0x55,0x55}; const unsigned char ReadRes1[5] = {0xB1,0x55,0x55,0x55,0x55}; const unsigned char ReadRes2[5] = {0xB2,0x55,0x55,0x55,0x55}; const unsigned char ReadRes3[5] = {0xB3,0x55,0x55,0x55,0x55}; //========================================= //以下为初始化函数

void InitialPort(void);//初始化端口

//以下为内部函数定义,详细说明见函数部分 //======================================= void InitialGP2(void);//初始化GP2

void OpCodeGP2 (unsigned char opcode); // 写1 Byte op-codes 到 GP2 void ConfigGP2 (unsigned long data); // 配置GP2寄存器 void MeasureTemp(void);//温度测量

void ReadGP2ST(void);//读GP2状态寄存器

void ReadData(unsigned char Count,volatile unsigned char *

point,volatile unsigned char * point_d);//读GP2结果寄存器数据 void FireStart(void);// 发送点火脉冲 void CalStart(void);//校准时钟测量

void HXOn(void);//切换校准时钟时高速晶振状态到开启 void HXOff(void);//切换校准时钟时高速晶振状态到关闭 void NOP10(void);// 10_NOP()机器周期延迟 void TempCal(void);//温度校准测量

//======================================= #pragma memory = no_init unsigned long

DisplayData[8];//0:heat,1:cool,2:volume,3:heatKwh,4:volumeL,5:workTime,6:LCDDate,7:LCDTime #pragma memory = default

unsigned char SPITxCount,SPIRxCount; unsigned char SPIMaskBuf[5]; unsigned char Res0[4]; unsigned char Res1[4]; unsigned char Res2[4]; unsigned char Res3[4]; unsigned int GP2ST;

unsigned long T1,T2,T3,T4; unsigned long PT1,PT2; float CorrectFactor;

const float A = 3.862314E-3; const float B = -6.531493E-7;

2

//======================================= union longValue {

unsigned long uWord; unsigned char lByte[4]; };

union intValue {

unsigned int uInt;

unsigned char lByte[2]; };

//======================================= void main(void) {

InitialPort(); _EINT();

SPIRxCount = 0; Delay(50000);

_NOP();

PVCCOn;

InitialGP2(); //初始化GP2

while(1) {

//HXOn();

//Delay(5000);

CalStart(); //时钟校准 //HXOff();

//MeasureTemp(); //_NOP(); //TempCal(); //Display(PT1);

//LCDRAM[3] |= 0x80; //StopWTD(); //AdjustLCD(); //RunLCD(); Close; Up;

FireStart(); Close; Down;

3

LPM0;

ReadGP2ST();

ReadData(5,(unsigned char *)ReadRes0,(unsigned char *)Res0); ReadData(5,(unsigned char *)ReadRes1,(unsigned char *)Res1); ReadData(5,(unsigned char *)ReadRes2,(unsigned char *)Res2); ReadData(5,(unsigned char *)ReadRes3,(unsigned char *)Res3); _NOP(); StopWTD(); LPM3;

/*InitialGP2(); Close; Down;

FireStart(); Close; Up;

//LPM0; StopWTD(); LPM3; */ } }

//======================================= //端口初始化

void InitialPort(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

//FLL_CTL0 = XCAP14PF; //set load capacitance for 32k xtal //SCFQCTL = SCFQ_4M;

//LCDCTL = LCDON + LCD4MUX + LCDP1; // STK LCD 4Mux, S0-S19 //BTCTL = BTFRFQ0; // STK LCD freq

P5SEL = 0xFC; // Common and Rxx all selected

P4DIR |= BIT0; //PVCC P1SEL |= BIT5;

P1DIR |= BIT2 + BIT3 + BIT5 + BIT6 + BIT7; //P1.2 SW2,P1.3 SW1 //P1.5 32.768kHz Output as GP2 Clock

//P1.6 as GP2 En_Start,P1.7 as GP2 En_Stop

P2DIR |= BIT2 + BIT3; P2IE |= BIT7; P2IES |= BIT7;

4