/////////////csb.c文件#includecsb.h#includeUSART0.hchartable[30];longunsignedintchannel[3];//存放时间uintchannel_count[6]={0};//存放计数值ucharchannel_flag[3]={0};ucharyichu_flag=0;//溢出标志floatdistance1=0;floatdistance2=0;floatdistance3=0;/**************变量定义****************/unsignedcharflag=0;longtime;/*******************************************函数名称:voidInitSys()函数功能:时钟初始化入口参数:无返回值:无*******************************************/voidInitSys(){unsignedintiq0;_DINT();BCSCTL1&=~XT2OFF;do{IFG1&=~OFIFG;//清除振荡器失效标志for(iq0=0xFF;iq00;iq0--);//延时,等待XT2起振}while((IFG1&OFIFG)!=0);//判断XT2是否起振BCSCTL2=SELM1+SELS;//MCLK,SMCLK时钟为XT2}/*******************************************函数名称:voidinit_dvice(void)函数功能:超声波模块初始化入口参数:无返回值:无*******************************************/voidinit_dvice(void){P2DIR|=BIT0+BIT1+BIT2;//输出方向(用来产生大于10us的脉冲)P4DIR&=~(BIT1+BIT2+BIT3);//P1的管脚为输入P4SEL|=(BIT1+BIT2+BIT3);//CCI0A输入TBCTL=TBSSEL_2+TBCLR+ID_3+MC_2;//MCLK,清除TAR,八分频,连续计数模式//CCTL0=CM_1+CCIS_0+CAP+CCIE;//上升沿捕捉模式,选择CCIxA作为捕捉事件的输入信号,//工作在捕捉模式,捕获/比较中断使能TBCCTL3|=CM_1+SCS+CAP+CCIE;//捕获模式,同步捕获,中断打开,上降沿捕获TBCCTL1|=CM_1+SCS+CAP+CCIE;//捕获模式,同步捕获,中断打开,上降沿捕获TBCCTL2|=CM_1+SCS+CAP+CCIE;//捕获模式,同步捕获,中断打开,上降沿捕获//TBCCTL4|=CM_1+SCS+CAP+CCIE;//捕获模式,同步捕获,中断打开,上降沿捕获//TBCTL|=TASSEL_1+TACLR+ID_1+MC_1;//TIMER_B工作于增计数方式//TBCCTL0|=CCIE;//TBCCR0=4096;_EINT();//使能GIE}/*******************************************函数名称:voidstart(void)函数功能:超声波开始信号,产生10us的脉冲入口参数:无返回值:无*******************************************/voidstart1(void){Trig_L1;delay_us(10);Trig_H1;delay_us(10);Trig_L1;}voidstart2(void){Trig_L2;delay_us(10);Trig_H2;delay_us(10);Trig_L2;}voidstart3(void){Trig_L3;delay_us(10);Trig_H3;delay_us(10);Trig_L3;}/*******************************************函数名称:voidcount_distance(void)函数功能:计算距离入口参数:无返回值:无*******************************************/floatcount_distance(longunsignedintti1){floatdista;dista=ti1*0.000173;returndista;//sprintf(table,%f\r\n,distance);//PutString0(table);//Send_Num0(distance);//Disp_Float(24,100,100,distance,1);}voidCSB_scan(void){if(channel_flag[0]==1)//通道1捕获完成{channel_flag[0]=0;if(channel_count[1]channel_count[0])channel[0]=channel_count[1]-channel_count[0];else{channel[0]=65535-channel_count[0]+channel_count[1];yichu_flag=0;}distance1=count_distance(channel[0]);sprintf(table,%f\r,distance1);PutString1(table);}if(channel_flag[1]==1)//通道2捕获完成{channel_flag[1]=0;if(channel_count[3]channel_count[2])channel[1]=channel_count[3]-channel_count[2];else{channel[1]=65535-channel_count[2]+channel_count[3];yichu_flag=0;}distance2=count_distance(channel[1]);sprintf(table,%f\r,distance2);PutString1(table);}if(channel_flag[2]==1)//通道3捕获完成{channel_flag[2]=0;if(channel_count[5]channel_count[4])channel[2]=channel_count[5]-channel_count[4];else{yichu_flag=0;channel[2]=65535-channel_count[4]+channel_count[5];}distance3=count_distance(channel[2]);sprintf(table,%f\r\n,distance3);PutString1(table);}}#pragmavector=TIMERB1_VECTOR__interruptvoidtimera1_vec(){switch(TBIV){case2:if(TBCCTL1&CM_1)//上升沿{TBCCTL1=(TBCCTL1&(~CM_1)|CM_2);//改成下降沿channel_count[2]=TBCCR1;TBCCTL1&=~CCIFG;//清除中断标志}else//下降沿{TBCCTL1=(TBCCTL1&(~CM_2)|CM_1);//改成下降沿channel_count[3]=TBCCR1;channel_flag[1]=1;TBCCTL1&=~CCIFG;//清除中断标志}break;case4:if(TBCCTL2&CM_1)//上升沿{TBCCTL2=(TBCCTL2&(~CM_1)|CM_2);//改成下降沿channel_count[2]=TBCCR2;TBCCTL2&=~CCIFG;//清除中断标志}else//下降沿{TBCCTL2=(TBCCTL2&(~CM_2)|CM_1);//改成下降沿channel_count[3]=TBCCR2;channel_flag[1]=1;TBCCTL2&=~CCIFG;//清除中断标志}break;case6:if(TBCCTL3&CM_1)//上升沿{TBCCTL3=(TBCCTL3&(~CM_1)|CM_2);//改成下降沿channel_count[4]=TBCCR3;TBCCTL3&=~CCIFG;//清除中断标志}else//下降沿{TBCCTL3=(TBCCTL3&(~CM_2)|CM_1);//改成下降沿channel_count[5]=TBCCR3;channel_flag[2]=1;TBCCTL3&=~CCIFG;//清除中断标志}break;case10:yichu_flag++;break;}}////////////////scb.h头文件#ifndef__CSB_H#define__CSB_H#includemsp430x16x.h#includestdio.h#defineucharunsignedchar#defineuintunsignedint#defineCPU_F((double)8000000)#definedelay_us(x)__delay_cycles((long)(CPU_F*(double)x/1000000.0))#definedelay_ms(x)__delay_cycles((long)(CPU_F*(double)x/1000.0))#defineTrig_H1P2OUT|=BIT0//端口P2.0输出高电平#defineTrig_L1P2OUT&=~BIT0//端口P2.0输出低电平#defineTrig_H2P2OUT|=BIT1//端口P2.0输出高电平#defineTrig_L2P2OUT&=~BIT1//端口P2.0输出低电平#defineTrig_H3P2OUT|=BIT2//端口P2.0输出高电平#defineTrig_L3P2OUT&=~BIT2//端口P2.0输出低电平externvoidInitSys();externvoidinit_dvice(void);externvoidstart1(void);externvoidstart2(void);externvoidstart3(void);externfloatcount_distance(longunsignedintti1);externvoidCSB_scan(void);#endif