制作一个0-99的计数器,P1.7接按键,P2接数码管段,共阳极数码管,P3.0,P3.1接数码管位码,每按一次键,数码管显示加一。请写出单片机的C51代码
#include "reg51.h"
#include "stdio.h"
sbit in1 = P1^7;
sbit lse1 = P3^0;
sbit lse2 = P3^1;
typedef unsigned int u16;
unsigned char code SMG_duanma[11] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
void delay(u16 i)
{
while(i--);
}
void xianshi(int a,int b,int o)
{ P2=0xff;
lse1 =0;
lse2=1;
P2=SMG_duanma[a];
delay(1000);
P2=0xff;
if(o==1)
{
lse1 = 1;
lse2 = 0;
P2=SMG_duanma[b];
delay(1000);
}
P2=0xff;
}
void main()
{
int i=0;
int b=0;
int c=0;
while(1)
{
if(in1==0)
{
delay(100);
if(in1==0)
{
while(in1==0);
i=i+1;
while(in1!=0)
{
if(i<10)xianshi(i,0,0);
if(i>=10)xianshi(i%10,i/10,1);
if(in1==0)break;
}
P2=0xff;
}
}
}
}
如果是梅老师学生要把函数名字改一下
|