用89c52实现测量频率在10-1000Hz的方波,并用四位数码管显示,C语言代码实现
时间: 2024-05-07 21:19:42 浏览: 15
以下是基于89c52单片机的10-1000Hz方波测量和四位数码管显示的C语言代码实现:
```c
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit CLK=P3^5; //定义时钟引脚
sbit DIO=P3^4; //定义数据引脚
sbit LATCH=P3^6; //定义锁存引脚
uchar code DIG[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}; //数码管显示0-9, A-F, 空格的编码
void delay(uint i) //延时函数
{
uint j;
for(j=0;j<i;j++);
}
void Display(uchar dat) //数码管显示函数
{
DIO=1;
LATCH=0;
LATCH=1;
DIO=0;
DIO=1;
LATCH=0;
LATCH=1;
P1=DIG[dat];
}
void main()
{
uchar counter=0;
uint freq=0;
TMOD=0x01; //设置计时器0为16位定时器模式
TH0=(65536-1000)/256; //计时器初值
TL0=(65536-1000)%256;
ET0=1; //开启定时器0中断
EA=1; //开启总中断
TR0=1; //启动计时器0
while(1)
{
Display(counter); //数码管显示计数值
delay(100);
if(freq>=10 && freq<=1000) //测量频率在10-1000Hz之间
{
P2=0xff; //关闭蜂鸣器
}
else
{
P2=0xfe; //打开蜂鸣器
}
}
}
void Timer0() interrupt 1 //定时器0中断函数
{
TH0=(65536-1000)/256; //重新赋初值
TL0=(65536-1000)%256;
counter++; //计数器加1
if(counter==100) //每隔100个计数值进行一次频率测量
{
counter=0;
freq=65536*(TH1*256+TL1)/12; //计算频率
TH1=0; //清零计数器
TL1=0;
}
}
```
注:以上代码中使用了定时器0中断函数进行计数器的自增和定时器的重置,每隔100次计数进行一次频率测量,并且通过蜂鸣器进行频率异常的提示。同时,通过数码管显示计数器的计数值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)