Prj10--8088单板机C语言8259测试(1)

1.原理图

2.Deepseek示例代码

cpp 复制代码
#include <dos.h>
#include <conio.h>
#include <stdio.h>

#define PIC1_CMD  0x400   // 命令端口 (A0=0)
#define PIC1_DATA 0x401   // 数据端口 (A0=1)

volatile int int_count = 0;  // 中断计数器
void interrupt (*old_isr)(void);  // 原中断向量

// 中断服务程序
void interrupt new_isr(void) {
    int_count++;  // 增加中断计数
    
    // 显示中断信息
    printf("\nIRQ0 Triggered! Count: %d", int_count);
    
    // 发送EOI命令
    outportb(PIC1_CMD, 0x20);
}

// 初始化8259
void init_8259(void) {
    // ICW1: 边沿触发 | 单片 | 需要ICW4
    outportb(PIC1_CMD, 0x13);
    
    // ICW2: 中断向量基址=20h
    outportb(PIC1_DATA, 0x20);
    
    // ICW4: 8086模式 | 正常EOI
    outportb(PIC1_DATA, 0x01);
    
    // OCW1: 只允许IR0中断 (11111110b)
    outportb(PIC1_DATA, 0xFE);
}

int main(void) {
    clrscr();
    printf("8259 Test Running. Press ESC to exit...\n");
    
    // 保存原中断向量 (INT 20h)
    old_isr = getvect(0x20);
    
    // 设置新中断向量
    setvect(0x20, new_isr);
    
    // 初始化8259
    init_8259();
    
    // 启用中断
    enable();
    
    // 主循环
    while(!kbhit() && int_count < 10) {
        // 等待中断或按键
    }
    
    // 检查是否按ESC退出
    if(kbhit() && getch() == 0x1B) {
        printf("\nESC pressed. ");
    }
    
    // 恢复设置
    disable();
    setvect(0x20, old_isr);
    
    // 屏蔽所有中断
    outportb(PIC1_DATA, 0xFF);
    
    printf("\nProgram terminated. Total interrupts: %d", int_count);
    return 0;
}
相关推荐
小浣熊熊熊熊熊熊熊丶2 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子3 小时前
JDK 安装配置
java·开发语言
星哥说事3 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
等....3 小时前
Miniconda使用
开发语言·python
zfj3213 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧3 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop3 小时前
Aes加密 GCM java
java·开发语言·python
weixin_462446233 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang
JIngJaneIL4 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码4 小时前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web