Jeremy Leach
Senior Member
This is just a little experiment to play a note using a loop instead of using 'Sound' or 'tune' commands. Actually not sure it's working correctly yet ...(and no idea what I want to use it for, except was hoping could maybe do a sweeping siren sound !)
In the code below I first off used my Timer1 module to time the for-next loop setting the Pulseout value to 0. This gave time around the loop = 0.81166ms. So any Pulseout duration should just add to this.
If the Pulseout duration = D, then the Halfperiod of the output signal = D + 0.81166*10^-3 seconds
So FullPeriod = 2D + 0.00162333 Seconds
At 8MHz each unit of the Pulseout value (call it P) = 5uS.
So FullPeriod = P * 0.00001 + 0.00162333 Seconds
If F is frequency of output signal, FullPeriod = 1/F
So:
<b> P = [(1/F) - 0.00162333)/0.00001 </b> if I've got my maths right !
So, for MiddleC say, which is 278Hz, this gives P = 197. So the idea was to calculate all the constants for all sorts of notes, perhaps bung them in EEPROM.
I've used my scope and my loop time is seen as 1.2ms not 0.811ms ....which is a bit worrying, although could be the PICAXE clock ??
<code><pre><font size=2 face='Courier'>
Symbol Counter = w1
Symbol MiddleC = 197
Symbol NoteDuration = 60000
setfreq m8
PlayNote:
For Counter = 1 To NoteDuration
Pulsout 2, MiddleC 'to dummy pin just to get precise delay
Toggle 3
Next
Goto PlayNote
</font></pre></code>
Somehow I think this might not have the resolution to get the frequencies spot on, and it will sound a bit detuned !!
Edited by - jeremy leach on 07/01/2007 20:29:26
In the code below I first off used my Timer1 module to time the for-next loop setting the Pulseout value to 0. This gave time around the loop = 0.81166ms. So any Pulseout duration should just add to this.
If the Pulseout duration = D, then the Halfperiod of the output signal = D + 0.81166*10^-3 seconds
So FullPeriod = 2D + 0.00162333 Seconds
At 8MHz each unit of the Pulseout value (call it P) = 5uS.
So FullPeriod = P * 0.00001 + 0.00162333 Seconds
If F is frequency of output signal, FullPeriod = 1/F
So:
<b> P = [(1/F) - 0.00162333)/0.00001 </b> if I've got my maths right !
So, for MiddleC say, which is 278Hz, this gives P = 197. So the idea was to calculate all the constants for all sorts of notes, perhaps bung them in EEPROM.
I've used my scope and my loop time is seen as 1.2ms not 0.811ms ....which is a bit worrying, although could be the PICAXE clock ??
<code><pre><font size=2 face='Courier'>
Symbol Counter = w1
Symbol MiddleC = 197
Symbol NoteDuration = 60000
setfreq m8
PlayNote:
For Counter = 1 To NoteDuration
Pulsout 2, MiddleC 'to dummy pin just to get precise delay
Toggle 3
Next
Goto PlayNote
</font></pre></code>
Somehow I think this might not have the resolution to get the frequencies spot on, and it will sound a bit detuned !!
Edited by - jeremy leach on 07/01/2007 20:29:26