大家好。
我用的是TM4C123GH6PM来测量码盘发出的方波信号的上升沿和下降沿。
timer设置如下:
void TimerInit() {
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// configure the pins to capture.
GPIOPinConfigure(GPIO_PB0_T2CCP0);
GPIOPinConfigure(GPIO_PB1_T2CCP1);
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_0);
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_1);
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_0,
GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_1,
GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
//GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_DIR_MODE_IN);
//GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_DIR_MODE_IN);
// Configure the 32-bit periodic timers.
TimerConfigure(TIMER2_BASE, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_COUNT_UP |
TIMER_CFG_B_CAP_COUNT_UP));
// Configure the counters to count positive edges.
TimerControlEvent(TIMER2_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE);
TimerControlEvent(TIMER2_BASE, TIMER_B, TIMER_EVENT_NEG_EDGE);
TimerLoadSet(TIMER2_BASE, TIMER_A, 0);
TimerLoadSet(TIMER2_BASE, TIMER_B, 0);
TimerMatchSet(TIMER2_BASE, TIMER_A, encodingDiskCon);
TimerMatchSet(TIMER2_BASE, TIMER_B, 65535);
// INT
IntEnable(INT_TIMER2A);
IntEnable(INT_TIMER2B);
TimerIntEnable(TIMER2_BASE, TIMER_CAPA_MATCH);
TimerIntEnable(TIMER2_BASE, TIMER_CAPB_MATCH);
// Need this ???
// TimerIntRegister(TIMER2_BASE, TIMER_A, Timer2AIntHandler);
// enable timer
TimerEnable(TIMER2_BASE, TIMER_BOTH);
}
然后主函数用
while (1) {
UARTprintf("%d ", TimerLoadGet(TIMER2_BASE, TIMER_A));
SysCtlDelay(SysCtlClockGet() / 3);
}
发送TimerLoadGet(TIMER2_BASE, TIMER_A),不管码盘怎么转都输出0,而且用示波器检查了码盘输出信号也很正常。
求助!
Wellin Zhang:
David你好,
根据你的代码应该是捕获的下降沿,同时你用TimerLoadGet(TIMER2_BASE, TIMER_A)函数打印捕获个数是不行的。 该函数的作用是获取load到TimeA的值,根据程序你加载的是0.输出自然会是0.
试一下这个函数
TimerValueGet(TIMER2_BASE, TIMER_A);
dorene jong:
请问这是会work的吗?
Dorene