【Follow me第二季第2期】--入门任务--UNO R4 ADC--OPAMP--DAC--LED Matrix--串口采集
[复制链接]
UNO R4任务2:
视频: task2
1、任务分析
本次任务主要有以下模块:
(1)ADC产生正弦波。
(2)OPAMP放大。
(3)ADC采集。
(4)LED 显示。
(5)串口显示。
参考的官方文档资料:
https://docs.arduino.cc/tutorials/uno-r4-wifi/led-matrix/
https://docs.arduino.cc/tutorials/uno-r4-wifi/adc-resolution/
https://docs.arduino.cc/tutorials/uno-r4-wifi/dac/
https://docs.arduino.cc/tutorials/uno-r4-wifi/opamp/
2、电路说明
外接电路图:
由于在老家,没找到合适的电阻,暂时不进行放大。
3、程序设计系思路
为了便于调试,采取从后往前实现并调试各个模块。
(1)代码模拟一个正弦波,并通过串口打印出来。Arduino IDE非常好用,不仅有串口的监视器,还有串口的画图窗口,可以将打印的数据以图形形式显示出来。
(2)真正生成正弦波,用ADC采集,替代模拟产生的正弦波,调试。
(3)LED 点阵显示波形,由于LED点阵有8行12列,以12列为时间轴,每一列显示一个时刻的正弦波形。将ADC采集到的数据映射到0--8内,并以实体形式显示波形。
4、代码
#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[8][12] = {
{ 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[i][j] = frame[i][j+1];}
}
num = num / 16;
for(int i=0; i<num; i++)
{frame[i][idx] = 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;
}
|