單片機(jī)中斷實驗4
程序代碼如下:
//交通燈控制程序
#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ī)中斷實
相關(guān)推薦
技術(shù)專區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線
- 開關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車電子
- 轉(zhuǎn)換器
- 電源管理
- 信號放大器
評論