数码管显示(静态+动态),按下k1k2数
码管数字加一,按下k3k4数码管数字减一,K1k3
控制数码管左四位,k2k4控制数码管右四位。
(加需要能加到100,减需要能减到-10)。
“Devil组”引证GPT后的撰写:
以下是51单片机的代码实现,使用了共阳极数码管。
#include <reg51.h>
#define DIGIT_PIN P0
sbit K1 = P1^0;
sbit K2 = P1^1;
sbit K3 = P1^2;
sbit K4 = P1^3;
unsigned char digit_code[10] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
unsigned char left_digit = 0;
unsigned char right_digit = 0;
void delay(unsigned int count) {
unsigned int i;
for (i = 0; i < count; i++);
}
void display_digit(unsigned char digit) {
DIGIT_PIN = digit_code[digit];
}
void display_left() {
display_digit(left_digit);
}
void display_right() {
display_digit(right_digit);
}
void display_all() {
display_left();
display_right();
}
void increase_digit() {
if (right_digit < 9) {
right_digit++;
} else {
right_digit = 0;
if (left_digit < 9) {
left_digit++;
}
}
}
void decrease_digit() {
if (right_digit > 0) {
right_digit--;
} else {
right_digit = 9;
if (left_digit > 0) {
left_digit--;
}
}
}
void main() {
K1 = K2 = K3 = K4 = 1; // 初始化按键状态
while (1) {
// 检测按键
if (K1 == 0) {
delay(1000); // 延时去抖
if (K1 == 0) {
// K1按下,控制左四位
while (K1 == 0) {
display_left();
}
}
} else if (K2 == 0) {
delay(1000);
if (K2 == 0) {
// K2按下,控制右四位
while (K2 == 0) {
display_right();
}
}
} else if (K3 == 0) {
delay(1000);
if (K3 == 0) {
// K3按下,数字加一
increase_digit();
while (K3 == 0) {
display_all();
}
}
} else if (K4 == 0) {
delay(1000);
if (K4 == 0) {
// K4按下,数字减一
decrease_digit();
while (K4 == 0) {
display_all();
}
}
} else {
// 没有按键按下,静态显示数字
display_all();
}
}
}
一个使用树莓派GPIO控制数码管显示和输入的代码实现:
import RPi.GPIO as GPIO
import time
# 设置GPIO模式为BCM模式
GPIO.setmode(GPIO.BCM)
# 定义数码管共阳极接口
segments = (23, 18, 24, 8, 7, 12, 16, 20)
# 定义数码管位选接口
digits = (25, 13, 6, 5)
# 定义按键接口
buttons = (21, 19, 26, 17)
# 定义左右四位数码管显示的数字
leftDigits = [0, 0, 0, 0]
rightDigits = [0, 0, 0, 0]
# 定义数码管显示的数字和对应的GPIO信号
numbers = {
0: (1, 1, 1, 1, 1, 1, 0, 0),
1: (0, 1, 1, 0, 0, 0, 0, 0),
2: (1, 1, 0, 1, 1, 0, 1, 0),
3: (1, 1, 1, 1, 0, 0, 1, 0),
4: (0, 1, 1, 0, 0, 1, 1, 0),
5: (1, 0, 1, 1, 0, 1, 1, 0),
6: (1, 0, 1, 1, 1, 1, 1, 0),
7: (1, 1, 1, 0, 0, 0, 0, 0),
8: (1, 1, 1, 1, 1, 1, 1, 0),
9: (1, 1, 1, 1, 0, 1, 1, 0),
"-": (0, 0, 0, 0, 0, 0, 1, 0),
}
# 设置数码管接口为输出模式
for segment in segments:
GPIO.setup(segment, GPIO.OUT)
for digit in digits:
GPIO.setup(digit, GPIO.OUT)
# 设置按键接口为输入模式
for button in buttons:
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# 定义显示函数
def display(leftDigits, rightDigits, delayTime):
numDigits = len(digits)
# 循环显示左右两侧的数码管
for i in range(numDigits):
# 先选中左侧数码管
GPIO.output(digits[i], 1)
# 显示左侧数码管的数字
for j in range(len(segments)):
GPIO.output(segments[j], numbers[leftDigits[i]][j])
# 等待一段时间
time.sleep(delayTime)
# 关闭左侧数码管
GPIO.output(digits[i], 0)
# 选中右侧数码管
GPIO.output(digits[i], 1)
# 显示右侧数码管的数字
for j in range(len(segments)):
GPIO.output(segments[j], numbers[rightDigits[i]][j])
# 等待一段时间
time.sleep(delayTime)
# 关闭右侧数码管
GPIO.output(digits[i], 0)
#定义加一函数
def addOne():
global leftDigits, rightDigits
num = (leftDigits[0] * 1000 + leftDigits[1] * 100 + leftDigits[2] * 10 + leftDigits[3]) * 10 + rightDigits[0] * 1000 + rightDigits[1] * 100 + rightDigits[2] * 10 + rightDigits[3]
num += 1
if num > 100:
num = 0
leftDigits = [int(num / 1000), int(num / 100) % 10, int(num / 10) % 10, num % 10]
rightDigits = [0, 0, int(num / 10000), int(num / 1000) % 10]
#定义减一函数
def minusOne():
global leftDigits, rightDigits
num = (leftDigits[0] * 1000 + leftDigits[1] * 100 + leftDigits[2] * 10 + leftDigits[3]) * 10 + rightDigits[0] * 1000 + rightDigits[1] * 100 + rightDigits[2] * 10 + rightDigits[3]
num -= 1
if num < -10:
num = 0
leftDigits = [int(num / 1000), int(num / 100) % 10, int(num / 10) % 10, num % 10]
rightDigits = [0, 0, int(num / 10000), int(num / 1000) % 10]
#定义控制左侧数码管函数
def controlLeftDigits(button):
global leftDigits
if button == buttons[0]: # K1
leftDigits[0] += 1
if leftDigits[0] > 9:
leftDigits[0] = 0
elif button == buttons[2]: # K3
leftDigits[0] -= 1
if leftDigits[0] < 0:
leftDigits[0] = 9
elif button == buttons[1]: # K2
leftDigits[1] += 1
if leftDigits[1] > 9:
leftDigits[1] = 0
elif button == buttons[3]: # K4
leftDigits[1] -= 1
if leftDigits[1] < 0:
leftDigits[1] = 9
#定义控制右侧数码管函数
def controlRightDigits(button):
global rightDigits
if button == buttons[0]: # K1
rightDigits[0] += 1
if rightDigits[0] > 9:
rightDigits[0] = 0
elif button == buttons[2]: # K3
rightDigits[0] -= 1
if rightDigits[0] < 0:
rightDigits[0] = 9
elif button == buttons[1]: # K2
rightDigits[1] += 1
if rightDigits[1] > 9: