本篇来测试BPI-Pico-RP2040外接矩阵键盘。
4X4矩阵键盘只需要用到8个IO口,即可完成16位按键的读取。其本质原理也就是行列扫描。

矩阵键盘的原理图如下

矩阵键盘扫描的方式有两种
行列扫描适用于8个IO口接到了单片机8个连续的IO口,则可以进行行列扫描。逐行/逐列扫描适用于矩阵键盘接到了任意的IO口。
矩阵键盘的测试代码如下,使用BPI-Pico-RP2040的2~9引脚。
矩阵键盘的16按键先自定义对应的符号含义,当按下某个按键时,在OLED屏幕上显示对应的按键字符。
from machine import I2C,Pin
from ssd1306 import SSD1306_I2C
import time
row1 = Pin(6, Pin.OUT)
row2 = Pin(7, Pin.OUT)
row3 = Pin(8, Pin.OUT)
row4 = Pin(9, Pin.OUT)
row_list = [row1, row2, row3, row4]
col1 = Pin(5, Pin.IN, Pin.PULL_DOWN)
col2 = Pin(4, Pin.IN, Pin.PULL_DOWN)
col3 = Pin(3, Pin.IN, Pin.PULL_DOWN)
col4 = Pin(2, Pin.IN, Pin.PULL_DOWN)
col_list = [col1, col2, col3, col4]
names = [
["1", "2", "3", "+"],
["4", "5", "6", "-"],
["7", "8", "9", "*"],
["#", "0", "=", "/"]
]
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
oled.fill(1)
oled.show()
oled.fill(0)
oled.show()
oled.text("BPI-Pico-RP2040",0,9,1)
oled.show()
while True:
for i, row in enumerate(row_list):
for temp in row_list:
temp.value(0)
row.value(1)
time.sleep_ms(10)
for j, col in enumerate(col_list):
if col.value() == 1:
oled.fill(0)
oled.text(names[i][j],0,39,1)
oled.show()
time.sleep(0.1)
实际测试效果如下:

演示视频如下