利用
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); //检测按键
}
}
评分
查看全部评分
一周热门 更多>