完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
利用STM32F103,DA两路正弦波输出,利用定时器触发,数据保存在DMA里面,完全一样的设置,结果输出两路波频率不一样。想为群里的大神这是为什么?以下为程序。
#include "delay.h" #include "key.h" #include "sys.h" #include "dac.h" //#define DAC_DHR12RD_Address 0x40007414 //2ch #define DAC_DHR12R2_Address 0x40007414 //1ch #define DAC_DHR12R1_Address 0x40007408 uint16_t g_Wave[128]; uint16_t DAC1_Value[1]; const uint16_t SineData[32] = { 2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056, 3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, 599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647}; void RCC_DAC_Config(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_tiM2,ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); } void GPIO_DAC_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA,&GPIO_InitStructure); } void DAC_Config(void) { uint32_t i=0; DMA_InitTypeDef DMA_InitStructure; DAC_InitTypeDef DAC_InitStructure; DAC1_Value[0]=4096*0.1; for(i=0;i<32;i++) { //g_Wave[i]=DAC2_SineWave[i] *0.606; //Vpp =2.0V g_Wave[i]=SineData[i] *0.606; //Vpp =2.0V } RCC_DAC_Config(); GPIO_DAC_Config(); TIM_PrescalerConfig(TIM2,0,TIM_PSCReloadMode_Update); TIM_SetAutoreload(TIM2, 22); TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //#if 0 DAC_Init(DAC_Channel_1, &DAC_InitStructure); DMA_DeInit(DMA2_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&g_Wave; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel3, &DMA_InitStructure); DMA_Cmd(DMA2_Channel3, ENABLE); DAC_Cmd(DAC_Channel_1, ENABLE); DAC_DMACmd(DAC_Channel_1, ENABLE); TIM_Cmd(TIM2, ENABLE); //#else TIM_PrescalerConfig(TIM1,0,TIM_PSCReloadMode_Update); TIM_SetAutoreload(TIM1, 22); TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update); DAC_Init(DAC_Channel_2, &DAC_InitStructure); DMA_DeInit(DMA2_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&g_Wave; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel4,&DMA_InitStructure); DMA_Cmd(DMA2_Channel4,ENABLE); DAC_Cmd(DAC_Channel_2,ENABLE); DAC_DMACmd(DAC_Channel_2,ENABLE); TIM_Cmd(TIM1,ENABLE); //#endif } void jiance(u8 key) //检测按键 { u16 val=2000; if(key==1) //KEY_RIGHT按下,则执行校准程序 { GPIO_ResetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高 GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高 GPIO_ResetBits(GPIOC,GPIO_Pin_12); //Dac1_Set_Vol(val); DAC_Config(); } if(key==2) { GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高 GPIO_ResetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高 GPIO_ResetBits(GPIOC,GPIO_Pin_12); } if(key==3) { GPIO_ResetBits(GPIOB,GPIO_Pin_5); GPIO_ResetBits(GPIOE,GPIO_Pin_5); } if(key==4) { GPIO_ResetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高 GPIO_ResetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高 GPIO_ResetBits(GPIOC,GPIO_Pin_12); } if(key==5) { GPIO_ResetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高 GPIO_ResetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高 GPIO_ResetBits(GPIOC,GPIO_Pin_12); } } int main(void) { u8 key=0; u16 vol=1000; delay_init(); //延时函数初始化 NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 KEY_Init(); LED_Init(); //LED端口初始化 //Dac1_Init(); //Dac1_Set_Vol(vol); //vol:0~3300,代表0~3.3V while(1) { key=KEY_Scan(0);//扫描按键 jiance(key); //检测按键 } } |
|
相关推荐
1个回答
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-使用AHT20进行环境监测之AHT20传感器介绍
510 浏览 0 评论
792 浏览 0 评论
833 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
797 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
1244 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11812 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 07:51 , Processed in 0.652073 second(s), Total 78, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号