有没有大佬懂一些单片机的帮忙改下代码
从黑体的地方开始,我想通过矩阵键盘控制数码管显示0-15,但是不知道怎么搞两位数的显示,是不是延时什么的有问题#include <REGX52.H>
sfr P4 = 0xC0;
unsigned char code duanma={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc0,0x86,0x8e,0xbf,0x7f};
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C1 = P3^4;
sbit C2 = P3^5;
sbit C3 = P4^2;
sbit C4 = P4^4;
void HC573(unsigned char one)
{
switch(one)
{
case 4:
P2 = (P2 & 0x1f) | 0X80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0X1f) | 0Xc0;
break;
case 7:
P2 = (P2 & 0X1f) | 0Xe0;
break;
}
}
void Delay(unsigned char t)
{
while(t--);
}
void SHUMA(unsigned char two, unsigned char thr )
{
HC573(6);
P0 = 0x01 << thr;
HC573(7);
P0 = two;
}
unsigned char num = 0, yum = 0;
void Juzhen()
{
C1 = 0; C2 = C3 = C4 = 1;
R1 = R2 = R3 = R4 = 1;
if(R1 == 0)
{
while(R1 == 0);
num =0;
SHUMA(duanma,1);
}
else if(R2 == 0)
{
while(R2 == 0);
num = 1 ;
SHUMA(duanma,1);
}
else if(R3 == 0)
{
while(R3 == 0);
num =2;
SHUMA(duanma,1);
}
else if(R4 == 0)
{
while(R4 == 0);
num =3;
SHUMA(duanma,1);
}
C2 = 0; C1 = C3 = C4 = 1;
R1 = R2 = R3 = R4 = 1;
if(R1 == 0)
{
while(R1 == 0);
num =4;
SHUMA(duanma,1);
}
else if(R2 == 0)
{
while(R2 == 0);
num =5;
SHUMA(duanma,1);
}
else if(R3 == 0)
{
while(R3 == 0);
num =6;
SHUMA(duanma,1);
}
else if(R4 == 0)
{
while(R4 == 0);
num =7;
SHUMA(duanma,1);
}
C3 = 0; C2 = C1 = C4 = 1;
R1 = R2 = R3 = R4 = 1;
if(R1 == 0)
{
while(R1 == 0);
num =8;
SHUMA(duanma,1);
}
else if(R2 == 0)
{
while(R2 == 0);
num =9;
SHUMA(duanma,1);
}
else if(R3 == 0)
{
while(R3 == 0);
num = 0 , yum = 1;
SHUMA(duanma,1);
Delay(100);
SHUMA(duanma,0);
Delay(100);
}
else if(R4 == 0)
{
while(R4 == 0);
num = 1,yum = 1 ;
SHUMA(duanma,1);
Delay(100);
SHUMA(duanma,0);
while(R4 == 0);
}
C4 = 0; C2 = C3 = C1 = 1;
R1 = R2 = R3 = R4 = 1;
if(R1 == 0)
{
while(R1 == 0);
num = 2,yum = 1 ;
SHUMA(duanma,1);
Delay(500);
SHUMA(duanma,0);
}
else if(R2 == 0)
{
while(R2 == 0);
num = 3,yum = 1 ;
SHUMA(duanma,1);
Delay(500);
SHUMA(duanma,0);
}
else if(R3 == 0)
{
while(R3 == 0);
num = 4,yum = 1 ;
SHUMA(duanma,1);
Delay(500);
SHUMA(duanma,0);
}
else if(R4 == 0)
{
while(R4 == 0);
num = 5 ,yum = 1;
SHUMA(duanma,1);
Delay(500);
SHUMA(duanma,0);
}
}
void main()
{
while(1)
{
Juzhen();
}
} ? zhangjinxuan 发表于 2022-11-26 15:26
?
{:10_277:} 前排学习~ 学习,等待大佬~ 这我们也没有对应的硬件,没办法帮你调试程序
你都学到单片机了,还没有学会调试程序吗?
先停一停吧,先学调试程序吧,不然后面没法学了
后面基本上都要靠你自己了,我们帮不了你
他可能是没有仿真器吧,好像是也可以用仿真软件的,楼主可以试试 猜一下,
我看你这个代码好像不是直接用管脚直接驱动,
HC573是不是一个有缓存的驱动芯片啊,那样的话要显示两位数字,要把两个数字都存进去再显示,而不是先显示十位再显示个位
还有就是delay(100)这个是delay了100个指令周期,非常快,这点要注意,时间够不够要查看时序,如果前面显示1位数字正常,这个delay好像也没什么用处
还有一个 while(R4 == 0);这个东西是不能按键除颤的,你的输入会很不稳定,可能按了一次5,出了5555
人造人 发表于 2022-11-26 16:43
这我们也没有对应的硬件,没办法帮你调试程序
你都学到单片机了,还没有学会调试程序吗?
先停一停吧,先 ...
{:10_254:}好 两手空空儿 发表于 2022-11-26 17:55
猜一下,
我看你这个代码好像不是直接用管脚直接驱动,
HC573是不是一个有缓存的驱动芯片啊,那样的话要 ...
我给这两个位置套了个循环就能显示了,可能还是程序设计有问题{:10_250:}谢谢啦 学习一下
页:
[1]