基于普中单核A4开发板,按键数码管加减法计算:三位数码管显示0~999;独立按键K2按一下,数码管加或者减1;矩阵键盘按一下,数码管加或者减对应的键值,k1键用于清零,k3键用于实现加减法切换,数码管大于999时变为0
单片机源程序如下:- #include <reg52.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- uchar GetMatrixKey();
- sbit LSA = P2^2;
- sbit LSB = P2^3;
- sbit LSC = P2^4;
- sbit K1 = P3^0; // 清零键
- sbit K2 = P3^1; // 加减1键
- sbit K3 = P3^2; // 加减法切换键
- uchar matrixKey = 0;
- uchar code smgduan[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
- uchar disp_buff[3] = {0, 0, 0};
- int num = 0;
- bit isAddition = 1; // 默认使用加法
- void delay(uint i) {
- while (i--);
- }
- void DigDisplay() {
- uchar i;
- for (i = 0; i < 3; i++) {
- switch (i) {
- case 0: LSA = 1; LSB = 0; LSC = 0; break; // 个位
- case 1: LSA = 0; LSB = 1; LSC = 0; break; // 十位
- case 2: LSA = 0; LSB = 0; LSC = 1; break; // 百位
- }
- P0 = smgduan[disp_buff[i]];
- delay(100);
- P0 = 0x00;
- }
- }
- void KeyScan() {
- if (K1 == 0) { // 清零键
- num = 0;
- disp_buff[0] = disp_buff[1] = disp_buff[2] = 0;
- while (K1 == 0);
- }
- if (K2 == 0) { // 加减1键
- if (isAddition) {
- num = (num + 1) % 1000;
- } else {
- num = (num == 0) ? 999 : num - 1;
- }
- disp_buff[0] = num % 10; // 个位
- disp_buff[1] = (num / 10) % 10; // 十位
- disp_buff[2] = num / 100; // 百位
- while (K2 == 0);
- }
- if (K3 == 0) { // 加减法切换键
- isAddition = ~isAddition;
- while (K3 == 0);
- }
- matrixKey = GetMatrixKey();
-
- // 处理矩阵键盘输入
- if (matrixKey >= 1 && matrixKey <= 16) {
- // 根据矩阵键值加减对应的键值
- if (isAddition) {
- num = (num + matrixKey) % 1000;
- } else {
- num = (num - matrixKey + 1000) % 1000;
- }
- disp_buff[0] = num % 10; // 个位
- disp_buff[1] = (num / 10) % 10; // 十位
- disp_buff[2] = num / 100; // 百位
- }
-
- }
- // 获取矩阵键盘的键值,具体根据硬件情况实现
- uchar GetMatrixKey() {
- uchar KeyNumber = 0;
-
- P1 = 0xFF;
- P1_3 = 0;
- if (P1_7 == 0) { delay(20); while (P1_7 == 0); delay(20); KeyNumber = 1; }
- if (P1_6 == 0) { delay(20); while (P1_6 == 0); delay(20); KeyNumber = 5; }
- if (P1_5 == 0) { delay(20); while (P1_5 == 0); delay(20); KeyNumber = 9; }
- if (P1_4 == 0) { delay(20); while (P1_4 == 0); delay(20); KeyNumber = 13; }
- P1 = 0xFF;
- P1_2 = 0;
- if (P1_7 == 0) { delay(20); while (P1_7 == 0); delay(20); KeyNumber = 2; }
- if (P1_6 == 0) { delay(20); while (P1_6 == 0); delay(20); KeyNumber = 6; }
- if (P1_5 == 0) { delay(20); while (P1_5 == 0); delay(20); KeyNumber = 10; }
- if (P1_4 == 0) { delay(20); while (P1_4 == 0); delay(20); KeyNumber = 14; }
- P1 = 0xFF;
- P1_1 = 0;
- if (P1_7 == 0) { delay(20); while (P1_7 == 0); delay(20); KeyNumber = 3; }
- if (P1_6 == 0) { delay(20); while (P1_6 == 0); delay(20); KeyNumber = 7; }
- if (P1_5 == 0) { delay(20); while (P1_5 == 0); delay(20); KeyNumber = 11; }
- if (P1_4 == 0) { delay(20); while (P1_4 == 0); delay(20); KeyNumber = 15; }
- P1 = 0xFF;
- P1_0 = 0;
- if (P1_7 == 0) { delay(20); while (P1_7 == 0); delay(20); KeyNumber = 4; }
- if (P1_6 == 0) { delay(20); while (P1_6 == 0); delay(20); KeyNumber = 8; }
- if (P1_5 == 0) { delay(20); while (P1_5 == 0); delay(20); KeyNumber = 12; }
- if (P1_4 == 0) { delay(20); while (P1_4 == 0); delay(20); KeyNumber = 16; }
- return KeyNumber;
- }
- void main() {
- while (1) {
- KeyScan();
- DigDisplay();
- }
- }
复制代码
|