電話按鍵盤試接 Raspberry Pi |
稍早時,曾在舊零件箱中發現一個數字按鍵盤,是很久以前我將報廢電話按鍵拆下用做 8051 控制電動門鎖的輸入按鍵。現在像這種 3x4 的數字鍵盤應該只需要 7 條信號線即可,但是這東西卻有 13 個接線腳位,因為年代久遠記憶早已模糊,所以重新將接線量出記下備用。我想以此鍵盤做一個可適用手邊正在玩的一些 mcu/mpu 板子的控制面板部件。
舊型電話按鍵盤接線圖 |
如何讀取按鍵盤上某鍵被按下?其實就是用程式做很簡單的掃描-依序分別控制各行信號輸出為 HIGH,此時判讀各列的信號,信號為 HIGH 的行/列交集處即是按鍵所在。
試接 Raspberry Pi
再來,要將整理好的數字鍵盤試接 Raspberry Pi 的 GPIO (General-purpose input/output) 腳位,寫程式操控 GPIO 信號。
控制 GPIO 有很多種方法,可以參考這篇「讓你的 Raspberry Pi 透過 GPIO 閃爍 LED 燈」的介紹,另外最新的 Raspbian 應該有內裝了 Wiring Pi: GPIO Interface Library for Raspberry Pi 套件也可用。
實際接線前,若是手邊沒有 Raspberry Pi GPIO 腳位圖,有個指令 gpio readall 可用來先確認 GPIO 的腳位配置,撰寫程式時也是要弄清楚使用的腳位代號到底是 BCM? WiringPi? 或 Physical? 否則會發現結果都不對。
pi@raspberrypi ~ $ gpio readall +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5V | | | | 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | 28 | 17 | GPIO.17 | ALT2 | 0 | 51 || 52 | 0 | ALT2 | GPIO.18 | 18 | 29 | | 30 | 19 | GPIO.19 | ALT2 | 0 | 53 || 54 | 0 | ALT2 | GPIO.20 | 20 | 31 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+ pi@raspberrypi ~ $
我的數字鍵盤需要用到 7 個 GPIO 信號...
三條輸出:C1-#11(GPIO.0), C2-#12(GPIO.1), C3-#13(GPIO.2)
四條輸入:R1-#15(GPIO.3), R2-#16(GPIO.4), R3-#18(GPIO.5), R4-#22(GPIO.6)
用 python 試做:
import RPi.GPIO as GPIO import time cols= [11,12,13] rows= [15,16,18,22] keys= [['1','2','3'],['4','5','6'],['7','8','9'],['*','0','#']] def npscan(pin): GPIO.output(pin, GPIO.HIGH) time.sleep(0.05) for inp in rows: if GPIO.input(inp): print keys[rows.index(inp)][cols.index(pin)],"key is pressed @",str( inp),"/",str(pin) time.sleep(0.1) GPIO.output(pin, GPIO.LOW) time.sleep(0.05) return try: GPIO.setmode(GPIO.BOARD) for pin in cols: GPIO.setup(pin, GPIO.OUT) for pin in rows: GPIO.setup(pin, GPIO.IN, GPIO.PUD_DOWN) while True: for pin in cols: npscan(pin) except KeyboardInterrupt: GPIO.cleanup()測試結果:
pi@raspberrypi ~/playground $ ./numpad.py 1 key is pressed @ 15 / 11 2 key is pressed @ 15 / 12 3 key is pressed @ 15 / 13 4 key is pressed @ 16 / 11 5 key is pressed @ 16 / 12 6 key is pressed @ 16 / 13 7 key is pressed @ 18 / 11 8 key is pressed @ 18 / 12 9 key is pressed @ 18 / 13 * key is pressed @ 22 / 11 0 key is pressed @ 22 / 12 # key is pressed @ 22 / 13
下一步
這僅僅是試做,下一步想要做的是將接線改裝成 I²C(Inter-Integrated Circuit)bus 介面,否則我的數字鍵盤幾乎用完了 Raspberry Pi 的 GPIO 腳位,實用性大大受限。
沒有留言:
張貼留言