初中生
最后登录1970-1-1
在线时间 小时
注册时间2018-1-12
|
本人刚学STM32,买的指南者开发板给的DAC例程是双通道的,双通道波形显示正常,但是我想改成单通道一直不成功,有哪位大神可以指点下吗?以下是我改单通道例程#include "./dac/bsp_dac.h"
//ÕyÏò2¨μ¥¸öÖüÆúμÄμãêy
#define POINT_NUM 32
/* 2¨DÎêy¾Y ---------------------------------------------------------*/
const uint16_t Sine12bit[POINT_NUM] = {
2048 , 2460 , 2856 , 3218 , 3532 , 3786 , 3969 , 4072 ,
4093 , 4031 , 3887 , 3668 , 3382 , 3042 , 2661 , 2255 ,
1841 , 1435 , 1054 , 714 , 428 , 209 , 65 , 3 ,
24 , 127 , 310 , 564 , 878 , 1240 , 1636 , 2048
};
uint32_t DualSine12bit[POINT_NUM];
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_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ÅäÖÃDAC í¨μà1 */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; //ê1óÃTIM2×÷Îa′¥·¢Ô′
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; //2»ê1óÃ2¨Dη¢éúÆ÷
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; //2»ê1óÃDACêä3ö»o3å
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* ê1Äüí¨μà1 óéPA4êä3ö */
DAC_Cmd(DAC_Channel_1, ENABLE);
/* ê1ÄüDACμÄDMAÇëÇó */
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
static void DAC_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* ê1ÄüTIM2ê±Öó£¬TIM2CLK Îa72M */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* TIM2»ù±¾¶¨ê±Æ÷ÅäÖà */
// TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = (20-1); //¶¨ê±ÖüÆú 20
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //Ô¤·ÖÆ죬2»·ÖÆμ 72M / (0+1) = 72M
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //ê±Öó·ÖÆμÏμêy
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* ÅäÖÃTIM2′¥·¢Ô′ */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* ê1ÄüTIM2 */
TIM_Cmd(TIM2, ENABLE);
}
static void DAC_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* ê1ÄüDMA2ê±Öó */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
/* ÅäÖÃDMA2 */
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_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)
{
uint32_t Idx = 0;
DAC_Config();
DAC_TIM_Config();
/* ìî3äÕyÏò2¨DÎêy¾Y£¬Ë«í¨μàóò¶ÔÆë*/
for (Idx = 0; Idx < POINT_NUM; Idx++)
{
DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
}
DAC_DMA_Config();
}
|
|