最近在做ADC+DAC。大概就是ADC采集一个正弦波,DMA存到一个数组里面。再用DAC把这个数组里面的值输出。目前ADC和DAC这俩部分都可以单独工作了。但是,我昨天把这两部分连在一起时,发生了一个奇观现象。
问题就是,在main.c里面 bsp_InitADC(); DAC_Mode_Init(); 这两个函数不能同时作用。我注释掉ADC,DAC就可以输出正弦波。
我注释掉DAC,ADC就可以打印出采样值。不知道是什么原因???
[mw_shl_code=c,true]#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "stdio.h"
#include "adc.h"
#include "dac.h"
#define ADC_DMA_SIZE 2
extern volatile uint16_t ADC1ConvertedValue[ADC_DMA_SIZE];
extern __IO uint32_t ADC1ConvertedVoltage;
float ADC_ConvertedValueLocal;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //
delay_init(168); //
uart_init(115200); //
bsp_InitADC(); //ADC1 channel10 DMA2 PC0
DAC_Mode_Init(); // DAC
while(1)
{
uint16_t i;
ADC1ConvertedVoltage = 0;
for( i=0 ; i<ADC_DMA_SIZE ; i++ )
{
ADC1ConvertedVoltage += ADC1ConvertedValue;
}
printf("%d;
", ADC1ConvertedVoltage/2);
}
}
[/mw_shl_code]
ADC采集部分,我用的是ADC1——channel10,即PC0。再用DMA2传输数据。目前暂时在while死循环里面串口打印查看ADC采集的数据
[mw_shl_code=c,true]#include "adc.h"
#include "delay.h"
#define ADC_DMA_SIZE 32
/* 񄠨 ----------------------------------------------------------------------*/
__IO uint16_t ADC1ConvertedValue[ ADC_DMA_SIZE ];
__IO uint32_t ADC1ConvertedVoltage ;
/*
*********************************************************************************************************
* oˉ êy Ãû: bsp_InitADC
* 1|ÄüËμÃ÷: ADC3õê¼»ˉ
* DÎ 2Î: ÎT
* ·μ »Ø Öμ: ÎT
*********************************************************************************************************
*/
void bsp_InitADC(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
/* ê1Äü ADC1, DMA2 oí GPIO ê±Öó ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* DMA2 Stream0 channel2 ÅäÖÃ **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)( & ( ADC1->DR ) ); // è¡ADC1 μÄμØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC1ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADC_DMA_SIZE ;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
/* ÅäÖÃADC3í¨μà7òy½ÅÎaÄ£ÄaêäèëÄ£ê½******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/****************************************************************************
PCLK2 = HCLK / 2
ÏÂÃæÑ¡ÔñμÄêÇ2·ÖÆμ
ADCCLK = PCLK2 /2 = HCLK / 4 = 168 / 4 = 42M
ADC2éÑùÆμÂê£o Sampling Time + Conversion Time = 3 + 12 cycles = 15cyc
Conversion Time = 42MHz / 15cyc = 2.8Mbps.
***************************************************************************/
/* ADC1«122¿·Ö3õê¼»ˉ**********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC3 3õê¼»ˉ ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC3 1æÔò channel7 ÅäÖÃ *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_28Cycles);
/* ê1ÄüDMAÇëÇó(μ¥ADCÄ£ê½) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* ê1Äü ADC3 DMA */
ADC_DMACmd(ADC1, ENABLE);
// //Ñ¡ÔñDMA2êy¾Yá÷0í¨μà0
// NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn;
// //ÇàÕ¼ê½óÅÏ輶
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
// //Ïìó|ê½óÅÏ輶
// NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
// //í¨μàê1Äü
// NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
// //½«òÔéÏÅäÖÃó|óÃóúNVIC
// NVIC_Init(&NVIC_InitStructure);
// //ê1ÄüDMA′«êäÖD¶Ï
// DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
/* ê1Äü ADC3 */
ADC_Cmd(ADC1, ENABLE);
/* èí¼tÆô¶ˉADC×a»» */
ADC_SoftwareStartConv(ADC1);
}
[/mw_shl_code]
DAC部分,我用了一个32元素的正弦数组,DMA1数据流5 ,TIM2做DAC的触发,输出正弦波。
[mw_shl_code=c,true]#include "dac.h"
#define DAC_DHR12RD_Address 0x40007420 // Æ«òÆμØÖ·0x08
const uint16_t Sine12bit[32] = {
2448,2832,3186,3496,3751,3940,4057,4095,4057,3940,
3751,3496,3186,2832,2448,2048,1648,1264,910,600,345,
156,39,0,39,156,345,600,910,1264,1648,2048
};
uint32_t DualSine12bit[32];
static void DAC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitType;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//ê1ÄüDACê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //Ä£Äaêäèë
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //ÏÂà-
GPIO_Init(GPIOA, &GPIO_InitStructure); //3õê¼»ˉ
DAC_InitType.DAC_Trigger=DAC_Trigger_T2_TRGO ; //2»ê1óÃ′¥·¢1|Äü TEN1=0
DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None; //2»ê1óÃ2¨Dη¢éú
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0; //Æá±Î¡¢·ùÖμéèÖÃ
DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1êä3ö»o′æ1Ø±Õ BOFF1=1
DAC_Init(DAC_Channel_1,&DAC_InitType); //3õê¼»ˉDACí¨μà1
//DAC_Init(DAC_Channel_2,&DAC_InitType); //3õê¼»ˉDACí¨μà1
DAC_Cmd(DAC_Channel_1, ENABLE); //ê1ÄüDACí¨μà1
// DAC_Cmd(DAC_Channel_2, ENABLE); //ê1ÄüDACí¨μà2
DAC_SetChannel1Data(DAC_Align_12b_R, 0); //12λóò¶ÔÆëêy¾Y¸ñê½éèÖÃDACÖμ
// DAC_SetChannel2Data(DAC_Align_12b_R, 0); //12λóò¶ÔÆëêy¾Y¸ñê½éèÖÃDACÖμ
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
static void DAC_TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
/* TIM2»ù±¾¶¨ê±Æ÷ÅäÖà */
TIM_TimeBaseStructure.TIM_Period = 130; //¶¨ê±ÖüÆú 130 || f = 84M/(32*130) ¸Ä±ä¼ÆêyÖμ
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; //Ô¤·ÖÆμ
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //ê±Öó·ÖÆμÏμêy
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
TIM_Cmd(TIM2, ENABLE);
}
static void DAC_DMA_Config(DMA_Stream_TypeDef *DMA_Streamx)
{
DMA_InitTypeDef DMA_InitStructure;
if((u32)DMA_Streamx>(u32)DMA2)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);
}
else
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
}
DMA_DeInit(DMA_Streamx);
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
/* ê1ÄüDMA1ê±Öó */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
/* ÅäÖÃDMA1 */
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_Address;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DualSine12bit;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;
DMA_InitStructure.DMA_BufferSize = 32 ;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA_Streamx, &DMA_InitStructure);
/* ê1ÄüDMA1-7í¨μà */
DMA_Cmd(DMA1_Stream5, ENABLE);
}
void DAC_Mode_Init(void)
{
uint32_t Idx = 0;
DAC_Config();
DAC_TIM_Config();
DAC_DMA_Config(DMA1_Stream5);
/* ìî3äÕyÏò2¨DÎêy¾Y£¬Ë«í¨μàóò¶ÔÆë*/
for (Idx = 0; Idx < 1000; Idx++)
{
// DualSine12bit[Idx] = (Sine12bit[Idx] << 16) + (Sine12bit[Idx]);
DualSine12bit[Idx] = (Sine12bit[Idx]);
}
}
[/mw_shl_code]
问题就是,在main.c里面 bsp_InitADC(); DAC_Mode_Init(); 这两个函数不能同时作用。我注释掉ADC,DAC就可以输出正弦波。
我注释掉DAC,ADC就可以打印出采样值。
学会找问题的方法。
不能用肯定是寄存器变化了。
包括:
1,ADC/DAC的寄存器组
2,时钟控制寄存器(RCC,管理ADC/DAC/IO的时钟)
3,IO寄存器
一周热门 更多>