PC Speaker
There is a tiny speaker on motherboard which is used to simple beeps of given frequency.It is commonly referred as a PC beeper or a PC buzzer or a PC squeaker.
The speaker has 2 positions in and out which can be set through bit 1 of the 0x61 port(Keyboard Controller).If the bit is set, the speaker will move to its out position and if the bit is cleared it goes in.The speaker takes few milliseconds to change its position.
Controlling through the PIT Channel 2
The speaker is controlled by the Channel 2 of the Programmable Interval Timer(Channel 2 Register:0x42).The frequency set at the channel 2 will be the frequency of the sound produced by the speaker. Therefore changing the frequency of the Channel 2 would affect the sound produced by the speaker.
There is an article on PIT(Programmable Interval Timer) here.
Steps to make a beep:
IO Ports:
PIT Channel 2 0x42
PIT Command Register 0x43
Example Code in C:
void speaker_set(int hz)
{
int divisor = 1193180 / hz;
outportb(PIT_CMDREG, 0xB6);
outportb(PIT_CHANNEL2, (unsigned char) (divisor) );
outportb(PIT_CHANNEL2, (unsigned char) (divisor >> 8));
}
-------------------------------------------------------------------------------------------------------------------------------
void beep(unsigned int wait_time,unsigned int times )
{
unsigned char tempA =inportb(0x61);
unsigned char tempB = (inportb(0x61) & 0xFC);
unsigned int count;
for(count =0,count ==times,count++)
{
if(tempA != (tempA | 3))
{
outportb(0x61, tempA | 3);
}
wait(wait_time);
outportb(0x61, tempB); wait(1);
}
}
-------------------------------------------------------------------------------------------------------------------------------
void main ()
{
speaker_set(1000);
beep(2,2);
}
The speaker has 2 positions in and out which can be set through bit 1 of the 0x61 port(Keyboard Controller).If the bit is set, the speaker will move to its out position and if the bit is cleared it goes in.The speaker takes few milliseconds to change its position.
Controlling through the PIT Channel 2
The speaker is controlled by the Channel 2 of the Programmable Interval Timer(Channel 2 Register:0x42).The frequency set at the channel 2 will be the frequency of the sound produced by the speaker. Therefore changing the frequency of the Channel 2 would affect the sound produced by the speaker.
There is an article on PIT(Programmable Interval Timer) here.
Steps to make a beep:
- Set the speaker to out
- Set the frequency
- Play the sound
- Reset the speaker
IO Ports:
PIT Channel 2 0x42
PIT Command Register 0x43
Example Code in C:
- This function first tell the command register that we are sending the frequency of the channel 2.Then we send the frequency.This frequency which we set using this function will be the frequency of the sound played.
void speaker_set(int hz)
{
int divisor = 1193180 / hz;
outportb(PIT_CMDREG, 0xB6);
outportb(PIT_CHANNEL2, (unsigned char) (divisor) );
outportb(PIT_CHANNEL2, (unsigned char) (divisor >> 8));
}
-------------------------------------------------------------------------------------------------------------------------------
- This makes the beep for the given number of times and time taken per beep.
void beep(unsigned int wait_time,unsigned int times )
{
unsigned char tempA =inportb(0x61);
unsigned char tempB = (inportb(0x61) & 0xFC);
unsigned int count;
for(count =0,count ==times,count++)
{
if(tempA != (tempA | 3))
{
outportb(0x61, tempA | 3);
}
wait(wait_time);
outportb(0x61, tempB); wait(1);
}
}
-------------------------------------------------------------------------------------------------------------------------------
- Using those two functions.
void main ()
{
speaker_set(1000);
beep(2,2);
}