Datasheet
PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 29
3.4 LESSON 4: SWITCH INPUT
The demo board switch is used in the lesson to rotate the LEDs once on each press.
3.4.1 Files and the #define Directive
This lesson has added a header file to the project named
04 Switch Input.h
as shown in Figure 3-20.
FIGURE 3-20: HEADER FILE
While it is assumed that the reader is familiar with C language header files, we’ll note
that in the 04 Switch Input.h header file the #define directive has been used to
give more meaningful names to the switch I/O pin variable and a constant value.
As with other C compilers use of #define, MPLAB C will replace all instances of the
text “Switch_Pin” with the text “PORTBbits.RB0” at compile time. Remember, for the
compiler to know about the #define definitions, the header file must be included in
the C file, as is done in 04 Switch Input.c:
#include "04 Switch Input.h" // header file
Key Concepts
- The directive #define can be used to give SFR registers and bits more
meaningful names.
- I/O pins that share an analog input channel must be configured as digital
pins if used as digital inputs using SFR ADCON1, or they will always read
‘0’.
- The PORTx SFRs are used to read the logic state on an input port pin.
- Mechanical switch debouncing can be handled in software to eliminate
external components that may be otherwise required.
#define Switch_Pin PORTBbits.RB0
#define DetectsInARow 5