8259在系统中的连接如图所示,已知8259 中断类型号为:08h-0fh,8259的端口地址和发光 二极管输出端口地址可从图中分析得出,现要求实现如下功能:用按键模仿外设发出中断请求信号, 用8只发光二级管显示所发出的中断请求次数。 请编写主程序和中断服务程序。
时间: 2024-03-18 13:40:55 浏览: 22
根据题意,我们可以先确定需要用到的端口地址和中断类型号:
8259端口地址:0x20(主片),0xA0(从片)
发光二极管输出端口地址:0xED
中断类型号:08h-0Fh
然后,我们可以编写主程序和中断服务程序,实现如下功能:
主程序:
```c
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define LED_PORT 0xED
int int_count = 0;
void main() {
outp(LED_PORT, 0x0); // 清空发光二极管输出端口
// 初始化8259
outp(0x20, 0x11);
outp(0xA0, 0x11);
outp(0x21, 0x20);
outp(0xA1, 0x28);
outp(0x21, 0x04);
outp(0xA1, 0x02);
outp(0x21, 0x01);
outp(0xA1, 0x01);
outp(0x21, 0x0);
outp(0xA1, 0x0);
while (1) {
if (kbhit()) { // 按键模拟中断请求信号
int_count++;
outp(LED_PORT, int_count); // 发送中断计数到发光二极管输出端口
delay(200); // 延时一段时间,避免按键抖动
outp(0x20, 0x20); // 发送EOI信号
}
}
}
```
中断服务程序:
```c
#include <stdio.h>
#include <dos.h>
void interrupt (*old_int8)();
void interrupt new_int8() {
outp(0x20, 0x20); // 发送EOI信号
}
void main() {
old_int8 = getvect(0x08);
setvect(0x08, new_int8);
_enable();
while(1);
}
```
说明:
1. 主程序中,我们使用`outp`函数向发光二极管输出端口发送中断计数,并使用`delay`函数延时一段时间,避免按键抖动。同时,在中断请求处理完毕后,我们需要使用`outp`函数向8259主片发送EOI信号。
2. 中断服务程序中,我们使用`getvect`函数获取原中断向量地址,然后使用`setvect`函数设置新的中断向量地址,将中断服务程序替换为新的中断处理函数。在处理完中断请求后,我们需要向8259主片发送EOI信号。
注意:以上代码仅供参考,具体实现可能需要根据实际情况进行调整。同时,代码中可能存在错误和不足之处,需要进行测试和完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)