1、CC3200一共有4組定時器,每組兩個16位定時器,這兩個可以串聯成一個32位的定時器。
2、每個定時器都有5種模式可供選擇
3、使用定時器的配置
在SDK中提供了很好的定時器配置函數可供調用,可以很方便的完成定時器的配置。
(1)初始化定時器,包括定時器的時鐘使能和復位。定時器屬於外設,任何外設使用前都要經過這兩部——時鐘使能和復位。
MAP_PRCMPeripheralClkEnable(ePeripheral, PRCM_RUN_MODE_CLK);
MAP_PRCMPeripheralReset(ePeripheral);
(2)定時器的模式配置和分頻係數選擇
MAP_TimerConfigure(ulBase,ulConfig);
MAP_TimerPrescaleSet(ulBase,ulTimer,ulValue);
所謂模式,就是選擇定時器的上述五種模式之一。分頻係數只有16位定時器才能設置,當使用32位的定時器時,設置爲0;
上述四個函數被封裝成了一個函數:
//*****************************************************************************
//
//! Initializing the Timer
//!
//! \param ePeripheral is the peripheral which need to be initialized.
//! \param ulBase is the base address for the timer.
//! \param ulConfig is the configuration for the timer.
//! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH.
//! \param ulValue is the timer prescale value which must be between 0 and
//! 255 (inclusive) for 16/32-bit timers and between 0 and 65535 (inclusive)
//! for 32/64-bit timers.
//! This function
//! 1. Enables and reset the peripheral for the timer.
//! 2. Configures and set the prescale value for the timer.
//!
//! \return none
//
//*****************************************************************************
void Timer_IF_Init( unsigned long ePeripheral, unsigned long ulBase, unsigned
long ulConfig, unsigned long ulTimer, unsigned long ulValue)
{
//
// Initialize GPT A0 (in 32 bit mode) as periodic down counter.
//
MAP_PRCMPeripheralClkEnable(ePeripheral, PRCM_RUN_MODE_CLK);
MAP_PRCMPeripheralReset(ePeripheral);
MAP_TimerConfigure(ulBase,ulConfig);
MAP_TimerPrescaleSet(ulBase,ulTimer,ulValue);
}
(3)定時器中斷優先級與中斷函數註冊
MAP_IntPrioritySet(GetPeripheralIntNum(ulBase, ulTimer), INT_PRIORITY_LVL_1);
MAP_TimerIntRegister(ulBase, ulTimer, TimerBaseIntHandler);
GetPeripheralIntNum函數根據定時器基地址選擇定時器的中斷配置宏。
(4)使能定時器中斷允許
if(ulTimer == TIMER_BOTH)
{
MAP_TimerIntEnable(ulBase, TIMER_TIMA_TIMEOUT|TIMER_TIMB_TIMEOUT);
}
else
{
MAP_TimerIntEnable(ulBase, ((ulTimer == TIMER_A) ? TIMER_TIMA_TIMEOUT :
TIMER_TIMB_TIMEOUT));
}
還是被進一步封裝了。
(5)裝載初始值與使能定時器
//*****************************************************************************
//
//! starts the timer
//!
//! \param ulBase is the base address for the timer.
//! \param ulTimer selects amoung the TIMER_A or TIMER_B or TIMER_BOTH.
//! \param ulValue is the time delay in mSec after that run out,
//! timer gives an interrupt.
//!
//! This function
//! 1. Load the Timer with the specified value.
//! 2. enables the timer.
//!
//! \return none
//!
//! \Note- HW Timer runs on 80MHz clock
//
//*****************************************************************************
void Timer_IF_Start(unsigned long ulBase, unsigned long ulTimer,
unsigned long ulValue)
{
MAP_TimerLoadSet(ulBase,ulTimer,MILLISECONDS_TO_TICKS(ulValue));
//
// Enable the GPT
//
MAP_TimerEnable(ulBase,ulTimer);
}
對於裝載初始值函數MAP_TimerLoadSet(ulBase,ulTimer,MILLISECONDS_TO_TICKS(ulValue));內部的帶參宏,參見定時器定時計算那一章
秉承所見即所得的原則,參數vlValue就是要設置的定時時長,單位ms,帶參宏就是爲了實現這個所見即所得。那個宏就是讓80MHz的主頻除以1000,可以表示定時器1ms計的數,那麼乘以後面的參數vlValue,就是定時vlValue(ms)的的時長。
(6)和外部中斷一樣,他的中斷函數調用利用了函數指針。自己編寫需要被中斷執行的函數,並作爲參數傳給Timer_IF_IntSetup函數。
但是在中斷函數中需要注意,最好首先清除定時器的中斷標誌,防止退出定時器中斷函數的時候,中斷標誌還沒有被清除,這樣的話,會導致剛退出中斷又會在此進入。原因是,中斷標誌的清除需要花費幾個時鐘週期才能完成。
(7)官方SDK寫的都很棒,一定要利用好。