一、手册有关的定时器资料
下面来了解下ac781x定时器,定时器有下面特性和功能。
比较想了解的就是通道支持链模式。
手册上有关定时器链的介绍
除了定时器0不能连接其他定时器,其他定时器可以组成链模式。
二、定时器应用
2.1、timer.c
#include "ac78xx_timer.h"
#include "timer.h"
#include "led.h"
#define MAX_MIN_SECOND_COUNT 9
#define MAX_SECOND_MINUTE_COUNT 249
#define MAX_HOURS_COUNT 249
/* 以下四个全局变量为具体计数 */
uint8_t g_timer0Count = 0;
uint8_t g_timer1Count = 0;
uint8_t g_timer4Count = 0;
uint8_t g_timer7Count = 0;
/**
*
* @param[in] timerIndex: timer Index from 0 to 7
*
* [url=home.php?mod=space&uid=266161]@return[/url] none
*
* [url=home.php?mod=space&uid=247401]@brief[/url] TIMER0 CallBack
* TIMER0中断回调
*/
void TIM0_Task(uint8_t timerIndex)
{
if (TIMER_GetIntFlag(TIMER0))
{
TIMER_ClrIntFlag(TIMER0); //清中断标志
g_timer0Count ++;
if (g_timer0Count > MAX_MIN_SECOND_COUNT)
{
g_timer0Count = 0;
}
}
}
/**
*
* @param[in] timerIndex: timer Index from 0 to 7
*
* @return none
*
* @brief TIMER1 CallBack
* TIMER1中断回调
*/
void TIM1_Task(uint8_t timerIndex)
{
if (TIMER_GetIntFlag(TIMER1))
{
TIMER_ClrIntFlag(TIMER1);
g_timer1Count ++;
if (g_timer1Count > MAX_SECOND_MINUTE_COUNT)
{
g_timer1Count = 0;
LED1_TOGGLE;
}
}
}
/**
*
* @param[in] timerIndex: timer Index from 0 to 7
*
* @return none
*
* @brief TIMER4 CallBack
* TIMER4中断回调
*/
void TIM4_Task(uint8_t timerIndex)
{
if (TIMER_GetIntFlag(TIMER4))
{
TIMER_ClrIntFlag(TIMER4);
g_timer4Count ++;
LED2_TOGGLE;
if (g_timer4Count > MAX_SECOND_MINUTE_COUNT)
g_timer4Count = 0;
}
}
/**
*
* @param[in] timerIndex: timer Index from 0 to 7
*
* @return none
*
* @brief TIMER7 CallBack
* TIMER7中断回调
*/
uint8_t tt=0;
void TIM7_Task(uint8_t timerIndex)
{
if (TIMER_GetIntFlag(TIMER7))
{
TIMER_ClrIntFlag(TIMER7);
LED3_TOGGLE;
g_timer7Count ++;
if (g_timer7Count > MAX_HOURS_COUNT)
g_timer7Count = 0;
}
}
/**
*
* @param[in] none
*
* @return 0:Success
*
* @brief TIMER init
*(0)timer delay frequency = SYS_CLK / (timerloadValue + 1)
*(1)timer0 for 10ms Delay
*(2)tiemr1 for 0.25s Delay
*(3)timer2,timer3,timer4 used for link mode, for 0.5s delay
*(4)timer5,timer6,timer7 used for link mode, for 1s delay
* timer0,timer1是32位计数器,而其他计数器只有16位,则timerConfig0.loadValue的值应合理设置,不应溢出
* 在48M的总线频率下,timer单独使用的话,timer0~1,最大loadValue为4294967295;其他timer最大loadValue为65535,计数范围较小
* 在链接模式中,timer0是不能链接到任何其他timer上面的
*/
void Timer_Init(void)
{
TIMER_ConfigType timerConfig0 = {0};
TIMER_ConfigType timerConfig1 = {0};
TIMER_ConfigType timerConfig2 = {0};
TIMER_ConfigType timerConfig3 = {0};
TIMER_ConfigType timerConfig4 = {0};
TIMER_ConfigType timerConfig5 = {0};
TIMER_ConfigType timerConfig6 = {0};
TIMER_ConfigType timerConfig7 = {0};
timerConfig0.loadValue = Delay10ms; ///10ms timeout
timerConfig0.interruptEn = 1; //中断使能
timerConfig0.linkMode = 0; //禁能链接
timerConfig0.timerEn = 1; //使能TIMER0
timerConfig1.loadValue = Delay1ms;
timerConfig1.interruptEn = 1;
timerConfig1.linkMode = 0;
timerConfig1.timerEn = 1;
TIMER_SetCallback(TIMER0, TIM0_Task);
TIMER_SetCallback(TIMER1, TIM1_Task);
TIMER_Init(TIMER0, &timerConfig0); //TIMER0配置初始化
TIMER_Init(TIMER1, &timerConfig1);
timerConfig2.loadValue = Delay1ms;
timerConfig2.interruptEn = 0;
timerConfig2.linkMode = 0;
timerConfig2.timerEn = 1;
timerConfig3.loadValue = 249; //0.25s TimerOut
timerConfig3.interruptEn = 0;
timerConfig3.linkMode = 1; //使能链接
timerConfig3.timerEn = 1;
timerConfig4.loadValue = 1; //0.5s TimerOut
timerConfig4.interruptEn = 1; //使能中断
timerConfig4.linkMode = 1; //使能链接
timerConfig4.timerEn = 1;
TIMER_Init(TIMER2, &timerConfig2);
TIMER_Init(TIMER3, &timerConfig3);
TIMER_SetCallback(TIMER4, TIM4_Task); //TIMER4设置回调
TIMER_Init(TIMER4, &timerConfig4); //TIMER4配置初始化
timerConfig5.loadValue = Delay1ms; //1ms TimerOut
timerConfig5.interruptEn = 0; //禁能中断
timerConfig5.linkMode = 0; //禁能链接
timerConfig5.timerEn = 1;
timerConfig6.loadValue = 249; //0.25s TimerOut
timerConfig6.interruptEn = 0;
timerConfig6.linkMode = 1; //使能链接
timerConfig6.timerEn = 1;
timerConfig7.loadValue = 3; //1s TimerOut
timerConfig7.interruptEn = 1; //使能中断
timerConfig7.linkMode = 1; //使能链接
timerConfig7.timerEn = 1;
TIMER_Init(TIMER5, &timerConfig5);
TIMER_Init(TIMER6, &timerConfig6);
TIMER_SetCallback(TIMER7, TIM7_Task);
TIMER_Init(TIMER7, &timerConfig7);
}
2.2、程序实现的功能
1、定时器通道0 实现10ms定时中断
2、定时器通道1 实现0.25s定时中断
3、定时器通道2、通道3、通道4 实现链模式,实现0.5s定时中断
4、定时器通道5、通道6、通道7 实现链模式,实现1s定时中断
2.3、通过这个例程看到,定时器通道n是以通道n-1为基准来计时,实现长时间定时的功能。
|