问题
I'm new to PIC Microcontrollers & C programming and I've been set the task of creating my own Keypad scanning method that works exclusively on Port C (16F877A Microcontroller). Specifically, the program that uses this method only uses digits 1, 2, 4 and 5- so to be efficient, the scanner method is to only use RC0, RC1, RC2 and RC3 as the designated input/ outputs (I'm not allowed to use RC4-RC7). This essentially turns the 4x4 keypad into a 2x2 keypad.
I understand that the concept on scanning has Port lines set to high on rows, and lines set as input columns- when a button is pressed, this reads the low nibble of the port to detect the button.
Unfortunately, I have no idea how to code this in C, especially using the Port lines specified above. Can anybody shed some light on the problem and point me in the right direction? Thanks!
回答1:
One possible approach:
Hardware:
- Wire RC0 to row 0
- Wire RC1 to row 1
- Wire RC2 to column 0
- Wire RC3 to column 1
Software:
Setup:
- Configure RC0 and RC1 as outputs
- Configure RC2 and RC3 as inputs
Reading:
- Set RC0 high, RC1 low
- Read RC2. If high,
1
is being pressed - Read RC3. If high,
2
is being pressed - Set RC0 low, RC1 high
- Read RC2. If high,
4
is being pressed - Read RC3. If high,
5
is being pressed
Adding appropriate pull-ups/pull-downs and debouncing is left as an exercise to the reader.
回答2:
The way it works, is you enable an output voltage on one column of the 4. You then read the 4-bit row data in, to determine which key(s) are pressed that complete the circuit. You then cycle round the other columns. You can then use column * 4 + row where one (or more) key was closed to establish which key matrix position key was pressed. Usually that won't map directly to the intended key value, so you use a lookup table to convert, or some #define
statements. Also, the logic might be inverted.
来源:https://stackoverflow.com/questions/27407950/pic-microcontrollers-scan-inputs-on-a-4x4-keypad-using-only-port-c-rc0-rc3-in