5x7 Display Piezo Experiments Page2

EXPERIMENT-5P   CALLing  Hee Haw  routine
This experiment shows how to CALL Hee Haw routine when a certain event has occurred. In this case the program detects button B.       Hee Haw repeats 5 times then stops. 

               Experiment-5P for "Piezo on 5x7 Display" Project
                               ;PIC16F84 and only F84 chip 
                                          ;Calling Hee Haw Siren
 
 Start       ORG 0x00
               BSF 03,5                 ;Go to page1 to set up the ports 
               MOVLW 08h           ;Load W with 0000 1000
               MOVWF 05h           ; to make RA3 input 
               MOVLW 00h           ;Put 00 into W 
               MOVWF 06h           ;to make all RB lines output
               BCF 03,5                 ;Go to Page0 for programming
               GOTO Main


                          ;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
               RETURN


 Delay     DECFSZ 1Ah,1      ;Length of ON and OFF time for tone
               GOTO Delay 
               RETURN

 Main       BTFSS 05,3          ;Is button B pressed?
               GOTO Main          ;No
               MOVLW 05          ;Yes. Create 5 loops for Hee Haw
               MOVWF 1Dh 
 Main1    CALL Hee
               DECFSZ 1Dh,1
               GOTO Main1
 Main2    CALL Delay
               BTFSC 05,3               ;Is button B released?
               GOTO Main2              ;No
               GOTO Main               ;Yes button B is released!

               END


The block of numbers below is the HEX file for Experiment-5P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt5P.hex   
:100000008316083085000030860083122928FF30CF
:10001000860004309C0080309B0080308606B03023
:100020009A0026209B0B0D289C0B0B280030860085
:1000300004309C00D0309B008030860680309A00CF
:1000400026209B0B1C289C0B1A2808009A0B26289C
:100050000800851D292805309D0007209D0B2D28AF
:0800600026208519302829280B
:00000001FF

 
EXPERIMENT-6P  
Making a Note
Making a Note is slightly different to making a tone. A note has to be very close in frequency to the correct value so it will be recognised. 
The first step in making a tune is the creation of the notes and in this experiment we show how to make a note. 
Each note is made up of an ON time (the HIGH for the note) and an OFF time (the LOW for the note). This must then be repeated to produce the note.
The sub-routine "C" produces both the HIGH and LOW times by toggling the line connected to the piezo then calling the delay. This action is repeated endlessly to create the note. 
The delay has been specially designed to take exactly 8 "units of time" (one unit of time for each NOP and DECFSZ and two units for GOTO. File 1A is loaded with F0 (240) and this creates 240 x 8 = 1920 microseconds of delay. The "C" sub-routine takes 8 microseconds (CALL = 2 microseconds) and GOTO takes 2 microseconds of time. The last cycle of delay takes 9 microseconds. 
When this is all added up, the result is 1929 microseconds. The other half of the cycle takes the same length of time and when 1,000,000 is divided by (1929x2), the result is very close to 260Hz. 
Middle C is specified as 261.63Hz and you cannot get much closer. 
The diagram below shows one cycle of tone "C" with       HIGH = 1929 microseconds and   LOW = 1929 microseconds. 

 
               Experiment-6P for Piezo on "5x7 Display" Project
                                      ;PIC16F84 and only F84 chip 
                                      ;Making a Note - middle "C"

 Start      ORG 0x00
               BSF 03,5           ;Go to page1 to set up the ports 
               MOVLW 00h     ;Put 00 into W 
               MOVWF 05h     ;Make all RA lines output 
               MOVWF 06h     ;Make all RB lines output
               BCF 03,5           ;Go to Page0 for programming

                                    ;Producing "middle C"

 C            MOVLW 80h      ;Toggle the piezo line. 
               XORWF 06h,1
               MOVLW 0F0h
               MOVWF 1Ah
               CALL Delay        ;Call Delay 
               GOTO C              ;Repeat the routine 

 Delay     NOP                    ;Length of ON and OFF time for Note 
               NOP
               NOP
               NOP
               NOP
               DECFSZ 1Ah,1
               GOTO Delay 
               RETURN

               END

              


The block of numbers below is the HEX file for Experiment-6P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt6P.hex   
:1000000083160030850086008312803086067630A5
:100010009A000B20052800000000000000000000EE
:060020009A0B0B280800FA
:00000001FF

 
EXPERIMENT-7P  
Creating a Scale
The natural extension of note-making is to create a tune. But first we need to create the 8 notes in the octave from middle C to upper C. These will then be placed in a routine and looped. 
One feature that may go un-noticed is the length of each note. As the note increases in frequency, the number of cycles has to be increased so that the length of the note is the same as the others. That's why each note must have two variables - the duration of the HIGH/ LOW and the number of cycles. The advantage of knowing the time taken to execute each instruction means you can work out the time taken for each sub-routine and thus get the frequency of each note accurate without having to measure it with a Frequency Counter. We checked the frequency of each note and found it to be only about 2 cycles from the accepted value. 
We used a 4MHz crystal when writing this experiment and the notes will alter if the RC components on the PC board do not create a 4MHz oscillator. 
If you want a much cleaner and clearer output from the project, use a mini speaker in place of the piezo. 
               Experiment-7P for Piezo on "5x7 Display" Project
                                      ;PIC16F84 and only F84 chip 
                                     
 ;Creating a SCALE

 Start       ORG 0x00
               BSF 03,5          ;Go to page1 to set up the ports 
               MOVLW 00h    ;Put 00 into W 
               MOVWF 05h    ;Make all RA lines output 
               MOVWF 06h    ;Make all RB lines output
               BCF 03,5          ;Go to Page0 for programming
               GOTO Main

 Note       MOVLW 80h    ;Toggle the piezo line. 
               XORWF 06h,1 
               MOVF 1A,0      ;Copy file 1A into W
               MOVWF 1Ch    ;Move W into file 1C
               CALL Delay      ;Call Delay 
               DECFSZ 1Bh,1 ;Dec file 1B for length of note
               GOTO Note
               RETURN 

 Delay     NOP                ;Length of ON and OFF time for Note 
               NOP
               NOP
               NOP
               NOP
               DECFSZ 1Ch,1
               GOTO Delay 
               RETURN

 Main       MOVLW 0F0h      ;Frequency of note C - 261Hz
               MOVWF 1Ah
               MOVLW 80h        ;Length of note C
               MOVWF 1Bh
               CALL Note
               MOVLW 0D5h      ;Frequency of note D - 293Hz
               MOVWF 1Ah
               MOVLW 90h         ;Length of note D
               MOVWF 1Bh
               CALL Note
               MOVLW 0BDh       ;Frequency of note E - 330Hz
               MOVWF 1Ah
               MOVLW 0A0h       ;Length of note E
               MOVWF 1Bh
               CALL Note
               MOVLW 0B2h        ;Frequency of note F - 349Hz
               MOVWF 1Ah
               MOVLW 0B0h        ;Length of note F
               MOVWF 1Bh
               CALL Note
               MOVLW 09Fh         ;Frequency of note G - 392Hz
               MOVWF 1Ah
               MOVLW 0C0h         ;Length of note G
               MOVWF 1Bh
               CALL Note
               MOVLW 08Dh         ;Frequency of note A - 440Hz
               MOVWF 1Ah
               MOVLW 0D0h         ;Length of note A
               MOVWF 1Bh
               CALL Note
               MOVLW 07Eh          ;Frequency of note B - 494Hz
               MOVWF 1Ah
               MOVLW 0E0h          ;Length of note B
               MOVWF 1Bh
               CALL Note
               MOVLW 077h          ;Frequency of note C' - 523Hz
               MOVWF 1Ah
               MOVLW 0F0h          ;Length of note C'
               MOVWF 1Bh
               CALL Note
               GOTO Main

                END
              


The block of numbers below is the HEX file for Experiment-7P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt7P.hex   
:10000000831600308500860083121628803086060D
:100010001A089C000E209B0B062808000000000018
:100020000000000000009C0B0E280800F0309A0031
:1000300080309B000620D5309A0090309B0006202F
:10004000BD309A00A0309B000620B2309A00B0303C
:100050009B0006209F309A00C0309B0006208D3008
:100060009A00D0309B0006207E309A00E0309B0042
:0E007000062077309A00F0309B0006201628FC
:00000001FF

 
EXPERIMENT-8P  
Creating a tune 
This experiment shows how to set up sub-routines for each note so they can be CALLed by using a single instruction. 
               Experiment-8P for Piezo on "5x7 Display" Project
                                      ;PIC16F84 
                                      ;
Creating a TUNE
                                      ;Oh,  When The Saints

 Start      ORG 0x00
              BSF 03,5              ;Go to page1 to set up the ports 
              MOVLW 00h        ;Put 00 into W 
              MOVWF 05h        ;Make all RA lines output 
              MOVWF 06h        ;Make all RB lines output
              BCF 03,5              ;Go to Page0 for programming
              GOTO Main


 C           MOVLW 80h
              MOVWF 1Bh 
 NoteC    MOVLW 80h          ;Toggle the piezo line. 
              XORWF 06h,1 
              MOVLW 0F0h
              MOVWF 1Ah 
              CALL Delay            ;Call Delay 
              DECFSZ 1Bh,1        ;Dec file 1B for length of note
              GOTO NoteC
              RETURN 

 D           MOVLW 90h
              MOVWF 1Bh 
 NoteD   MOVLW 80h           ;Toggle the piezo line. 
              XORWF 06h,1 
              MOVLW 0D5h
              MOVWF 1Ah 
              CALL Delay              ;Call Delay 
              DECFSZ 1Bh,1         ;Dec file 1B for length of note
              GOTO NoteD
              RETURN 

 E           MOVLW 0A0h
              MOVWF 1Bh 
 NoteE    MOVLW 80h            ;Toggle the piezo line. 
              XORWF 06h,1 
              MOVLW 0BDh
              MOVWF 1Ah 
              CALL Delay              ;Call Delay 
              DECFSZ 1Bh,1         ;Dec file 1B for length of note
              GOTO NoteE
              RETURN 

 F           MOVLW 0B0h
              MOVWF 1Bh 
 NoteF    MOVLW 80h             ;Toggle the piezo line. 
              XORWF 06h,1 
              MOVLW 0B2h
              MOVWF 1Ah 
              CALL Delay               ;Call Delay 
              DECFSZ 1Bh,1          ;Dec file 1B for length of note
              GOTO NoteF
              RETURN 

 G          MOVLW 0C0h
              MOVWF 1Bh 
 NoteG   MOVLW 80h               ;Toggle the piezo line. 
              XORWF 06h,1 
              MOVLW 9Fh
              MOVWF 1Ah 
              CALL Delay                ;Call Delay 
              DECFSZ 1Bh,1           ;Dec file 1B for length of note
              GOTO NoteG
              RETURN 

 A          MOVLW 0D0h
              MOVWF 1Bh 
 NoteA   MOVLW 80h               ;Toggle the piezo line. 
              XORWF 06h,1 
              MOVLW 8Dh
              MOVWF 1Ah 
              CALL Delay                   ;Call Delay 
              DECFSZ 1Bh,1              ;Dec file 1B for length of note
              GOTO NoteA
              RETURN 

 BB        MOVLW 0E0h
              MOVWF 1Bh 
 NoteB    MOVLW 80h                     ;Toggle the piezo line. 
               XORWF 06h,1 
              MOVLW 7Eh
              MOVWF 1Ah 
              CALL Delay                       ;Call Delay 
              DECFSZ 1Bh,1                  ;Dec file 1B for length of note
              GOTO NoteB
              RETURN 

 CC        MOVLW 0F0h
              MOVWF 1Bh 
 NoteCC MOVLW 80h                  ;Toggle the piezo line. 
               XORWF 06h,1 
              MOVLW 77h
              MOVWF 1Ah 
              CALL Delay                     ;Call Delay 
              DECFSZ 1Bh,1                ;Dec file 1B for length of note
              GOTO NoteCC
              RETURN 



Delay      NOP               ;Length of ON and OFF time for Note 
              NOP
              NOP
              NOP
              NOP
              DECFSZ 1Ah,1
              GOTO Delay 
              RETURN
                          
                         ;Oh, When the Saints Come Marching in:


Main       CALL C
              CALL E
              CALL F

              CALL G
              CALL G
              CALL G
              CALL G

              CALL G
              CALL C
              CALL E
              CALL F

              CALL G
              CALL G
              CALL G
              CALL G

              CALL G
              CALL C
              CALL E
              CALL F

              CALL G
              CALL G
              CALL E
              CALL E

              CALL C
              CALL C
              CALL E
              CALL E

              CALL D
              CALL D
              CALL D
              CALL D

              CALL D
              CALL Delay
              CALL Delay
              CALL E
              CALL D

              CALL C
              CALL C
              CALL Delay
              CALL C
              CALL C

              CALL E
              CALL E
              CALL G
              CALL G

              CALL G
              CALL F
              CALL F
              CALL F

              CALL F
              CALL F
              CALL E
              CALL F

              CALL G
              CALL G
              CALL E
              CALL E

              CALL D
              CALL D
              CALL Delay
              CALL D
              CALL D

              CALL C
              CALL C
              CALL C
              CALL Delay
              CALL C

              GOTO Main

              END

         


The block of numbers below is the HEX file for Experiment-8P. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt8P.hex   
:10000000831600308500860083125E2880309B00B6
:1000100080308606F0309A0056209B0B0828080096
:1000200090309B0080308606D5309A0056209B0B7E
:1000300012280800A0309B0080308606BD309A0050
:1000400056209B0B1C280800B0309B008030860691
:10005000B2309A0056209B0B26280800C0309B0027
:10006000803086069F309A0056209B0B302808006F
:10007000D0309B00803086068D309A0056209B0B36
:100080003A280800E0309B00803086067E309A00D7
:1000900056209B0B44280800F0309B0080308606D9
:1000A00077309A0056209B0B4E2808000000000075
:1000B0000000000000009A0B5628080006201A20B5
:1000C00024202E202E202E202E202E2006201A2006
:1000D00024202E202E202E202E202E2006201A20F6
:1000E00024202E202E201A201A20062006201A2036
:1000F0001A201020102010201020102056205620EA
:100100001A201020062006205620062006201A203D
:100110001A202E202E202E202420242024202420AB
:1001200024201A2024202E202E201A201A201020CD
:1001300010205620102010200620062006205620D1
:0401400006205E280F
:00000001FF

You now have the option of going over the experiments again or starting to write your own programs. 
To produce your own programs you will need a program called MPASM. This will assemble the programs you write on Notepad and create a .hex file (as well as other files). 
Before you start writing programs, go to PIC Theory.