Arduino与Proteus仿真实例-简单信号频率计数仿真

简单信号频率计数仿真

Arduino 可用于测量电量(如电压、电流、功率等)或物理量(如温度、湿度、光强、湿度等)或电子元件值等。

本文将演示演示了如何使用 Arduino 测量脉冲的频率和占空比。

在通信领域,频率测量至多是必不可少的。占空比也是一个重要的测量参数,因为它给出了脉冲宽度的百分比——意味着脉冲的开启时间。在直流电机速度控制和伺服电机角度控制中,需要测量脉冲宽度。此外,还测量了脉冲宽度以检查某些应用(如数字信号接收器、中继器等)中的脉冲对称性。

本文通过Arduino实现简单测量脉冲的频率、开启时间、关闭时间和占空比,并将它们显示在 16x4 LCD 上。

在前面的文章中,我们介绍了如何驱动LCD1602,请参考:

1、仿真电路原理图

在这里插入图片描述

在仿真电路中,通过PCF8574驱动LCD1604,信号发生器信号输出端连接引脚7

2、仿真代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <Wire.h>
#include <LiquidCrystal\_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // PCF8574的通讯地址为0x27
#define pulse\_ip 7
long ontime,offtime,duty;
float freq,period;


void setup()
{
// Serial.begin(9600);
pinMode(pulse_ip,INPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Frequency Meter");
delay(1000);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Freq:");
lcd.setCursor(0,1);
lcd.print("Ton:");
lcd.setCursor(0,2);
lcd.print("Toff:");
lcd.setCursor(0,3);
lcd.print("Duty:");
}
void loop()
{
ontime = pulseIn(pulse_ip,HIGH);
offtime = pulseIn(pulse_ip,LOW);
period = ontime+offtime;
freq = 1000000.0/period;
duty = (ontime/period)\*100;
lcd.setCursor(4,1);
lcd.print(ontime);
lcd.print("us");
lcd.setCursor(5,2);
lcd.print(offtime);
lcd.print("us");
lcd.setCursor(5,0);
lcd.print(freq);
lcd.print("Hz ");
lcd.setCursor(6,3);
lcd.print(duty);
lcd.print('%');
//Serial.print("On Time:");
// Serial.println(ontime);

delay(1000);
}

本次实例中,通过pulseIn函数对输入信号的开启和关闭(即,高低电平)进行计数。计数步骤如下:

  • 当开始仿真时,信号发生器将脉冲信号发送到引脚7,Arduino首先等待脉冲变为高电平。当脉冲信号变高电平时,它会计算脉冲保持高电平的时间长度(以微秒为单位)。
1
2
ontime = pulseIn(pulse_ip,HIGH);

  • 然后计算脉冲保持低电平的时间长度(微秒为单位)。
1
2
offtime = pulseIn(pulse_ip,LOW);

  • 然后,将这两个时间间隔相加得到总时间,即周期。
1
2
period = ontime+offtime;

  • 频率计算公式为:频率 = 1 / 时间
1
2
freq = 1000000.0/period;

  • 然后,计算占空比
1
2
duty = (ontime/period)\*100; 

  • 最后将计算结果显示到LCD1604上。

仿真结果如下:

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/127816984