CC3200——定时器中断

版权声明:虽为原创,欢迎转载。 https://blog.csdn.net/m0_37655357/article/details/86489030

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写的都很棒,一定要利用好。

猜你喜欢

转载自blog.csdn.net/m0_37655357/article/details/86489030
今日推荐