Sie sind auf Seite 1von 11

8/6/2014

PIC LAB-1

PIC LAB-1
P3

EXPERIMENT 4
Counting on the 7-segment display
This experiment uses the push-button to increment the count on the 7-segment display. The countvalue is stored in a file and this value is incremented by the program each time the push button is
pressed. The count-value is used to look-up a table to pick up the display-value for the 7-segment
display.
Additional experiments on the website include a count-down routine, a 00 to 99 counter using a single
display and others.
The Table: The first value in a table is accessed when the Program Counter has an addition of "0"
because it naturally increments to the next location in memory. In the routines below, the number if
items in the table are counted and used to determine "end of table." The reason is the jump-value is
incremented before it is compared.
;Expt4.asm
;Project: Counting on 7-segment display
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
MOVLW 01
MOVWF 05
BCF 03,5
MOVLW 3Fh
MOVWF 06
CLRF 1E
CLRF 1F
GOTO Main

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Load W with 0000 0001
;Make RA0 input
;Go to Bank 0 - the program memory area.
;
;Put "0" on display at reset
;Clear the count file
;Clear the button-press file

Table

;Add W to the Program Counter to create a jump.


; format= gfedcba
;1 If any table value has a leading letter, it must be
;2 preceded with a "0." E.g: 0A3h, 0FFh, 0CCh
;3
;4
;5

ADDWF 02h,1
NOP
RETLW 06h
RETLW 5Bh
RETLW 4Fh
RETLW 66h
RETLW 6Dh

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

1/11

8/6/2014

PIC LAB-1

RETLW
RETLW
RETLW
RETLW
RETLW

7Dh
07h
7Fh
6Fh
3Fh

;6
;7
;8
;9
;0

Delay MOVLW 0A
MOVWF 1B
Del1 NOP
DECFSZ 1A,1
GOTO Del1
DECFSZ 1B,1
GOTO Del1
RETURN

;Create 1mS debounce delay

Main Main CLRF 1E


Main1 BTFSS 05,0
GOTO Main2
CALL Delay
BTFSC 1F,0
GOTO Main1
INCF 1E,1
MOVLW 0B
XORWF 1E,0
BTFSC 03,2
GOTO Main
MOVF 1E,0
CALL Table
MOVWF 06
BSF 1F,0
GOTO Main1
Main2 CALL Delay
BCF 1F,0
GOTO Main1

;1E holds the count-value. Next increment: file=1


;Test the input line on port A
;Button not pressed
;Debounce the button
;Button pressed first time?
;Button already pressed
;First time button pressed. Increment count.
;Has count reached eleven?
;Compare file 1E with eleven
;Check the zero flag in Status file
;
;Copy count into W
;W will return with display-value
;Output display value
;Set button-press flag
;Loop Main1
;Button not pressed. Call Delay
;Clear button-press flag
;Loop Main1

END

;Tells assembler end of program

EXPERIMENT 4a
Binary Counting
This experiment produces a binary count on the 8 LEDs. The output will always show the content of
file 06. File 06 can be incremented, decremented, and shifted, etc just like any of the other files. The
program increments the count in file 06 and shows it for 0.5sec, before incrementing to the next value.
A files will show values in binary, from 00 to 127, making a total of 128 for each cycle of the file.
;Expt4a.asm
;Project: Binary Counting
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
CLRF 06

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.
;Turn off all LEDs

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

2/11

8/6/2014

PIC LAB-1

GOTO Main
Delay NOP
DECFSZ 1A,1
GOTO Delay
DECFSZ 1B,1
GOTO Delay
RETURN

;Create approx 250mS delay

Main

CALL Delay
CALL Delay
INCF 06
GOTO Main

;Show LEDs for 250mS


;Show LEDs for 250mS
;Increment the count in file 6
;Loop

END

;Tells assembler end of program

EXPERIMENT 4b
Binary Counting - up/down
This experiment counts up and down in binary. A push button reverses the count.
It is very interesting to see how a file increments and decrements. This will help you understand binary
numbers. The numbers increment at 4 counts per second. At the same time you will see all
combinations of segments on the 7-segment display.
;Expt4b.asm
;Project: Binary Counting up/down
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
MOVLW 01
MOVWF 05
CLRF 06
BCF 03,5
CLRF 06
CLRF 1F
GOTO Main

;This is the start of memory for the program.


;Go to Bank 1
;Load W with 0000 0001
;Make RA0 input
;Make all port B output
;Go to Bank 0 - the program memory area.
;Turn off all LEDs
;Clear the button-press file

Delay NOP
DECFSZ 1A,1
GOTO Delay
DECFSZ 1B,1
GOTO Delay
RETURN

;Create approx 250mS delay

Main

;Show LEDs for 250mS


;Increment the count in file 6
;Push-button pressed?
;No
;Test button-press flag
;First pass for button?

CALL Delay
INCF 06,1
BTFSS 05,0
GOTO MainA
BTFSS 1F,0
GOTO Main2
BSF 1F,0
GOTO Main
MainA BCF 1F,0
GOTO Main

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

3/11

8/6/2014

PIC LAB-1

Main2 CALL Delay


DECF 06
BTFSS 05,0
GOTO MainB
BTFSS 1F,0
GOTO Main
BSF 1F,0
GOTO Main2
MainB BCF 1F,0
GOTO Main2
END

;Show LEDs for 250mS


;Decrement the count in file 6
;Push-button pressed?
;No
;Test button-press flag
;First pass for button?

;Tells assembler end of program

EXPERIMENT 4c
Producing letters on the 7-segment display
This experiment shows letters of the alphabet on the 7-segment display. Almost all the letters can be
displayed except k, m, v, w, x, z. Sometimes only a capital or small can be displayed and this results
in a mixture for some words. However it is nearly always possible to use words that can be easily
displayed. To produce a word, the letters are flashed on the display for a short period of time with a
brief blank between each letter. This allows doubles to be displayed, especially double numbers for
telephone numbers etc. The following routine automatically displays all the letters. Experiment 4 has
the hex values for the numbers 0-9. Some of the other difficult letters can be created on the display and
if readers accept them as readable, they will be added to the list.
The program displays each letter for 0.75sec and turns the display off for 0.25sec. No input button is
required for the execution of the program.
;Expt4c.asm
;Project: Displaying letters
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
GOTO Main

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.

Table ADDWF 02h,1


RETLW 77h
RETLW 7Ch
RETLW 39h
RETLW 5Eh
RETLW 79h
RETLW 71h
RETLW 6Fh
RETLW 76h
RETLW 06h
RETLW 1Eh
RETLW 38h
RETLW 37h
RETLW 3Fh
RETLW 73h
RETLW 67h

;Add W to the Program Counter to create a jump.


;A This is jump=0 location. format= gfedcba
;b
;C
;d
;E
;F
;g
;H
;I
;J
;L
;N
;O
;P
;q

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

4/11

8/6/2014

PIC LAB-1

RETLW
RETLW
RETLW
RETLW
RETLW

50h
6Dh
78h
3Eh
6Eh

;r
;S
;t
;U
;y

Delay NOP
DECFSZ 1A,1
GOTO Delay
DECFSZ 1B,1
GOTO Delay
RETURN

;Create approx 250mS delay

Main CLRF 1E
Main1 MOVF 1E,0
CALL Table
MOVWF 06
CALL Delay
CALL Delay
CALL Delay
CLRF 06
CALL Delay
INCF 1E,1
MOVLW 14h
XORWF 1E,0
BTFSS 03,2
GOTO Main1
GOTO Main

;File 1E holds the jump-value for the table


;Copy the jump-value into W
;W will return with display-value
;Output display value
;Display for 0.25sec
;Display for 0.25sec
;Display for 0.25sec
;Clear the display
;Blank the display for 0.25sec
;Increment jump-value to look at next table value
;The number of table-values (in hex)
;Has the jump-value reached 14h?
;Test the zero bit in the Status register
;Loop to display next value
;Start again

END

;Tells assembler end of program

EXPERIMENT 4d
Displaying WORDS
This experiment shows "PUSH BUttON" on the 7-segment display. The sub-routine is called "Word1"
and can be called from Main. You can add this sub-routine to any of your programs. A second word
can be created by calling "Word2" etc. If a number of words are needed, the structure of the program
can be altered so that a standard word calling routine is used that picks up a word from a table and
looks for FF to end the word.
;Expt4d.asm
;Project: Displaying WORDS
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
GOTO Main

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.

Table1 ADDWF 02h,1


RETLW 73h
RETLW 3Eh
RETLW 6Dh

;Add W to the Program Counter to create a jump.


;P This is jump=0 location. format= gfedcba
;U This is jump=1 location.
;S This is jump=2 location.

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

5/11

8/6/2014

PIC LAB-1

RETLW
RETLW
RETLW
RETLW
RETLW
RETLW
RETLW
RETLW
RETLW

76h
00h
7Ch
3Eh
78h
78h
3Fh
37h
00h

;H
;blank
;B
;U
;t
;t
;O
;N
;blank

Delay NOP
DECFSZ 1A,1
GOTO Delay
DECFSZ 1B,1
GOTO Delay
RETURN

;Create approx 250mS delay

Word1 MOVF 1E,0


CALL Table1
MOVWF 06
CALL Delay
CALL Delay
CALL Delay
CLRF 06
CALL Delay
INCF 1E,1
MOVLW 0Ch
XORWF 1E,0
BTFSS 03,2
GOTO Word1
RETURN

;Copy the jump-value into W


;W will return with display-value
;Output display value
;Display for 0.25sec
;Display for 0.25sec
;Display for 0.25sec
;Clear the display
;Show display for 0.25sec
;Increment jump-value to look at next table value
;The number of table-values (in hex)
;Has the jump-value reached 0Ch?
;Test the zero bit in the Status register
;Loop to display next table-value
;Start again

Main

CLRF 1E
CALL Word1
GOTO Main

;File 1E holds the jump-value for the table

END

;Tells assembler end of program

EXPERIMENT 4e
Push "A" to display a word
This experiment shows "ENtEr" on the 7-segment display, after button "A" is pressed. The experiment
shows how to combine two sub-routines. The program is constantly POLLING button "A" and when it
is pushed, a flag is set and the micro returns to Main. The flag file is 1F and the flag bit is bit0. In Main,
the flag bit is checked and if it is set, the micro goes to Word1 to display "ENtEr," from Table1.
;Expt4e.asm
;Project: Push "A" to display a word
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
CLRF 1F

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.
;Clear the button-press file

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

6/11

8/6/2014

PIC LAB-1

GOTO Main
Table1 ADDWF 02h,1
RETLW 79h
RETLW 37h
RETLW 78h
RETLW 79h
RETLW 50h
RETLW 00h

;Add W to the Program Counter to create a jump.


;E This is jump=0 location. format= gfedcba
;N This is jump=1 location.
;t
This is jump=2 location.
;E
;r
;blank

Delay NOP
DECFSZ 1A,1
GOTO Delay
DECFSZ 1B,1
GOTO Delay
RETURN

;Create approx 250mS delay

Sw

;Test the push-button input


;Sw NOT pushed
;Switch pushed. Set button flag.

BTFSS 05,0
RETURN
BSF 1F,0
RETURN

Word1 MOVF 1E,0


CALL Table1
MOVWF 06
CALL Delay
CALL Delay
CALL Delay
CLRF 06
CALL Delay
INCF 1E
MOVLW 06h
XORWF 1E,0
BTFSS 03,2
GOTO Word1
RETURN

;Copy the jump-value into W


;W will return with display-value
;Output display value
;Display for 0.25sec
;Display for 0.25sec
;Display for 0.25sec
;Clear the display
;Show display for 0.25sec
;Increment jump-value to look at next table value
;The number of table-values (in hex)
;Has the jump-value reached 06h?
;Test the zero bit in the Status register
;Loop to display next table-value

Main

CLRF 1E
CALL Sw
BTFSC 1F,0
CALL Word1
BCF 1F,0
GOTO Main

;File 1E holds the jump-value for the table


;Poll (look at) push-button "A"
;Has switch been pushed?
;Yes. Display "ENtEr"
;Clear button flag

END

;Tells assembler end of program

EXPERIMENT 5
Creating a tone
This experiment creates a tone. A tone is simply the action of turning on an output, executing a delay,
turning off the output, executing a delay, then repeating the sequence.
This action produces a square-wave and the microcontroller activates a driver transistor that drives a
piezo diagram to produce the characteristic harsh sound.
The microcontroller can drive a piezo diaphragm directly but we have opted to add a driver transistor so
the circuit can also drive a mini speaker, if needed. Place your finger on and around the components to
see how the resistance of your finger affects the performance of the circuit. This indicates the sensitive
components.
http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

7/11

8/6/2014

PIC LAB-1

Once you know how to produce a tone, the whole world opens up to sounds, noises, tones and tunes.
;Expt5.asm
;Project: Creating a tone
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
CLRF 06
GOTO Main

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.
;Clear outputs of junk

Delay NOP
DECFSZ 1A,1
GOTO Delay
RETURN

;Create 1mS delay

Main

BSF 06,7
CALL Delay
BCF 06,7
CALL Delay
GOTO Main

;Turn on driver transistor


;Create ON time
;Turn off driver transistor
;Create OFF time
;Loop

END

;Tells assembler end of program

EXPERIMENT 6
Creating a tune
This experiment creates a tune. It is an extension of experiment 5.
A sequence of tones is produced by making a table containing a pair of values for each note. The first
value produces the time-delay between the high and low of the output and thus creates the frequency
of the tone. The second value creates the length of time for the note.
This value is a little bit more complex than first appears.
Suppose we want to reproduce a middle C minum:
The tune "Hey Jude" is played at at speed know as "Allegro." This has a metronome rate of between
120 and 160. Suppose we choose the centre-value of 140. This is 140 beats per minute and represents
the time taken to play a minum or half-note. (A minum is a hollow oval with a plain riser). You may
recall, a metronome beats or "clicks" each time it moves to the left and to the right. The time between
the "click-click" is 1/140 minute. This gives 430mS for a minum. A Crotchet is a solid oval with a riser
and takes 215mS. A Quaver has a flag on the riser and takes 107mS. A semi-quaver has two flags and
takes 53mS.
Middle C is 262Hz. From this we know the length of the delay between the high and low output, to
produce one cycle.
The other value we need to know is the number of cycles of middle-C in 430mS. The answer is 112.
For each frequency we need to work out the number of cycles for each length of note.
With this we can create a table of values. The program will pick up a pair of values and play the note for
the correct duration. The end of the table is assigned FF. The program looks for FF to repeat.
;Expt6.asm
;Project: Creating a tune
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

8/11

8/6/2014

PIC LAB-1

ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
CLRF 06
SetUp1 MOVLW 01
MOVWF 0Ch
GOTO Main
Table

Delay

ADDWF 02h,1
RETLW 0A8h
RETLW 5Bh
RETLW 0FAh
RETLW 6Bh
RETLW 46h
RETLW 6Bh
RETLW 54h
RETLW 5Bh
RETLW 5Eh
RETLW 51h
RETLW 0FCh
RETLW 7Ah
RETLW 0FFh
RETLW 0FFh
NOP
NOP
NOP
NOP
NOP
NOP
RETURN

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.
;Clear outputs of junk

;duration - 168 loops


;G - 392Hz 1.27mS HIGH,LOW - 91 loops
;duration - 250 loops
;E - 330Hz 1.51mS HIGH,LOW - 107 loops
;duration - 70 loops
;E - 330Hz
;duration - 84 loops
;G - 392Hz
;duration - 94 loops
;A - 440Hz - 1.13mS HIGH,LOW - 81 loops
;duration - 252 loops
;D - 292Hz - 1.71mS HIGH,LOW - 122 loops
;End of table
;End of table
;Create 10uS delay

Delay2 CALL Delay


DECFSZ 1A,1
GOTO Delay2
RETURN

;Create 3mS delay

Delay3 NOP
DECFSZ 1A,1
GOTO Delay3
DECFSZ 1B,1
GOTO Delay3
RETURN

;250mS delay

Main

;Dec jump value to re-look at values


;Copy jump-value into W
;Return with table-value in W
;Length of note into file 0F
;Increment the table-value
;Copy jump-value into W
;Return with table-value in W
;Frequency of note into file 0D
;Frequency of note into file 0E

Main1
Main2

DECF 0C,1
MOVF 0Ch,0
CALL Table
MOVWF 0F
INCF 0Ch,1
MOVF 0Ch,0
CALL Table
MOVWF 0D
MOVWF 0E
BSF 06,7
CALL Delay
DECFSZ 0D,1

;Create HIGH time


;Each loop = 14uS

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

9/11

8/6/2014

Main3

Main4

PIC LAB-1

GOTO Main2
BCF 06,7
CALL Delay
DECFSZ 0E,1
GOTO Main3
DECFSZ 0F,1
GOTO Main1
BCF 06,7
CALL Delay2
CALL Delay2
CALL Delay2
INCF 0Ch,1
MOVF 0Ch,0
CALL Table
MOVWF 10h
MOVLW 0FFh
XORWF 10h,0
BTFSC 03,2
GOTO Main4
INCF 0Ch,1
GOTO Main
CALL Delay3
CALL Delay3
CALL Delay3
GOTO SetUp1
END

;Create LOW time

;Length of note

;3mS between notes


;3mS between notes
;3mS between notes
;Increment pointer to next value in table
;Copy jump-value into W
;Return with table-value in W
;Put "end of table" into file 10h
;Check for 'end of table'
;Compare file 10h with FF (result in W)
;Look at Zero flag in status file
;Start again
;Increment the table-value
;Go to next note

;Tells assembler end of program

EXPERIMENT 7
Siren Sound
This experiment creates a Siren sound.
It can be added to an alarm and when played through an 8-watt horn speaker, the output can be earshattering.
The program shows how the instructions create a delay between the output being HIGH and LOW, to
produce one cycle. When this is repeated, a tone is produced. It is actually a square-wave output. As
the delay between the HIGH and LOW is reduced or increased, the tone rises or falls. Additional alarm
sounds are described on the website.
;Expt7.asm
;Project: Siren Sound
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh
;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC
ORG 0
SetUp BSF 03,5
CLRF 06
BCF 03,5
CLRF 06
GOTO Siren

;This is the start of memory for the program.


;Go to Bank 1
;Make all port B output
;Go to Bank 0 - the program memory area.
;Clear display

Siren

;Number of cycles for each tone

MOVLW 80h
MOVWF 0Eh
MOVWF 10h
MOVLW 50h
MOVWF 0Fh

;Number of steps
;File 0F holds the number of steps

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

10/11

8/6/2014

PIC LAB-1

MOVLW 50h
MOVWF 0Ch
Repeat MOVF 0C,0
MOVWF 0D
On
BSF 06,7
DECFSZ 0D,1
GOTO On
MOVWF 0Dh
Off
BCF 06,7
DECFSZ 0D,1
GOTO Off
DECFSZ 10h,1
GOTO Repeat
DECF 0C,1
INCF 0E,1
MOVF 0E,0
MOVWF 10h
DECFSZ 0F,1
GOTO Repeat
GOTO Siren
END

;Determines frequency
;File 0C determines the frequency
;File 0C is moved to W
;W is moved to file 0D for decrementing
;Length of HIGH time to Piezo

;W is moved to file 0D again


;Length of LOW time to Piezo

;Number of cycles for each tone


;HIGH and LOW is shortened -tone rises
;Increase the number of cycles
;File 0E to W
;W to file 10h
;Number of steps

;Tells assembler end of program

To Top

http://www.talkingelectronics.com/projects/PIC_LAB-1/PicLab1_experiments-P3.html

11/11

Das könnte Ihnen auch gefallen