This experiment demonstrates how to create a tone from a piezo. A Piezo is very similar to a 22n capacitor as far as the circuit is concerned and the micro sees the piezo as a capacitor. In other words it does not see it a resistive load. In fact it is not a load but a small amount of effort is required to send energy into the capacitor and then “drain it out.” When this is done thousands of times a second, the end result is small amount of energy is required. When a voltage is delivered to the piezo, a thin white crystal substance between two plates sees this voltage and expands. Since it is glued to one side of a thin sheet of metal, it make the side of the metal expand and this “dishes” the metal. When the crystal substrate sees no voltage or a reverse voltage, it shrinks and this flattens or reverses the metal disk. If this is repeated fast enough, the result is a horrible, tinny sound that we have come to know as a “piezo squeal.” As the pulse-rate increases, the output frequency of the piezo increases and a point is reached when the output increases enormously. This is the “resonant frequency” of the piezo and that is why some piezos give an extremely loud output. They are not only “good quality” piezos but are operating at their optimum frequency.
The program below produces a tone of approx 500Hz from the piezo.
The delay routine consists of 255 loops of decrementing file 1A. This file does not have to be loaded with a value if you want 255 decrements as the file is decremented first then tested and thus it starts at 255 at the beginning of decrementing.
The circuit for “5x7 Display” is shown below:
Experiment-1P for “Piezo on 5x7 Display” Project
;PIC16F84 and only F84 chip
;Creating a tone
Start ORG 0x00 BSF 03,5 ;Go to page1 for setting-up the ports MOVLW 00h ;Put 00 into W MOVWF 05h ; to make all RA lines output MOVWF 06h ;to make all RB lines output BCF 03,5 ;Go to Page0 for programming Tone1 BSF 06,7 ;this is the piezo line. Turn it ON CALL Delay ;Call Delay BCF 06,7 ;Turn off the piezo CALL Delay ;Call Delay GOTO Tone1 ;Repeat the routine Delay DECFSZ 1Ah,1 ;Delay for tone GOTO Delay RETURN END
The block of numbers below is the HEX file for Experiment-1P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt-1P.hex
:100000008316003085008600831286170A20861327 :0A0010000A2005289A0B0A280800B0 :00000001FF
This experiment produces a beep. It is repeated every second.
One of the biggest problems with creating a tone or a beep is the computer-time taken up. As you can see from the program, a tone (or beep) is created by turning ON an output, calling a delay routine, then turning the piezo OFF. Even though it takes only one instruction to turn the piezo on or off, the delay between each operation is fairly short and quite often the program is required to concentrate on producing the beep. This is ok if the beep is very short as the routine can be hidden inside a longer delay that may be displaying a column of LEDs, for example, or during the time when the screen is blanked.
If you want to scan the screen while producing a tone tone at the same time, you have to integrate two routines and execute them at the same time. This is very difficult to do as you must know exactly the length of the second routine so that the tone has the correct frequency. The second routine cannot have any variation in its length as this will alter the frequency.
The routine below produces a beep … beep . . .beep . . beep … from the piezo.
4 loops i.e: 4 delay routines are needed to produce the effect. The first delay routine is needed after the piezo has been turned on. The second delay routine is needed after the piezo has been turned off. This action has to be repeated to actually produce a tone as the operation of turning the piezo on and then off does not produce a tone. It’s only when this is repeated many times in a short period of time that the sound is generated.
The piezo is connected to the micro via a buffer transistor so that a choke can be added to the circuit to increase the sound output. The piezo-circuit must not be left ON after a tone is emitted as the circit takes a very high current in the ON condition.
If you want the piezo to be louder, a choke can be added to the board as marked, in place of the 1k resistor.
Experiment-2P for Piezo on “5x7 Display” Project
;PIC16F84 and only F84 chip
;Creating a beep - beep - beep
Start ORG 0x00 BSF 03,5 ;Go to page1 for setting-up the ports MOVLW 00h ;Put 00 into W MOVWF 05h ; to make all RA lines output MOVWF 06h ;to make all RB lines output BCF 03,5 ;Go to Page0 for programming Beep MOVLW 40h MOVWF 1Bh Beep1 BSF 06,7 ;This is the piezo line. Turn it ON CALL Delay ;Call Delay BCF 06,7 ;Turn off the piezo CALL Delay DECFSZ 1Bh,1 ;Repeat for 40h cycles GOTO Beep1 CALL LongDel ;Call Long Delay GOTO Beep ;Repeat the routine Delay MOVLW 40h MOVWF 1Ah Delay1 DECFSZ 1Ah,1 ;Delay for Beep GOTO Delay1 RETURN LongDel MOVLW 04 ;Long delay between beeps MOVWF 1Ch Del2 DECFSZ 1Ah,1 GOTO Del2 DECFSZ 1Bh,1 GOTO Del2 DECFSZ 1Ch,1 GOTO Del2 RETURN END
The block of numbers below is the HEX file for Experiment-2P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt-2P.hex
:100000008316003085008600831240309B008617DF :100010000F2086130F209B0B072814200528403043 :100020009A009A0B1128080004309C009A0B16289D :0A0030009B0B16289C0B16280800F5 :00000001FF
This experiment produces a beep after button A is pressed two beeps after button B is pressed, and three beeps for button C. (The program would be much simpler if only a single beep is required.)
Experiment-3P for Piezo on “5x7 Display” Project
;PIC16F84 and only F84 chip
;Beep after Button A, B and C
Start ORG 0x00 BSF 03,5 ;Go to page1 for setting-up the ports MOVLW 00h ;Put 00 into W MOVLW 1C ;Put 0001 1100 into W MOVWF 05h ;make RA2, RA3 and RA4 input MOVLW 00h MOVWF 06h ;make all RB lines output BCF 03,5 ;Go to Page0 for programming GOTO Main SwA BTFSS 05,2 ;Is switch A pushed? GOTO SwA2 ;YES. MOVF 11h,0 ;NO. Copy 11h into W XORLW 00h ;XOR W with 00 BTFSS 03,2 ;Is file 11h=0? GOTO SwA1 ;NO. RETURN ;YES. SwA1 DECF 11h,1 ;Decrement the debounce file RETURN SwA2 MOVF 11h,0 ;11h is the debounce file XORLW 00h BTFSS 03,2 ;Is file 11h=0? RETURN ;NO. CALL Beep1 ;YES. MOVLW 80h ;Load W with 80h loops MOVWF 11h ;Put 80h into debounce file RETURN SwB BTFSS 05,3 ;Is switch B pushed? GOTO SwB2 ;YES. MOVF 12h,0 ;NO. Copy 12h into W XORLW 00h ;XOR W with 00 BTFSS 03,2 ;Is file 12h=0? GOTO SwB1 ;NO. RETURN ;YES. SwB1 DECF 12h,1 ;Decrement the debounce file RETURN SwB2 MOVF 12h,0 ;12h is the debounce file XORLW 00h BTFSS 03,2 ;Is file 12h=0? RETURN ;NO. CALL Beep2 ;YES. MOVLW 80h ;Load W with 80h loops MOVWF 12h ;Put 80h into debounce file RETURN SwC BTFSS 05,4 ;Is switch C pushed? GOTO SwC2 ;YES. MOVF 13h,0 ;NO. Copy 13h into W XORLW 00h ;XOR W with 00 BTFSS 03,2 ;Is file 13h=0? GOTO SwC1 ;NO. RETURN ;YES. SwC1 DECF 13h,1 ;Decrement the debounce file RETURN SwC2 MOVF 13h,0 ;13h is the debounce file XORLW 00h BTFSS 03,2 ;Is file 1Dh=0? RETURN ;NO. CALL Beep3 ;YES. MOVLW 80h ;Load W with 80h loops MOVWF 13h ;Put 80h into debounce file RETURN Beep1 MOVLW 40h MOVWF 1Bh Beepx BSF 06,7 ;This is the piezo line. Turn it ON CALL Delay ;Call Delay BCF 06,7 ;Turn off the piezo CALL Delay DECFSZ 1Bh,1 ;Repeat for 40h cycles GOTO Beepx RETURN Beep2 CALL Beep1 CALL LongDel CALL Beep1 RETURN Beep3 CALL Beep1 CALL LongDel CALL Beep1 CALL LongDel CALL Beep1 RETURN Delay MOVLW 30h MOVWF 1Ah Delay1 DECFSZ 1Ah,1 ;Delay for Beep GOTO Delay1 RETURN LongDel MOVLW 01 MOVWF 1Ch Del2 DECFSZ 1Ah,1 GOTO Del2 DECFSZ 1Bh,1 GOTO Del2 DECFSZ 1Ch,1 GOTO Del2 RETURN Main CALL SwA CALL SwB CALL SwC GOTO Main END
The block of numbers below is the HEX file for Experiment-3P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt-3P.hex
:10000000831600301C3085000030860083125C2887 :10001000051D11281108003A031D0F28080091033F :1000200008001108003A031D08003B2080309100B1 :100030000800851D22281208003A031D2028080008 :10004000920308001208003A031D08004420803083 :1000500092000800051E33281308003A031D3128BA :100060000800930308001308003A031D0800482005 :1000700080309300080040309B0086174E20861386 :100080004E209B0B3D2808003B2053203B200800BE :100090003B2053203B2053203B20080030309A0067 :1000A0009A0B5028080001309C009A0B55289B0B96 :1000B00055289C0B55280800082019202A205C2868 :00000001FF
This experiment produces a Hee Haw siren suitable for an alarm. The first column of LEDs flash in sequence with the sound.
This is the simplest routine you can get for a Hee Haw effect. So, why is it so complex?
Let’s explain:
The Hee routine is exactly the same as the Haw routine except the length of the ON and OFF time is longer so that the tone has a lower frequency.
If we take the Hee routine, we first have to turn ON all the LEDs. Next we find a nested routine made up of two files (file 1C and 1B). We need two files because one file will not produce a tone for a long enough period and the final result is a very fast Hee Haw sound. With two files we can make a very slow Hee Haw.
Next we come to two instructions that toggle the piezo line. It does not matter if the line is originally HIGH or LOW. The two instructions change the state of the line.
This means we can loop around these two instructions and each time the micro passes, the piezo will turn ON or OFF.
After this we load the delay file with a value and call the delay so that the piezo line is HIGH (or LOW) for quite a few thousand cycles. This will produce the actual tone and the files around these instructions produce the duration of the tone.
Once the duration of the LOW frequency (the Hee tone) has ended, the LEDs are turned off and the Haw tone is executed.
Once you understand these basics, routines like this will be taken from a Library of Routines and CALLed. But it’s important to know how they work so that individual features can be altered as necessary. You may require to increase one of the tones, or produce the tone for 25 repetitions and STOP, or call the routine when certain conditions have occurred.
Nothing is standard in programming. You are always required to meet certain criteria with each program and you must know the absolute basics with everything you write. And that’s what we are doing in these experiments.
Experiment-4P for Piezo on “5x7 Display” Project
;PIC16F84
;Hee Haw Siren with flashing LEDs
Start ORG 0x00 BSF 03,5 ;Go to page1 for setting-up the ports MOVLW 00h ;Put 00 into W MOVWF 05h ; to make all RA lines output MOVWF 06h ;to make all RB lines output BCF 03,5 ;Go to Page0 for programming ;Hee - this is the LOW frequency Hee MOVLW 0FFh MOVWF 06 ;Turn ON all LEDs MOVLW 04 MOVWF 1C Hee1 MOVLW 080h MOVWF 1B Hee2 MOVLW 80h ;Toggle the piezo line. XORWF 06h,1 MOVLW 0B0h MOVWF 1Ah CALL Delay ;Call Delay DECFSZ 1B,1 GOTO Hee2 ;Repeat the routine as per file 1B DECFSZ 1C,1 GOTO Hee1 ;Haw - This is the HIGH frequency Haw MOVLW 00 MOVWF 06 ;Turn OFF all LEDs MOVLW 04 MOVWF 1C Haw1 MOVLW 0D0h ;Create D0 loops MOVWF 1B Haw2 MOVLW 80h ;Toggle the piezo line. XORWF 06h,1 MOVLW 80h MOVWF 1Ah CALL Delay ;Call Delay DECFSZ 1B,1 GOTO Haw2 ;Repeat the routine as per file 1B DECFSZ 1C,1 GOTO Haw1 GOTO Hee Delay DECFSZ 1Ah,1 ;Length of ON and OFF time for tone GOTO Delay RETURN END
The block of numbers below is the HEX file for Experiment-4P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt-4P.hex
:1000000083160030850086008312FF30860004309E :100010009C0080309B0080308606B0309A002420FF :100020009B0B0B289C0B09280030860004309C0099 :10003000D0309B008030860680309A0024209B0BB5 :0E0040001A289C0B182805289A0B2428080063 :00000001FF
On the next page we show how to CALL the Hee Haw routine.
Go to the next page of experiments: 5x7 PIEZO EXPERIMENTS: Page-2
Quick Links
Legal Stuff
Social Media