?? timer.c
字號:
#define TIMER_PARA
#include "config.h"
static unsigned char timer0_tick;
/*******************************************************************************
程序名:timer0_isr
說 明:定時器0中斷服務程序,提供系統是中,每10ms中斷一次,timer0_tick為可調的時基
********************************************************************************/
void timer0_isr (void) interrupt TF0_VECTOR using 0
{
TR0 = 0; /* stop timer 0 */
TH0 = PERIODH0;
TL0 = PERIODL0; //10ms
TR0 = 1; /* start timer 0 */
timer0_tick++;
if(stimer_tick < 200) stimer_tick++; //防止溢出,很重要
if(led_tick < 200) led_tick++;
if(others_tick<200) others_tick++;
}
/*******************************************************************************
程序名:timer0_init
說 明:定時器0初始化程序,包括軟定時器初始化
********************************************************************************/
void timer0_init (void)
{
INT_DISABLE; /* disable interrupts */
TR0 = 0; /* stop timer 0 */
TMOD &= 0xF0; /* clear timer 0 mode bits - bottom 4 bits */
TMOD |= 0x01; /* put timer 0 into 16-bit no prescale */
TH0 = PERIODH0;
TL0 = PERIODL0; //定時器0賦初值,定時器0每10ms中斷一次
PT0 = 0; /* set low priority interrupt for timer 0 */
ET0 = 1; /* enable timer 0 interrupt */
TR0 = 1; /* start timer 0 */
vSoftTimer_Init();
stimer_tick = 0;
led_tick = 0;
timer0_tick = 0;
INT_ENABLE; /* enable interrupts */
}
/*------------------------------------------------------------------------------
程序名: timer0_count
說 明: 返回當前timer0_tick的值
------------------------------------------------------------------------------*/
unsigned char timer0_count(void)
{
unsigned char t;
INT_DISABLE; // disable interrupts to read a non-changing value
t = timer0_tick;
INT_ENABLE; // enable interrupts
return(t);
}
/*------------------------------------------------------------------------------
程序名: timer0_delay
說 明: 延時子程序,延時時間為count*10ms
------------------------------------------------------------------------------*/
void timer0_delay(unsigned char count)
{
unsigned char start_count;
start_count = timer0_count(); /* get the start count */
while ((timer0_count() - start_count) <= count) /* wait for count "ticks" */
{
PCON |= 0x01; // Idle MCU to wait for timer tick
}
}
/*------------------------------------------------------------------------------
程序名: delay_10ms
說 明: 延時100ms
------------------------------------------------------------------------------*/
void delay_10ms()
{
timer0_delay(2);
}
/*------------------------------------------------------------------------------
程序名: delay_100ms
說 明: 延時100ms
------------------------------------------------------------------------------*/
void delay_50ms()
{
timer0_delay(5);
}
/*------------------------------------------------------------------------------
程序名: delay_100ms
說 明: 延時100ms
------------------------------------------------------------------------------*/
void delay_100ms()
{
timer0_delay(10);
}
/*------------------------------------------------------------------------------
程序名: delay_1sec
說 明: 延時1s
------------------------------------------------------------------------------*/
void delay_1sec()
{
timer0_delay(100);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -