Page 452 - Programming Microcontrollers in C
P. 452

Keyboard     437

                   KPSR.KDSC=ON;
                   KPSR.KDIE=OFF;
                   KPSR.KRIE=OFF;
                              After the key position is detected, and its value stored in the array,
                          the value 0xff is placed in the next location in the array. This value is
                          used in the decode routine that changes the key positions to ASCII
                          characters for display. A termination character is needed to show the
                          end of the input data has been reached. There is an ‘=’, equal sign on
                          the keyboard, and it was decided to use it as the termination character.
                          We will see later that the position value for the ‘=’ symbol is 0x11.
                          Therefore, if the value c is 0x11 at this point in the code flow, it is
                          time to decode the buffer contents and proceed further. After the data
                          are decoded, the semaphore is released and control is returned to the
                          interrupted program. The COLUMNS bits are all set to zero, the key
                          depress interrupt is disabled and the key release interrupt is enabled
                          prior to the return.
                              It turns out that it is next to impossible to get your finger off a
                          key soon enough to allow this code to work without detecting the
                          key release. The key release interrupt is set up in the key depress
                          interrupt service routine. The main operation that takes place in the
                          key release isr is to disable the key release interrupt and set up for
                          a key depress interrupt. Without this approach, each time the key is
                          depressed, it will get through the isr several times before the key is
                          released and the buffer will be filled with duplicate values each time
                          the isr is executed. The last seven lines of code in the isr above
                          are executed in the key release isr.
                              The decode function converts a value that represents the position
                          of the depressed key on the keyboard to an ASCII representation of
                          the character. The position data is an 8-bit unit with the upper 4 bits
                          representing the column and the lower 4 representing the row. Only
                          one column or row bit should be enabled at a time. More than one
                          row or column is enabled if multiple keys have been depressed. The
                          acceptable row and column values are 1, 2, 4, and 8. The circuitry
                          fixes the row value of 1 corresponding to the bottom row with the
                          value 8 as the top row. Also, the right-most column corresponds to
                          column 1 and the left-most column corresponds to column 8. The
                          inner rows and columns follow the pattern established above. A look
                          at the keyboard has the top row, read from left to right, containing
   447   448   449   450   451   452   453   454   455   456   457