请教为什么下面程序明明捕获值是3,可为什么单片机实际情况却是复位后第一次捕获值是3,以后却是4了,搞了好久始终想不通,请大神帮帮忙吧。
/*******************************************
开发坏境:CCSv5.4
开发板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:16位定时器,计数捕获模式,递增计数模式
程序说明:捕获引脚PB6,当捕获边沿数达到Match时产生中断
编程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
//5分频,使用PLL,外部晶振16M,system时钟源选择 main osc。系统时钟40MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//使能TIMER0 32位(TIMER0A16位+TIMER0B16位)
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//使能GPIOF和GPIOC外设
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinConfigure(GPIO_PB6_T0CCP0); //#define GPIO_PC4_WT0CCP0 0x00021007
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);
//计数捕获模式,上升沿捕获,Two half-width timers
TimerConfigure(TIMER0_BASE,TIMER_CFG_A_CAP_COUNT_UP|TIMER_CFG_SPLIT_PAIR);
//捕获模式,A定时器,上升沿捕获,增计数模式,到达匹配值后可自动清零
TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_POS_EDGE);
//TimerLoadSet(TIMER0_BASE, TIMER_A, 6);//溢出值6
TimerMatchSet(TIMER0_BASE, TIMER_A, 3);//匹配值3
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_MATCH);
IntEnable(INT_TIMER0A);
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
while(1)
{
}
}
void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE,TIMER_CAPA_MATCH);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 14);
SysCtlDelay(SysCtlClockGet() / 30);//100ms
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
|