C编程控制PC蜂鸣器

在《I/O映射之I/O端口》一文中,我们实现了在Linux内核驱动里实现IO端口操作8254定时计数器芯片里的寄存器来实现PC主机蜂鸣器的控制,那么在C语言里是不是一样也能用io控制函数来操作呢?当然是可以的,只不过作为应用程序,其需要使用iopl或ioperm函数先获得IO操作的权限,其函数原型如下: 

1.iopl - change I/O privilege level

#include
int iopl(int level);

2.ioperm - set port input/output permissions

#include /* for libc5 */
#include /* for glibc */
int ioperm(unsigned long from, unsigned long num, int turn_on);

其中,iopl修改程序运行时操作io的权限级别,默认是0,最大是3;而ioperm是设置从from开始的num个io端口turn_on状态。(详细看man) 有了《I/O映射之I/O端口》一文和上面的内容学习,接下来用C来实现就很容易了,其中关于inb等函数头可使用man inb命令查阅,下面上代码吧:

#include
#include
#include

/* The clock frequency of the i8253/i8254 PIT */
#define PIT_TICK_RATE 1193182ul

void beep(unsigned int val)
{
        unsigned int count = 0;
        if (val > 20 && val < 32767)
                count = PIT_TICK_RATE / val;

        iopl(3);

        if (count) {
                outb_p(0xB6, 0x43);
                outb_p(count & 0xFF, 0x42);
                outb((count >> 8) & 0xFF, 0x42);
                outb_p(inb_p(0x61) | 3, 0x61);
        } else {
                outb(inb_p(0x61) & 0xFC, 0x61);
        }

        iopl(0);
}

int main(int argc,char * argv[])
{
       unsigned int val;
       if (argc != 2) {
               printf("Usage:%s value\n 20               return 0;
       }

       val = atoi(argv[1]);
       beep(val);

       return 0;
}

相应的Makefile文件内容如下:

all:
        make ioport_beep
        #gcc -o ioport_beep ioport_beep.c

clean:
        rm -rf ioport_beep

相应的源码文件目录树如下: 

/home/xinu/xinu/c_cpp/ioport_beep/
├── ioport_beep.c
└── Makefile 

参考网址: 

http://exbob.com/post/linux/2013-01-24-linux_beep
http://dxwang.blog.51cto.com/384651/129906
http://www.tldp.org/HOWTO/IO-Port-Programming-2.html
http://www.linux.org.tw/CLDP/MiniHOWTO/prog/IO-Port-Programming/IO-Port-Programming-2.html

你可能感兴趣的:(C编程控制PC蜂鸣器)