Need Help! generate tone using C

Thread Starter

nearownkira

Joined Feb 19, 2008
20
Is there a way that u can write the code in C to generate tone such , A or G tone like in assembly?I know how to write the delay function, but I did not see any equivalent code in writing to the timer register.
 

mgalvez

Joined Mar 27, 2008
2
// PC Running MS-DOS, Borland Turbo C++
#include <stdio.h>
void sound(int frequency);
void main(void) {
int myFrequency;
myFrequency = 2000; // Sample frequency to make this listing work...

sound(myFrequency); // Whatever the frequency for Middle C is...
}

void sound(int frequency) {
asm {
mov al, 182; out 43h, al; mov ax, frequency

out 42h, al; mov al, ah; out 42h, al; in al, 61h
or al, 2; out 61h, al; mov bx, 25
task1:
mov cx, 65535
task2:
dec cx; jne task2; dec bx; jne task1; in al, 61h
and al, 252; out 61h, al
}
}
 

SgtWookie

Joined Jul 17, 2007
22,221
Using the code & /code tags helps a great deal when posting program listings - it preserves the original formatting.
Rich (BB code):
// PC Running MS-DOS, Borland Turbo C++
#include <stdio.h>
void sound(int frequency);
void main(void) {
   int myFrequency;
   myFrequency = 2000; // Sample frequency to make this listing work...
   sound(myFrequency); // Whatever the frequency for Middle C is...
}
void sound(int frequency) {
   asm {
      mov al, 182; out 43h, al; mov ax,  frequency
      out 42h, al; mov al,  ah; out 42h, al; in al, 61h
      or  al,   2; out 61h, al; mov bx, 25
task1:
      mov cx, 65535
task2:
      dec cx; jne task2; dec bx; jne task1; in al, 61h
      and al, 252; out 61h, al
   }
}
Middle C=261.63 Hz, 262 is close.
Middle A=440 Hz exactly.
Middle G=392 Hz exactly.
See: http://www.phy.mtu.edu/~suits/notefreqs.html
 
Top