新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 單片機(jī)中斷實驗4

單片機(jī)中斷實驗4

作者: 時間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
電路圖:

程序代碼如下:
//交通燈控制程序
#include
unsigned char t0, t1; //定義全局變量,用來保存延時時間循環(huán)次數(shù)
//delay0_5s1
//功能:用T1的工作方式1編制0.5s延時程序,假定系統(tǒng)采用12Mhz晶振,定時器1,工作方式1定時50ms,再循環(huán)10次即可定時到0.5s
void delay0_5s1 () {
for (t0=0; t0<0x0a; t0++) {
TH1 = 0x3c;
TL1 = 0xb0;
TR1 = 1;
while (!TF1);
TF1=0;
}
}
//delay_t1
//功能:實現(xiàn)0.5s * 延時
//形參:unsigned char t;
// 延時時間為0.5s*t;
void delay_t1 (unsigned char t) {
for (t1=0; t1
delay0_5s1();
}
//int_0
//外部中斷0中斷函數(shù),緊急情況處理,當(dāng)cpu響應(yīng)外部中斷0的中斷請求時,自動執(zhí)行該函數(shù),實現(xiàn)兩個方向的紅燈同時亮10s
void int_0 () interrupt 0 {
unsigned char i,j,k,l,m;
i = P1; //保護(hù)現(xiàn)場,暫存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中斷函數(shù), 特殊情況處理,當(dāng)cpu,當(dāng)cpu響應(yīng)外部中斷1的中斷請求時, 自動執(zhí)行該函數(shù),實現(xiàn)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);
}



關(guān)鍵詞: 單片機(jī)中斷實

評論


技術(shù)專區(qū)

關(guān)閉