使用资源:STM32F407ZGT6芯片,板载DAC1通道1(定时器触发),板载ADC1通道5,板载DMA1数据流5,通道7,CH340串口USB模块。
大体思想:使用u16的数组产生原始正弦波数据序列,通过DMA1将原始数据写入DAC寄存器,同时产生正弦波,AD读取频率稍大于DA频率。将AD读取寄存器中的数据以串口发送至matlab软件,以实现对DAC原始数据以及ADC采集所得到的数据的实时波形图的绘制。
matlab代码如下:
%%
clc;
global t;
global x;
global m;
global ii;
t=10;
x=10;
ii=10;
m=[0];
global t1;
global x1;
global m1;
global ii1;
t1=10;
x1=10;
ii1=10;
m1=[0];
global counter;
global flag;
flag = 0;
counter = 0;
%x=0;
%figure(1);
subplot(1,2,1);
p = plot(t,m,'r-','EraseMode','background','MarkerSize',5);
axis([0 20 -5 5]);
grid on;
title('DA原始数据显示');
xlabel('时间/ms');
ylabel('振幅/V');
%figure(2);
subplot(1,2,2);
q = plot(t1,m1,'r-','EraseMode','background','MarkerSize',5);
axis([0 20 -5 5]);
grid on;
title('AD采集波形显示');
xlabel('时间/ms');
ylabel('振幅/V');
%%
try
s=serial('com5');
catch
error('cant serial');
end
set(s,'BaudRate',115200,'DataBits',8,'StopBits',1,'Parity','none','FlowControl','none');
s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn = {@callbackcom,p,q};
fopen(s);
pause;
fclose(s);
delete(s);
clear s
close all;
clear all;
%%
function callbackcom(s, ~, p,q)
global t;
global x;
global m;
global ii;
global t1;
global x1;
global m1;
global ii1;
global counter;
global flag;
out = fscanf(s);
data_o = str2double(out);
data = 3.3*data_o/4096;
if data_o>15000&&data_o<25000
flag = 1;
counter = 1;
end
if data_o>25000&&data_o<34000
flag = 1;
counter = 2;
end
if flag == 0
if counter == 1 %绘制原始DA数据图
counter = 0;
t = [t,ii];
m = [m,data];
set(p, 'xData',t,'yData',m(1,:));
drawnow;
x = x+0.0390625;
axis([x-10 x+10 -5 5]); %移动坐标绘图
grid on;
ii = ii+0.0390625;
end
if counter == 2 %绘制AD采集的数据
counter = 0;
t1 = [t1,ii1];
m1 = [m1,data];
set(q, 'xData',t1,'yData',m1(1,:));
drawnow;
x1 = x1+0.0390625;
axis([x1-10 x1+10 -5 5]); %移动坐标绘图
grid on;
ii1 = ii1+0.0390625;
end
end
flag = 0;
end
单片机部分代码:
int main(void)
{
delay_init(168);
uart_init(115200);
sin_Generation();
GPIO_Configuration();
TIM6_Configuration();
DAC_DMA_Configuration();
Adc_Init();
while(1)
{
for(i = 0; iDAC配置:#define PI 3.14159
#define DAC_DHR12R1_ADDRESS 0x40007408
u16 sinTable[tableSize];
void sin_Generation(void)
{
u16 n;
for(n=0;n