单片机中断实验4
程序代码如下:
//交通灯控制程序
#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 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); }
单片机中断实 相关文章:
- 单片机中断实验3(11-21)
- 单片机中断实验1(11-21)
- 单片机中断实验2(11-21)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)