液晶显示没有问题
#include "msp430.h"
#include "cry1602.h"
typedef unsigned char uchar;
typedef unsigned int uint;
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define NUM 11
unsigned int new_cap=0;
unsigned int old_cap=0;
long diff[NUM]; //测量值
int index=0; //测量次数
int N1; //溢出次数
long data;
unsigned char dN[5];
void InitCsb()
{
P3DIR|=BIT5; //P34输出
P3OUT&=~BIT5;
P3OUT|=BIT5;
delay_us(20); //20usTTL 启动超声波
P3OUT&=~BIT5;
}
void disp(long datas)
{
dN[0]=datas%10;
dN[1]=datas/10%10;
dN[2]=(datas/100)%10;
dN[3]=datas/1000;
}
void main( void )
{
int k;
uchar i;
WDTCTL = WDTPW + WDTHOLD; //关狗
/*------选择系统主时钟为8MHz-------*/
BCSCTL1 &= ~XT2OFF; //打开XT2高频晶体振荡器
do
{
IFG1 &= ~OFIFG; //清除晶振失败标志
for (i = 0xFF; i > 0; i--); //等待8MHz晶体起振
}
while ((IFG1 & OFIFG)); //晶振失效标志仍然存在?
BCSCTL2 |= SELM_2 + SELS; //MCLK和SMCLK选择高频晶振
//计数时钟选择SMLK=8MHz,1/8分频后为1MHz
TACTL |= TASSEL_2 + ID_3;
P1SEL|=BIT5; //TA0 检测输入信号
CCTL0=CM_3+SCS+CCIS_0+CAP+CCIE; //上升沿下降沿都捕获+同步捕获+CCIS0(TA0)+捕获模式+中断使能
TACTL=TASSEL_2+MC_2+TAIE; //SMCLK+连续模式+中断允许
//打开全局中断
_EINT();
LcdReset();
InitCsb();
P3DIR|=BIT5; //P35输入
P3OUT|=BIT5;
delay_ms(1); //等待内部模块响应
DispStr(0,0,"the distance is:");
//循环读数显示
while(1)
{
if(index==0) //多次测量取平均值
{
data=0;
for(k=1;k<NUM-1;k++) //舍去第一次和最后一次
data+=diff[k];
data=data/9*170;
}
disp(data);
Disp1Char(4,1,dN[3]+0x30);
Disp1Char(5,1,dN[2]+0x30);
Disp1Char(6,1,0x2e);
Disp1Char(7,1,dN[1]+0x30);
Disp1Char(8,1,dN[0]+0x30);
}
}
#pragma vector=TIMERA0_VECTOR
__interrupt void TIMER_A0(void)
{
new_cap=TACCR0;
diff[index]=65536*N1+new_cap-old_cap;
index++;
if(index==NUM)
index=0;
old_cap=new_cap;
N1=0;
}
#pragma vector=TIMERA1_VECTOR
__interrupt void TIMER_A(void) //定时器A中断
{
if(TAIV==10)
N1++; //溢出加1
}
|