5x7 Display Experiments Page 11

EXPERIMENT-27   LED Dice with Sound-1 
This experiment adds sound to the LED Dice experiment from Page 10.  You should have already carried out the Piezo experiments from Piezo Experiments Page1 and Piezo Experiments Page2
Adding sound to a project needs a little bit of thought. The micro cannot do two things at the same time unless they are multiplexed. In other words they have to be interleaved together so that the micro can jump from one routine to the other very quickly. 
Essentially, the micro's time is completely taken up by scanning the display in all the programs we have presented, and the same applies to creating a tone. There is however, a short delay in the heart of each routine where the micro can go to another routine, carry out a task and return before the delay needs to be ended. 
In this way the micro can jump from one sub-routine to another and effectively carry out both operations. 
The one thing that has to be remembered is TIMING. In other words the time-interval when the micro is being sent away to another routine, must be accurately worked-out. If not, the sound will not be the correct tone and/or the display will not have the same brightness overall. 
In this experiment we have used the existing Scan routine to turn the piezo on and off by setting or clearing the piezo bit. We are just fortunate that the scan-rate is very similar to the tone-rate from a piezo experiment. 
The only extra sub-routine we added was Face6B. It turns on the piezo when the "piezo-bit"  (Bit 7 of file 1F) is SET. 

               Experiment-27 for "5x7 Display" Project
                               ;PIC16F84 and only F84 chip 
                                          ;
LED Dice with Sound-1
 
 Start     ORG 0x00
             BSF 03,5               ;Go to page1 for setting-up the ports
             MOVLW 04h          ;Put 04 into W 
             MOVWF 05h          ; to make RA2 input for button A 
             MOVLW 00h          ;Put 00 into W 
             MOVWF 06h          ;and make port 6 (port B) all output
             BCF 03,5                ;Go to page0 for programming
             MOVWF 1Ch
             BCF 1F,7                ;Bit for tone
             GOTO Main

Table1 ADDWF 02h,1          ;Add W to Program Counter     
             RETLW 03h
             RETLW 07h
             RETLW 06h
             RETLW 04h
             RETLW 02h
             RETLW 06h
             RETLW 07h
             RETLW 03h
             RETLW 05h
             RETLW 02h
             RETLW 02h
             RETLW 03h
             RETLW 05h
             RETLW 04h
             RETLW 02h
             RETLW 07h
             RETLW 06h
             RETLW 07h
             RETLW 04h
             RETLW 03h
             RETLW 02h
             RETLW 05h
             RETLW 07h
             RETLW 06h
             RETLW 03h
             RETLW 02h
             RETLW 02h
             RETLW 03h
             RETLW 04h
             RETLW 05h
             RETLW 04h
             RETLW 03h
             RETLW 07h
             RETLW 04h
             RETLW 06h
             RETLW 07h
             RETLW 02h
             RETLW 03h
             RETLW 04h
             RETLW 05h
             RETLW 01h


 Face1  MOVLW 00h      ;place the 5 values into the 5 
             MOVWF 11h      ; files to produce the first face
             MOVLW 08h 
             MOVWF 12h
             MOVLW 1Ch
             MOVWF 13h
             MOVLW 08h
             MOVWF 14h
             MOVLW 00h
             MOVWF 15h
             GOTO Scan

 Face2  MOVLW 03h       ;place the 5 values into the 5 
             MOVWF 11h       ; files to produce the second face
             MOVLW 03h 
             MOVWF 12h
             MOVLW 00h
             MOVWF 13h
             MOVLW 60h
             MOVWF 14h
             MOVLW 60h
             MOVWF 15h
             GOTO Scan

 Face3  MOVLW 03h      ;place the 5 values into the 5 
             MOVWF 11h      ; files to produce the third face
             MOVLW 0Bh  
             MOVWF 12h
             MOVLW 1Ch
             MOVWF 13h
             MOVLW 68h
             MOVWF 14h
             MOVLW 60h
             MOVWF 15h
             GOTO Scan

 Face4  MOVLW 63h        ;place the 5 values into the 5 
             MOVWF 11h        ; files to produce the fourth face
             MOVLW 63h  
             MOVWF 12h
             MOVLW 00h
             MOVWF 13h
             MOVLW 63h
             MOVWF 14h
             MOVLW 63h
             MOVWF 15h
             GOTO Scan

 Face5  MOVLW 63h        ;place the 5 values into the 5 
             MOVWF 11h        ; files to produce the fifth face
             MOVLW 6Bh 
             MOVWF 12h
             MOVLW 1Ch
             MOVWF 13h
             MOVLW 6Bh
             MOVWF 14h
             MOVLW 63h
             MOVWF 15h
             GOTO Scan

 Face6  BTFSS 1F,7
             GOTO Face6B 
             MOVLW 0EBh        ;place the 5 values into the 5 
             MOVWF 11h           ; files to produce the sixth face 
             MOVLW 80h
             MOVWF 13h
Face6A MOVLW 6Bh 
             MOVWF 12h
             MOVLW 6Bh
             MOVWF 14h
             MOVLW 6Bh
             MOVWF 15h
             GOTO Scan

 Face6B MOVLW 6B        ;This sub-routine only allows the 
               MOVWF 11h      ;tone to be produced 
               MOVLW 00        ;when the final face6 appears on
               MOVWF 13h      ;the screen.
               GOTO Face6A

 Scan    BSF 05,1         ;Reset 4017
             NOP
             BCF 05,1
             MOVF 11h,0     ;Move file 11 into W
             MOVWF 06h
             CALL DelD
             MOVWF 06h
             CALL DelD
             MOVF 13h,0    ;Move file 13 into W
             MOVWF 06h
             CALL DelD
             MOVF 14h,0     ;Move file 14 into W
             MOVWF 06h
             CALL DelD
             MOVF 15h,0      ;Move file 15 into W
             MOVWF 06h
             CALL DelD
             RETURN

 DelD     DECFSZ 1Bh,1
             GOTO DelD
             MOVLW 00h        ;Zero port B to prevent mirroring
             MOVWF 06
             Clk BSF 05,0        ;Clock the 4017
             NOP
             BCF 05,0
             RETURN

 XX        MOVF 1Ch,0         ;Move 1C to W
             CALL Table1
             MOVWF 1Dh         ;Move W to 1D for decrementing
             DECFSZ 1Dh,1
             GOTO XX1
             MOVLW 00            ;Detects end of table
             MOVWF 1Ch          ;Zero 1C to start at top of table
             GOTO XX
XX1      DECFSZ 1Dh,1
             GOTO XX2
             GOTO Face1
XX2      DECFSZ 1Dh,1
             GOTO XX3
             GOTO Face2
XX3       DECFSZ 1Dh,1
             GOTO XX4
XX4       DECFSZ 1Dh,1
             GOTO XX5
             GOTO Face4
XX5       DECFSZ 1Dh,1 
             GOTO Face6 
             GOTO Face5

 Main     MOVLW 30h       ;Each 10h represents 100mS 
             MOVWF 0Ch
 Main1   CALL XX 
             DECFSZ 0Ch
             GOTO Main1
             INCF 1C

             MOVLW 40h        ;Each 10h represents 100mS 
             MOVWF 0Ch
Main2 CALL XX                ;Display Face for 400mS
             DECFSZ 0Ch
             GOTO Main2
             INCF 1C

             MOVLW 50h         ;Each 10h represents 100mS 
             MOVWF 0Ch
Main3    CALL XX                 ;Display Face for 500mS
             DECFSZ 0Ch
             GOTO Main3
             INCF 1C

             MOVLW 60h        ;Each 10h represents 100mS 
             MOVWF 0Ch
Main4    CALL XX             ;Display Face for 600mS
             DECFSZ 0Ch
             GOTO Main4
             INCF 1C

             MOVLW 70h        ;Each 10h represents 100mS 
             MOVWF 0Ch
Main5   CALL XX              ;Display Face for 700mS
             DECFSZ 0Ch
             GOTO Main5
             INCF 1C

Main6    BSF 1F,7
             CALL XX           ;Display until button A pressed
             BTFSS 05,2       ;Test for button A
             GOTO Main6
             BCF 1F,7
             GOTO Main

             END

                  


The block of numbers below is the HEX file for Experiment-27. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt-27.hex   
:100000008316043085000030860083129C009F1305
:10001000AE2882070334073406340434023406342D
:100020000734033405340234023403340534043411
:1000300002340734063407340434033402340534FC
:1000400007340634033402340234033404340534F0
:1000500004340334073404340634073402340334DC
:1000600004340534013400309100083092001C3013
:10007000930008309400003095007C2803309100F4
:10008000033092000030930060309400603095009F
:100090007C28033091000B3092001C3093006830B4
:1000A0009400603095007C286330910063309200AA
:1000B0000030930063309400633095007C286330F7
:1000C00091006B3092001C3093006B3094006330D1
:1000D00095007C289F1F7728EB309100803093009B
:1000E0006B3092006B3094006B3095007C286B3045
:1000F00091000030930070288514000085101108CD
:1001000086008F20120886008F20130886008F201B
:10011000140886008F20150886008F2008009B0B8E
:100120008F280030860005140000051008001C0808
:1001300009209D009D0B9F2800309C0097289D0B57
:10014000A22833289D0BA5283E289D0BA8284928C6
:100150009D0BAB2854289D0B6A285F2830308C00FB
:1001600097208C0BB0289C0A40308C0097208C0B79
:10017000B6289C0A50308C0097208C0BBC289C0A17
:1001800060308C0097208C0BC2289C0A70308C0049
:1001900097208C0BC8289C0A9F179720051DCC28F8
:0401A0009F13AE28D3
:00000001FF

  
EXPERIMENT-28          LED Dice with Sound-2
This is an extension of experiment 27. In this experiment we add sound as a separate sub-routine (called Tone) so you can see how to add it to your own programs.  The display represents the face of a dice. The numbers flicker and change and come to rest on a random number. When the face shows "six," the tone is produced. Press button "A" for the next "throw."
The program is complete and runs satisfactorily but two areas need improvement. 
A: The distribution of the numbers on the dice is not very even. This is controlled by three things. 
1. The number of faces being displayed during each operation (5 faces then the final face in this example), 
2: The number of elements in the table. 
3: The value at every sixth position (for example). Changing these three variables will alter the result on the display. 
B: The tone produced by the piezo could be improved. This is determined by the ON/OFF time in the Tone routine and the number of cycles. Change these values to improve the tone. 
There are lots of extra features you can add to this program. 
The activation button can be changed to Button B or C,
The number of faces displayed on the screen can be increased or decreased and the length of time they are displayed can be altered. 
A tone can be produced for each display, making it easy to know the final result, without watching the display. 
The display can automatically start after a pre-determined time, to speed-up the playing of a game, 
Whenever you make any changes to a program, make sure the previous program can be retrieved and only change or add a few instructions at a time.
 

               Experiment-28 for "5x7 Display" Project
                                      ;PIC16F84 and only F84 chip 
                         ;LED Dice with Sound-2

 Start       ORG 0x00
               BSF 03,5               ;Go to page1 for setting-up the ports
               MOVLW 04h         ;Put 04 into W 
               MOVWF 05h         ; to make RA2 input for button A 
               MOVLW 00h         ;Put 00 into W 
               MOVWF 06h         ;and make port 6 (port B) all output
               BCF 03,5               ;Go to page0 for programming
               MOVWF 1Ch
               MOVWF 0Fh          ;Zero the number of cycles for Tone
               BCF 1F,7                ;Bit for tone
               GOTO Main

 Table1   ADDWF 02h,1         ;Add W to Program Counter     
               RETLW 03h
               RETLW 07h
               RETLW 06h
               RETLW 04h
               RETLW 02h
               RETLW 06h
               RETLW 07h
               RETLW 03h
               RETLW 05h
               RETLW 02h
               RETLW 02h
               RETLW 03h
               RETLW 05h
               RETLW 04h
               RETLW 02h
               RETLW 07h
               RETLW 06h
               RETLW 07h
               RETLW 04h
               RETLW 03h
               RETLW 02h
               RETLW 05h
               RETLW 07h
               RETLW 06h
               RETLW 03h
               RETLW 02h
               RETLW 02h
               RETLW 03h
               RETLW 04h
               RETLW 05h
               RETLW 04h
               RETLW 03h
               RETLW 07h
               RETLW 04h
               RETLW 06h
               RETLW 07h
               RETLW 02h
               RETLW 03h
               RETLW 04h
               RETLW 05h
               RETLW 06h
               RETLW 01h

 Face1    MOVLW 00h        ;place the 5 values into the 5 
               MOVWF 11h        ; files to produce the first face
               MOVLW 08h 
               MOVWF 12h
               MOVLW 1Ch
               MOVWF 13h
               MOVLW 08h
               MOVWF 14h
               MOVLW 00h
               MOVWF 15h
               GOTO Scan

 Face2    MOVLW 03h        ;place the 5 values into the 5 
               MOVWF 11h        ; files to produce the second face
               MOVLW 03h 
               MOVWF 12h
               MOVLW 00h
               MOVWF 13h
               MOVLW 60h
               MOVWF 14h
               MOVLW 60h
               MOVWF 15h
               GOTO Scan

 Face3    MOVLW 03h        ;place the 5 values into the 5 
               MOVWF 11h        ; files to produce the third face
               MOVLW 0Bh  
               MOVWF 12h
               MOVLW 1Ch
               MOVWF 13h
               MOVLW 68h
               MOVWF 14h
               MOVLW 60h
               MOVWF 15h
               GOTO Scan

 Face4    MOVLW 63h         ;place the 5 values into the 5 
               MOVWF 11h         ; files to produce the fourth face
               MOVLW 63h  
               MOVWF 12h
               MOVLW 00h
               MOVWF 13h
               MOVLW 63h
               MOVWF 14h
               MOVLW 63h
               MOVWF 15h
               GOTO Scan

 Face5    MOVLW 63h        ;place the 5 values into the 5 
               MOVWF 11h        ; files to produce the fifth face
               MOVLW 6Bh  
               MOVWF 12h
               MOVLW 1Ch
               MOVWF 13h
               MOVLW 6Bh
               MOVWF 14h
               MOVLW 63h
               MOVWF 15h
               GOTO Scan

 Face6    BTFSC 1F,7
               GOTO Face6B 
 Face6A MOVLW 6Bh        ;place the 5 values into the 5 
               MOVWF 11h        ; files to produce the sixth face 
               MOVLW 6Bh
               MOVWF 12h
               MOVLW 00h
               MOVWF 13h
               MOVLW 6Bh 
               MOVWF 14h
               MOVLW 6Bh
               MOVWF 15h
               GOTO Scan

 Face6B DECFSZ 0Fh,1        ;Decrement the tone cycles
               GOTO Face6C
               INCF 0Fh,1
               GOTO Face6A
 Face6C BSF 05,1               ;Reset 4017
               NOP
               BCF 05,1
               MOVLW 6Bh ;
               MOVWF 06h
               CALL Tone 
               MOVLW 6Bh ;
               MOVWF 06h 
               CALL Tone
               MOVLW 00 ;
               MOVWF 06h 
               CALL Tone
               MOVLW 6Bh ;
               MOVWF 06h
               CALL Tone
               MOVLW 6Bh ;
               MOVWF 06h
               CALL Tone
               RETURN

 Tone      MOVLW 0Ah           ;The number of cycles
               MOVWF 0Dh 
 Tone1    MOVLW 60h            ;the ON and OFF time for the tone
               MOVWF 0Eh
               MOVLW 80h             ;Toggle the piezo line
               XORWF 06h,1
 Tone2    DECFSZ 0Eh,1
               GOTO Tone2
               DECFSZ 0Dh,1
               GOTO Tone1
               GOTO Clk


 Scan      BSF 05,1        ;Reset 4017
               NOP
               BCF 05,1
               MOVF 11h,0      ;Move file 11 into W
               MOVWF 06h
               CALL DelD
               MOVF 12h,0        ;Move file 12 into W
               MOVWF 06h
               CALL DelD
               MOVF 13h,0       ;Move file 13 into W
               MOVWF 06h
               CALL DelD
               MOVF 14h,0        ;Move file 14 into W
               MOVWF 06h
               CALL DelD
               MOVF 15h,0        ;Move file 15 into W
               MOVWF 06h
               CALL DelD
               RETURN


 DelD       MOVLW 04h          ;A 2-file delay routine
               MOVWF 0Dh         ;to give sufficient time
 DelE       DECFSZ 0Eh,1       ;for the tone routine
               GOTO DelE 
               DECFSZ 0Dh,1
               GOTO DelE
               MOVLW 00h         ;Zero port B to prevent mirroring
               MOVWF 06
 Clk          BSF 05,0                   ;Clock the 4017
               NOP
               BCF 05,0
               RETURN

 XX          MOVF 1Ch,0           ;Move 1C to W
               CALL Table1
               MOVWF 1Dh            ;Move W to 1D for decrementing
               DECFSZ 1Dh,1
               GOTO XX1
               MOVLW 00              ;Detects end of table
               MOVWF 1Ch            ;Zero 1C to start at top of table
               GOTO XX
 XX1       DECFSZ 1Dh,1
               GOTO XX2
               GOTO Face1
 XX2        DECFSZ 1Dh,1
               GOTO XX3
               GOTO Face2
 XX3       DECFSZ 1Dh,1
               GOTO XX4
               GOTO Face3
 XX4        DECFSZ 1Dh,1
               GOTO XX5
               GOTO Face4
 XX5        DECFSZ 1Dh,1 
               GOTO Face6 
               GOTO Face5

 Main       BSF 0Fh,5            ;Set 32 cycles for tone
               MOVLW 05           ;5 faces before final face 
               MOVWF 1E
 Main1     MOVLW 10h 
               MOVWF 0Ch
 Main2     CALL XX 
               DECFSZ 0Ch
               GOTO Main2
               INCF 1C,1
               DECFSZ 1Eh,1
               GOTO Main1 
               BSF 1F,7               ;tone will only sound during final display
 Main3    CALL XX                ;Display until button A pressed
               BTFSS 05,2            ;Test for button A
               GOTO Main3
               BCF 1Fh,7
               GOTO Main

               END


The block of numbers below is the HEX file for Experiment-28. Copy and paste it into a text program such as TEXTPAD or NOTEPAD and call it: Expt-28.hex   
:100000008316043085000030860083129C008F0028
:100010009F13D12882070334073406340434023492
:10002000063407340334053402340234033405340F
:1000300004340234073406340734043403340234FD
:1000400005340734063403340234023403340434F0
:1000500005340434033407340434063407340234DA
:100060000334043405340634013400309100083080
:1000700092001C30930008309400003095009B28BB
:100080000330910003309200003093006030940000
:10009000603095009B28033091000B3092001C309B
:1000A000930068309400603095009B286330910085
:1000B0006330920000309300633094006330950009
:1000C0009B28633091006B3092001C3093006B30A2
:1000D0009400633095009B289F1B79286B3091001A
:1000E0006B309200003093006B3094006B309500C1
:1000F0009B288F0B7D288F0A6E28851400008510A1
:100100006B30860090206B30860090200030860097
:1001100090206B30860090206B3086009020080085
:100120000A308D0060308E00803086068E0B962857
:100130008D0B9228B62885140000851011088600C2
:10014000AE2012088600AE2013088600AE201408E8
:100150008600AE2015088600AE20080004308D0011
:100160008E0BB0288D0BB0280030860005140000DF
:10017000051008001C080A209D009D0BC2280030B5
:100180009C00BA289D0BC52835289D0BC8284028FF
:100190009D0BCB284B289D0BCE2856289D0B6C28F9
:1001A00061288F1605309E0010308C00BA208C0B11
:1001B000D6289C0A9E0BD4289F17BA20051DDD283F
:0401C0009F13D12890
:00000001FF

Go to the next page of experiments:    5x7 PIEZO EXPERIMENTS: Page-1