模式一&模式二:单次计时&周期计时/*******************************************
开发坏境:CCSv5.4
开发板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:16位定时器,单次定时模式和周期性定时模式
程序说明:
编程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.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_GPIOF);
//使能GPIOF外设
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//使能TIMER0
//TimerConfigure(TIMER0_BASE, TIMER_CFG_ONE_SHOT);//单次计数模式
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);//周期性计数模式
TimerLoadSet(TIMER0_BASE, TIMER_A,SysCtlClockGet() / 10 - 1);//计数频率10HZ
IntEnable(INT_TIMER0A);//NVIC
//使能TIMER0A
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//TIMEOUT标志位触发中断
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
//TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断
while(1)
{
}
}
void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//清除标志位
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2))
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 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
IntEnable(INT_TIMER0A);
//使能TIMER0A
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_MATCH);
//定时器A捕获事件触发中断
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
//TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断
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);
}
模式四:计时捕获
/*******************************************
开发坏境:CCSv5.4
开发板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:16位定时器,计时捕获模式
程序说明:捕获引脚PB6
编程者: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);
TimerConfigure(TIMER0_BASE,TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_SPLIT_PAIR);//计时捕获模式,上升沿捕获
TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_POS_EDGE);
//捕获模式,A定时器,上升沿捕获
IntEnable(INT_TIMER0A);
//使能TIMER0A
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
//定时器A捕获事件触发中断
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
//TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断
while(1)
{
}
}
void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE,TIMER_CAPA_EVENT);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
SysCtlDelay(SysCtlClockGet() / 30);//100ms
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 14);
}
本文来自论坛,点击查看完整帖子内容。