下面面是飞思卡尔XS128单片机测距的程序:
while(1)
{
PT1AD0_PT1AD00 = 1;//给超声波模块输入高脉冲
PITINTE_PINTE1=1; //打开PIT1定时器
while(!(counter0》=4)); //等待20us
PITINTE_PINTE1=0;counter0 = 0;//关闭定时器,计数清零
PT1AD0_PT1AD00 = 0; //trig管脚拉低
PORTB_PB0 = 0; //指示灯0
while(!(PT1AD0_PT1AD01 == 1)); //等待echo输出上升沿
PORTB_PB1 = 0; //指示灯1
PITINTE_PINTE0=1; //打开PIT0定时器
while(!(PT1AD0_PT1AD01 == 0)); //等待下降沿
distance = counter*17/20; //计算距离,单位CM
PITINTE_PINTE0=0; //关闭定时器
PORTB_PB2 = 0; //指示灯2
PITINTE_PINTE0=1; //打开定时器定时500ms,数码管显示
while(!(counter》=10000))
{
Showing(distance); //显示距离,精确1cm
}
PITINTE_PINTE0=0;counter=0; //关闭定时器,清零
} |