PDA

View Full Version : Musical sounds



AvionicsMaster1
- 21st June 2012, 14:22
Ladies and Gents,
I'll ask the question first then expand. Why will the first program, ImperialMarch.bas, play the notes only one time through and requires a reprogram for the notes to be played again and the second, This works, play the notes without having to reprogram the chip? I know the first is set up to loop and the second is triggered but why does the first require a reprogram to play the music?

Background: Someone asked if I could write a program simulating the sounds of the Imperial Death March using a PIC. I had a12F683 a piezo speaker and some time and this is what I came up with. Other than the speaker hooked up to gpio.2 there is a resistor pulling gpio.5 high or low as needed and a resistor pulling gpio.3 high. The first program is just a loop, or it should be, and the second is triggered by a switch.

The first program, imperailmarch.bas, will play the sounds when programmed then won't play them again until the chip is reprogrammed. It will, if left alone long enough, intermittently make a scratchy sound like a modems initial negotiation for speed.


'************************************************* ***************
'* Name : ImperialMarch.BAS *
''************************************************ ****************
' This version will only play one time through and stop.

clear

@ device mclr_off
@ device bod_off
@ device fcmen_off
@ device ieso_off
@ device intrc_osc_noclkout
@ device pwrt_on
@ device protect_off

define osc4
osccon = %01100111

ansel = 0 ' set all ports digital
cmcon0 = 7 ' comparators off
adcon0 = 0 ' adc off

'------------------------variables

c con 91 ' converting values to frequency and musical notes
a con 105
dsH con 112
f con 99
fH con 114
fsH con 115
gs con 104
cs con 92
d con 95
ds con 96
notta con 97
e con 98
aH con 117
cH con 109
eH con 113
gsh con 116
gH con 115
aS con 106
b con 108 ' end of music

speaker var gpio.2
'-------------------------------------------finally the program
main:

goto playsong

pause 1000

goto main ' do it forever


playsong:
sound speaker, [a,50]
sound speaker, [a,50]
sound speaker, [a,50]
sound speaker, [f,40]
sound speaker, [cH,20]
sound speaker, [a,50]
sound speaker, [f,43]
sound speaker, [cH,20]
sound speaker, [a,65]

pause 150

sound speaker, [eH,40]
sound speaker, [eH,40]
sound speaker, [eH,40]
sound speaker, [fh,30]
sound speaker, [cH,12]
sound speaker, [gS,40]
sound speaker, [f,33]
sound speaker, [cH,12]
sound speaker, [a,55]

pause 150

return


end ' supposed to put an end to the program




The second program, This works.bas, will play the notes anytime I drag GPIO.5 low.


'************************************************* ***************
'* Name : thisworks.BAS * *
'************************************************* ***************
' This version will replay the march each time gpio.5 is brought low.
' It draws less than 40uA typically but if allowed to rest long enough
' it will wander up to around 200uA. Why it changes I also don't understand.
' ***

clear

@ device mclr_off
@ device bod_off
@ device fcmen_off
@ device ieso_off
@ device intrc_osc_noclkout
@ device pwrt_on
@ device protect_off

define osc4
osccon = %01100111
ansel = 0 ' set all ports digital
cmcon0 = 7 ' comparators off
adcon0 = 0 ' adc off

'------------------------variables

c con 91 ' converting values to frequency corresponding to musical notes
a con 105
dsH con 112
f con 99
fH con 114
fsH con 115
gs con 104
cs con 92
d con 95
ds con 96
notta con 97
e con 98
aH con 117
cH con 109
eH con 113
gsh con 116
gH con 115
aS con 106
b con 108 ' end of music

speaker var gpio.2

'-----------------------------begin program

main:

If gpio.5 = 0 then

sound speaker, [a,40,a,40,a,40,f,30,cH,12,a,40,f,33,cH,12,a,55]

pause 150

sound speaker, [eH,40,eH,40,eH,40,fh,30,cH,12,gS,40,f,33,cH,12,a,5 5]

endif

nap 5

goto main

end ' supposed to put an end to the program







If anyone can expound upon why one works and the other doesn't I'd appreciate your input.

pedja089
- 21st June 2012, 14:48
You have GOTO playsong, and on end of playsong is RETURN.
Return can be used ONLY if you have GOSUB or CALL label.
Replace GOTO playsong with GOSUB playsong, them try it.

AvionicsMaster1
- 21st June 2012, 21:29
Doh! What's that saying about the forest and the trees. Thanks.