📄 timer.c
字号:
/*------------------------------------------------------------------------------
TIMER.C: Timer tick routines.
------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
Copyright (c) 2002 ST Microelectronics
This example demo code is provided as is and has no warranty,
implied or otherwise. You are free to use/modify any of the provided
code at your own risk in your applications with the expressed limitation
of liability (see below) so long as your product using the code contains
at least one uPSD products (device).
LIMITATION OF LIABILITY: NEITHER STMicroelectronics NOR ITS VENDORS OR
AGENTS SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
--------------------------------------------------------------------------*/
#include "upsd_cfg.h"
#include "upsd3200.h"
#include "timer_func.h"
/*------------------------------------------------------------------------------
Local Macro and Manifest Constant Declarations
------------------------------------------------------------------------------*/
#define INT_DISABLE EA = 0
#define INT_ENABLE EA = 1
#define TIMER0_COUNT (0-((OSC/(12*TIMER0_PERIOD))-17))
//#define TIMER0_COUNT 0xdb62 /* 10000h - ((16 Mhz / (12 * FREQ)) - 17) */
/* 16Mhz = 0xcbfc 100hz */
/* 12mHZ = 0xd8ff */
/* 11.059Mhz = 0xdb62 */
/* 6Mhz = 0xec89 */
/*------------------------------------------------------------------------------
Local Variable Declarations
------------------------------------------------------------------------------*/
static idata unsigned int timer0_tick;
/*------------------------------------------------------------------------------
static void timer0_isr (void);
This function is an interrupt service routine for TIMER 0. It should never
be called by a C or assembly function. It will be executed automatically
when TIMER 0 overflows.
------------------------------------------------------------------------------*/
static void timer0_isr (void) interrupt TF0_VECTOR using 1
{
/*------------------------------------------------
Adjust the timer 0 counter so that we get another
interrupt in 10ms.
------------------------------------------------*/
TR0 = 0; /* stop timer 0 */
TL0 = TL0 + (TIMER0_COUNT & 0x00FF);
TH0 = TH0 + (TIMER0_COUNT >> 8);
TR0 = 1; /* start timer 0 */
/*------------------------------------------------
Increment the timer tick. This interrupt should
occur approximately every 10ms. So, the resulotion
of the timer will be 100Hz not including interrupt
latency.
------------------------------------------------*/
timer0_tick++;
do_timer0();
}
/*------------------------------------------------------------------------------
This function enables TIMER 0. TIMER 0 will generate a synchronous interrupt
once every 1kHz.
------------------------------------------------------------------------------*/
void timer0_initialize (void)
{
INT_DISABLE; /* disable interrupts */
timer0_tick = 0;
TR0 = 0; /* stop timer 0 */
TMOD &= ~0x0F; /* clear timer 0 mode bits */
TMOD |= 0x01; /* put timer 0 into 16-bit no prescale */
TL0 = (TIMER0_COUNT & 0x00FF);
TH0 = (TIMER0_COUNT >> 8);
PT0 = 0; /* set low priority for timer 0 */
ET0 = 1; /* enable timer 0 interrupt */
TR0 = 1; /* start timer 0 */
INT_ENABLE; /* enable interrupts */
}
/*------------------------------------------------------------------------------
This function returns the current timer0 tick count.
------------------------------------------------------------------------------*/
unsigned int timer0_count (void)
{
unsigned int t;
INT_DISABLE;
t = timer0_tick;
INT_ENABLE;
return (t);
}
/*------------------------------------------------------------------------------
This function returns the number of timer ticks that have elapsed since the
specified count.
------------------------------------------------------------------------------*/
unsigned int timer0_elapsed_count (unsigned int count)
{
return (timer0_count () - count);
}
/*------------------------------------------------------------------------------
This function waits for 'count' timer ticks to pass.
------------------------------------------------------------------------------*/
void timer0_wait (unsigned int count)
{
unsigned int start_count;
start_count = timer0_count ();
while (timer0_elapsed_count (start_count) <= count)
{
//
}
}
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -