问题描述:我想通过一个按键控制数码管显示9600 4800 2400 ,现在时显示到2400后,再按一次会有显示一个单个0 才显示9600.#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uint num=0;
sbit k1=P3^7;
sbit led=P3^6;
uchar duan[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay (uint z);
void display (uint dat);
void key ();
main()
{
while(1)
{
key();
if(num==1)
{
display(9600);
}
if(num==2)
{
display(4800);
}
if(num==3)
{
display(2400);
//P1=0X00;
}
}
}
void display (uint dat )
{
uchar qian,bai,shi,ge;
qian=dat/1000; //得到千位数
bai=dat%1000/100; //得到百位数
shi=dat%100/10; //得到十位数
ge=dat%10; //得到个位
P2=0xFF;
P1=0X01;
P2=duan[qian];
delay(1);
P2=0xFF;
P1=0X02;
P2=duan[bai];
delay(1);
P2=0xFF;
P1=0X04;
P2=duan[shi];
delay(1);
P2=0xFF;
P1=0X08;
P2=duan[ge];
delay(1);
}
void delay (uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=200;y>0;y--);
}
void key ()
{
if(k1==0)
delay(10);
if(k1==0)
{
while(!k1);
num=num+1;
if(num>3)
num=0;
}
}
|