5x7 Display
Testing
Page3
ADDING
PUSH-BUTTONS
Up to now, each of the programs has run as soon as the power was applied to
the circuit.
In this section we add 3 push-buttons.
Push-buttons can also be called "Keys" or "input
Devices" and to make sure they will be be read instantly, it is
important they are "poled" (scanned) on a regular basis.
This involves putting the "Buttons" in a place where the micro is
passing very often. A suitable position is inside a delay
routine.
Adding push-buttons to a project gives you the capability of selecting, starting or stopping a
routine as well as calling one up at any time during the running of another
routine.
Writing the code for a set of push buttons can be quite complex as there are
a number of things that must be taken into account, to get a push-button to
work correctly.
The main problem is DEBOUNCING the button.
Suppose you are using a button to increment a number on a display. When the
button is pressed, the program increments the display and returns to
scan the button again. This all happens very quickly and the program thinks
the button has been pushed again, so the display is incremented once
more.
The micro generally goes through a routine so fast that the display can get
incremented lots of times before you have time to release the button.
To prevent this from happening, the micro must look to see if the button has
been released before another increment can occur.
But there's still one more feature that has to be included in the
program.
When the button is pushed, the contacts make and break a number of times
before finally making contact.
The micro sees this as a number of presses and the counter can be
advanced a number of counts on a single press.
This also occurs when the button is being released. In some of the
experiments, you can see the effect of switch-bounce by pressing and
releasing the button very slowly. The counter will count more than one digit
or an animation will appear on the screen two times.
To prevent this, the program must have a "loop counter" or delay
so that only one count is recorded in say 1/10th of a second.
This is called fully debouncing the switch and this type of routine should
be included in all programs to prevent any type of
"double-counting."
The next series of programs in this test section will show how to interface a
push-switch to a LED and test it.
The circuit diagram for the project is shown below so you can see how the
push-buttons are connected to the microcontroller. When the button is not
pressed, the input line sees a LOW. When the button is pressed, the input
line sees a HIGH.
SWITCH
PROGRAM-1
This program turns on a LED on the display when buttonA is pressed.
In this program there is no debouncing. The program sets up the ports with
PortA as input and PortB as output. The program then loops around (poles)
the switch looking for a keypress. The program then turns on a LED and loops.
There is an important point to note with the program.
For the 3rd, 4th, 5th and 6th instructions, the program is accessing a
special bank of registers that are in a different part of the
microcontroller to the programming and port files. We get into the
"special section" via the instruction BSF 03,5 and get out of the
"special section" via BCF 03,5.
The instruction on line 6: MOVLW 06h moves the "value in W" into a
register called the IN/OUT CONTROL REGISTER. This register is called 06 and
is not the register we call the PORT REGISTER.
The 8th instruction seems to do the same thing. BUT IT DOESN'T.
The micro has come out of page1 and is in the programming page. The
programming page is Page0.
The 8th instruction takes the value in W (we put 00 into W in the 5th
instruction) and put it into file 06. We call file 06 PORTB REGISTER.
This time file 06 is PortB and the instruction will make all outputs of
portB LOW.
The program only works once. You have to turn the power off and on again to
re-test the program.
|
Switch-1 Program for 5x7
Display
;PIC16F84 and only F84 chip
;Detects ButtonA and illuminates a LED
Start ORG 0x00
BSF 03,5
;Go to page1 for setting IN/OUT
MOVLW 1Ch
;Make RA2, RA3 and RA4 inputs
MOVWF 05h
MOVLW 00h
;Put 00 into W
MOVWF 06h
;Make all RB lines output
BCF 03,5
;Go to Page0 for programming
MOVWF 06h ;Put 00 into output port to
turn off all LEDs
Loop1 BTFSS 05,2
;Test button A
GOTO Loop1
; line is LOW
Loop2 MOVLW 01h
;Line is HIGH. Put 1 into W
MOVWF 06h
;output 1 to the display
GOTO Loop2
;do-nothing in a loop
END
|
|
The block of
numbers below is the HEX file for Switch 1. Copy and paste it
into a text program such as TEXTPAD or NOTEPAD and call it: Switch-1.hex
|
|
| :1000000083161C3085000030860083128600051997
:080010000728013086000928D1
:00000001FF |
The next program: Switch-2, is also a very simple program. It tests buttons
A, B, C and puts a LED on the screen for each button.
|
Switch-2 Program for 5x7
Display
;PIC16F84 and only F84 chip
;Detects Buttons A, B and C and illuminates LEDs
Start ORG 0x00
BSF 03,5
MOVLW 1Ch ;Make RA2, RA3 and RA4 inputs
MOVWF 05h
MOVLW 00h ;Put 00 into W
MOVWF 06h ;Make all RB lines output
BCF 03,5
;Go to Page0 for programming
MOVWF 06h ;Put 00 into output port to
turn off all LEDs
Loop1 BTFSS 05,2 ;Test button A to see if line is
HIGH
GOTO Loop2 ;
MOVLW 01h ;Put 1 into W to turn on lower LED
MOVWF 06h ;output 1 to the display
Loop2 BTFSS 05,3
;Test button B to see if line is HIGH
GOTO Loop3
MOVLW 08h ;Put 08 into W to turn on middle LED
MOVWF 06h ;Output 08 to display
Loop3 BTFSS 05,4 ;Test button C to see if line is
HIGH
GOTO Loop1
MOVLW 40h ;Put 40h into W to turn on top LED
MOVWF 06h
GOTO Loop1 ;Loop all the switches again
END
|
|
The block of
numbers below is the HEX file for Switch 2. Copy and paste
it into a text program such as TEXTPAD or NOTEPAD and call
it: Switch-2.hex
|
|
|
:1000000083161C3085000030860083128600051997
:100010000B280130860085190F2808308600051A44
:08002000072840308600072884
:00000001FF |
Switch-3 Program has debounce. It detects the 3 switches and puts a flashing LED on
the screen for each switch. To do this requires a lot of programming. The
micro has to scan the keys, toggle the LEDs ON or OFF and flash the
LEDs.
|
Switch-3 Program for 5x7 Display
;PIC16F84 and only F84 chip
;Detects Buttons A, B and C (with debounce) and flashes LEDs
Start ORG 0x00
BSF 03,5
MOVLW 1Ch ;Make RA2, RA3 and RA4 inputs
MOVWF 05h
MOVLW 00h ;Put 00 into W
MOVWF 06h ;Make all RB lines output
BCF 03,5
;Go to Page0 for programming
MOVWF 06h ;Put 00 into output port to turn off all LEDs
MOVWF 1Dh ;zero the debounce file
MOVWF 1Eh ;Zero the flag file
Scan1 MOVLW 0FFh
MOVWF 1Ah
MOVLW 080h
MOVWF 1Bh
Scan2 BTFSC 05,2
;Test if Button A pressed. (Not pressed, line = LOW)
GOTO AA
;Button A is pressed
BTFSS 1E,5
;A not pressed. Check to see if flag bit 5 is SET
GOTO Scan3 ;Not SET
MOVF 1Dh,0
;Bit 5 SET. Copy 1D into W
XORLW 00
BTFSC 03,2
;Is file 1D = 0?
GOTO Scan5
;Debounce file is zero
DECFSZ 1Dh,1
;Decrement debounce file
GOTO Scan5
BCF 1E,5
Scan3 BTFSC 05,3
;Test if button B pressed
GOTO BB
;B pressed
BTFSS 1E,6
;B not pressed. Check to see if flag bit 6 is SET
GOTO Scan4
;Not SET
MOVF 1Dh,0
;Bit 6 SET. Copy 1D into W
XORLW 00
BTFSC 03,2
;Is file 1D = 0?
GOTO Scan5
;Debounce file is zero
DECFSZ 1Dh,1
;Decrement debounce file
GOTO Scan5
BCF 1E,6
Scan4 BTFSC 05,4
;Test if button C pressed.
GOTO CC
;C pressed
BTFSS 1E,7
;C not pressed. Check to see if flag bit 7 is SET
GOTO Scan5
;Not SET
MOVF 1Dh,0
;Bit 7 SET. Copy 1D into W
XORLW 00
BTFSC 03,2
;Is file 1D = 0?
GOTO Scan5
;Debounce file is zero
DECFSZ 1Dh,1
;Decrement debounce file
GOTO Scan5
BCF 1E,7
Scan5 NOP
Del1 DECFSZ 1Ah,1
;Delay section
GOTO Del1
DECFSZ 1Bh,1
GOTO Scan2
BTFSS 1E,2
;Test for LED A active
GOTO Scan8
MOVLW 01
XORWF 06h,1
;Toggle LED A
Scan6 BTFSS 1E,3
;Test for LED B active
GOTO Scan9
MOVLW 08
XORWF 06h,1
;Toggle LED B
Scan7 BTFSS 1Eh,4
;Test for LED C active
GOTO Scan10
MOVLW 40h
XORWF 06h,1
;Toggle LED C
GOTO Scan1
Scan8 BCF 06h,0
GOTO Scan6
Scan9 BCF 06h,3
GOTO Scan7
Scan10 BCF 06h,6
GOTO Scan1
AA MOVLW 00
XORWF 1D,0
;Is 1D = 0?
BTFSS 03,2
GOTO Scan5
;1D is not zero
BSF 1E,5
;1D is zero. Put a flag in 1E for button A
MOVLW 04h
;Toggle LED A ON/OFF
XORWF 1E,1
MOVLW 10h
;Put 10h into debounce file 1D
MOVWF 1Dh
GOTO Scan5
BB MOVLW 00
XORWF 1D,0
;Is 1D = 0?
BTFSS 03,2
GOTO Scan5
;1D is not zero
BSF 1E,6
;1D is zero. Put a flag in 1E for button B
MOVLW 08h
;Toggle LED B ON/OFF
XORWF 1E,1
MOVLW 10h
;Put 10h into debounce file 1D
MOVWF 1Dh
GOTO Scan5
CC MOVLW 00
XORWF 1D,0
;Is 1D = 0?
BTFSS 03,2
GOTO Scan5
;1D is not zero
BSF 1E,7
;1D is zero. Put a flag in 1E for button C
MOVLW 10h
;Toggle LED C ON/OFF
XORWF 1E,1
MOVLW 10h
;Put 10h into debounce file 1D
MOVWF 1Dh
GOTO Scan5
END
|
|
The block of
numbers below is the HEX file for Switch 3. Copy and
paste it into a text program such as TEXTPAD or NOTEPAD and
call it:
Switch-3.hex
|
|
|
:1000000083161C30850000308600831286009D0018
:100010009E00FF309A0080309B00051D46289E1EE2
:1000200018281D08003A03192E289D0B2E289E1211
:10003000851D50281E1F23281D08003A03192E284D
:100040009D0B2E281E13051E5A289E1F2E281D08A4
:10005000003A03192E289D0B2E289E1300009A0BA0
:100060002F289B0B0D281E1D4028013086069E1D43
:100070004228083086061E1E44284030860609287D
:100080000610372886113B280613092800301D0664
:10009000031D2E289E1604309E0610309D002E282B
:1000A00000301D06031D2E281E1708309E06103036
:1000B0009D002E2800301D06031D2E289E1710308F
:0800C0009E0610309D002E2861
:00000001FF |
All the testing has now been
done. We have checked the wiring of the microcontroller to the LEDs,
the scanning provided by the 4017 chip, the connection of the buttons and
the debounce routine. We are now ready for the REAL THING! The programs for
the 5x7.
The 5x7 is like a beautifully-bound BLANK notebook. In it you can write
anything you like. It can be a murder mystery, poem, short story or complete
novel.
Exactly the same applies with this project. You can write one program, 10 programs,
add more input or output devices, or join it up with other modules to create
a larger screen.
In fact, almost anything is possible and although the screen is only 5
pixels wide by 7 pixels high, it represents a module or "window"
that can be expanded to a "Las Vegas" billboard by simply
adding more modules.
Without any more discussion, let's go to the Programs Page:
5x7
EXPERIMENTS: Page-1.
|