研究生
最后登录1970-1-1
在线时间 小时
注册时间2016-9-25
|
一直用官方给的例程修改,结果一直不成功,跑回去看官方手册才明白,调试了一天了。累的我啊,下面吧代码分享给大家。
优化到,只要在任意地方调用一个函数,就可以实现改变电压角度了。
#define POINT_NUM 1 //DMAÅäÖÃo궨òå
#define DAC_DHR12RD_ADDRESS (DAC_BASE+0x20) //DACμØÖ·
/* 2¨DÎêy¾Y ---------------------------------------------------------*/
uint32_t DualSine12bit = 0; //DMAÄú′æμØÖ·
static void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
/* ê1ÄüGPIOAê±Öó */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* ê1ÄüDACê±Öó */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DACμÄGPIOÅäÖã¬Ä£Äaêäèë */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ÅäÖÃDAC í¨μà1 */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; //ê1óÃTIM2×÷Îa′¥·¢Ô′ DAC_Trigger_Softwar
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //2»ê1óÃ2¨Dη¢éúÆ÷
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //2»ê1óÃDACêä3ö»o3å
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* ÅäÖÃDAC í¨μà2 */
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* ê1Äüí¨μà1 óéPA4êä3ö */
DAC_Cmd(DAC_Channel_1, ENABLE);
/* ê1Äüí¨μà2 óéPA5êä3ö */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* ê1ÄüDACμÄDMAÇëÇó */
DAC_DMACmd(DAC_Channel_2, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R,0);
}
/**
* @brief ÅäÖÃTIM
* @param ÎT
* @retval ÎT
*/
/**
* @brief ÅäÖÃDMA
* @param ÎT
* @retval ÎT
*/
static void DAC_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* ê1ÄüDMA2ê±Öó */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
/* ÅäÖÃDMA2 */
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_ADDRESS; //íaéèêy¾YμØÖ·
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DualSine12bit ; //Äú′æêy¾YμØÖ· DualSine12bit
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //êy¾Y′«êä·½ÏòÄú′æÖáíaéè
DMA_InitStructure.DMA_BufferSize = POINT_NUM; //»o′æ′óD¡ÎaPOINT_NUM×Ö½ú
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //íaéèêy¾YμØÖ·1춨
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Äú′æêy¾YμØÖ·×ÔÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //íaéèêy¾YòÔ×ÖÎaμ¥λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //Äú′æêy¾YòÔ×ÖÎaμ¥λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //Ñ-»·Ä£ê½
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //¸ßDMAí¨μàóÅÏ輶
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //·ÇÄú′æÖáÄú′æÄ£ê½
DMA_Init(DMA2_Channel4, &DMA_InitStructure);
/* ê1ÄüDMA2-14í¨μà */
DMA_Cmd(DMA2_Channel4, ENABLE);
}
void DAC_Mode_Init(void)
{
DAC_Config();
DAC_DMA_Config();
}
然后把
DAC_Mode_Init();
放MAIN函数里面初始化。
接着要用的时候
调用
DAC_SetChannel1Data(DAC_Align_12b_R,0);
就可以了。最后的0就是你的电压值。取值0-4096
|
|