单片机程序修改,实现按键控制数码管0-9的复位和暂停

下面是我写的程序,数码管已经成功实现0-9循环变化,我该怎么加一个按键,从而实验,第一次按按键,实现暂停,第二次按按键,实现复位呢
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar aa,num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=table[0];
while(1)
{

if(aa==20)
{
aa=0;
num++;
if(num==10)
num=0;
P2=table[num];
}
}
}

void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}
帮忙修改程序的朋友最好附上你添加按键的仿真图,帮忙解决加送200分

第1个回答  2011-06-21
仿真图就不上了,按键加在P3.0
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit KEY=P3^0;

uchar aa,num,flag;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delay(uint ms)
{
uchar t;
while(ms--)
for(t=0;t<123;t++);
}
void main()
{
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=table[0];
while(1)
{
while(!KEY)
{
delay(10);
if(KEY)
{
flag++;
if(flag==1)
TR0=0;
if(flag==2)
{
P2=0x3f;
flag=0;
}
}
}
if(aa==20)
{
aa=0;
num++;
if(num==10)
num=0;
P2=table[num];
}
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}追问

不给力啊,因为复位的时候不会继续走动了,可以再修改下让复位后自动继续走动吗

追答

你又没说复位后要走^_^\
#include
#include
#define uint unsigned int
#define uchar unsigned char
sbit KEY=P3^0;

uchar aa,num,flag;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delay(uint ms)
{
uchar t;
while(ms--)
for(t=0;t<123;t++);
}
void main()
{
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=table[0];
while(1)
{
while(!KEY)
{
delay(10);
if(KEY)
{
flag++;
{
if(flag==1)
TR0=0;
if(flag==2)
{
TR0=1;
P2=0x3f;
aa=0;
num=0;
flag=0;
TR0=1;
}
}
}
}

if(aa==20)
{
aa=0;
num++;
if(num==10)
num=0;
P2=table[num];
}
}
}

void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}

追问

郁闷,你可以再帮帮我吗,那个要求居然改了http://zhidao.baidu.com/question/282403010.html

追答

帮你改了。

本回答被提问者采纳
第2个回答  2011-06-21
这个按键链接到P3.2,程序如下
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar aa,num,bb,cc;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
num=0;
aa=0;
bb=0;
cc=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=table[0];
while(1)
{
if(bb=1)
{
bb=1;
aa=19;
cc=num;
P2=table[cc];
}
if(bb=2)
{
bb=0;
num=0;
P2=table[num];
}

if(aa==20)
{
aa=0;
num++;
if(num==10)
num=0;
P2=table[num];
}
}
}

void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}
void extern0_ISR() interrupt 0
{
bb++;
if(bb>2);
bb=0;
}
第3个回答  2011-06-21
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit KEY=P3^0;

uchar aa,num,flag;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delay(uint ms)
{
uchar t;
while(ms--)
for(t=0;t<123;t++);
}
void main()
{
num=0;
aa=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P2=table[0];
while(1)
{
while(!KEY)
{
delay(10);
if(KEY)
{
flag++;
{
if(flag==1)
TR0=0;
if(flag==2)
{
TR0=1;
P2=0x3f;
aa=0;
num=0;
flag=0;
TR0=1;
}
}
}
}

if(aa==20)
{
aa=0;
num++;
if(num==10)
num=0;
P2=table[num];
}
}
}

void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}
第4个回答  2011-06-22
解决没有?
第5个回答  2011-06-21
已经PM你
相似回答