PDA

View Full Version : Christmas Carols on a PIC



LEDave
- 20th December 2010, 17:55
With Xmas nearly upon us, it might be a nice idea to build a library of Christmas carols that can be played on a PIC. So if you have a tune you'd like to add, please do.

Here's one to get you started:


'************************************************* ***************
'* Name : Jingle Bells.BAS *
'* Author : David Taylor (LEDave) *
'* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 14/12/2010 *
'* Version : 1.0 *
'* Notes : For a 16F684 easily adapted though *
'* : *
'************************************************* ***************
ANSEL = %00000000 'Disable analog select so ports work as digital i/o.
CMCON0 = %00000111 'Disable analog comparators.
TRISA = %00000000 'Set PORTA as OUTPUT.
PORTA = %00000000 'Set PORTA pins all low.
TRISC = %00000000 'Set PORTC as OUTPUT.
PORTC = %00000000 'Set PORTC pins all low.
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON

MPIN VAR PORTA.5 'Pos+ side of Sounder attached here.

Y VAR BYTE 'Note delay time VAR

MAIN:
' @ SLEEP 'Put the PIC to sleep until MCLRE pressed.
' @ NOP


START:
PAUSE 150
LET y = 4 'Delay between notes = 28 mSec
Sound MPIN, [98,30,0,Y,98,30,0,Y,98,70,0,Y,98,30,0,Y,98,30,0,Y, 98,70,0,Y,98,30,0,Y,102,30,0,Y]
SOUND MPIN, [90,30,0,Y,94,30,0,Y,98,70,0,Y,99,30,0,Y,99,30,0,Y, 99,50,0,Y,99,10,0,Y,99,30,0,Y]
sound mpin, [98,30,0,Y,98,40,0,Y,98,10,0,Y,98,10,0,Y,98,30,0,Y, 94,30,0,Y,94,30,0,Y,98,30,0,Y,94,60,0,Y,102,80,0,y]
Sound MPIN, [98,30,0,Y,98,30,0,Y,98,70,0,Y,98,30,0,Y,98,30,0,Y, 98,70,0,Y,98,30,0,Y,102,30,0,Y]
SOUND MPIN, [90,30,0,Y,94,30,0,Y,98,70,0,Y,99,30,0,Y,99,30,0,Y, 99,50,0,Y,99,10,0,Y,99,30,0,Y]
SOUND MPIN, [98,30,0,Y,98,40,0,Y,98,10,0,Y,98,10,0,Y,102,30,0,Y ,102,30,0,Y,99,30,0,y,94,30,0,y,90,80,0,y]
pause 250 'Second verse
SOUND MPIN, [90,30,0,Y,105,30,0,Y,102,30,0,Y,99,30,0,Y,90,70,0, Y,90,10,0,Y,90,10,0,Y,90,30,0,Y,105,30,0,Y,102,30, 0,Y,99,30,0,Y,94,70,Y]
SOUND MPIN, [94,30,0,Y,107,30,0,Y,105,30,0,Y,102,30,0,Y,98,70,0 ,Y,109,30,0,Y,109,30,0,Y,107,30,0,Y,102,30,0,Y,105 ,70,0]
SOUND MPIN, [90,30,0,Y,105,30,0,Y,102,30,0,Y,99,30,0,Y,90,70,0, Y,90,10,0,Y,90,10,0,Y,90,30,0,Y,105,30,0,Y,102,30, 0,Y,99,30,0,Y,94,70,Y]
SOUND MPIN, [94,40,0,Y,107,30,0,Y,105,30,0,Y,102,30,0,Y,109,30, 0,Y,109,30,0,Y,109,30,0,Y,111,30,0,Y,109,30,0,Y,10 7,30,0,Y,102,30,0,Y,99,100,0,Y]
GOTO MAIN
Dave

mackrackit
- 21st December 2010, 01:08
Sounds like Jingle Bells to me.

Did you come up with the frequencies by trial and error or did you work out/know the frequencies of the notes and just code them in?

LEDave
- 21st December 2010, 14:07
Sounds like Jingle Bells to me.

Cheers mackrackit, it's somewhere handy I think, not perfect but not too bad.


Did you come up with the frequencies by trial and error or did you work out/know the frequencies of the notes and just code them in?

Well the way I did it was this. I sat at the keyboard at tapped out the notes for Jingle Bells (no music) and wrote the notes down as I played.

So taking the first three notes ((Jin + gle) + Bells) ('Jingle' is made up of two notes) gives us these three notes: E E E two E's for Jingle and one for Bells. This then generates this code:


98,30,0,Y, (Jin) 98,30,0,Y, (gle) 98,70,0,Y (Bells)

But wait a minute you all say, how do you get:


98,70,0,Y = to the Note 'E'?

Well, before I even lit my first led, I came across this chart (see attachment) and being interested in music I saved it.

So from the chart, we know we want the note 'E' and the chart gives us: E4 which is E in the fourth octave (the octave above middle 'C') as = 98. So three E's would be 98_98_98 which is what we have (we're not quite finished yet though).

Having the three E's ((Jin + gle) + Bells) is good but we need to have the notes stay on for a certain lenght of time, the equivalent of holding you finger down on the key your pressing.

Therefore the second number of the 'Note' and our first three notes were:

[CODE]
98,30,0,Y, (Jin) 98,30,0,Y, (gle) 98,70,0,Y (Bells)

gives us:


30 - 30 - 70

This is the duration that the notes stay on in 12mili_sec intervals times the value, namley 30 x 12mili_sec and 70 x 12mili_secs for our first three notes.

And finely, 'Y' is the VAR time between the actual notes playing. In this program Y = 4 so 4 x 12mili_secs between notes.

Now I think all the above is accurate but a lot of it was down to my interpretation of what's actually happening. If anyone can see any glaring mistakes, then please say so.

Oh one last thing to add. If you do give music a go and it's fun so why not, it can be a long / slow process.

Ah, one very last thing. All the zero's in the code as in code:


98,30,0,Y

I now don't think they need to be there. The first time I ever say a music program they were there, so I added them in.

Dave