[net.micro.pc] Using the speaker

ka@spanky.UUCP (09/10/83)

Here are some routines for using the speaker.  They are written in C but
should be easy to convert to assembly language.  (Inportb reads a port
and outportb writes to a port.)  Typical usage is:
	setfreq(800);			/* generate an 800 hertz tone */
	speaker(ON) ;			/* turn speaker on */
	for (i=0 ; i<2000 ; i++) ;	/* delay for a while */
	speaker(OFF) ;			/* turn speaker back off */
In order to produce sound you have to call setfreq to get the oscillator
running at an audible frequency and speaker(ON) to connect the output of
the oscillator to the speaker.
					Kenneth Almquist


---------------------------------
#define ON 1
#define OFF 0
 
 
/*
 * Set frequency of oscillator feeding speaker.
 */
 
setfreq(hertz) {
         unsigned divisor = 1193180L/hertz ;
         outportb(0x43, 0xB6) ;
         outportb(0x42, divisor & 0377) ;
         outportb(0x42, divisor >> 8) ;
}
 
 
/*
 * Turn speaker on or off.
 */
 
speaker(on) {
         int portval ;
 
         portval = inportb(0x61) ;
         if (on)   portval |= 03 ;
         else      portval &=~ 03 ;
         outportb(0x61, portval) ;
}