C编程演奏中文版“生日快乐歌”
有了《C编程控制PC蜂鸣器》一文的基础后,我们在其基础上修改函数入口参数,由原来的1个参数(频率)改为现在的2个(频率、延时<即该频率响多长时间>),然后就按照节奏实现中文版的“生日快乐歌”,其源码如下:
#include <unistd.h>
#include <sys/io.h>
/* The clock frequency of the i8253/i8254 PIT */
#define PIT_TICK_RATE 1193182ul
void beep(unsigned int freq, unsigned int delay)
{
unsigned int count = PIT_TICK_RATE / freq;
iopl(3);
outb_p(0xB6, 0x43);
outb_p(count & 0xFF, 0x42);
outb((count >> 8) & 0xFF, 0x42);
outb_p(inb_p(0x61) | 3, 0x61);
usleep(1000*delay);
outb_p(inb_p(0x61) & 0xfc, 0x61);
io