本次学习的目的是如何使用Timer,使用定时器中断,来改变LED状态
定时器操作涉及到的相关函数:
l TimerConfigure 配置定时器;
l TimerDisable 禁止定时器;
l TimerEnable 使能定时器;
l TimerIntClear 清除定时器中断源;
l TimerIntEnable 使能指定的定时器中断源;
l TimerIntStatus 获取当前的中断状态;
l TimerLoadSet 设置定时器装载值。
1.同样首先我们需要在startup_rvmdk.S中定义中断服务子程序,这里使用timer0,注意修改的位置
EXTERN Timer0IntHandler
DCD Timer0IntHandler ; Timer 0 subtimer A
2.使能TIMER0功能,同时配置LED,这和前面的是一样的
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIODirModeSet(GPIO_PORTF_BASE,LED,GPIO_DIR_MODE_OUT);
GPIOPadConfigSet(GPIO_PORTF_BASE,LED,GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD);
3. 配置Timer0为周期性触发模式
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
4.配置定时器装载值,为1S
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet());
5. 使能Timer0的相关中断
IntEnable(INT_TIMER0A);//使能Timer0外设
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);//设置Timer0为
TimerEnable(TIMER0_BASE, TIMER_A);//使能Timer0
实验的效果的是每隔1S钟,LED状态变化一次
实验代码如下:
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "drivers/rit128x96x4.h"
#define LED GPIO_PIN_0
Void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
GPIOPinWrite(GPIO_PORTF_BASE, LED, LED^GPIOPinRead(GPIO_PORTF_BASE,LED));
}
Int main(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIODirModeSet(GPIO_PORTF_BASE,LED,GPIO_DIR_MODE_OUT);
GPIOPadConfigSet(GPIO_PORTF_BASE,LED,GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD);
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet());
IntEnable(INT_TIMER0A);
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
TimerEnable(TIMER0_BASE, TIMER_A);
while(1)
{
}
}