Quantcast

Maximum PC

It is currently Thu Aug 28, 2014 5:46 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 16 posts ] 
Author Message
 Post subject: PC Speaker Pulse
PostPosted: Mon Aug 02, 2004 11:45 am 
8086
8086

Joined: Mon Aug 02, 2004 10:59 am
Posts: 50
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.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 9:43 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Nice.... :) I'm going to be of very little help here. Just curious to see how this turns out.

First, you've probably pissed of the programming gods by not using K&R, so don't expect debugging to be rational... :)

I am curious....

why ( a / b ) in this case?
int countdown = (1193180 / freq);

why caste to int? int / int == int
int high = (int)(countdown / 256);

I really don't understand what con_speaker is suppose to do. Wht does con mean? At first glance, I thought you were setting up a high and low frequency range or something - "//I think 131 is middle C". After reading into it, I get the impression you're setting up a timer to run over a certain time period. Is that right?


Top
  Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 8:35 am 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
Gadget wrote:
Nice.... :) I'm going to be of very little help here. Just curious to see how this turns out.

First, you've probably pissed of the programming gods by not using K&R, so don't expect debugging to be rational... :)

I am curious....

why ( a / b ) in this case?
int countdown = (1193180 / freq);

why caste to int? int / int == int
int high = (int)(countdown / 256);

I really don't understand what con_speaker is suppose to do. Wht does con mean? At first glance, I thought you were setting up a high and low frequency range or something - "//I think 131 is middle C". After reading into it, I get the impression you're setting up a timer to run over a certain time period. Is that right?


I am sorry if this seems like a stupid question, but what is K&R?


Top
  Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2004 12:42 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
baldeagle wrote:
Gadget wrote:
Nice.... :) I'm going to be of very little help here. Just curious to see how this turns out.

First, you've probably pissed of the programming gods by not using K&R, so don't expect debugging to be rational... :)

I am curious....

why ( a / b ) in this case?
int countdown = (1193180 / freq);

why caste to int? int / int == int
int high = (int)(countdown / 256);

I really don't understand what con_speaker is suppose to do. Wht does con mean? At first glance, I thought you were setting up a high and low frequency range or something - "//I think 131 is middle C". After reading into it, I get the impression you're setting up a timer to run over a certain time period. Is that right?


I am sorry if this seems like a stupid question, but what is K&R?

http://www.amazon.com/exec/obidos/tg/de ... 9?v=glance

A common programming style popularized by those two guys....

Code:
while (1) {
    i++;
}

//instead of
while (1)
{
   i++;
}

It tends to be shunned by C++ textbooks (why? I have no idea), but I've never seen a significant project or known of that doesn't use the K&R style either - all the free software uses K&R (linux, every bsd, etc), all the highest rated programmers at TC use it, the java programming sytle is similiar (minor change), and it is the prefered style of Stroustrup as well. Good company to keep.

The main benefit is that it saves vertical space on the screen (and in the books which often need the extra space even more... irony). You also know immediately from looking at the control structure line (ie the while in this case) whether you're dealing with a block or not. And in case you were wondering, that other style doesn't have a name - it isn't worthy!

*In all seriousness, it might have a name, but I'll be damned if I know anyone that uses that style that knows it. :)


Top
  Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 6:15 pm 
8086
8086

Joined: Mon Aug 02, 2004 10:59 am
Posts: 50
"con" stood for "connect", but that wasn't really appropriate; what I really wanted to do was program timer two and use it to set the frequency on the speaker (but that's it, it doesn't really connect anything, more like initializes)... the cast to int? don't know about that one, just wanted to be safe (but you're right, totally unnecessary, since 'division' means 'integer division').

low and high is 'low order' and 'high order', because that's how you have to send it to the port (otherwise there is a much smaller range).

Yeah, my style is different (from both of them), but that's what happens when you learn by yourself; I might switch if it precludes me from getting a job.

speaker_on and speaker_off do exactly what they say...

The weird thing is, I tested it on an identical machine and the above code works...and I mean identical. Same BIOS, same BIOS settings, same everything, yet it doesn't play on my primary testing machine. Maybe some registers are getting trashed? Any insight would be helpful. (and yes, the speaker is enabled in the BIOS).


Top
  Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2004 11:04 am 
8086
8086

Joined: Mon Aug 02, 2004 10:59 am
Posts: 50
Okay, I've decided to learn "Stroustrup Style" or "K&R for C++", since that is fairly close to my current style (but I absolutely refuse to rewrite anything unless someone else wants it). Does anyone have a good book/something for teaching this style (I found a short one on http://www.research.att.com/~bs/bs_faq2.html but I want to know if there is more to it)?


Top
  Profile  
 
 Post subject:
PostPosted: Sun Aug 08, 2004 9:44 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Cplusplus wrote:
Okay, I've decided to learn "Stroustrup Style" or "K&R for C++", since that is fairly close to my current style (but I absolutely refuse to rewrite anything unless someone else wants it). Does anyone have a good book/something for teaching this style (I found a short one on http://www.research.att.com/~bs/bs_faq2.html but I want to know if there is more to it)?

See the linux kernel docs..... they have a short howto on using K&R. As far as good book, see the resources section and the book by Stroustrup, while it doesn't teach K&R, is written entirely in this style and is the most recommended C++ book in existance. You can also refer to the Java Coding style at Sun, which is essentially K&R as well.

Any progress on your project?


Top
  Profile  
 
 Post subject: Re: PC Speaker Pulse
PostPosted: Sun Aug 15, 2004 8:25 am 
I'd rather be modding!
I'd rather be modding!
User avatar

Joined: Fri Jun 25, 2004 3:47 pm
Posts: 3731
Location: Las Vegas
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


Top
  Profile  
 
 Post subject: Re: PC Speaker Pulse
PostPosted: Sun Aug 15, 2004 12:18 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
MantaBase wrote:
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
Not only would a pc speaker not go down to 20 hertz, but most subwoofers can't either! I doubt a pc speaker can go below 100.

Good points.... so that's why JPL pays you the big bucks. :)


Top
  Profile  
 
 Post subject:
PostPosted: Sun Aug 15, 2004 1:13 pm 
8086
8086

Joined: Fri Aug 13, 2004 12:26 pm
Posts: 9
Location: Lafayette, IN
Hey Cplusplus have you seen my PM?


Top
  Profile  
 
 Post subject: Re: PC Speaker Pulse
PostPosted: Sun Aug 15, 2004 2:07 pm 
I'd rather be modding!
I'd rather be modding!
User avatar

Joined: Fri Jun 25, 2004 3:47 pm
Posts: 3731
Location: Las Vegas
Gadget wrote:
Not only would a pc speaker not go down to 20 hertz, but most subwoofers can't either! I doubt a pc speaker can go below 100.

Good points.... so that's why JPL pays you the big bucks. :)


Heh - I am still waiting for a travel reimbursment from them :)

Manta


Top
  Profile  
 
 Post subject: Thanks
PostPosted: Sat Aug 28, 2004 5:03 pm 
8086
8086

Joined: Mon Aug 02, 2004 10:59 am
Posts: 50
Thaks for helping me. The speaker just 'clicked' before, as you (meaning the collective you) expected.


Top
  Profile  
 
 Post subject: Re: Thanks
PostPosted: Sat Aug 28, 2004 5:29 pm 
I'd rather be modding!
I'd rather be modding!
User avatar

Joined: Fri Jun 25, 2004 3:47 pm
Posts: 3731
Location: Las Vegas
Cplusplus wrote:
Thaks for helping me. The speaker just 'clicked' before, as you (meaning the collective you) expected.


As I suspected. Did you get it fixed?

Manta


Top
  Profile  
 
 Post subject:
PostPosted: Sat Aug 28, 2004 5:31 pm 
8086
8086

Joined: Mon Aug 02, 2004 10:59 am
Posts: 50
Yeah, I just upped the frequency (in Hertz), and now I can hear it.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Aug 28, 2004 5:36 pm 
I'd rather be modding!
I'd rather be modding!
User avatar

Joined: Fri Jun 25, 2004 3:47 pm
Posts: 3731
Location: Las Vegas
Cplusplus wrote:
Yeah, I just upped the frequency (in Hertz), and now I can hear it.


Kewl

Manta


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 6:08 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Middle A (A4) is 440 Hz.

You can find the frequency of another note with this formula:

Freq. = ((2^(1/12))^s) * 440 Hz

Where Freq. is the freqeuncy of the note, and s is the number of half steps up from A4 the note is.

This can be expressed in C as:

Code:
int s=-9;//back nine steps rfom a4 to middle c
unsigned int freq = 440 * pow(pow(2,(1/12)), s);


To go up or down an octave, multiply or divide by two (respectively).


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group