1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
使用资源: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; i printf("%dn", k); printf("%dn", sinTable); printf("%dn", j); printf("%dn", Get_Adc_Average(ADC_Channel_5,1)); //delay_ms(30); } } } DAC配置: #define PI 3.14159 #define DAC_DHR12R1_ADDRESS 0x40007408 u16 sinTable[tableSize]; void sin_Generation(void) { u16 n; for(n=0;n sinTable[n] = (sin(2*PI*n/tableSize)+1)*2047; } } void TIM6_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); TIM_PrescalerConfig(TIM6, 83, TIM_PSCReloadMode_Update); TIM_SetAutoreload(TIM6, 6900); TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); TIM_Cmd(TIM6, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); } void DAC_DMA_Configuration(void) { DAC_InitTypeDef DAC_InitStructure; DMA_InitTypeDef DMA_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DMA_DeInit(DMA1_Stream5); DMA_InitStructure.DMA_Channel = DMA_Channel_7; DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&sinTable; DMA_InitStructure.DMA_BufferSize = tableSize; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStructure); DMA_Cmd(DMA1_Stream5, ENABLE); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_DMACmd(DAC_Channel_1, ENABLE); } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1767 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1619 浏览 1 评论
1069 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
724 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1673 浏览 2 评论
1935浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
727浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
567浏览 3评论
592浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
551浏览 3评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 21:53 , Processed in 0.667861 second(s), Total 76, Slave 60 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号