|
本帖最后由 ZhengLang 于 2014-7-20 21:23 编辑
void Ultrasonic_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB4_T1CCP0);
GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPinTypeTimer(GPIO_PORTB_BASE,GPIO_PIN_4);
//
TimerClockSourceSet(TIMER1_BASE,TIMER_CLOCK_SYSTEM);
//
TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_CAP_TIME_UP);
TimerControlEvent(TIMER1_BASE, TIMER_A, TIMER_EVENT_BOTH_EDGES);
TimerIntEnable(TIMER1_BASE, TIMER_CAPA_EVENT);
IntEnable(INT_TIMER1A);
TimerEnable(TIMER1_BASE, TIMER_A );
}
flagX是用来判断是上升沿还是下降沿
void TIMER1A_Handler(void)
{
TimerIntClear(TIMER1_BASE,TIMER_CAPA_EVENT);
flagX++;
if(flagX==2)
StartX=TimerValueGet(TIMER1_BASE,TIMER_A);
if(flagX ==3)
{
EndX=TimerValueGet(TIMER1_BASE,TIMER_A);
flagX=1;
}
}
这个函数用来得到高低电平之间的脉冲数
float UltrasonicValueGet(void)
{
if(EndX
{Distance = 0xffffFF-StartX+EndX;}
if(EndX>StartX)
{Distance = EndX-StartX;}
}
由于我这个程序高电平时间脉冲数少于0xffffff.所有最多有一次从最高到最低的跳变。如果高电平时间比较长。程序要修改下
|
|