一个按键控制数码管的程序,有点小问题,求帮助?
问题:按键按下后,就直接停留在当前数值,不会复位。
程序:
[code]<div>#include <reg52.h></div><div>#define uchar unsigned char</div><div>#define uint unsigned int</div><div>
</div><div>uint number;</div><div>uchar flag_50ms, bai, shi, ge, us, flag;</div><div>uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};</div><div>
</div><div>sbit segment = P2^7;</div><div>sbit bitcode = P2^6;</div><div>sbit key = P2^4;</div><div>
</div><div>void delay(uchar us);//延时约为500us</div><div>void key_scan();</div><div>
</div><div>void init()</div><div>{</div><div> TMOD = 0x11;</div><div> number = 0;</div><div> EA = 1;</div><div> ET0 = 1;</div><div><span class="Apple-tab-span" style="white-space:pre">
98. if(1 == flag)
99. {
......
106. }
在右话括号前加
display(bai,shi,ge);
程序:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uint number;
- uchar flag_50ms, bai, shi, ge, us, flag;
- uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- sbit segment = P2^7;
- sbit bitcode = P2^6;
- sbit key = P2^4;
- void delay(uchar us);//延时约为500us
- void key_scan();
- void init()
- {
- TMOD = 0x11;
- number = 0;
- EA = 1;
- ET0 = 1;
- flag = 0;
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- TR0 = 1;
- }
- void delay(uchar us)
- {
- uchar x, y;
- for(x=0;x<us;x++)
- for(y=0;y<110;y++);
- }
- void key_scan()
- {
- if(0 == key)
- {
- delay(10);
- if(0 == key)
- {
- while(!key);
- flag = 1;
- bai = 0;
- shi = 0;
- ge = 0;
- }
- }
- }
- void display(uchar bai, uchar shi, uchar ge)
- {
- bai = number/100;
- shi = number%100/10;
- ge = number%10;
- bitcode = 1;
- P0 = 0xdf;
- bitcode = 0;
- P0 = 0xff;
- segment = 1;
- P0 = table[bai];
- segment = 0;
- P0 = 0xff;
- delay(10);
- bitcode = 1;
- P0 = 0xbf;
- bitcode = 0;
- P0 = 0xff;
- segment = 1;
- P0 = table[shi];
- segment = 0;
- P0 = 0xff;
- delay(10);
- bitcode = 1;
- P0 = 0x7f;
- bitcode = 0;
- P0 = 0xff;
- segment = 1;
- P0 = table[ge];
- segment = 0;
- P0 = 0xff;
- delay(10);
- }
- int main(void)
- {
- init();
- while(1)
- {
- key_scan();
- if(1 == flag)
- {
- TR0 = 0;
- bai = 0;
- shi = 0;
- ge = 0;
- flag_50ms = 0;
- flag = 0;
- }
- else if(20 == flag_50ms)
- {
- if(number == 1000)
- {
- number = 0;
- }
- display(bai,shi,ge);
- flag_50ms = 0;
- number++;
- }
- else
- display(bai,shi,ge);
- }
- return 0;
- }
- void Timer0() interrupt 1
- {
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- flag_50ms++;
- }
你好,谢谢你的回答。但问题的是出现在变量number上,因为bai,shi,ge是从nungber中的出来的所以如果number没有清零,显示时就会有问题。
你好,谢谢你的回答。但问题的是出现在变量number上,因为bai,shi,ge是从nungber中的出来的所以如果number没有清零,显示时就会有问题。
根据你的电路板写了这个程序,应该能用。功能上做了一点修改:开机后显示000,按下开关秒表启动,再按下开关停止计时,再按开关清零,如此循环。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit bitcode=P2^6;
sbit segment=P2^7;
sbit key =P2^4;
uint number;
uchar a; //用来计数按键按下的次数,为每一次设定不同的功能
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //有这10个数就够了,不用把A,B,C,D,E,F 也写在里面
void delay(uchar us)
{
uchar x,y;
for(x=0;x<us;x++)
for(y=0;y<110;y++);
}
void key_scan()
{
if(key==0)
{
delay(10);
if(key==0)
{
a++;
while(!key);
if(a==3)
{
a=0;
}
}
}
switch(a) //取上面的a值,给每次按下按键提供不同功能,由下面部分执行各自任务
{
case 0:
TR0=0;
number=0;
break;
case 1:
TR0=1;
break;
case 2:
TR0=0;
break;
}
}
void display()
{
uchar bai,shi,ge;
bai=number/100;
shi=number%100/10;
ge =number%10;
bitcode=1;
P0=0xdf;
bitcode=0;
segment=1;
P0=table[bai];
segment=0;
delay(5);
bitcode=1;
P0=0xbf;
bitcode=0;
segment=1;
P0=table[shi];
segment=0;
delay(5);
bitcode=1;
P0=0x7f;
bitcode=0;
segment=1;
P0=table[ge];
segment=0;
delay(5);
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
number=0;
while(1)
{
key_scan();
display();
}
}
void time0()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
number++;
if(number==1000)
{
number=0;
}
}
真服了你了,你的display(bai,shi,ge)函数中的bai,shi,ge居然是给display作为其局部变量使用;
sorry,我没有逐句看你的代码;
因为你对 bai,shi,ge 都进行了赋0操作,所以我就认为你的现实是用 bai,shi,ge 来显示的,所以也就没有继续往下看;
你说的是对的,但是这样的话,你对 bai,shi,ge 的赋0操作就没有意义了,display直接就可以设计成display(),不用参数。最后98-106行之间也还是要安排一个display调用。
它们本来就只在display()内使用,为什么还要设成全局变量呢?全局变量number已经把计算好的数值送入display()内了,这个程序在我的测试板上运行良好,工作正常。
你的程序风格有点像是从郭天祥老师的教材里学的,不知道是不是?我喜欢这样的程序,好懂。
时许逻辑问题,慢慢想吧哈哈!