MCP41010和LM567实现识别300-6000HZ的频率,附源代码
时间: 2024-02-28 18:54:24 浏览: 32
以下是使用MCP41010和LM567实现识别300-6000HZ频率的源代码:
```
#include <SPI.h>
#include <MCP41xxx.h>
#include < Tone.h>
#define POT_PIN 10
#define TONE_PIN 9
MCP41xxx digitalPot(POT_PIN);
Tone tone(TONE_PIN);
int lastFreq = 0;
void setup() {
Serial.begin(9600);
pinMode(TONE_PIN, OUTPUT);
digitalPot.begin();
digitalPot.setWiper(128);
}
void loop() {
int freq = detectFrequency();
if (freq > 0 && freq != lastFreq) {
tone.play(freq);
lastFreq = freq;
} else if (freq == 0 && lastFreq != 0) {
tone.stop();
lastFreq = 0;
}
}
int detectFrequency() {
int freq = 0;
int maxFreq = 0;
int maxVal = 0;
for (int i = 300; i <= 6000; i += 10) {
int val = readLM567(i);
if (val > maxVal) {
maxFreq = i;
maxVal = val;
}
}
if (maxVal > 100) {
freq = maxFreq;
}
return freq;
}
int readLM567(int freq) {
int val = 0;
tone.setFrequency(freq);
delay(1);
for (int i = 0; i < 100; i++) {
val += analogRead(A0);
}
return val / 100;
}
```
代码中使用了MCP41010数字电位器控制信号的幅度,使其与LM567产生的信号频率匹配,然后通过Tone库播放声音。detectFrequency()函数用于检测频率,它通过调用readLM567()函数读取LM567产生的信号值。readLM567()函数使用Tone库产生频率,并通过模拟读取A0引脚读取信号值,最后返回平均值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)