TDC-GP2与MSP430F413通讯C程序
更新时间:2023-12-03 18:09:01 阅读量: 教育文库 文档下载
- tdc gp2推荐度:
- 相关推荐
TDC-GP2与MSP430F413通讯C程序 .......................................................... 1
TDC-GP2与MSP430F413通讯C程序
//======================================== #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
TACTL = TASSEL_1 + MC_2 + TACLR; //ACLK, continous mode ME1 |= USPIE0; // Enable USART0 SPI mode
UCTL0 |= CHAR + SYNC + MM; // 8-bit SPI Master **SWRST** UTCTL0 |= /*CKPH + */SSEL1 + SSEL0 + STC; // SMCLK, 3-pin mode UBR00 = 0x02; // UCLK/2 UBR10 = 0x00; // 0
UMCTL0 = 0x00; // no modulation
UCTL0 &= ~SWRST; // Initalize USART state machine IE1 |= URXIE0; // Receive interrupt enable P3SEL |= 0x0E; // P3.1-3 SPI option select P3DIR |= BIT0 + BIT4; // P3.0 output direction
TBCTL = TBSSEL_1 + ID_2 + TBCLR; //Setup Timer_B TBCCR1 = 16384; TBCCTL1 =CCIE; TBCTL |= MC_2; }
//======================================== //GP2 寄存器配置
void InitialGP2(void) {
unsigned char i = 0; SSNDs; _NOP(); RSTNHigh; _NOP(); RSTNLow; _NOP(); RSTNHigh;
StartEn; //开启start_en StopEn; //开启stop_en
OpCodeGP2(0x50); // GP2复位 _NOP();
ConfigGP2(0x80338468);//内部不分频,fire脉冲频率为1MHz,发送3个fire脉冲。对于4MHz时钟校准是基于8个周期的32K时钟,高速时钟持续工作,温度测量应用高速时钟128us周期时间,2次热身测量,4个温度测量端口(2个温度传感器,一个测冷水,一个测热水)。TDC自动校准,选择测量范围2 _NOP();
ConfigGP2(0x81214400);//ALU计算第一个stop - start _NOP();
ConfigGP2(0x82E01900); //启动所有GP2中断源,设置第一个stop的使能窗口,在50us后允许接受第一个stop脉冲 _NOP();
5
SPIRxCount ++; //SSNDs; } else {
SPIRxCount = 0; }
LPM0_EXIT; }
//======================================== void NOP10(void) {
_NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); }
//=========================================
interrupt [TIMERB1_VECTOR] void timerb1_int(void) {
TBCCR1 +=16384; TBCCTL1 &=~CCIFG; //ClearWTD();
_BIC_SR_IRQ(LPM3_bits); }
//=========================================== interrupt [PORT2_VECTOR] void GP2_int(void) {
P2IFG = 0; LPM0_EXIT; }
11
/******************************************************************** *
* Standard register and bit definitions for the Texas Instruments * MSP430 microcontroller. *
* This file supports assembler and C/EC++ development for * MSP430x43x devices. *
* Version 2.0 *
********************************************************************/
#ifndef __msp430x43x #define __msp430x43x
#if (((__TID__ >> 8) & 0x7F) != 0x2b) /* 0x2b = 43 dec */ #error MSP430X43X.H file for use with ICC430/A430 only #endif
12
#ifdef __IAR_SYSTEMS_ICC__ #include
#pragma language=extended
#define DEFC(name, address) __no_init volatile unsigned char name @ address;
#define DEFW(name, address) __no_init volatile unsigned short name @ address;
#endif /* __IAR_SYSTEMS_ICC__ */
#ifdef __IAR_SYSTEMS_ASM__
#define DEFC(name, address) sfrb name = address; #define DEFW(name, address) sfrw name = address;
#endif /* __IAR_SYSTEMS_ASM__*/
#ifdef __cplusplus #define READ_ONLY
13
#else
#define READ_ONLY const #endif
/************************************************************ * STANDARD BITS
************************************************************/
#define BIT0 (0x0001) #define BIT1 (0x0002) #define BIT2 (0x0004) #define BIT3 (0x0008) #define BIT4 (0x0010) #define BIT5 (0x0020) #define BIT6 (0x0040) #define BIT7 (0x0080) #define BIT8 (0x0100) #define BIT9 (0x0200) #define BITA (0x0400) #define BITB (0x0800) #define BITC (0x1000)
14
#define BITD (0x2000) #define BITE (0x4000) #define BITF (0x8000)
/************************************************************ * STATUS REGISTER BITS
************************************************************/
#define C (0x0001) #define Z (0x0002) #define N (0x0004) #define V (0x0100) #define GIE (0x0008) #define CPUOFF (0x0010) #define OSCOFF (0x0020) #define SCG0 (0x0040) #define SCG1 (0x0080)
/* Low Power Modes coded with Bits 4-7 in SR */
#ifndef __IAR_SYSTEMS_ICC /* Begin #defines for assembler */ #define LPM0 (CPUOFF)
15
正在阅读:
消防安全培训个人心得体会范文合集8篇08-04
演讲比赛通知01-06
毕业论文—水果损伤红外检测系统设计07-25
近代史论文06-02
人教3上语文课后练(第3单元)05-11
奖学金管理信息系统02-28
浴室装修注意事项12-31
顶管施工组织设计06-27
企业青年岗位能手事迹材料两篇02-24
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- MSP430F413
- 通讯
- 程序
- TDC
- GP
- 莫言诺贝尔获奖演讲稿
- 热学习题集-2
- 义务教育湖南文艺出版社六年级下册音乐全册教案
- SkylineV6二次开发培训教程 - 图文
- 《大学计算机基础》课程上机实验指导 - 图文
- 创业管理案例分析题--资料
- ks5u2010届高三地理每周精析精练:天气系统与气候变化
- 微机原理与接口技术题库
- 人教新课标六年级语文上册第七单元《习作七》教学设计
- 大跨度连续梁桥施工控制中的线形控制
- 青科大复合材料原理与工艺课程习题 - 答案
- 阳江市工商业联合会(总商会)第六届执委会成员
- 高淳县污水处理厂二期工程环评
- 气相色谱习题及答案
- 最详细全面的屋面防水施工做法 - 图文
- 清华大学国际经济学期末考试试卷
- 论未来我国民法典中应如何完善不动产登记制度
- 必修1 2.1大气的热力作用、热力环流 - 图文
- 2015年南通职教二模计算机试卷及答案
- (高中)