C编程控制PC蜂鸣器

在《I/O映射之I/O端口》一文中,我们实现了在Linux内核驱动里实现IO端口操作8254定时计数器芯片里的寄存器来实现PC主机蜂鸣器的控制,那么在C语言里是不是一样也能用io控制函数来操作呢?当然是可以的,只不过作为应用程序,其需要使用iopl或ioperm函数先获得IO操作的权限,其函数原型如下:
1.iopl - change I/O privilege level
#include <sys/io.h>
int iopl(int level);
2.ioperm - set port input/output permissions
#include <unistd.h> /* for libc5 */
#include <sys/io.h> /* 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 <stdio.h>
#include <unistd.h>
#include <sys/io.h>

/* 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<value<32767,other value will stop!\n",argv[0]);
               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

评论

此博客中的热门博文

I/O映射之I/O端口

通过Netlink检测网线插拔

使用seq_file