定时器控制流水灯闪烁 外部中断1,显示奇数位灯循环亮 ,外部中断0,显示偶数位灯循环亮
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint code table[2][4]={{0x01,0x04,0x10,0x40},{0x02,0x08,0x20,0x80}};
sbit key1=P3^2;//定义按键作为中断触发
sbit key2=P3^3;
sbit led=P1;
uint num,num1,num2;
//延时//
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//中断初始化//
void init()
{///外部中断12初始化//
EA=1;
EX0=1;
EX1=1;
IT1=0;
IT0=0;
///定时器初始化//
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
ET0=1;
TR0=1;
}
void main()
{init();
while(1)
{
P1=table[num1][num2];//调用数组
delay(10);
}
}
//外部中断1///
void exter0() interrupt 0
{
EX0 = 0;
delay(3);
if(key1== 0)
{
num1=0;
}
delay(10);
EX0 = 1;
}
//外部中断2//
void exter1() interrupt 2
{EX1 = 0;
delay(3);
if(key2== 0)
{
num1=1;
}
delay(10);
EX1 = 1;
}
//定时器中断//
void T0_TIME() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)//1s闪烁
{
num=0;
num2++;
if(num2==4)
num2=0;
}
}
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint code table[2][4]={{0x01,0x04,0x10,0x40},{0x02,0x08,0x20,0x80}};
sbit key1=P3^2;//定义按键作为中断触发
sbit key2=P3^3;
sbit led=P1;
uint num,num1,num2;
//延时//
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//中断初始化//
void init()
{///外部中断12初始化//
EA=1;
EX0=1;
EX1=1;
IT1=0;
IT0=0;
///定时器初始化//
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
ET0=1;
TR0=1;
}
void main()
{init();
while(1)
{
P1=table[num1][num2];//调用数组
delay(10);
}
}
//外部中断1///
void exter0() interrupt 0
{
EX0 = 0;
delay(3);
if(key1== 0)
{
num1=0;
}
delay(10);
EX0 = 1;
}
//外部中断2//
void exter1() interrupt 2
{EX1 = 0;
delay(3);
if(key2== 0)
{
num1=1;
}
delay(10);
EX1 = 1;
}
//定时器中断//
void T0_TIME() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num++;
if(num==20)//1s闪烁
{
num=0;
num2++;
if(num2==4)
num2=0;
}
}