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

📄 timer_us.c

📁 51系列单片机定时器的使用样例
💻 C
字号:
// zzwj5120

#include <reg52.h>
#include <intrins.h>

//sbit led1=P1^0;
//sbit led2=P1^1;
//sbit led3=P1^2;
//sbit led4=P1^3;


unsigned char count;
unsigned char direction;

unsigned char delay1;
unsigned char delay2;


void timer0(void) interrupt 1 using 0
{
	TL0=0x06;
	TH0=delay2;

    /* 定时器0中断 */
	if (++delay1 > 4){
		delay1 = 0;
		if (++count > 3) count = 0;
		P1 |= 0x0f;
		P1 &= ~(0x01 << count);
	}
    /* 定时器0中断 */
}
void timer1(void) interrupt 3 using 0
{
	TL1=0x06;
	TH1=0x00;


    /* 定时器1中断 */
	if (direction){
        if (++delay2 == 255)direction = 0;
    }
    else {
        if (--delay2 == 0)direction = 1;
    }
    /* 定时器1中断 */
}

void serial() interrupt 4 using 3
{

}

// 定时器功能演示程序
// P01、P02、P03、P04脚接发光二极管led1、led2、led3、led4 
// 每个发光二极管串上电阻接到5V电源上, 运行本演示程序
// 将会看到发光二极管被依次点亮和熄灭, 比街上的彩灯好看
// 多了,不信你试试看。

void main(void){
	EA = 1;                           /* 开总中断 */

	ET0 = 1;                          /* 允许定时器0中断 */
	ET1 = 1;                          /* 允许定时器1中断 */

	TMOD = 0x11;                      /* 定时器工作方式选择 */

	TL0 = 0x06;     
	TH0 = 0x00;                       /* 定时器赋予初值 */

	TL1 = 0x06;     
	TH1 = 0x00;                       /* 定时器赋予初值 */

	TR0 = 1;                          /* 启动定时器 */
	TR1 = 1;                          /* 启动定时器 */


 	delay2 = 0;
        direction = 1;

	while(1){
  		_nop_ ();                     /* delay for hardware */
	}
}

⌨️ 快捷键说明

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