PC Speaker

From OSDev.wiki
Revision as of 13:32, 25 March 2008 by osdev>Stevo14 (monor formatting)
Jump to navigation Jump to search

The PC Speaker is the most primitive sound device available on PC compatible systems. It is characterized by the distinctive "beeps" and "squeaks" that it can be made to produce and is therefore sometimes referred to as the "PC Beeper" or the "PC Squeaker".

The Raw Hardware

The speaker itself has two possible positions, "in" and "out". This position can be set through bit 1 of port 0x61 on the Keyboard Controller. If this bit is set (=1), the speaker will move to the "out" position, if it is cleared (=0) then the speaker will move to the "in" position. It is important to note that the speaker will not instantly jump to the desired position. Rather it will move towards the desired position ("in" or "out") over a short period of time. This allows the speaker to produce actual beeps and whistles instead of just clicks.

Modes of Operation

Through the Programmable Interval Timer (PIT)

The PC Speaker can be connected directly to the output of timer number 2 on the Programmable Interval Timer by setting bit 0 of port 0x61 (=1). In this mode, when the timer goes "high" (=1) the speaker will move to the "out" position. Likewise, when the timer goes "low" (=0) the speaker will move to the "in" position. By changing the frequency at which timer 2 "ticks", the PC Speaker can be made to output sound of the same frequency. This mode is very popular because it is easy to program and because it is asynchronous from the rest of the computer's operation, meaning that it takes very little CPU time. It should also be noted that this is the "official" way to program the PC Speaker and, if a sound card is present, should be the only way that the PC Speaker is programmed.

Pulse Width Modulation

In the absence of a real sound card, the PC Speaker can be used to output low quality digital sound. See The Wikipedia article Pulse-width modulation.