test.c 743 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/io.h>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #define PORT_SPKR 0x61
  8. #define PORT_CTRL 0x43
  9. #define PORT_CH2 0x42
  10. int main()
  11. {
  12. unsigned char val;
  13. if (ioperm(PORT_CTRL, 1, 1) < 0 || ioperm(PORT_SPKR, 1, 1) < 0 || ioperm(PORT_CH2, 1, 1) < 0) {
  14. perror("Failed to request I/O port permission");
  15. return 1;
  16. }
  17. // 设置timer2
  18. outb(0xB6, PORT_CTRL);
  19. // 设置频率
  20. outb(0x33, PORT_CH2);
  21. outb(0x05, PORT_CH2);
  22. // 打开speaker
  23. val = inb(PORT_SPKR);
  24. outb(val | 3, PORT_SPKR);
  25. // 响1秒
  26. sleep(1);
  27. // 关闭speaker
  28. outb(val, PORT_SPKR);
  29. return 0;
  30. }