标题:
想要搞个简单单片机频率计 中断1秒计算频率,请帮忙看看
[打印本页]
作者:
wjx111123
时间:
2021-6-25 10:31
标题:
想要搞个简单单片机频率计 中断1秒计算频率,请帮忙看看
#include"reg51.h"
#include"lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
uchar count=0,valueH=0,valueL=0;
uchar str[]={"0123456789"};
uchar str1[]={"FREQUENCE:"};
uint fre=0;
uint num;
void initex0()
{
IT0=1;
EX0=1;
EA=1;
}
void inittimer0()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
}
void T0_time()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
if(num==20)
{}
}
void datadeal()
{
uint value=0;
value=valueH*256+valueL;
fre=10000000/value;
}
void display()
{
uchar temp0=0,temp1=0,temp2=0,temp3=0,temp4=0,temp5=0;
uchar i=0;
temp0=(fre%1000000)/100000;
temp1=(fre%100000)/10000;
temp2=(fre%10000)/1000;
temp3=(fre%1000)/100;
temp4=(fre%100)/10;
temp5=fre%10;
writecom(0x80);
delay(1);
while(str1[i]!='\0')
{
writedat(str1[i]);
delay(1);
i++;
}
writecom(0x80+0x40+5);
delay(1);
writedat(str[temp0]);
delay(1);
writedat(str[temp1]);
delay(1);
writedat(str[temp2]);
delay(1);
writedat(str[temp3]);
delay(1);
writedat(str[temp4]);
delay(1);
writedat('.');
delay(1);
writedat(str[temp5]);
delay(1);
writedat('H');
delay(1);
writedat('Z');
delay(1);
}
void main()
{
initex0();
inittimer0();
initlcd();
while(1)
{
datadeal();
display();
}
}
void ex0_isr() interrupt 0
{
if(count==0)
{
TH0=0x00;
TL0=0x00;
TR0=1;
}
else if(count==1)
{
TR0=0;
valueH=TH0;
valueL=TL0;
count=0;
}
count++;
}
复制代码
作者:
xuyaqi
时间:
2021-6-25 11:28
还是一步步来,你的外中断计数就有问题。
作者:
yzwzfyz
时间:
2021-6-25 13:44
本帖最后由 yzwzfyz 于 2021-6-26 10:03 编辑
计数要做如下三件事,你是否同意?
1、何时开始?
2、何时结束?
3、谁来数数?
这三条你是如何做的呢?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1