Here is a program I've been working on to play Jingle Bells on a PIC.
I'm sure I'm not the first to do this, so if anyone has any other Carols they'd like to add, please do so.
Merry Xmas.
Dave
'**************************************************************** '* 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,107,30,0,Y,102,30,0,Y,99,100,0,Y] GOTO MAIN
http://www.picbasic.co.uk/forum/show...7465#post97465
This is how I did it:
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)
98,70,0,Y = to the Note 'E'?
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:
98,30,0,Y, (Jin) 98,30,0,Y, (gle) 98,70,0,Y (Bells)
30 - 30 - 70
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
Dave
Re: K42 and Timer Interrupts
Thanks for the explanation.
Ioannis - 28th April 2025, 19:28I misinterpreted these paragraphs. My understanding was to have ASYNC cleared and use Fosc/4.
Ioannis