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

📄 timer.c

📁 8051 单片机的内部定时器编程,采用硬件目标板时需要使用以下外部函数
💻 C
字号:
#include "tdp.h"
#include <reg51.h>
#define TIMER0_COUNT	0xDC11  /* 10000h-((11059200Hz/(128FREQ))-17)  */ 

static xdata unsigned timer0_tick;

/*定时器T0中断服务函数*/
static void timer0_isr(void) interrupt 1 using 1
{
	P1 |= 0x80;
	TR0 = 0;	/*停止T0*/
	TL0 = TL0 + (TIMER0_COUNT & 0x00FF);
	TH0 = TH0 + (TIMER0_COUNT >> 8);
	TR0 = 1;	/*启动T0*/
	timer0_tick++;
	P1 &= ~0x80;
}

/*定时器T0初始化函数*/
void timer0_initialize(void)
{
	INT_DISABLE;	/*停止T0*/
	timer0_tick = 0;
	TR0 = 0;
	TMOD &= ~0x0F;	/*停止T0*/
	TMOD |= 0x01;
	TL0 = (TIMER0_COUNT & 0x00FF);
	TH0 = (TIMER0_COUNT >> 8);
	PT0 = 0;	/*设置T0中断优先级*/
	ET0 = 1;	/*允许T0中断*/
	TR0 = 1;	/*开中断*/
}

/*定时器T0节拍函数*/
unsigned timer0_count (void)
{
	xdata unsigned t;
	INT_DISABLE;
	t = timer0_tick;
	INT_ENABLE;
	return(t);
}

/*定时器T0节拍计数函数*/
unsigned timer0_elapsed_count (unsigned count) 
{
	return (timer_count () - count);
}

/*定时器T0延时函数*/
void timer0_wait (unsigned count)
{
	xdata unsigned start_count;
	start_count = timer0_count();
	while (timer0_elapsed_count (start_count) <= count)
 	  {
	  }
}

⌨️ 快捷键说明

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