您的位置 首页 动态

单片机中止试验4

电路图:程序代码如下://交通灯控制程序#includereg51.h>unsignedchart0,t1;//定义全局变量,用来保存延时时间循环次数//delay0…

电路图:

程序代码如下:
//交通灯控制程序
#include
unsigned char t0, t1; //界说全局变量,用来保存延时时刻循环次数
//delay0_5s1
//功用:用T1的工作方式1编制0.5s延时程序,假定体系选用12Mhz晶振,守时器1,工作方式1守时50ms,再循环10次即可守时到0.5s
void delay0_5s1 () {
for (t0=0; t0<0x0a; t0++) {
TH1 = 0x3c;
TL1 = 0xb0;
TR1 = 1;
while (!TF1);
TF1=0;
}
}
//delay_t1
//功用:完成0.5s * 延时
//形参:unsigned char t;
// 延时时刻为0.5s*t;
void delay_t1 (unsigned char t) {
for (t1=0; t1
delay0_5s1();
}
//int_0
//外部中止0中止函数,紧急情况处理,当cpu呼应外部中止0的中止请求时,主动履行该函数,完成两个方向的红灯一起亮10s
void int_0 () interrupt 0 {
unsigned char i,j,k,l,m;
i = P1; //维护现场,暂存P1, t0, t1, TH1, TL0;
j = t0;
k = t1;
l = TH1;
m = TL1;
P2 = 0xdb; //两个方向都是红灯
delay_t1 (20); //延时10s
P2 = i;
t0 = j;
t1 = k;
TH1 = 1;
TL1 = m;
}
//int_1
//功用:外部中止1中止函数, 特殊情况处理,当cpu,当cpu呼应外部中止1的中止请求时, 主动履行该函数,完成a到放行5s
void int_1 () interrupt 2 {
unsigned char i,j,k,l,m;
EA = 0;
i = P1;
j = t0;
k = t1;
l = TH1;
m = TL1;
EA = 1;
P2 = 0xf3;
delay_t1 (10);
EA = 0;
P2 = i;
t0 = j;
t1 = k;
TH1 = l;
TL1 = m;
EA = 1;
}
void main () {
unsigned char k;
TMOD = 0x10; //T1在工作方式1
EA = 1; //开总中止
EX0 = 1;
IT0 = 1;
EX1 = 1;
IT1 = 1;
while (1) {
P2 = 0xf3;
delay0_5s1();
P2 = 0xfb;
delay0_5s1();
}
P2 = 0xeb;
delay_t1(4);
P2 = 0xde;
delay_t1(110);
for (k=0; k<3; k++) {
P2 = 0xde;
delay0_5s1();
P2 = 0xdf;
delay0_5s1();
}
P2 = 0xdd;
delay_t1(4);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/dongtai/262794.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部