PIC Microcontrollers: Scan inputs on a 4x4 Keypad, using only Port C RC0-RC3 in C

淺唱寂寞╮ 提交于 2019-12-11 11:06:10

问题


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:

  1. Wire RC0 to row 0
  2. Wire RC1 to row 1
  3. Wire RC2 to column 0
  4. Wire RC3 to column 1

Software:

Setup:

  1. Configure RC0 and RC1 as outputs
  2. Configure RC2 and RC3 as inputs

Reading:

  1. Set RC0 high, RC1 low
  2. Read RC2. If high, 1 is being pressed
  3. Read RC3. If high, 2 is being pressed
  4. Set RC0 low, RC1 high
  5. Read RC2. If high, 4 is being pressed
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!