写代码:51单片机用矩阵按键可以输入1~9,可以用矩阵按键随便输入一个两位数,最后输入的数乘以1.1用0.96寸OLED显示屏显示出来
时间: 2024-06-12 16:06:16 浏览: 100
抱歉,我是语言模型AI,无法直接操作硬件和编写代码。以下是参考代码:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit ROW1 = P1^0;
sbit ROW2 = P1^1;
sbit ROW3 = P1^2;
sbit COL1 = P1^3;
sbit COL2 = P1^4;
sbit COL3 = P1^5;
uchar code KEY_TBL[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
uchar key_scan(void)
{
uchar i, j;
COL1 = 0;
COL2 = 1;
COL3 = 1;
_nop_();
_nop_();
_nop_();
if (ROW1 == 0) {
i = 0;
j = 0;
while (!ROW1);
}
else if (ROW2 == 0) {
i = 1;
j = 0;
while (!ROW2);
}
else if (ROW3 == 0) {
i = 2;
j = 0;
while (!ROW3);
}
else {
COL1 = 1;
COL2 = 0;
COL3 = 1;
_nop_();
_nop_();
_nop_();
if (ROW1 == 0) {
i = 0;
j = 1;
while (!ROW1);
}
else if (ROW2 == 0) {
i = 1;
j = 1;
while (!ROW2);
}
else if (ROW3 == 0) {
i = 2;
j = 1;
while (!ROW3);
}
else {
COL1 = 1;
COL2 = 1;
COL3 = 0;
_nop_();
_nop_();
_nop_();
if (ROW1 == 0) {
i = 0;
j = 2;
while (!ROW1);
}
else if (ROW2 == 0) {
i = 1;
j = 2;
while (!ROW2);
}
else if (ROW3 == 0) {
i = 2;
j = 2;
while (!ROW3);
}
else {
return 0xFF;
}
}
}
return KEY_TBL[i][j];
}
void delay(uint t)
{
while (t--);
}
void write_cmd(uchar cmd)
{
uchar i;
for (i = 0; i < 8; i++) {
P2 &= ~0x02;
P2 &= ~0x04;
if (cmd & 0x80) {
P2 |= 0x01;
}
else {
P2 &= ~0x01;
}
P2 |= 0x04;
P2 &= ~0x04;
cmd <<= 1;
}
}
void write_dat(uchar dat)
{
uchar i;
for (i = 0; i < 8; i++) {
P2 |= 0x02;
P2 &= ~0x04;
if (dat & 0x80) {
P2 |= 0x01;
}
else {
P2 &= ~0x01;
}
P2 |= 0x04;
P2 &= ~0x04;
dat <<= 1;
}
}
void init_oled(void)
{
write_cmd(0xAE);
write_cmd(0x00);
write_cmd(0x10);
write_cmd(0x40);
write_cmd(0xB0);
write_cmd(0x81);
write_cmd(0xFF);
write_cmd(0xA1);
write_cmd(0xA6);
write_cmd(0xA8);
write_cmd(0x3F);
write_cmd(0xC8);
write_cmd(0xD3);
write_cmd(0x00);
write_cmd(0xD5);
write_cmd(0x80);
write_cmd(0xD9);
write_cmd(0xF1);
write_cmd(0xDA);
write_cmd(0x12);
write_cmd(0xDB);
write_cmd(0x40);
write_cmd(0x20);
write_cmd(0x02);
write_cmd(0x8D);
write_cmd(0x14);
write_cmd(0xA4);
write_cmd(0xAF);
}
void show_num(uint num)
{
uchar buf[6] = {0};
uchar i, j;
for (i = 0; i < 6; i++) {
buf[i] = num % 10 + '0';
num /= 10;
if (num == 0) {
break;
}
}
for (j = i; j > 0; j--) {
write_dat(buf[j]);
}
write_dat(buf[0]);
}
void main()
{
uchar key, num[2] = {0};
uint value;
init_oled();
while (1) {
key = key_scan();
if (key >= '1' && key <= '9') {
if (num[0] == 0) {
num[0] = key;
}
else if (num[1] == 0) {
num[1] = key;
value = (num[0] - '0') * 10 + (num[1] - '0');
value = value * 11 / 10;
show_num(value);
}
}
delay(5000);
}
}
阅读全文