In the main program, bit 4-7 of port B are initialized as inputs with Schmitt-trigger
and pull-ups while bit 3-0 are configure as outputs. Scanning is accomplished by outputting
a zero on a column. If any key on that column is pressed, a zero will be read in the row
where that key is located. Due to the internal pull-ups, all inactive inputs will be read as 1 s.
The RTCC is initialized to be incremented on internal clock cycles. With a prescaler
ratio of 8, coupled with a value of 250 in the RETIW interrupt routine which will be loaded
into RTCC, it will give us approximately 1 mS per interrupt.
The state machine is initialized to start with fast scan which output zeroes to all
columns and detect all keys at the same time. Once any key is detected, debouncing will be
started. Normally this is dependent on the mechanical characteristics of the keys. In this
case, we are using a 20 mS debouncing time.
After debouncing is done, a detailed scanning will be done. Each column will have a
zero output at a time to detect if any key in that certain column is pressed. If that is the case,
a value other than 11110000 (binary) will be read from the inputs (RB4-7) and we will
know that a key is pressed. Otherwise, the detection that we made from the fast scan stage
may just have been noise and we transition back to fast scan state.
When a value other than 11110000 (binary) is read, then we use that value to find
out on which row the key is. That row count and the column count that we keep during
column scan will give us the exact location of the key hit by the formula:
index to virtual matrix = 4*row count + column count
Column count (cnt1) 3 2 1 0
RB3 RB2 RB1 RB0
scan pattern (RB3-0) 0111 1011 1101 1110
Row count (cnt2)
input value (RB7-4)
3
F
E
D
C
1110
RB4
B
A
9
8
1101 RB5
2
1
7
6
5
4
1011 RB6
0
3
2
1
0
0111 RB7
Note: Index to virtual matrix is indicated at each intersection.