TM4C123GH6PM的定时器捕获中断实现

时间:2022-12-07 23:30:23
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_nvic.h"
#include "inc/hw_ints.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/timer.h"
#include "utils/uartstdio.h"

void Timer0B_Int_Handler();
int main(void)
{
//system config
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL
|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlDelay(200);//配置完系统寄存器后要延时一段时间

  //GPIO config
GPIOPinConfigure(GPIO_PB7_T0CCP1);
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_7);

//Timer config
TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_CAP_TIME_UP);
TimerControlEvent(TIMER0_BASE, TIMER_B, TIMER_EVENT_POS_EDGE);

//Int config
IntEnable(INT_TIMER0B);
TimerIntEnable(TIMER0_BASE, TIMER_CAPB_EVENT);
IntMasterEnable();
TimerEnable(TIMER0_BASE, TIMER_B);

 while(1);
}
void Timer0B_Int_Handler()
{
TimerIntClear(TIMER0_BASE, TIMER_CAPB_EVENT);
}
在PB7上接入高电平,就可以检测到上升沿进入中单咯~
要特别注意的:
_要记得在中断向量表(startup_ccs.c)中先声明中断处理函数(extern Timer0B_Int_Handler();),
并将对应的中断的中断处理函数(IntDefaultHandler)替换为自己写的函数名。