Cplusplus wrote:
I am working a simple OS, and have worked on the kernel and use GRUB as my bootloader. Now I want to program the PC speaker (not the sound card) to emit a POST-like series of beeps if something fails. Can anyone tell me why this code doesn't work?
Code:
int con_speaker(int freq)
{ if (freq <= 15)
{ freq = 131;
//middle C (I think).
}
int countdown = (1193180 / freq);
int high = (int)(countdown / 256);
int low = countdown - high;
outportb(0x43, 0xB6);
//send 'timer 2' a message to program countdown
outportb(0x42, low);
//send low.
outportb(0x42, high);
//send high.
return 0;
}
int speaker_on()
{ int val = inportb(0x61);
val |= 3;
//Turn on bits 1 and 2
outportb(0x61, val);
//send it back;
return 0;
}
int speaker_off()
{ int val = inportb(0x61);
val &= 252;
//Turn off bits 1 and 2
outportb(0x61, val);
//send it back;
return 0;
}
If anyone want's to see outportb and inportb just ask, but I'm sure they work because the keyboard input/output is fine. The calling function does this (stripped version):
Code:
int test()
{ con_speaker(131);
speaker_on();
return 0;
}
Don't worry, the speaker gets turned off later.
I r Python programmer (beware)
First, I think maybe try 400 instead of 131. Is 131 supposed to be the thue freq? as in 131 hetrz? That might be outside a PC speaker's range.
Forgive me if I am off the mark here. Also, can you ring the bell? Like ^g, just to confirm the speaker is working?
also " if (freq <= 15)" you should set to 20 (actually set it to 300 for a test). Most folks can't hear less than 20 and a PC speaker is not going to get near 20 in reality. You would just hear a "click" as it activates.
Also, what is the max on freq?
The above all assumes you are using hertz for freq.
Manta