inglewoodpete
Senior Member
I recently developed a wireless link and needed sound alerts to indicate certain events or conditions. While PICAXE Basic has the Sound and Tune commands, both are blocking: all execution stops for the duration of the sound.
The code was being developed for a PICAXE 20X2, using both background Serial data reception (via the hSerial port) and the programmable background timer. I added some PWM management code to the background timer interrupt to allow different series of beeps to be played on a small speaker (I used Rev-Ed's SPE002 basic piezo transducer) while allowing the other functions of the data terminal to continue execution.
The wireless application required a fast execution speed (32MHz or higher). To keep PWM frequencies within the range of the piezo sounder, the maximum execution frequency is 32MHz is used.
The attached code is configured for a 20X2 but can easily be adapted to 28X2 or 40X2. The M2-series of chips do not have the programmable timer of the X2s, limiting the codes usefulness for them. The demonstration code requires 3 outputs, piezo speaker and 2 LEDs, to demonstrate the code.
To get the best effect, I used a 125mS timer interrupt - this is the standard 'unit length' of the beeps.
Due to forum posting limitations, the code is supplied in two separate posts.
The code was being developed for a PICAXE 20X2, using both background Serial data reception (via the hSerial port) and the programmable background timer. I added some PWM management code to the background timer interrupt to allow different series of beeps to be played on a small speaker (I used Rev-Ed's SPE002 basic piezo transducer) while allowing the other functions of the data terminal to continue execution.
The wireless application required a fast execution speed (32MHz or higher). To keep PWM frequencies within the range of the piezo sounder, the maximum execution frequency is 32MHz is used.
The attached code is configured for a 20X2 but can easily be adapted to 28X2 or 40X2. The M2-series of chips do not have the programmable timer of the X2s, limiting the codes usefulness for them. The demonstration code requires 3 outputs, piezo speaker and 2 LEDs, to demonstrate the code.
To get the best effect, I used a 125mS timer interrupt - this is the standard 'unit length' of the beeps.
Due to forum posting limitations, the code is supplied in two separate posts.
Rich (BB code):
'Code to demonstrate the use of PWM to allow non-blocking sounds to be generated
Symbol Version = 0 '10-Nov-2017 284 bytes Testing sound generation using PWM
'
Symbol Major = 1 ' Major revision ID
'
#PICAXE 20X2
#COM 4
#Terminal 38400
'
'#No_Table
'
' **** Hardware Pins Definitions - i prefix for inputs; o for outputs; b for bothway pins
'
Symbol oYellowLED = C.7
'
Symbol oSpeaker = C.5
'
Symbol oBlueLED = C.3
'
' **** Variables - t prefix: bit variable; b: byte; w: word; r: other RAM; s: scratchpad; e: EEPROM
'
Symbol bCounter = b4 'w2
Symbol bData = b5 'w2
Symbol bSoundNum = b6 'w3
Symbol bSoundPtr = b7 'w3
Symbol bPWMPeriod = b8 'w4
Symbol bSoundSeq = b9 'w4
Symbol bLoop = b10 'w5
Symbol bEighthSecs = b11 'w5 1/8 Seconds (125mS Tick)
Symbol bSeconds = b12 'w6
Symbol wMinutes = w7 'b14/15
'
Symbol wPWMDuty = w8 'b16/17
Symbol wPWMDuty.Lo = b16 'w8
Symbol wPWMDuty.Hi = b17 'w8
'
' **** Constants - Prefix = c
'
Symbol False = 0
Symbol True = 1
Symbol cDQuote = 34
Symbol cDisabled = 255
'
Symbol mskBGTimer = %10000000 'For the background timer (only) interrupt
Symbol flgBGTimer = %10000000 'For the background timer (only) interrupt
Symbol tmrIntOn1stTick = 65535 'Interrupt to be caused by roll over on first major tick
Symbol t125mS_32 = 49911 'SetTimer value for 1/8 second ticks @ 32MHz
'
Symbol cOneMinute = 60 '60 Seconds
'
Symbol c80mS = 320 ' 80mS
Symbol c100mS = 400 ' 100mS
Symbol c1S = 4000 '1000mS
'
' **** EEPROM - Prefix = e (256 Bytes - 0 to 255d)
'
'Bytes 0-31 free
'The following data is used to drive PWM sounds to the piezo speaker
Symbol ePip = 32 'Lock' sound
EEPROM ePip, (1, 127, 0, 255, 0) '3900Hz
Symbol ePipPip = 37 'Un-Lock' sound
EEPROM ePipPip, (1, 127, 0, 255) '3900Hz
EEPROM (2, 0, 0, 0) 'No sound
EEPROM (3, 127, 0, 255, 0) '3900Hz
Symbol eBlip = 50 'Good' sound
EEPROM eBlip, (1, 191, 1, 127) '2600Hz
EEPROM (2, 166, 1, 77, 0) '3000Hz
Symbol eBloop = 59 'Bad' sound
EEPROM eBloop, (1, 166, 1, 77) '3000Hz
EEPROM (2, 191, 1, 127) '2600Hz
EEPROM (3, 191, 1, 127, 0) '2600Hz
Symbol eNoSound = 72 'Silence
EEPROM eNoSound, (cDisabled)
'
Last edited: