Need Help! generate tone using C

Discussion in 'Programmer's Corner' started by nearownkira, Mar 26, 2008.

  1. nearownkira

    Thread Starter Member

    Feb 19, 2008
    20
    0
    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.
     
  2. mgalvez

    New Member

    Mar 27, 2008
    2
    0
    First of all, sound is typically platform specific. What's the platform and I'll see what I can muster up for ya.
     
  3. mgalvez

    New Member

    Mar 27, 2008
    2
    0
    // 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
    }
    }
     
  4. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Using the code & /code tags helps a great deal when posting program listings - it preserves the original formatting.
    Code ( (Unknown Language)):
    1.  
    2. // PC Running MS-DOS, Borland Turbo C++
    3. #include <stdio.h>
    4. void sound(int frequency);
    5. void main(void) {
    6.    int myFrequency;
    7.    myFrequency = 2000; // Sample frequency to make this listing work...
    8.    sound(myFrequency); // Whatever the frequency for Middle C is...
    9. }
    10. void sound(int frequency) {
    11.    asm {
    12.       mov al, 182; out 43h, al; mov ax,  frequency
    13.       out 42h, al; mov al,  ah; out 42h, al; in al, 61h
    14.       or  al,   2; out 61h, al; mov bx, 25
    15. task1:
    16.       mov cx, 65535
    17. task2:
    18.       dec cx; jne task2; dec bx; jne task1; in al, 61h
    19.       and al, 252; out 61h, al
    20.    }
    21. }
    22.  
    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
     
Loading...