home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / UTSPKR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  2.0 KB  |  74 lines

  1. /**
  2. *
  3. * Name        UTSPKR -- Turn the speaker on or off.
  4. *
  5. * Synopsis    utspkr(freq);
  6. *
  7. *        unsigned freq      The frequency of the sound wave to
  8. *                  produce, or 0 to turn speaker off.
  9. *
  10. * Description    If freq is nonzero, this function turns the speaker on
  11. *        so that it produces a (square) sound wave with a specified
  12. *        frequency.  The resulting sound is produced until UTSPKR
  13. *        is invoked again.
  14. *
  15. *        If freq is zero, the speaker is turned off.
  16. *
  17. *        Freq must be at least 19.
  18. *
  19. * Method    The 8253 Programmable Interrupt Timer expects a "latch
  20. *        register value" which when multiplied by the wave
  21. *        frequency is the clock frequency, 1,193,180 Hertz.  The
  22. *        latch value is computed and the 8253 is programmed
  23. *        accordingly.
  24. *
  25. * Returns    Nothing.
  26. *
  27. * Version    6.00 (C)Copyright Blaise Computing Inc.  1984,1987,1989
  28. *
  29. **/
  30.  
  31. #include <conio.h>
  32. #include <dos.h>
  33.  
  34. #include <butil.h>
  35.  
  36.  
  37. /* The following are hardware port addresses needed to access the   */
  38. /* 8255 (speaker) and 8253 (timer) chips.                */
  39.  
  40. #define PB8255      0x61              /* Port B of the 8255 chip.   */
  41. #define LTC8253   0x42              /* Latch register 8253 (timer)*/
  42. #define CMD8253   0x43              /* Command register of 8253.  */
  43.  
  44. #define CLKFREQ   1193180L          /* System clock frequency.    */
  45.  
  46.  
  47. void utspkr(freq)
  48. unsigned freq;
  49. {
  50.     unsigned latch;
  51.  
  52.     if (freq != 0)
  53.     {
  54.     /* Enable speaker data and timer gate signals on PortB.     */
  55.     utoutp (PB8255, utinp (PB8255) | 3);
  56.  
  57.     /* Now program channel 2 of the 8253 timer to generate a    */
  58.     /* square wave of frequency "freq" Hz.                      */
  59.  
  60.     /* Divide clock frequency by requested speaker frequency to */
  61.     /* generate value for the timer latch.                */
  62.  
  63.     utoutp (CMD8253, 0x0b6);
  64.  
  65.     latch = (unsigned int) (CLKFREQ / freq);
  66.  
  67.     utoutp (LTC8253, utlobyte(latch));
  68.     utoutp (LTC8253, uthibyte(latch));
  69.  
  70.     }
  71.     else        /* Clear the lower two bits of Port B.        */
  72.     utoutp(PB8255, utinp (PB8255) & ~3);
  73. }
  74.