定时器功能演示程序

2022-12-26  

#include

#include

//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 */

}

}


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。