Advertisement
rnort

AVMIS

Sep 24th, 2012
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <dos.h>
  4.  
  5. void PrintBinary(int);
  6.  
  7.  
  8. class Speaker{
  9.  
  10. public:
  11.     static void On()
  12.     {
  13.         int portState = 0x0;
  14.         portState = inp(0x61);
  15.         portState |= 0x03;
  16.         outp( 0x61, portState);
  17.     }
  18.     static void Off()
  19.     {
  20.         int portState = inp(0x61);
  21.         portState &= 0xFC;
  22.         outp( 0x61, portState );
  23.     }
  24.     static void Beep( unsigned int frequence, int duration)
  25.     {
  26.         Speaker::SetFrequence(frequence);
  27.         delay(duration);
  28.     }
  29.     static void ReadStates()
  30.     {
  31.         outp(0x43, 0xE2); // 11 1 0 001 0
  32.         cout << "\n0x40 ";
  33.         PrintBinary((int)inp(0x40));
  34.         outp(0x43, 0xE4); // 11 1 0 010 0
  35.         cout <<  "\n0x41 ";
  36.         PrintBinary((int)inp(0x41));
  37.         outp( 0x43, 0xE8); // 11 1 0 100 0
  38.         cout << "\n0x42 ";
  39.         PrintBinary((int)inp(0x42));
  40.     }
  41. private:
  42.     static void SetFrequence( unsigned int freq)
  43.     {
  44.         int result = 0;
  45.         if ( freq <= 0 ) return;
  46.         const long TimerClock = 1193180;
  47.         int val = 0;
  48.         val = TimerClock / freq;
  49.         outp( 0x43, 0xB6);
  50.         result = val % 256;
  51.         outp( 0x42, result);
  52.         result = val / 256;
  53.         outp( 0x42, result);
  54.     }
  55. };
  56.  
  57.  
  58. void PrintBinary( int decimal )
  59. {
  60.     int copy = decimal;
  61.     int i = 0;
  62.     short array[20] = {0};
  63.     for( ; copy != 0; ++i )
  64.     {
  65.         array[i] = copy % 2;
  66.         copy /= 2;
  67.     }
  68.     i = 7;
  69.     while ( i >= 0)
  70.     {
  71.         cout << array[i--];
  72.     }
  73. }
  74.  
  75. void main()
  76. {
  77.     clrscr();
  78.     // ----------------
  79.     Speaker::On();
  80.     Speaker::Beep(1193, 500);
  81.     Speaker::ReadStates();
  82.     Speaker::Off();
  83.     // ----------------
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement