注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
一二三亖的个人空间 https://home.eeworld.com.cn/space-uid-770570.html [收藏] [复制] [分享] [RSS]
日志

stm32双路DAC程序,幅值频率可调

已有 2979 次阅读2017-8-2 14:35 |个人分类:stm32f103

#include "DA.h"
uint16_t g_Wave1[128];
uint16_t g_Wave2[128];
uint16_t DAC1_Value[1];
#define DAC_DHR12R2_Address      0x40007414    //1ch
#define DAC_DHR12R1_Address       0x40007408
#define _800Hz (u16)(72000000/sizeof(SineData)*2/500) //800Hz
#define _1600Hz (u16)(72000000/sizeof(SineData)*2/500) //1600Hz

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};
/*const uint16_t SineData[256]={2048, 2098, 2148, 2198, 2248, 2298, 2348, 2398, 2447, 2496,
                              2545, 2594, 2642, 2690, 2737, 2785, 2831, 2877, 2923, 2968,
                              3013, 3057, 3100, 3143, 3185, 3227, 3267, 3307, 3347, 3385,
                              3423, 3460, 3496, 3531, 3565, 3598, 3631, 3662, 3692, 3722,
                              3750, 3778, 3804, 3829, 3854, 3877, 3899, 3920, 3940, 3958,
                              3976, 3992, 4007, 4021, 4034, 4046, 4056, 4065, 4073, 4080,
                              4086, 4090, 4093, 4095, 4095, 4095, 4093, 4090, 4086, 4080,
                              4073, 4065, 4056, 4046, 4034, 4021, 4007, 3992, 3976, 3958,
                              3940, 3920, 3899, 3877, 3854, 3829, 3804, 3778, 3750, 3722,
                              3692, 3662, 3631, 3598, 3565, 3531, 3496, 3460, 3423, 3385,
                              3347, 3307, 3267, 3227, 3185, 3143, 3100, 3057, 3013, 2968,
                              2923, 2877, 2831, 2785, 2737, 2690, 2642, 2594, 2545, 2496,
                              2447, 2398, 2348, 2298, 2248, 2198, 2148, 2098, 2047, 1997,
                              1947, 1897, 1847, 1797, 1747, 1697, 1648, 1599, 1550, 1501,
                              1453, 1405, 1358, 1310, 1264, 1218, 1172, 1127, 1082, 1038,
                              995, 952, 910, 868, 828, 788, 748, 710, 672, 635,
                              599, 564, 530, 497, 464, 433, 403, 373, 345, 317,
                              291, 266, 241, 218, 196, 175, 155, 137, 119, 103,
                              88, 74, 61, 49, 39, 30, 22, 15, 9, 5,
                              2, 0, 0, 0, 2, 5, 9, 15, 22, 30,
                              39, 49, 61, 74, 88, 103, 119, 137, 155, 175,
                              196, 218, 241, 266, 291, 317, 345, 373, 403, 433,
                              464, 497, 530, 564, 599, 635, 672, 710, 748, 788,
                              828, 868, 910, 952, 995, 1038, 1082, 1127, 1172, 1218,
                              1264, 1310, 1358, 1405, 1453, 1501, 1550, 1599, 1648, 1697,
                              1747, 1797, 1847, 1897, 1947, 1997 }; */  

void RCC_DAC_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM6,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);
}
static void SineWave_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM6, ENABLE);

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period =_800Hz; 
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Period = _1600Hz; 
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);

TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
}

void DAC_Config(float daT_1,float daT_2)
{
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_Wave1[i]=SineData[i]*0.303*daT_1;; 
g_Wave2[i]=SineData[i]*0.303*daT_2;
}
SineWave_TIM_Config();
RCC_DAC_Config();
GPIO_DAC_Config();
//DAC1
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
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_Wave1;
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);
//DAC2
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
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_Wave2;
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(TIM6,ENABLE);
}

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章