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;
}
相关推荐
微笑星河q1 小时前
C++高阶之【多态】
开发语言·c++
ClkLog-开源埋点用户分析4 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
yuzhiboyouye5 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
郝学胜-神的一滴6 小时前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu6 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
时间的情敌8 小时前
Three.js 射线拾取进阶:hover 高亮、点击弹窗、多物体过滤、穿透拾取
开发语言·javascript·three.js
Amumu121388 小时前
TypeScript学习: 基本类型(一)
开发语言·前端·typescript
敲上瘾9 小时前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
船神丿男人10 小时前
C++ STL 入门:用 std::string 读配置,四个容易漏掉的边界
开发语言·c++
30955666621 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口