Play tune received from Master Picaxe

tarzan

Senior Member
This code is to receive a tune sent by master Picaxe followed by CR to terminate end of string.
Code:
 SYMBOL MUSIC_OUT  = B.0
 SYMBOL TEMPO  = B0
 
 #com 0 
 #picaxe 20x2
 #no_data
 #no_table
 
 SETFREQ M4
 
 HSERSETUP B9600_4, %111
 
 FLAG5  = 0
 PTR   = 0
 HSERINPTR  = 0
 
 SETINTFLAGS %00100000,%00100000
 
MAIN:
 DO
 LOOP
GOTO MAIN
 
INTERRUPT:
 DO UNTIL @PTRINC = CR 'Hold until finished loading tune
 LOOP
 
 PTR = 0
 TEMPO = @PTR MIN 1 MAX 15
 PTR = 1
 DO
  TUNE MUSIC_OUT, TEMPO,(@PTRINC) 
 LOOP UNTIL PTR = HSERINPTR
 
 FLAG5  = 0
 PTR   = 0
 HSERINPTR  = 0
 SETINTFLAGS %00100000,%00100000
RETURN
P.S. Turn on Collapsing Blocks makes life easier.
 

Attachments

jim1248

Member
Hey Dan would you mind if I republished this piece of code.
I am writing up a tutorial on PicaxeX2 Interrupts and your piece of code looks like it would be a great way do demonstrate how to use the serial interrupt.The code would be kept intact(except for the adding of comments) and I would acknowledge your work.

Jimmy
 

hippy

Technical Support
Staff member
I think there's a bug in the code which means there may be problems for subsequent tunes sent.

The "DO UNTIL @ptrinc = CR:LOOP" command continually scans scratchpad looking for a CR, so if "abc...xyz<CR>" is sent that CR is detected and the tune plays. If a subsequent byte, ( say "X" ) is recieved that's placed in the buffer at location 0, the interrupt fires and the CR is looked for. At that time the buffer will be "Xbc...xyz<CR>" so the CR is detected and the tune is played, though the tune has not yet been fully received.

I'm not sure if this is intended behaviour but suspect not. The quick solution would be to clear the scratchpad byte containing CR once the first tune has played.
 
Top