开发者

How does the following code make PC beeps?

void Sound(开发者_如何学Cint f)
{  
    USHORT   B=1193180/f; 

    UCHAR temp = In_8(0x61);
    temp = temp | 3; 
    Out_8(0x61,temp);

    Out_8(0x43,0xB6);
    Out_8(0x42,B&0xF);
    Out_8(0x42,(B>>8)&0xF);
}

In_8/Out_8 reads/writes 8 bit to/from a specified port(implementation details omitted).

How does it make PC beep?

UPDATE

Why &0xF is used here? Shouldn't it be 0xFF?


The PC has a 8255 timer chip, which is controlled using the ports 0x61, 0x43 and 0x42.

When port 0x61 bit 0 is set to 1, this means "turn on the timer that is connected to the speaker". When port 0x61 bit 1 is set to 1, this means "turn on the speaker".

This is done in the first paragraph of your code.

The second part puts the "magic value" 0xB6 on port 0x43, which means that the following two bytes arriving at port 0x42 will be interpreted as the divisor for the timer frequency. The division's resulting frequency (1193180 / divisor) will then be sent to the speaker.

http://gd.tuwien.ac.at/languages/c/programming-bbrown/advcw3.htm#sound

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜