【Follow me第二季第2期】--入门任务--UNO R4 ADC--OPAMP--DAC--LED Matrix--串口采集
<p>UNO R4任务2:</p><p>视频:85a3dfe1ff445d5d475bbc1117c96e98</p>
<p><strong>1、任务分析</strong></p>
<p><strong>本次任务主要有以下模块:</strong></p>
<p><strong>(1)ADC产生正弦波。</strong></p>
<p><strong>(2)OPAMP放大。</strong></p>
<p><strong>(3)ADC采集。</strong></p>
<p><strong>(4)LED 显示。</strong></p>
<p><strong>(5)串口显示。</strong></p>
<p> </p>
<p><b>参考的官方文档资料:</b></p>
<p><b><a href="https://docs.arduino.cc/tutorials/uno-r4-wifi/led-matrix/" target="_blank">https://docs.arduino.cc/tutorials/uno-r4-wifi/led-matrix/</a></b></p>
<p><strong><a href="https://docs.arduino.cc/tutorials/uno-r4-wifi/adc-resolution/" target="_blank">https://docs.arduino.cc/tutorials/uno-r4-wifi/adc-resolution/</a></strong></p>
<p><strong><a href="https://docs.arduino.cc/tutorials/uno-r4-wifi/dac/" target="_blank">https://docs.arduino.cc/tutorials/uno-r4-wifi/dac/</a></strong></p>
<p><strong><a href="https://docs.arduino.cc/tutorials/uno-r4-wifi/opamp/" target="_blank">https://docs.arduino.cc/tutorials/uno-r4-wifi/opamp/</a></strong></p>
<p> </p>
<p><strong>2、电路说明</strong></p>
<p><b>外接电路图:</b></p>
<p> </p>
<div style="text-align: center;"></div>
<p> </p>
<p>由于在老家,没找到合适的电阻,暂时不进行放大。</p>
<p><strong>3、程序设计系思路</strong></p>
<p><strong>为了便于调试,采取从后往前实现并调试各个模块。</strong></p>
<p><strong>(1)代码模拟一个正弦波,并通过串口打印出来。Arduino IDE非常好用,不仅有串口的监视器,还有串口的画图窗口,可以将打印的数据以图形形式显示出来。</strong></p>
<p><strong>(2)真正生成正弦波,用ADC采集,替代模拟产生的正弦波,调试。</strong></p>
<p><strong>(3)LED 点阵显示波形,由于LED点阵有8行12列,以12列为时间轴,每一列显示一个时刻的正弦波形。将ADC采集到的数据映射到0--8内,并以实体形式显示波形。</strong></p>
<p><strong>4、代码</strong></p>
<pre>
<code class="language-cpp">#include "Arduino_LED_Matrix.h"
#include "analogWave.h"
#include <OPAMP.h>
// Create an instance of the analogWave class, using the DAC pin
analogWave wave(DAC);
// LEDMatrix
ArduinoLEDMatrix matrix;
byte frame = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;// Analog input pin that the potentiometer is attached to
const int analogOutPin = 9;// Analog output pin that the LED is attached to
int freq = 1;
int time_idx = 0;
int sensorValue = 0;// value read from the pot
int outputValue = 0;// value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(19200);
//change to 14-bit resolution
analogReadResolution(10);
// Generate a sine wave with the initial frequency
wave.sine(freq);
wave.amplitude(0.5);
// LED
matrix.begin();
}
void set_matrix(int idx, int num)
{
for(int i=0; i<8; i++)
{
for(int j=0;j<11;j++)
{frame = frame;}
}
num = num / 16;
for(int i=0; i<num; i++)
{frame = 1;}
}
void loop()
{
int reading = analogRead(A3);
int serial_num = map(reading, 0, 1023, 0, 255);
Serial.println(String(serial_num));
if(time_idx>=12)
{time_idx=0;}
set_matrix(10, serial_num);
matrix.renderBitmap(frame, 8, 12);
delay(50);
time_idx = time_idx + 1;
}
</code></pre>
<p> </p>
<p><br />
</p>
<p>LED点阵动态显示正弦波。</p>
页:
[1]