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

📄 timer.c

📁 Vitesse 24port gigabit Switch Source Code
💻 C
字号:
/*

    Copyright (c) 2002-2005 Vitesse Semiconductor Corporation "Vitesse".  
    All Rights Reserved.  Unpublished rights reserved under the copyright laws
    of the United States of America, other countries and international treaties.
    The software is provided without a fee. Permission to use, copy, store and 
    modify, the software and its source code is granted. Permission to integrate
    into other products, disclose, transmit and distribute the software in an
    absolute machine readable format (e.g. HEX file) is also granted. 

    The source code of the software may not be disclosed, transmitted or
    distributed without the written permission of Vitesse. The software and its
    source code may only be used in products utilizing a Vitesse VSC73xx product.
 
    This copyright notice must appear in any copy, modification, disclosure,
    transmission or distribution of the software. Vitesse retains all ownership,
    copyright, trade secret and proprietary rights in the software.  

    THIS SOFTWARE HAS BEEN PROVIDED "AS IS," WITHOUT EXPRESS OR IMPLIED WARRANTY
    INCLUDING, WITHOUT LIMITATION, IMPLIED WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR USE AND NON-INFRINGEMENT.

*/
#include "common.h"
#include "hwconf.h"
#include "timer.h"


/* ************************************************************************ **
 *
 *
 * Public data
 *
 *
 *
 * ************************************************************************ */

data ushort tick_count = 0;
bit ms_10_timeout_flag = 0;
bit sec_1_timeout_flag = 0;

/* ************************************************************************ **
 *
 *
 * Defines
 *
 *
 *
 * ************************************************************************ */

#define PRESET_1_MSEC      0x10000 - (CLOCK_FREQ / 12 / (1000 / 1))

#define PRESET_L PRESET_1_MSEC & 0x0FF
#define PRESET_H PRESET_1_MSEC >> 8

/* ************************************************************************ **
 *
 *
 * Typedefs and enums
 *
 *
 *
 * ************************************************************************ */

/* ************************************************************************ **
 *
 *
 * Prototypes for local functions
 *
 *
 *
 * ************************************************************************ */


/* ************************************************************************ **
 *
 *
 * Local data
 *
 *
 *
 * ************************************************************************ */

static uchar data timer_count = 0;

static bit sw_timer_active_flag = FALSE;
static bit ms_1_timeout_flag = FALSE;

/* ************************************************************************ */
void timer_1_init (void) small
/* ------------------------------------------------------------------------ --
 * Purpose     : Set timer 1 to generate interrupt every 1 msec.
 * Remarks     :
 * Restrictions:
 * See also    :
 * Example     :
 * ************************************************************************ */
{
    /* Set timer 1 to mode 1 */
    TMOD &= 0X0F;
    TMOD |= 0X10;

    TL1 = PRESET_L;
    TH1 = PRESET_H;

    PT1 = 0; /* Set low priority for timer 1 */
    ET1 = 1; /* Enable timer 1 interrupt */

    TR1 = 1; /* Start timer 1 */
}

/* ************************************************************************ */
void timer_1_interrupt (void) small interrupt 3 using 1
/* ------------------------------------------------------------------------ --
 * Purpose     : Timer interrupt to be activated every 1 msec. Tick SW timers
 *               and set flags accordingly.
 * Remarks     :
 * Restrictions:
 * See also    :
 * Example     :
 * ************************************************************************ */
{
    static uchar data ms_1_count  = 10;
    static uchar data ms_10_count = 100;

    TR1 = 0; /* Stop timer */

    /* reload timer */
    TL1 = PRESET_L;
    TH1 = PRESET_H;

    TR1 = 1; /* Restart timer */

    tick_count++;
    ms_1_timeout_flag = TRUE;

    if (--ms_1_count == 0) {
        ms_1_count = 10;

        /* Request 10 msec jobs to be done */
        ms_10_timeout_flag = 1;

        /* If 1 sec elapsed, request 1 sec jobs to be done */
        if (--ms_10_count == 0) {
            ms_10_count = 100;
            sec_1_timeout_flag = TRUE;
        }

        /* If the delay/timeout timer is active, tick it */
        if (sw_timer_active_flag) {
            if (--timer_count == 0) {
                sw_timer_active_flag = FALSE;
            }
        }

    }
}

/* ************************************************************************ */
void delay (uchar delay_in_10_msec) small
/* ------------------------------------------------------------------------ --
 * Purpose     : Make a delay by means of timer interrupt function.
 * Remarks     : delay_in_10_msec specifies delay in granularity of 10 msec.
 *               There is no synchronization with the HW timer, so the actual
 *               delay may be +/- 10 msec (so specifying 1 (= 10 msec) may 
 *               result in almost no delay at all).
 * Restrictions: May only be called when interrupt is enabled. Can not be
 *               used simultaneously with start_timer/timeout mechanism.
 * See also    :
 * Example     :
 * ************************************************************************ */
{
    /* set timer value to be ticked by interrupt function */
    sw_timer_active_flag = FALSE;
    timer_count = delay_in_10_msec;
    sw_timer_active_flag = TRUE;

    /* Await that time has elapsed */
    while (sw_timer_active_flag) {
        /* do nothing but wait */
    }
}

/* ************************************************************************ */
void delay_1 (uchar delay_in_1_msec) small
/* ------------------------------------------------------------------------ --
 * Purpose     : Make a delay by means of timer interrupt function.
 * Remarks     : delay_in_1_msec specifies delay in granularity of 1 msec.
 *               There is no synchronization with the HW timer, so the actual
 *               delay may be +/- 1 msec (so specifying 1 (= 1 msec) may 
 *               result in almost no delay at all).
 * Restrictions: May only be called when interrupt is enabled.
 * See also    :
 * Example     :
 * ************************************************************************ */
{
    /* set timer value to be ticked by interrupt function */
    ms_1_timeout_flag = FALSE;

    /* Await that time has elapsed */
    while (delay_in_1_msec-- > 0) {
        while (!ms_1_timeout_flag) {
        }
        ms_1_timeout_flag = FALSE;
    }
}

/* ************************************************************************ */
void start_timer (uchar time_in_10_msec) small
/* ------------------------------------------------------------------------ --
 * Purpose     : Start SW timer running via interrupt function. Status of timer
 *               can be read via timeout function.
 * Remarks     : time_in_10_msec specifies time in granularity of 10 msec.
 *               There is no synchronization with the HW timer, so the actual
 *               time may be +/- 10 msec (so specifying 1 (= 10 msec) may 
 *               result in almost no time at all).
 * Restrictions:
 * See also    : timeout function
 * Example     : 
 *                  start_timer(5);
 *                  while (!timeout()) {
 *                     ... do something
 *                  }
 * ************************************************************************ */
{
    sw_timer_active_flag = 0;
    timer_count = time_in_10_msec;
    sw_timer_active_flag = 1;
}

/* ************************************************************************ */
bool timeout (void) small
/* ------------------------------------------------------------------------ --
 * Purpose     : To check if time specified via call to start_timer function
 *               HAS expired.
 * Remarks     : Return TRUE, if time has expired, otherwiSe FALSE.
 * Restrictions:
 * See also    : start_timer
 * Example     :
 * ************************************************************************ */
{
    return (!sw_timer_active_flag); 
}


⌨️ 快捷键说明

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