|
楼主 |
发表于 2011-3-28 14:26:34
|
显示全部楼层
引用第11楼管兴春于2011-03-26 22:22发表的:
程序和原理图能共享吗 这个是电路图:
程序的大致思路是在两个脉冲间查数,然后用一个基数去除这个数就是转数。
程序:
#include <reg51.h>
#define RLDH 0xF6
#define RLDL 0x18
unsigned long isum=600000;
sbit P3_g=P3^1;
sbit P3_s=P3^0;
sbit P3_b=P3^5;
sbit P3_q=P3^7;
void ledinit(void);
void ledwait(int dltm);
void ledout(void);
void getspeed(void);
unsigned int dit=38888;
unsigned int dtm=888;
unsigned int led[4];
unsigned char lednum;
unsigned int i,sum,n;
unsigned int out=0,ot1=1,ot2=2;
char code ledt[]={0xC0,0xCF,0xA2,0x86,0x8D,0x94,0x90,0xCE,0x80,0x84,0xFF,0xB0};
void main(void)
{
sum=0;
led[0]=0;
ledinit();
while(1)
{
ledout();
}
}
void ledinit(void)
{
// 检查数码管是否正常工作
lednum=ledt[8];
P3_g=1; P3_s=1; P3_b=1; P3_q=0;
ledwait(dit);
P3_g=1; P3_s=1; P3_b=0; P3_q=1;
ledwait(dit);
P3_g=1; P3_s=0; P3_b=1; P3_q=1;
ledwait(dit);
P3_g=0; P3_s=1; P3_b=1; P3_q=1;
ledwait(dit);
P3_g=0; P3_s=0; P3_b=0; P3_q=0;
ledwait(dit);
P3_g=1; P3_s=1; P3_b=1; P3_q=1;
IE=0x03;
TMOD=0x00;
TCON=0x01;
TH0=RLDH;
TL0=RLDL;
EA=1;
}
void ledwait(int dltm)
{
for (i=0;i<dltm;i++)
P1=lednum;
}
void ledout(void)
{
P3_q=1;
P3_g=0;
lednum=ledt[led[0]];
ledwait(dtm);
P3_g=1;
P3_s=0;
lednum=ledt[led[1]];
ledwait(dtm);
P3_s=1;
P3_b=0;
lednum=ledt[led[2]];
ledwait(dtm);
P3_b=1;
P3_q=0;
lednum=ledt[led[3]];
ledwait(dtm);
}
void getspeed(void)
{
if(sum>90&&sum<1100)
{
ot2=2500/sum+1;
if(ot2<2)ot2=2;
ot1=ot2-1;
n=isum/sum;
n=n/10;
led[1]=n%10;
n=n/10;
led[2]=n%10;
led[3]=n/10;
if(led[3]==0)led[3]=10;
}
}
void signal_in(void) interrupt 0
{
TR0=0;
out++;
if(out>=ot2)
{
getspeed();
sum=0;
out=0;
}
else if(out>=ot1)
{
TR0=1;
}
}
void system_tick(void) interrupt 1
{
TH0=RLDH;
TL0=RLDL;
sum++;
} |
|