⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configtimer23.c

📁 ti-Chipcon CC1010 1G以下Soc源码库。包括rf,powermodes,clockmodes,flashRW,interrupts,timer,pwm,uart...所有底层驱动源码
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                            CHIPCON CC1010                    *
 *      ***   + +   ***                 HAL - ConfigTimer23                  *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *****************************************************************************
 * Author:              ROH                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: ConfigTimer23.c,v $
 * Revision 1.4  2004/01/08 18:57:04  tos
 * Corrected limit algorithm for Timer23 function.
 *
 * Revision 1.3  2003/08/11 15:02:38  tos
 * Corrected reentrant usage in library: reentrant only if using large memory model.
 *
 * Revision 1.2  2003/07/29 11:25:32  tos
 * Made halWait() and halConfigTimer23() reentrant.
 *
 * Revision 1.1  2002/10/14 13:04:30  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/hal.h>


//----------------------------------------------------------------------------
// ulong halConfigTimer23(...);
//
//  Description:
//      This function configures timer 2 or 3 (depending on the value given in
//      _option_ as either an interrupt timer (an interrupt is generated at
//      certain intervals in time, as specified by _period) or a pulse width 
//      modulator (PWM).
//      If _period_ is specified as 0, then, in timer mode the timeout peiod will
//      be set to the maximum possible, and in PWM mode the period will be
//      set as long as possible. Using the PWM mode of timer 2/3 overrides the
//      normal operation of ports P3.4/P3.5 and can thus not be used in conjunction
//      with timer 0/1 configured as counters. The duty cycle is set to 50% (128/255)
//      initially in PWM mode.
//      The timer/PWM must be started with macro TIMERx_RUN(TRUE).
//
//  Arguments:
//      byte options
//          Options indicating which timer to configure and how. Different
//          constants for _option_ is defined below.
//      ulong period
//          The desired period between interrupts in microseconds. In PWM mode
//          the duty cycle will be set as close to 50% as possible. This duty
//          cycle can be changed (in an ISR or at any other time) by using the
//          appropriate PWMx_SET_DUTY_CYCLE(...) macro. If _period_
//          is 0, then the maximum period possible will be set. The period can
//          also be adjusted dynamically with the PWMx_SET_PERIOD(...) macro.
//      word clkFreq
//          The XOSC clock frequency in kHz.
//
//  Return value:
//      ulong
//          The actual period in microseconds or zero if the desired period is
//          too high.
//----------------------------------------------------------------------------
#if (__MODEL__ == 0) // Memory model: 0 = SMALL, 1 = COMPACT, 2 = LARGE
ulong halConfigTimer23(byte options, ulong period, word clkFreq) {
#else
ulong halConfigTimer23(byte options, ulong period, word clkFreq) reentrant {
#endif

    byte tx, txpre, mode;

    // Calculate achievable period:
    if (period >= 0x10000) {
        period = (((period>>4)*(clkFreq>>4))+(127500>>8))/(255000>>8);
    } else {
        period = ((period*clkFreq)+127500)/255000;
    }

    if (period)
        period--;

    mode=0;

    if (options&TIMER23_PWM) {
        // PWM mode selected
        if (period&0xFFFFFF00)     // >255
            return 0;
        txpre=(byte)period;
        tx=128;
        mode=1;
    } else {
        // Timer mode selected        
        if (period&0xFFFF0000)      // >63535
            return 0;
        txpre=(byte)((word)period>>8);
        tx=(byte)period;
    }
  
    if (options&TIMER3) {
        // Timer 3 selected
        if (options&TIMER23_INT_TIMER) {
            EXIF&=~0x80;    // Clear interrupt flag
            ET3=1;          // Enable timer interrupt
            EA=1;           // Enable global interrupt
        }
        T3PRE=txpre;
        T3=tx;
        TCON2=(TCON2&~0x0C)|(mode<<2);
    } else {
        // Timer 2 selected
        if (options&TIMER23_INT_TIMER) {
            EXIF&=~0x20;    // Clear interrupt flag
            ET2=1;          // Enable timer interrupt
            EA=1;           // Enable global interrupt
        }
        T2PRE=txpre;
        T2=tx;            
        TCON2=(TCON2&~0x03)|mode;
    }

    period=(period+1)*255000/clkFreq;

    return period;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -