#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar shu=0,count=0;
uchar qian,bai,shi,ge;
long f;
uchar code dutable[]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
void display(uchar qian,uchar bai,uchar shi,uchar ge);
void int0() interrupt 0
{
TR1=1;
shu++;
if(shu==16)//计15
shu=0;
TR1=0;
EX0=0;
}
void t0() interrupt 3
{
count++;
}
void init()
{
TMOD=0x10;
TH1=0;
TL1=0;
IE=1;
ET1=1;
EX0=1;
IT1=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
init();
while(1)
{
f=(count*65536+TH1*256+TL1)/15;
f=1000000/f;
qian=f/1000;
bai=f%1000/100;
shi=f%100/10;
ge=f%10;
display(qian,bai,shi,ge);
count=0;
TH1=0;
TL1=0;
TR1=1;
EX0=1;
}
}
void display(uchar qian,uchar bai,uchar shi,uchar ge)
{
P2=0x01;
P0=dutable[qian];
delay(5);
P2=0x02;
P0=dutable[bai];
delay(5);
P2=0x04;
P0=dutable[shi];
delay(5);
P2=0x08;
P0=dutable[ge];
delay(5);
}
写的是低频率计,加信号时没反应.如果要两位小数的,如何改??
此帖出自
小平头技术问答
void int0() interrupt 0
{
TR1=1;
shu++;
if(shu==16)//计15
shu=0;
TR1=0;
EX0=0;
}
//这里,至少写成t1吧,我一般习惯写成TimerX
//但关系不大,只是0 和 1 总要分清楚吧,差点混淆了,幸好有个interrupt 3
void t0() interrupt 3
{
count++;
}
//额滴神!!
//兄弟,你拼命刷掉TH1 TL1,它能溢出才怪!
while(1)
{
f=(count*65536+TH1*256+TL1)/15;
f=1000000/f;
qian=f/1000;
bai=f%1000/100;
shi=f%100/10;
ge=f%10;
display(qian,bai,shi,ge);
count=0;
TH1=0;
TL1=0;
TR1=1;
EX0=1;
}
“写的是低频率计,加信号时没反应.如果要两位小数的,如何改??”
你先把 反应 弄出来再说......
void int0() interrupt 0
{
TR1=1;
shu++;
if(shu==16)//计15
shu=0;
TR1=0;
EX0=0;
}
void int0() interrupt 0
{
TH0=0;重装
TL0=0;
TR1=1;
shu++;
if(shu==16)//计15
shu=0;
TR1=0;
EX0=0;
}
while(1)
{
f=(count*65536+TH1*256+TL1)/15;
f=1000000/f;
qian=f/1000;
bai=f%1000/100;
shi=f%100/10;
ge=f%10;
display(qian,bai,shi,ge);
count=0;
TH1=0;/去掉
TL1=0;/去掉
TR1=1;
EX0=1;
}
自己试了之后,没加信号数码管就乱显.
说实话,没怎么看明白你的程序,首先当我看到你的main的时候,似乎EA=1你都不写!!我感觉有点蛋疼!那我看着你的程序写写吧,不知道行不行!对了,顺便说一下,我用的是T0和T1! 我没有仿真,不知道对不对,你可以试试,然后告诉我!至于那个小数点的,回头再说吧
我又重新看了一下下,似乎有点不对。。。。你再试试这个
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar shu=0,count=0;
//sfr16 DPTR=0X82;//这个是用来存放定时器0中TH0,TL0中的值
uchar qian,bai,shi,ge;
long f;
uchar code dutable[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
[ 本帖最后由 _Justin 于 2011-10-9 10:53 编辑 ]{
IE=0X8A;
TMOD=0X15;
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
TR1=1;
TH0=0;
TL0=0;
TR0=1;
}
void display(uchar qian,uchar bai, uchar shi,uchar ge)
{
P2=0X01;
P0=dutable[qian];
delay(5);
P2=0X02;
P0=dutable[bai];
delay(5);
P2=0X04;
P0=dutable[shi];
delay(5);
P2=0X08;
P0=dutable[ge];
delay(5);
}
void main()
{
while(1)
{
init();
if(shu==20)
{
shu=0;
TR1=0;
// delay(10); //这里是延时,矫正一下下误差,这种计数器准确率并不高!
TR0=0;
// DPL=TL0;
// DPH=TH0;
// f=DPTR;
f=TH0*256+TL0;
qian=f/1000;
bai=f%1000/100;
shi=f%100/10;
ge=f%10;
}
display(qian,bai,shi,ge);
}
}
void timer0() interrupt 1
{
count++;
}
void timer1() interrupt 3
{
TH1=(65535-50000)/256;
TL1=(65535-50000)%256;
shu++;
}
写好了!
不知道对呢,对呢,对呢,还是对呢!
要是不对!
再说吧!
好吧!
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar qian,bai,shi,ge;
long f;
bit flag;
uint count;
sbit fin=P1^3;
sbit fout=P1^7; //用导线将P1_7脚(发生频率引脚)的频率输出连接P1_3脚(测量频率管脚)
uchar code dutable[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar qian,uchar bai,uchar shi,uchar ge)
{
P2=0X01;
P0=dutable[qian];
delay(5);
P2=0X02;
P0=dutable[bai];
delay(5);
P2=0X04;
P0=dutable[shi];
delay(5);
P2=0X08;
P0=dutable[ge];
delay(5);
}
void main()
{
uint temp;
EA=1;
IT0=1;
TMOD=0X11;
TH1=(65535-500)/256;
TL1=(65535-500)%256;
ET0=1;
ET1=1;
TR1=1;
while(1)
{
bit fin_before=0;
bit fin_now=0;
uchar flag=0;
// uint count;
// TH0=0;
// TL0=0;
while(1)
{
fin_now=fin;
if(fin_before&&!fin_now)
{
flag++;
EX0=1;
}
if(flag==2)
{
EX0=0;
f=1000000/count;
return;
}
fin_before=fin_now;
}
qian=f/1000;
bai=f%1000/100;
shi=f%100/10;
ge%10;
display(qian,bai,shi,ge);
}
}
void timer0() interrupt 0
{
count++;
}
void timer1() interrupt 3
{
TH1=(65535-1000)/256;
TL1=(65535-1000)%256;
}
一周热门 更多>