Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  The programmable timer chip is the heart of the standard PC models'
  sound-making abilities. As we have seen, channel 2 of the timer chip is
  dedicated to sound generation. To create sounds, you must program channel
  2 properly and then use the pulses from channel 2 to drive the speaker.

  The timer can be programmed to produce pulses at whatever frequency you
  want, but because it does not keep track of how long the sound continues,
  the sound will continue forever unless it is turned off. Therefore, your
  programs must choose when to end a sound through some sort of timing
  instruction.

  Programming the timer chip

  To program timer channel 2, load the timer chip with an appropriate
  countdown value for the channel 2 counter. (The timer chip holds this
  value in an internal register so that it can reset the counter each time
  it reaches zero.) The countdown value takes effect immediately after you
  load it into the timer chip. The timer chip decrements the counter with
  each cycle of its 1.19318 MHz clock until the counter reaches zero, and
  then it sends an output pulse on channel 2 to the sound generator
  circuitry and starts counting down again.

  In effect, the timer "divides" the countdown value into the clock
  frequency to produce an output frequency. The result is that the timer
  sends out a series of pulses that produce a sound of a certain frequency
  when you turn on the speaker.

  The controlling count and the resulting frequency have a reciprocal
  relationship, as shown by these formulas:

    Count=1,193,180.Frequency
    Frequency=1,193,180.Count

  You can see that a low-frequency (low-pitched) sound is produced by a high
  count and that a high-frequency (high-pitched) sound is produced by a low
  count. A count of 100 would produce a high pitch of roughly 11,931 cycles
  per second, and a count of 10,000 would produce a low pitch of about 119
  cycles per second.

  You can produce just about any frequency, within the limitations of 16-bit
  arithmetic. The lowest frequency is 18.2 hertz with a divisor of 65,535
  (FFFFH), and the highest is 1.193 megahertz with a divisor of 1. BASIC
  holds this to a practical range of 37 through 32,767 hertz. The following
  program demonstrates that the actual frequency range of the internal
  speaker is even less than BASIC provides.

  Once you calculate the count that you need for the frequency you want, you
  send it to the timer channel 2 registers. This is done with three port
  outputs. The first port output notifies the timer that the count is coming
  by sending the value B6H (decimal 182) to port 43H (decimal 67). The next
  two outputs send the low- and high-order bytes of the count, a 16-bit
  unsigned word, to port 42H (decimal 66)--the low-order byte followed by
  the high-order byte. The BASIC program on the following page illustrates
  the process.

  10 COUNT = 1193280! / 3000      ' 3000 is the desired frequency
  20 LO.COUNT = COUNT MOD 256     ' calculate low-order byte value
  30 HI.COUNT = COUNT / 256       ' calculate high-order byte value
  40 OUT &H43, &HB6               ' get timer ready
  50 OUT &H42, LO.COUNT           ' load low-order byte
  60 OUT &H42, HI.COUNT           ' load high-order byte

  Activating the speaker

  After you have programmed the timer, you still need to activate the
  speaker circuitry in order to use the signal that the timer is generating.
  As with most other parts of the PC and PS/2, the speaker is manipulated by
  sending certain values to a specific port, a process illustrated in Figure
  7-3. The speaker is controlled by changing the values of bits 0 and 1 at
  I/O port 61H (decimal 97). Only 2 of the port's 8 bits are used by the
  speaker: the low-order bits numbered 0 and 1. The other 6 bits are used
  for other purposes, so it is important that you don't disturb them while
  working with the speaker.

          Get                               Send pulses
          timer        Port                 to
  +-----+ ready        43H  +--------------+speaker+-----------+
  |     |------------------.|              |------.|           |
  |     | Load              |    8253-5    |       |           |
  |     | frequency    Port | Programmable |       |           |    +-----+
  |     | count        42H  |    timer     |       |           |  +-+     |
  | CPU |------------------.|              |       | Amplifier |-.|Speaker|
  |     |                   +--------------+       |           |  +-+     |
  |     | Turn on                             Port |           |    +-----+
  |     | speaker                             61H  |           |
  |     |-----------------------------------------.|           |
  +-----+                                          +-----------+

  Figure 7-3.  How sound frequencies are generated through the system timer
  and speaker.

  The lowest bit, bit 0, controls transmission of the timer chip's output
  signal to the speaker. The second bit, bit 1, controls the pulsing of the
  speaker. Both bits must be set to make the speaker respond to the timer
  chip's signal. You can turn them on without disturbing the nonspeaker bits
  with an operation like this:

  70 OLD.PORT = INP (&H61)          ' read the value at port 61H
  80 NEW.PORT = (OLD.PORT OR &H03)  ' set bits 0 and 1
  90 OUT &H61, NEW.PORT             ' turn speaker on

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson