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); iopl(0); } void play(unsigned int* freq, unsigned int* time) { int i; for(i=0;freq[i]!=0;i++) { beep(freq[i], time[i]); } } int main(int argc,char * argv[]) { /*Happy Birthday chinese version*/ unsigned frequency[]= { 392,392,440,392,523,494, 392,392,440,392,587,523, 392,392,784,659,523,494,440, 698,698,659,523,587,523 }; unsigned delay[]= { 375,125,500,500,500,1000, 375,125,500,500,500,1000, 375,125,500,500,500,500,1000, 375,125,500,500,500,1000, }; play(frequency, delay); return 0; }
相应的Makefile文件内容如下:
all: make beep #gcc -o beep beep.c clean: rm -rf beep
对应的源码文件目录树如下:
/home/xinu/xinu/c_cpp/beep_song/
├── beep.c
└── Makefile
参考资料:
http://blog.csdn.net/sky_j123/article/details/19574955
http://www.cnblogs.com/sankye/articles/3219479.html
/home/xinu/xinu/c_cpp/beep_song/
├── beep.c
└── Makefile
参考资料:
http://blog.csdn.net/sky_j123/article/details/19574955
http://www.cnblogs.com/sankye/articles/3219479.html
评论
发表评论