#include #include #include #include #include #include #define PORT_SPKR 0x61 #define PORT_CTRL 0x43 #define PORT_CH2 0x42 int main() { unsigned char val; if (ioperm(PORT_CTRL, 1, 1) < 0 || ioperm(PORT_SPKR, 1, 1) < 0 || ioperm(PORT_CH2, 1, 1) < 0) { perror("Failed to request I/O port permission"); return 1; } // 设置timer2 outb(0xB6, PORT_CTRL); // 设置频率 outb(0x33, PORT_CH2); outb(0x05, PORT_CH2); // 打开speaker val = inb(PORT_SPKR); outb(val | 3, PORT_SPKR); // 响1秒 sleep(1); // 关闭speaker outb(val, PORT_SPKR); return 0; }