Specifications

mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix B: Examples
; W register and number TEST (00000101)
btfsc STATUS,Z ; If numbers are equal, result is 0 and
bsf PORTD,3 ; bit STATUS,Z = 1. Bit PORTD,3 is set
goto loop ; and jump to label loop is executed
end ; End of program
EXAMPLE 7
Using macros in the program, using debounce routine
You have probably noticed in the previous example that the microcontroller does not always operate as expected.
Namely, by pressing the push-button, the number on port B is not always incremented by 1. Mechanical push-buttons
make several short successive contacts when they have been activated. You guess, the microcontroller registers and
counts all that...
There are several ways to solve this problem. This program uses program delay known as debounce. Basically, it is a
simple procedure. Upon input change detection (button press), a short program delay is provided and the program waits
for another change (button release). Only after this, the program comes to a conclusion that the button is activated.
In this very case, the push-button is tested by means of macro called button. Besides, this macro contains a program delay
which is provided by means of another macro pausems.
The main program is relatively simple and enables the variable "cnt" to be incremented and decremented by using two
push-buttons. This variable is thereafter copied to port B and affects the LED (logic one (1) turns LED diode on, while logic
zero (0) turns LED diode off).
Example 7:
;********************** Header **********************
;************* DEFINING VARIABLES *************************
cblock 0x20 ; Block of variables starts at address 20hex
http://www.mikroe.com/en/books/picmcubook/appb/ (30 of 54)5/3/2009 11:36:02 AM