Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream.h>
- #include <conio.h>
- #include <dos.h>
- void PrintBinary(int);
- class Speaker{
- public:
- static void On()
- {
- int portState = 0x0;
- portState = inp(0x61);
- portState |= 0x03;
- outp( 0x61, portState);
- }
- static void Off()
- {
- int portState = inp(0x61);
- portState &= 0xFC;
- outp( 0x61, portState );
- }
- static void Beep( unsigned int frequence, int duration)
- {
- Speaker::SetFrequence(frequence);
- delay(duration);
- }
- static void ReadStates()
- {
- outp(0x43, 0xE2); // 11 1 0 001 0
- cout << "\n0x40 ";
- PrintBinary((int)inp(0x40));
- outp(0x43, 0xE4); // 11 1 0 010 0
- cout << "\n0x41 ";
- PrintBinary((int)inp(0x41));
- outp( 0x43, 0xE8); // 11 1 0 100 0
- cout << "\n0x42 ";
- PrintBinary((int)inp(0x42));
- }
- private:
- static void SetFrequence( unsigned int freq)
- {
- int result = 0;
- if ( freq <= 0 ) return;
- const long TimerClock = 1193180;
- int val = 0;
- val = TimerClock / freq;
- outp( 0x43, 0xB6);
- result = val % 256;
- outp( 0x42, result);
- result = val / 256;
- outp( 0x42, result);
- }
- };
- void PrintBinary( int decimal )
- {
- int copy = decimal;
- int i = 0;
- short array[20] = {0};
- for( ; copy != 0; ++i )
- {
- array[i] = copy % 2;
- copy /= 2;
- }
- i = 7;
- while ( i >= 0)
- {
- cout << array[i--];
- }
- }
- void main()
- {
- clrscr();
- // ----------------
- Speaker::On();
- Speaker::Beep(1193, 500);
- Speaker::ReadStates();
- Speaker::Off();
- // ----------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement