微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机中断实验4

单片机中断实验4

时间:11-21 来源:互联网 点击:
电路图:


程序代码如下:

//交通灯控制程序

#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);

}


上一篇:LED显示计数
下一篇:单片机中断实验2

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top