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

📄 timer23.c

📁 基于cc1010的设计实例
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***          Simple timer demonstration program          *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * This program demonstrates the use of timers.                              *
 *                                                                           *
 * The program uses timer 3 to generate interrupts at variable intervals     *
 * (initially 50 msecs). The interrupt service routine resets the timer and  *
 * toggles the red and the yellow LEDs                                       *
 *                                                                           *   
 * Use switch 1 or 2 to increase or decrease the timeout.                    *
 *****************************************************************************
 * Author:              TEL, JOL                                             *
 *****************************************************************************
 * Revision history:                                                         *
 * 1.0  2002/08/29      First Public Release                                 *
 *                                                                           *
 * $Log: timer23.c,v $
 * Revision 1.4  2004/01/12 16:40:52  tos
 * Updated timer limit monitor according to lib-fix.
 *
 * Revision 1.3  2003/08/18 12:25:49  tos
 * Synchronisation with library update (reentry issue).
 *
 * Revision 1.2  2002/11/19 15:45:21  kht
 * Added startup macros
 *
 * Revision 1.1  2002/10/14 11:17:14  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/reg1010.h>
#include <chipcon/cc1010eb.h>
#include <chipcon/hal.h>


//ulong timeout;
unsigned long timeout;
int sw1_confirm_count;
int sw2_confirm_count;

#define  SW_CONFIRM_TIMEOUT  0x0700

//----------------------------------------------------------------------------
//  MAIN PROGRAM
//----------------------------------------------------------------------------
void main() {

    // Disable watchdog timer
    WDT_ENABLE(FALSE); 

    // Set optimum settings for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);
    
    // Initialize LEDS
    RLED_OE(TRUE); RLED = LED_OFF;
    YLED_OE(TRUE); YLED = LED_ON;
    GLED_OE(TRUE);

    // Initialise timer 3 to generate interrupt every 50 msec.
    timeout = 50000;
    halConfigTimer23(TIMER3 | TIMER23_INT_TIMER, timeout, CC1010EB_CLKFREQ);

    // Setup timer 3 as an interrupt source
    INT_ENABLE(INUM_TIMER3, INT_ON);
    INT_PRIORITY(INUM_TIMER3, INT_LOW);

    // Turn on interrupts
    INT_GLOBAL_ENABLE(INT_ON);

    TIMER3_RUN(TRUE);

    // Initialise switch/button-press confirmation
    sw1_confirm_count = SW_CONFIRM_TIMEOUT;
    sw2_confirm_count = SW_CONFIRM_TIMEOUT;

    // Loop forever: step up/down pwm duty-cycle according to button press
    while (TRUE) {

        if (SW1_PRESSED) {
            sw2_confirm_count = SW_CONFIRM_TIMEOUT;
            if (sw1_confirm_count-- < 0) {
                sw1_confirm_count = SW_CONFIRM_TIMEOUT;
                // Upper timer limit = ~ 1.13 sec (xTAL = 14.746 MHz)
                if (timeout < 1133983-100) {
                    timeout += 100;
                }
                INT_ENABLE(INUM_TIMER3, INT_OFF);
                INT_SETFLAG (INUM_TIMER3, INT_CLR);
                halConfigTimer23(TIMER3 | TIMER23_INT_TIMER, timeout, CC1010EB_CLKFREQ);
                TIMER3_RUN(TRUE);
            }
        } else if (SW2_PRESSED) {
            sw1_confirm_count = SW_CONFIRM_TIMEOUT;
            if (sw2_confirm_count-- < 0) {
                sw2_confirm_count = SW_CONFIRM_TIMEOUT;
                // Lower timer limit = ~ 17 usec (xTAL = 14.746 MHz)
                if (timeout >= 100) {
                    timeout -= 100;
                }
                INT_ENABLE(INUM_TIMER3, INT_OFF);
                INT_SETFLAG (INUM_TIMER3, INT_CLR);
                halConfigTimer23(TIMER3 | TIMER23_INT_TIMER, timeout, CC1010EB_CLKFREQ);
                TIMER3_RUN(TRUE);
            }
        } else {
            sw1_confirm_count = SW_CONFIRM_TIMEOUT;
            sw2_confirm_count = SW_CONFIRM_TIMEOUT;
        }
    }

} // main


//----------------------------------------------------------------------------
//  Timer 3 interrupt service routine:
//----------------------------------------------------------------------------
//void TIMER3_ISR() interrupt INUM_TIMER3 {
void TIMER3_ISR() interrupt INUM_TIMER3 {

    INT_SETFLAG (INUM_TIMER3, INT_CLR);

    // Toggle leds
    RLED = ~RLED;   
    YLED = ~YLED;

} // TIMER3_ISR

⌨️ 快捷键说明

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