c#实现51单片机频率计的代码分享(数字频率计设计)
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//按键
sbit button=p1^7;
//led
sbit led2=p3^5;
sbit led3=p3^7;
//数码管位选
sbit c0 = p3^0;
sbit c1 = p3^1;
sbit c2 = p3^2;
sbit c3 = p3^3;
//数码管段码
uchar code dm[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar wm[]={0,0,0,0};
ulong pl;
uchar mode;
void delay3ms(void) //误差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=176;b>0;b--)
for(a=7;a>0;a--);
}
void smg()
{
static uchar i=0;
c0=1;c1=1;c2=1;c3=1;
p1 = 0xff;
switch(i)
{
case 0: c0=0;
break;
case 1: c1=0;
break;
case 2: c2=0;
break;
case 3: c3=0;
break;
}
p1 = dm[wm[i]];
i++;
if(i>3)
{
i=0;
}
}
void ledmode()
{
switch(mode)
{
case 0:led2=led3=0;
break;
case 1:led2=1;led3=0;
break;
case 2:led2=0;led3=1;
break;
case 3:led2=led3=1;
break;
}
}
void key()
{
static uchar num;
delay3ms();
button=1;
switch(num)
{
case 0: if(!button)
{
num=1;
}
break;
case 1: if(!button)
{
if(mode<3)
{
mode++;
}
else
{
mode=0;
}
num=2;
}
else
{
num=0;
}
break;
case 2: if(button)num=3;
break;
case 3: if(button)num=0;
break;
}
}
void timeinit()
{
tmod=0x15; //设置t0t1控制字
th0=0x00;
tl0=0x00;
th1=0xfc;
tl1=0x18;
et0=1; //定时器0中断允许
et1=1;
tr0=1; //启动t0计数
tr1=1; //并启动t1计数
ea=1; //cpu开中断
}
void main()
{
timeinit();
while(1)
{
smg();
ledmode();
key();
}
}
void t1()interrupt 3
{
static uint time;
tr0=0;
th1=(65536-10000)/256;
tl1=(65536-10000)%256;
time++;
if(time>99)
{
time=0;
pl=th0*256+tl0;
wm[0]=(pl/1000)%10;
wm[1]=(pl/100)%10;
wm[2]=(pl/10)%10;
wm[3]=pl%10;
th0=tl0=0;
}
tr0=1;
}
上一篇: 微信支付开发交易通知实例