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

📄 timer.c

📁 一个基于XMODEM协议的下载字库的程序
💻 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 + -