Counting on the 7-segment display

This experiment uses the push-button to increment the count on the 7-segment display. The count-value 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.

7-segment display

                    ;Expt4.asm
;Project: Counting on 7-segment display
List P = 16F84
#include <p16F84.inc>
__CONFIG 1Bh    ;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC

SetUp

Table

Delay

Del1

Main
Main1

Main2

ORG 0
BSF 03,5
CLRF 06
MOVLW 01
MOVWF 05
BCF 03,5
MOVLW 3Fh
MOVWF 06
CLRF 1E
CLRF 1F
GOTO Main

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

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

Main CLRF 1E
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
CALL Delay
BCF 1F,0
GOTO Main1

END

;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

;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
;6
;7
;8
;9
;0

;Create 1mS debounce delay

;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

;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

SetUp

Delay

Main

ORG 0
BSF 03,5
CLRF 06
BCF 03,5
CLRF 06
GOTO Main

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

CALL Delay
CALL Delay
INCF 06
GOTO Main

END

;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

;Create approx 250mS delay

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

;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

SetUp

Delay

Main

MainA

Main2

MainB

ORG 0
BSF 03,5
MOVLW 01
MOVWF 05
CLRF 06
BCF 03,5
CLRF 06
CLRF 1F
GOTO Main

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

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

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

END

;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

;Create approx 250mS delay

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

;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

SetUp

Table

Delay

Main
Main1

ORG 0
BSF 03,5
CLRF 06
BCF 03,5
GOTO Main

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
RETLW 50h
RETLW 6Dh
RETLW 78h
RETLW 3Eh
RETLW 6Eh

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

CLRF 1E
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

END

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

;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
;r
;S
;t
;U
;y

;Create approx 250mS delay

;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

;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

SetUp

Table1

Delay

Word1

Main

ORG 0
BSF 03,5
CLRF 06
BCF 03,5
GOTO Main

ADDWF 02h,1
RETLW 73h
RETLW 3Eh
RETLW 6Dh
RETLW 76h
RETLW 00h
RETLW 7Ch
RETLW 3Eh
RETLW 78h
RETLW 78h
RETLW 3Fh
RETLW 37h
RETLW 00h

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

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

CLRF 1E
CALL Word1
GOTO Main

END

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

;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.
;H
;blank
;B
;U
;t
;t
;O
;N
;blank

;Create approx 250mS delay

;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

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

;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

SetUp

Table1

Delay

Sw

Word1

Main

ORG 0
BSF 03,5
CLRF 06
BCF 03,5
CLRF 1F
GOTO Main

ADDWF 02h,1
RETLW 79h
RETLW 37h
RETLW 78h
RETLW 79h
RETLW 50h
RETLW 00h

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

BTFSS 05,0
RETURN
BSF 1F,0
RETURN

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

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

END

;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

;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

;Create approx 250mS delay

;Test the push-button input
;Sw NOT pushed
;Switch pushed. Set button flag.

;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

;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

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

SetUp

Delay

Main

ORG 0
BSF 03,5
CLRF 06
BCF 03,5
CLRF 06
GOTO Main

NOP
DECFSZ 1A,1
GOTO Delay
RETURN

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

END

;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

;Create 1mS delay

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

;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

SetUp

SetUp1

Table

Delay

Delay2

Delay3

Main

Main1

Main2

Main3

Main4

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

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

CALL Delay
DECFSZ 1A,1
GOTO Delay2
RETURN

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

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

;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

;Create 3mS delay

;250mS delay

;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

;Create HIGH time
;Each loop = 14uS

;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 ear-shattering.
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

SetUp

Siren

Repeat

On

Off

ORG 0
BSF 03,5
CLRF 06
BCF 03,5
CLRF 06
GOTO Siren

MOVLW 80h
MOVWF 0Eh
MOVWF 10h
MOVLW 50h
MOVWF 0Fh
MOVLW 50h
MOVWF 0Ch
MOVF 0C,0
MOVWF 0D
BSF 06,7
DECFSZ 0D,1
GOTO On
MOVWF 0Dh
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

;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

;Number of cycles for each tone

;Number of steps
;File 0F holds the number of steps
;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

For more detail: Counting on the 7-segment display

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter