#include "n32g45x.h"
/** @addtogroup DAC_DualModeDMA_SineWave
* @{
*/
//#define DAC_DHR12RD_Address 0x40007420
#define DAC_DR12CH1_Address 0x40007408
#define DAC_DR12CH2_Address 0x40007414
/* Init Structure definition */
DAC_InitType DAC_InitStructure;
uint32_t Idx = 0;
const uint16_t Sine12bit[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};
uint32_t DualSine12bit[32];
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_TrgoInit(void);
void DAC_SineWaveInit(void);
void DMA_Config(void);
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program.
*/
int main(void)
{
GPIO_Configuration();
TIM_TrgoInit();
DAC_SineWaveInit();
/* Fill Sine32bit table */
// for (Idx = 0; Idx < 32; Idx++)
// {
// DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
// }
/* DMA Config */
DMA_Config();
TIM_Enable(TIM2, ENABLE);
TIM_Enable(TIM4, ENABLE);
while (1)
{
}
}
/**
* @brief Configures the different system clocks.
*/
void RCC_Configuration(void)
{
/* DAC Periph clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
*/
void GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
/* GPIOA Periph clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}
/**
* @brief TIM4 Init.
*/
void TIM_TrgoInit(void)
{
TIM_TimeBaseInitType TIM_TimeBaseStructure;
/* TIM4 Periph clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2 , ENABLE); // RCC_APB2_PERIPH_TIM8
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4 , ENABLE);
TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.Period =39;
TIM_TimeBaseStructure.Prescaler = 0x0;
TIM_TimeBaseStructure.ClkDiv = 0x0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseStructure.Period =21;
TIM_TimeBaseStructure.Prescaler = 0x0;
TIM_TimeBaseStructure.ClkDiv = 0x0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM4, &TIM_TimeBaseStructure);
TIM_SelectOutputTrig(TIM2, TIM_TRGO_SRC_UPDATE);
TIM_SelectOutputTrig(TIM4, TIM_TRGO_SRC_UPDATE);
}
/**
* @brief DAC SineWave Config.
*/
void DAC_SineWaveInit(void)
{
/* DAC Periph clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
DAC_InitStructure.Trigger = DAC_TRG_T2_TRGO;
DAC_InitStructure.WaveGen = DAC_WAVEGEN_NONE;
DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_DISABLE;
DAC_Init(DAC_CHANNEL_1, &DAC_InitStructure);
DAC_InitStructure.Trigger = DAC_TRG_T4_TRGO;
DAC_InitStructure.WaveGen = DAC_WAVEGEN_NONE;
DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_DISABLE;
DAC_Init(DAC_CHANNEL_2, &DAC_InitStructure);
DAC_Enable(DAC_CHANNEL_1, ENABLE);
DAC_Enable(DAC_CHANNEL_2, ENABLE);
DAC_DmaEnable(DAC_CHANNEL_1, ENABLE);
DAC_DmaEnable(DAC_CHANNEL_2, ENABLE);
}
/**
* @brief DMA Config.
*/
void DMA_Config(void)
{
DMA_InitType DMA_InitStructure;
/* DMA2 clock enable */
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA2, ENABLE);
// DMA_DeInit(DMA2_CH3);
DMA_InitStructure.PeriphAddr = DAC_DR12CH1_Address;
DMA_InitStructure.MemAddr = (uint32_t)&Sine12bit;
DMA_InitStructure.Direction = DMA_DIR_PERIPH_DST;
DMA_InitStructure.BufSize = 32;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord ;
DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
DMA_Init(DMA2_CH3, &DMA_InitStructure);
DMA_EnableChannel(DMA2_CH3, ENABLE);
// DMA_DeInit(DMA2_CH4);
DMA_InitStructure.PeriphAddr =DAC_DR12CH2_Address;
DMA_InitStructure.MemAddr = (uint32_t)&Sine12bit;
DMA_InitStructure.Direction = DMA_DIR_PERIPH_DST;
DMA_InitStructure.BufSize = 32;
DMA_InitStructure.PeriphInc = DMA_PERIPH_INC_DISABLE;
DMA_InitStructure.DMA_MemoryInc = DMA_MEM_INC_ENABLE;
DMA_InitStructure.PeriphDataSize = DMA_PERIPH_DATA_SIZE_HALFWORD;
DMA_InitStructure.MemDataSize = DMA_MemoryDataSize_HalfWord ;
DMA_InitStructure.CircularMode = DMA_MODE_CIRCULAR;
DMA_InitStructure.Priority = DMA_PRIORITY_HIGH;
DMA_InitStructure.Mem2Mem = DMA_M2M_DISABLE;
DMA_Init(DMA2_CH4, &DMA_InitStructure);
DMA_EnableChannel(DMA2_CH4, ENABLE);
}