Picaxe 08M2 noise generator

Dave Harpe

New Member
Playing with Picaxe generated sounds with an 08M2 Picaxe chip and a few other parts, on a Picaxe board for that chip. I can't show a picture of the board and this drawing, so I chose the drawing.

How it's wired.jpg

Here is the code so far. It just does one sound, after another and then repeats. There is still a lot of room to add more sounds, as you get ideas, the memory is about half full. If you come up with one you would like to share, add it as a comment.

Unfortunately, there seems to be a character limit, which makes it impossible for me to post the code. Does anyone know how to get around that?
 

Dave Harpe

New Member
Apparently, code can't be posted as a comment, same problem. I'll try half at a time.
Code:
#Rem
Testing and playing around with sound that could be made
with one of these.
The best way to hook up this is to connect it to the high impedance
connection of a guitar amplifier, the one which you would usually
plug your Strat into. Output impedance is high, and also, the capacitor
will not filter high frequencies if the input impedance of the amplifier
is low. Start with volume all the way down, and turn it up to the best level.
Voltage from this device is higher than the voltage from an electric guitar.
It may be necessary to add a series capacitor to the output, some amplifiers
may not have this, and the DC offset would be a problem if yours is like that.
If so, another 0.1 uF capacitor will work for this purpose.
#endrem
'Variables:
symbol scratch1=b27
symbol counter1=b26
symbol counter2=b25
symbol volcontrol=b24
symbol longcount1=w11
symbol tone1=b21
symbol tone2=b20
symbol wildcard=w9
symbol math1=w8
symbol reedpos=w7
symbol speed=w6
symbol binpointer=b11
symbol dummy1=b10
'Constants:
symbol center=34816 ;was 32768, changed because there was an offset
symbol kick=2780 '2780 These two numbers produce the wave is it is now
symbol gravity=198 '198 They affect amplitude and frequency
'Devices:
symbol led=c.0 ;This goes to a transistor which drives the LED, and also is a sound output.
symbol pulse=c.2 ;This is the PWM, which can also produce sounds, and is also connected.
'Starts here*************
SineMaker:
#rem
This is my effort to produce something close to a sine wave using
the 08M2, which does not have higher math functions like SIN. It
actually worked out surprisingly well, but this microcontroller
probably can't do this fast enough to produce a sine wave as a sound.
It turns out that its highest frequency is just 12 Hz, for a wave that has
64 points in it, and of course, increasing the frequency will reduce
the resolution. I use the "sertxd" to generate a graph of the wave, and
when I have what I want, I save that with the "poke" instruction, and
then play it just by "peeking" it and setting the DAC. This can run much
faster than the generator, up to 90 Hz for waves with 64 points, and almost twice
that for a wave with 32. Fewer points than that, and it's too distorted to
be a good sine wave without a lot of filtering after it's generated.
The 0.1 uF capacitor is a filter which makes the sound more "mellow".  I
used a movable jumper to connect it or not to.
If the amplifier is connected while it's sending text, it makes quite a racket,
because both the DAC and the text output are on the same pin on
08M2, and I think that's true of all the Picaxe chips.
To see the chart, use the sertxd instructions and loops by just removing the first character,
which is a semicolon or an apostrophe, and it is processed as an instruction instead of a
comment.
This section runs at 4 Mhz so it can print at the normal baud rate.
#endrem
;sertxd ("test",13,10)
speed=center
speed=speed+kick
reedpos=center
;pause 8000
printasine:
for counter1=0 to 63
    if reedpos>=center then
        if speed>center then
            math1=speed-center
            reedpos=reedpos+math1
            speed=speed-gravity
        else
            math1=center-speed
            reedpos=reedpos-math1
            speed=speed-gravity
        endif
    else
        if speed<=center then
            math1=center-speed
            reedpos=reedpos-math1
            speed=speed+gravity
        else
            math1=speed-center
            reedpos=reedpos+math1
            speed=speed+gravity
        endif
    endif
    scratch1=reedpos/2048
    ;Got it now poke it
    binpointer=counter1+32
    poke binpointer, scratch1 ;Hopefully "poke" can't work where it is not supposed to.
;    for counter2=0 to scratch1 ;Use this for charting a sine wave
;        sertxd ("*")
;    next counter2
;    sertxd ("  ",#scratch1,13,10)
next counter1
 
Last edited:

Dave Harpe

New Member
Maybe the other half, just copy and paste right after that last half.
Code:
setfreq m32 ;When generating sound, the highest clock rate is usually the best
DooDaaLoop:
dacsetup %10100000 'Need to do this before using it
daclevel 0
wildcard=3456;Just a seed number for randomization of this variable.
'Sine Wave Test, this actually generated as it's played, only very low freq is possible
gosub hummer
pause 5000
;Play a sine wave from a pre calculated chart
gosub ReadAndSing
pause 5000
'Try it at twice frequency (It's actually a little lower than that)
gosub UpOctaveSing
pause 5000
'Modify a square wave of low frequency
gosub squaredance
pause 5000
'First thing with PWM. Might also try mixing both
gosub synth1
pause 5000
' 61 Hz Triangle Wave, Buzzzz, at 32Mhz clock
'followed by 2 more, at different frequency.
gosub trangle61
pause 5000
'It's just a 3 Khz beep at 32 Mhz clock
gosub simplebeep
pause 5000
'Digital rendition of a fart
gosub fart
pause 5000
'Simple square wave
gosub square1
pause 5000
'Two tones mixed
gosub twotone
pause 5000
'Another 2 tone thing
gosub twotone2
pause 5000
'Whatever, tried to do a drum sound, weird, ain't it?
gosub AAALIENZ
pause 5000
'Yet another 2 tone
gosub SHTFAlarm
pause 5000
'Uh oh, did someone die in a video game?
gosub stepdown6
pause 5000
''Zappityzappityzappp!
gosub SciFiGun
pause 5000
'The End, Blow it all up!
gosub KaBoom
pause 5000
daclevel 0
dacsetup 0 'go back to programming setup
goto doodaaloop
'*********************************
;Play a Sine Wave From the table made during startup
Readandsing:
for longcount1=1 to 500
    for binpointer=32 to 95 step 2
        peek binpointer, scratch1 
        daclevel scratch1
    next binpointer
next longcount1
daclevel 0
return
;Try it at twice the frequency, expected to be 180 Hz (Not quite, but close)
UpOctaveSing:
for longcount1=1 to 1000
    for binpointer=32 to 95 step 4
        peek binpointer, scratch1 
        daclevel scratch1
    next binpointer
next longcount1
daclevel 0
return
'See what is possible with sine wave generator
'I expect only frequencies too low will be possible, but here goes.
Hummer:
for counter2=1 to 100
speed=center
speed=speed+kick
reedpos=center
for counter1=1 to 64
    if reedpos>=center then
        if speed>center then
            math1=speed-center
            reedpos=reedpos+math1
            speed=speed-gravity
        else
            math1=center-speed
            reedpos=reedpos-math1
            speed=speed-gravity
        endif
    else
        if speed<=center then
            math1=center-speed
            reedpos=reedpos-math1
            speed=speed+gravity
        else
            math1=speed-center
            reedpos=reedpos+math1
            speed=speed+gravity
        endif
    endif
    scratch1=reedpos/2048
    daclevel scratch1
next counter1
next counter2
daclevel 0
return
'Changing duty on a square wave
squaredance:
for counter1=1 to 3
for longcount1=1 to 300
    daclevel 0
    scratch1=longcount1/9
    pause scratch1
    daclevel 12
    scratch1=35-scratch1
    pause scratch1
next longcount1
next counter1
daclevel 0
return
'First try using PWM for something, will try mixing both as well
Synth1:
;full duty is 1023, lowest freq at 32 Mhz is 488 Hz
pwmout pwmdiv64, 2, 255, 0
for counter1=1 to 100
    pause 100
    math1=counter1*3
    pwmduty c.2, math1
next counter1
pwmduty c.2, 0
'This was done in the original while waiting for the
'battery test to get ready. Now, it's just another
'way to make noise.
return
trangle61:
for counter2=1 to 100
    for counter1=0 to 31 'Ramp up
        daclevel counter1
    next counter1
    pause 12
    for counter1=31 to 0 step -1 'Ramp down
        daclevel counter1
    next counter1
next counter2
for counter2=1 to 100
    for counter1=0 to 25 'Ramp up
        daclevel counter1
    next counter1
    pause 5
    for counter1=25 to 0 step -1 'Ramp down
        daclevel counter1
    next counter1
next counter2
for counter2=1 to 200
    for counter1=1 to 29 'Ramp up
        daclevel counter1
    next counter1
    pause 8
    for counter1=29 to 0 step-1 'Ramp down
        daclevel counter1
    next counter1
next counter2
daclevel 0
return
simplebeep:
for longcount1=1 to 5000
    daclevel 0
    daclevel 1
next longcount1
daclevel 0
return
fart:
daclevel 0
for counter1=1 to 31
    daclevel 31
    scratch1=31-counter1
    scratch1=scratch1*4
    daclevel 0
    pause 124
    daclevel 31
    daclevel 0
    pause scratch1
next counter1
daclevel 0
return
'simple square wave
square1:
for longcount1=1 to 300
    daclevel 0
    pause 35
    daclevel 10
    pause 35
next longcount1
daclevel 0
return
'Trying to make 2 tones at once, see how that works:
twotone:
tone1=0
tone2=0
for longcount1=1 to 5000
    inc tone1
    if tone1=5 then
        daclevel 16
        daclevel 0
        tone1=0
    endif
    inc tone2
    if tone2=8 then
        daclevel 16
        daclevel 0
        tone2=0
    endif
next longcount1
daclevel 0
return
'Another 2 tone thing...
twotone2:
tone1=0
tone2=0
for longcount1=1 to 5000
    inc tone1
    if tone1=3 then
        daclevel 16
        daclevel 0
        tone1=0
    endif
    inc tone2
    if tone2=4 then
        daclevel 16
        daclevel 0
        tone2=0
    endif
next longcount1
daclevel 0
return
'Started as a try at a drum sound, whatever!
AAALIENZ:
for longcount1=1 to 10
for counter2=20 to 1 step -1
    for counter1=0 to 31 'Ramp up
        math1=counter1*counter2/20
        daclevel math1
    next counter1
    for counter1=31 to 0  step-1 'Ramp down
        math1=counter1*counter2/20
        daclevel math1
    next counter1
next counter2
next longcount1
daclevel 0
return
'PANIC, PANIC IT'S HITTING THE FAN!!
SHTFAlarm:
tone1=0
tone2=0
for counter1=1 to 9
for longcount1=1 to 500
    inc tone1
    if tone1=4 then
        daclevel 16
        daclevel 0
        tone1=0
    endif
    inc tone2
    if tone2=9 then
        daclevel 24
        daclevel 0
        tone2=0
    endif
next longcount1
pause 500
next counter1
daclevel 0
return
'Down the stairs
stepdown6:
scratch1=0
for longcount1=1 to 600
    daclevel 12
    daclevel 0
    scratch1=longcount1/50
    scratch1=scratch1+6
    pause scratch1
next longcount1
for longcount1=1 to 200
    daclevel 12
    daclevel 0
    pause 19
next longcount1
daclevel 0
return
'Zapzapzapzap!!
ScifiGun:
tone1=0
scratch1=0
for longcount1=1 to 500
    daclevel 12
    daclevel 0
    if tone1<31 then
        inc tone1
    else
        tone1=0
    endif
    pause tone1
next longcount1
daclevel 0
return
'Drop the Big Bomb!    
KaBoom:
for longcount1=1 to 4096
random wildcard
scratch1=wildcard/2048
volcontrol=longcount1/128
volcontrol=volcontrol+1 ;Oops! Never divide by zero.
scratch1=scratch1/volcontrol
daclevel scratch1
next longcount1
daclevel 0
return
That's it for now, don't make your room mate or neighbors crazy, OK? :D
 
Top