zigbee 协议栈的时钟和定时器分频

系统时钟和定时器节拍时钟 设置寄存器


tickspd用于产生timer1 ,3,4的基本节拍,最大可以是32MHZ
此时定时器加1的所需事件最短,为1/32 us 
定时器除了tickspd的分频,还可以在tickspd的基础上分频,比如timer3




clkspd用于产生cpu执行指令的基本节拍,最大可以使32MHZ,
此时执行asm("NOP")所用时间是 < 0.1 us(如下测试输出方波周期是0.375*2)
while(1)
{
asm("NOP");
P1_0=!P1_0;
}


协议栈中默认tickspd为0,即ticker=32MHZ
但是如果使用了HalTimerInit函数,就不是了

void HalTimerInit (void)
{
  T1CCTL0 = 0;    /* Make sure interrupts are disabled */
  T1CCTL1 = 0;    /* Make sure interrupts are disabled */
  T1CCTL2 = 0;    /* Make sure interrupts are disabled */
  T3CCTL0 = 0;    /* Make sure interrupts are disabled */
  T3CCTL1 = 0;    /* Make sure interrupts are disabled */
  T4CCTL0 = 0;    /* Make sure interrupts are disabled */
  T4CCTL1 = 0;    /* Make sure interrupts are disabled */

  /* Setup prescale & clock for timer0 */
  halTimerRecord[HW_TIMER_1].prescale    = HAL_TIMER1_16_PRESCALE;
  halTimerRecord[HW_TIMER_1].clock       = HAL_TIMER_32MHZ;
  halTimerRecord[HW_TIMER_1].prescaleVal = HAL_TIMER1_16_PRESCALE_VAL;

  /* Setup prescale & clock for timer2 */
  halTimerRecord[HW_TIMER_3].prescale    = HAL_TIMER3_8_PRESCALE;
  halTimerRecord[HW_TIMER_3].clock       = HAL_TIMER_32MHZ;
  halTimerRecord[HW_TIMER_3].prescaleVal = HAL_TIMER3_8_PRESCALE_VAL;

  /* Setup prescale & clock for timer3 */
  halTimerRecord[HW_TIMER_4].prescale    = HAL_TIMER4_8_PRESCALE;
  halTimerRecord[HW_TIMER_4].clock       = HAL_TIMER_32MHZ;
  halTimerRecord[HW_TIMER_4].prescaleVal = HAL_TIMER4_8_PRESCALE_VAL;

  /* Setup Timer1 Channel structure */
  halTimerChannel[HW_TIMER_1].TxCCTL =  TCHN_T1CCTL;
  halTimerChannel[HW_TIMER_1].TxCCL =   TCHN_T1CCL;
  halTimerChannel[HW_TIMER_1].TxCCH =   TCHN_T1CCH;
  halTimerChannel[HW_TIMER_1].TxOVF =   TCNH_T1OVF;
  halTimerChannel[HW_TIMER_1].ovfbit =  TCHN_T1OVFBIT;
  halTimerChannel[HW_TIMER_1].intbit =  TCHN_T1INTBIT;

  /* Setup Timer3 Channel structure */
  halTimerChannel[HW_TIMER_3].TxCCTL =  TCHN_T3CCTL;
  halTimerChannel[HW_TIMER_3].TxCCL =   TCHN_T3CCL;
  halTimerChannel[HW_TIMER_3].TxCCH =   TCHN_T3CCH;
  halTimerChannel[HW_TIMER_3].TxOVF =   TCNH_T3OVF;
  halTimerChannel[HW_TIMER_3].ovfbit =  TCHN_T3OVFBIT;
  halTimerChannel[HW_TIMER_3].intbit =  TCHN_T3INTBIT;

  /* Setup Timer4 Channel structure */
  halTimerChannel[HW_TIMER_4].TxCCTL =  TCHN_T4CCTL;
  halTimerChannel[HW_TIMER_4].TxCCL =   TCHN_T4CCL;
  halTimerChannel[HW_TIMER_4].TxCCH =   TCHN_T4CCH;
  halTimerChannel[HW_TIMER_4].TxOVF =   TCNH_T4OVF;
  halTimerChannel[HW_TIMER_4].ovfbit =  TCHN_T4OVFBIT;
  halTimerChannel[HW_TIMER_4].intbit =  TCHN_T4INTBIT;
}

比如
        // 使用硬件定时器4
        HalTimerInit ( );
        
        HalTimerConfig (HAL_TIMER_2,                        // 8bit timer2
                      HAL_TIMER_MODE_NORMAL,                 // Clear Timer on Compare
                      HAL_TIMER_CHANNEL_A,           // Channel 1 - default
                      HAL_TIMER_CH_MODE_OVERFLOW,   // 溢出模式
                      TRUE,                         // Use interrupt
                      IR38KHZTimerCallBack);             // Channel Mode
                     
        HalTimerStart(HAL_TIMER_2,1);//halTimerSetCount
        halTimerSetPrescale(HAL_TIMER_2,0xa0);//0x00-20-40-60-80-a0-c0-e0----> /1---/128
extern uint8 halTimerSetPrescale (uint8 hwtimerid, uint8 prescale);
extern uint8 halTimerSetCount (uint8 hwtimerid, uint32 timePerTick);
void IR38KHZTimerCallBack ( uint8 timerId, uint8 channel, uint8 channelMode);
void IR38KHZTimerCallBack ( uint8 timerId, uint8 channel, uint8 channelMode)
{

  if ((timerId == HAL_TIMER_2) && (channelMode == HAL_TIMER_CH_MODE_OUTPUT_COMPARE))
  {
  //osal_update_timers();
  }
}



posted on 2013-02-16 19:03  _song  阅读(1003)  评论(0编辑  收藏  举报