import machine
import utime
# Define GPIO pins for the 7-segment display (a-g segments)
seg_pins = [0, 1, 2, 3, 4, 5, 6]
# Define GPIO pins for the keypad rows and columns
row_pins = [26,22,21,20]
col_pins = [19,18,17,16]
# Define the characters to display for each keypad key (including letters A, B, C, D)
keypad_characters = [
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
]
# Initialize the GPIO pins for the 7-segment display
for pin in seg_pins:
machine.Pin(pin, machine.Pin.OUT)
# Initialize the keypad and set up the GPIO pins
for row_pin in row_pins:
machine.Pin(row_pin, machine.Pin.IN, machine.Pin.PULL_UP)
for col_pin in col_pins:
machine.Pin(col_pin, machine.Pin.OUT)
# Function to scan the keypad and return the pressed key
def scan_keypad():
for col_pin in col_pins:
machine.Pin(col_pin, machine.Pin.OUT)
for row_pin in row_pins:
if not machine.Pin(row_pin).value():
machine.Pin(col_pin, machine.Pin.IN, machine.Pin.PULL_UP)
return (col_pins.index(col_pin), row_pins.index(row_pin))
machine.Pin(col_pin, machine.Pin.IN, machine.Pin.PULL_UP)
return None
# Function to display a character on the 7-segment display
def display_character(character):
# Define the 7-segment display patterns for characters
display_patterns = {
'0': (0, 0, 0, 0, 0, 0, 1),
'1': (1, 0, 0, 1, 1, 1, 1),
'2': (0, 0, 1, 0, 0, 1, 0),
'3': (0, 0, 0, 0, 1, 1, 0),
'4': (1, 0, 0, 1, 1, 0, 0),
'5': (0, 1, 0, 0, 1, 0, 0),
'6': (0, 1, 0, 0, 0, 0, 0),
'7': (0, 0, 0, 1, 1, 1, 1),
'8': (0, 0, 0, 0, 0, 0, 0),
'9': (0, 0, 0, 1, 1, 0, 0),
'A': (0, 0, 0, 1, 0, 0, 0),
'B': (0, 0, 0, 0, 0, 0, 0),
'C': (0, 1, 1, 0, 0, 0, 1),
'D': (0, 0, 0, 0, 0, 0, 1),
'*': (1, 1, 1, 1, 1, 1, 1),
'#': (0, 0, 1, 1, 0, 0, 0)
}
if character in display_patterns:
pattern = display_patterns[character]
for pin, state in enumerate(pattern):
machine.Pin(seg_pins[pin], machine.Pin.OUT).value(state)
# Main loop
while True:
key = scan_keypad()
if key is not None:
col, row = key
displayed_character = keypad_characters[col + row * 4]
display_character(displayed_character)