#includemsp430x14x.h#includelcd12864.h#defineucharunsignedchar#defineuintunsignedint/***************端口定义*******************************/#definetrig_hP2OUT|=BIT0//触发端口#definetrig_lP2OUT&=~BIT0#defineechoP2IN&BIT1//接收端口uintge,shi,bai,dian1,dian2,dian3,cnt;unsignedlonginttim_data,dista_data,dista_data_all;/***************延时*******************************/voiddelay(uintn){uchari;for(;n0;n--)for(i=10;i0;i--);}/********************系统时钟初始化****************/voidinit_clk(void)//初始化系统时钟{uchari;BCSCTL1&=~XT2OFF;//打开XT2振荡器BCSCTL2|=SELM_2+SELS;do{IFG1&=~OFIFG;//清除错误标志for(i=0;i0xff;i++);//延时等待}while((IFG1&OFIFG)!=0);IFG1&=~OFIFG;}/********************滤波,取十次数据平均值***************/voiddista_f(unsignedlongintdistance_data){dista_data_all=dista_data_all+distance_data;cnt=cnt+1;if(cnt==10){cnt=0;dista_data_sum=dista_data_all/10;dista_data_all=0;}}voidmain(void){//StopwatchdogtimertopreventtimeoutresetWDTCTL=WDTPW+WDTHOLD;P1DIR=0XFF;P1OUT|=0XFF;P2DIR=0XFD;P2OUT|=0XFF;P3DIR=0XFF;P3OUT|=0XFF;P4DIR|=0XFF;P4OUT|=0Xff;P5DIR|=0XFF;P5OUT|=0Xff;P6DIR|=0XFF;P6OUT|=0Xff;init_clk();lcd12864_init();init();TACTL|=TACLR+TASSEL_2+ID_3;//定时器A初始化ge=0;shi=0;bai=0;dian1=0;dian2=0;dian3=0;tim_data=0;cnt=0;dista_data=0;P6DIR|=BIT2;P6OUT|=BIT2;P2IE|=BIT1;//打开P2端口中断P2IES&=~BIT1;//设置为上升沿中断_EINT();//打开总中断while(1){trig_h;//超声波触发delay(2);trig_l;delay(50);while(echo);}}/*****************************P2口中断*******************/#pragmavector=PORT2_VECTOR__interruptvoidport_init(void){if(echo)//上升沿中断{TACTL|=TACLR+TASSEL_2+ID_3;TACTL|=MC_2;//接收到信号,打开计数器开始计数P2IES|=BIT1;//改为P2口下降沿中断}else//下降沿中断{TACTL=0;//计数结束,停止计数TACTL|=TASSEL_2+ID_3;P2IES&=~BIT1;//改为P2口上升沿中断tim_data=TAR;//取计数值dista_data=(tim_data*1000/58);//计算为距离(厘米=us/58)dista_f(dista_data);//取十次平均值display_shujc(1,0,dista_data_sum);//在12864上显示距离}P2IFG&=~BIT1;//P2口中断标志清除}/*****************************lcd12864*********************************/#defineucharunsignedchar#defineuintunsignedint#defineRS_0(P3OUT&=~BIT0)#defineRS_1(P3OUT|=BIT0)#defineRW_0(P3OUT&=~BIT1)#defineRW_1(P3OUT|=BIT1)#defineEN_0(P3OUT&=~BIT2)#defineEN_1(P3OUT|=BIT2)unsignedlongintdista_data_sum;/****************延时1ms******************/voiddelayNms(uchartime){uinti,j;for(i=0;itime;i++){for(j=0;j150;j++);}}/*************IO初始化*****************************/voidlcd12864_init(void){P4DIR=0XFF;P6DIR|=BIT2;P6DIR|=BIT3;P3DIR|=0X07;}/**********lcdd写指令**************************/voidwrite_con(ucharcon){RS_0;RW_0;EN_0;P4OUT=con;delayNms(2);EN_1;EN_0;}/****************lcd写数据********************************/voidwrite_data(uchardata){RS_1;RW_0;EN_0;P4OUT=data;delayNms(2);EN_1;EN_0;}/***********lcd初始化****************************/voidinit(){P6OUT|=0X04;P6OUT|=BIT3;write_con(0x30);//基本指令集delayNms(2);write_con(0x02);//地址归位delayNms(2);write_con(0x0c);//显示delayNms(2);write_con(0x01);//清屏delayNms(2);write_con(0x06);//点设定delayNms(2);}/*****************lcd写指令/数据地址****************************/voidwrite_ardd(ucharhang,ucharlie){switch(hang){case1:write_con(0x80+lie);break;case2:write_con(0x90+lie);break;case3:write_con(0x88+lie);break;case4:write_con(0x98+lie);break;default:break;}}/*********************lcd显示数据串**************************************/voiddisplay_shujc(ucharhang,ucharlie,unsignedlongintshujc){uinti;uchartab[7];write_ardd(hang,lie);tab[0]=dista_data_sum/100000;tab[1]=(dista_data_sum%100000)/10000;tab[2]=(dista_data_sum%10000)/1000;tab[3]=0x2e;tab[4]=(dista_data_sum%1000)/100;tab[5]=(dista_data_sum%100)/10;tab[6]=dista_data_sum%10;for(i=0;i3;i++){write_data(0x30+tab[i]);}write_data(tab[3]);for(i=4;i7;i++){write_data(0x30+tab[i]);}}