Sound generation, sound library...
Audio signals are often used when it is necessary to call the user’s attention to do something, to confirm that one of the push buttons is pressed, to warn that minimum or maximum values are reached etc. It can be just a ‘beep’ signal as well as longer or shorter melodies. This example demonstrates how to generate a sound using functions belonging to the Sound library. In adition to these functions, the Button function is also used for testing push buttons.'Header****************************************************** program example_13 ' Program name sub procedure Tone1() Sound_Play(659, 250) ' Frequency = 659Hz, duration = 250ms end sub sub procedure Tone2() Sound_Play(698, 250) ' Frequency = 698Hz, duration = 250ms end sub sub procedure Tone3() Sound_Play(784, 250) ' Frequency = 784Hz, duration = 250ms end sub sub procedure Melody() ' Play the melody "Yellow house" Tone1() Tone2() Tone3() Tone3() Tone1() Tone2() Tone3() Tone3() Tone1() Tone2() Tone3() Tone1() Tone2() Tone3() Tone3() Tone1() Tone2() Tone3() Tone3() Tone3() Tone2() Tone2() Tone1() end sub sub procedure ToneA() ' Tones used in Melody2 function Sound_Play( 880, 50) end sub sub procedure ToneC() Sound_Play(1046, 50) end sub sub procedure ToneE() Sound_Play(1318, 50) end sub sub procedure Melody2() ' Play Melody2 dim counter as byte for counter = 9 to 1 step -1 ToneA() ToneC() ToneE() next counter end sub main: ' Start of program ANSEL = 0 ' Configure analog pins as digital I/O ANSELH = 0 C1ON_bit = 0 ' Disable comparators C2ON_bit = 0 TRISB = 0xF0 ' Configure RB7..RB4 as inputs and RB3 as output Sound_Init(PORTD, 3) Sound_Play(880, 5000) while TRUE ' Endless loop if (Button(PORTB,7,1,1)) then ' If PORTB.7 is pressed play Tone1 Tone1() while (RB7_bit <> 0) nop ' Wait for the button to be released wend end if if (Button(PORTB,6,1,1)) then ' If PORTB.6 is pressed play Tone1 Tone2() while (RB6_bit <> 0) nop ' Wait for the button to be released wend end if if (Button(PORTB,5,1,1)) then ' If PORTB.5 is pressed play Tone1 Melody2() while (RB5_bit <> 0) nop ' Wait for the button to be released wend end if if (Button(PORTB,4,1,1)) then ' If PORTB.4 is pressed play Tone1 Melody() while (RB4_bit <> 0) nop ' Wait for the button to be released wend end if wend end. ' End of programIn order to make this example work properly, it is necessary to check the following libraries in the Library Manager prior to compiling: