现在的位置: 首页 > 综合 > 正文

I/O接口的输入输出编程原理

2013年01月23日 ⁄ 综合 ⁄ 共 745字 ⁄ 字号 评论关闭

I/O接口的输入输出编程原理

程序中由rand( )函数产生一个0-32767之间的伪随机整数,为使声音柔和,在freq<=1000时,退出循环而调用sound发声函数,该函数由传来的参数freq来设定频率计数值,count.divisor为119328/freq(占用两个地址单元),由于采用了单元结构,共用一个存储区,故联合结构成员count.c[0]和count.c[1]取该数的低8位和高8位(二进制位),用于初始化定时器的频率计数值.
运行该程序后,将会听到美妙的声音,当要停止时,可按任意键而由khbit( )函数控制do循环结束来实现.
由于PC机主频不同,发声效果将不同,选取freq>1000,将会在386PC机上(主频为33M)发出清脆的声音,当主频小于33M时,可适当增加此数.

void sound(unsigned int freq);
main()
{
unsigned int freq;
do
{
do
{
freq=rand();
}while(freq>1000);
sound(freq);
}while(!kbhit());
}
void sound(unsigned int freq)
{
unsigned i;
union
{
long divisor;
unsigned char c[2];
}count;
unsigned char bits;
count.divisor=119328/freq;
outportb(0x43,0xb6);
outportb(0x42,count.c[0]);
outportb(0x42,count.c[1]);
bits=inportb(0x61);
outportb(0x61,bits|3);
for(i=0;i<20000;i++);
outportb(0x61,bits&0xfc);
}

 

 

抱歉!评论已关闭.