初中生
最后登录1970-1-1
在线时间 小时
注册时间2019-7-11
|
单独用ADC+TIM+DMA没有问题,可以采集,但是加上DMA中断后就不行了 串口一直输出的是一个定值 比如0X0FFF 求大神指点迷津 代码如下
static void Rheostat_ADC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RHEOSTAT_ADC_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = RHEOSTAT_ADC_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //2»éÏà-2»ÏÂà-
GPIO_Init(RHEOSTAT_ADC_GPIO_PORT, &GPIO_InitStructure);
}
static void Rheostat_ADC_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RHEOSTAT_ADC_CLK , ENABLE);
RCC_AHB1PeriphClockCmd(RHEOSTAT_ADC_DMA_CLK, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = RHEOSTAT_ADC_DR_ADDR;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode =DMA_Mode_Circular;//DMA_Mode_Normal;// 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_InitStructure.DMA_Channel = RHEOSTAT_ADC_DMA_CHANNEL;
DMA_Init(RHEOSTAT_ADC_DMA_STREAM, &DMA_InitStructure);
NVIC_InitStruct.NVIC_IRQChannel=DMA2_Stream0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStruct);
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TC); //Çå3yÖD¶Ï±êÖ¾
DMA_ITConfig(RHEOSTAT_ADC_DMA_STREAM,DMA_IT_TC,ENABLE); //ê1Äü·¢ËíÖD¶Ï
DMA_Cmd(DMA2_Stream0, ENABLE);
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);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(RHEOSTAT_ADC, &ADC_InitStructure);
//---------------------------------------------------------------------------
ADC_RegularChannelConfig(RHEOSTAT_ADC, RHEOSTAT_ADC_CHANNEL, 1, ADC_SampleTime_3Cycles);
ADC_DMARequestAfterLastTransferCmd(RHEOSTAT_ADC, ENABLE);
ADC_DMACmd(RHEOSTAT_ADC, ENABLE);
ADC_Cmd(RHEOSTAT_ADC, ENABLE);
ADC_SoftwareStartConv(RHEOSTAT_ADC);
}
void MY_Time(u16 sample)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(GENERAL_TIM_CLK, ENABLE);
TIM_TimeBaseStructure.TIM_Period = (10000-1)/sample;
TIM_TimeBaseStructure.TIM_Prescaler = 9000-1;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure); //éèÖÃè±ê¡Öμ,Õaò»2½×îoüóéÏ·àÖ1·Åμ½′®¿ú3õê¼»ˉoó3öÎêìa
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 90000000/sample/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(GENERAL_TIM, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);
TIM_Cmd(GENERAL_TIM, ENABLE);
TIM_CtrlPWMOutputs(GENERAL_TIM, ENABLE);
}
/**
* @brief ADC13õê¼»ˉ
* @param ÎT
* @retval ÎT
*/
void Rheostat_Init(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Rheostat_ADC_GPIO_Config();
Rheostat_ADC_Mode_Config();
}
void DMA2_Stream0_IRQHandler()
{
if(DMA_GetITStatus(RHEOSTAT_ADC_DMA_STREAM,DMA_IT_TCIF0)) //ÅD¶ÏêÇ·ñÎaDMA1·¢Ëííê3éÖD¶Ï;
{
DMA_ClearITPendingBit( DMA2_Stream0 ,DMA_IT_TCIF0);
DMA_Cmd(RHEOSTAT_ADC_DMA_STREAM, ENABLE); //1رÕDMA;
ADC_Flag=1;
}
}
int main(void)
{
/*3õê¼»ˉUSART1*/
Debug_USART_Config();
Rheostat_Init();
MY_Time(20000);
while (1)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*(float)3.3; // ¶áè¡×a»»μÄADÖμ
printf("\r\n The current AD value = 0x%04X \r\n", ADC_ConvertedValue);
printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal);
Delay(0xffffee);
}
}
static void Delay(__IO uint32_t nCount) //¼òμ¥μÄÑóê±oˉêy
{
for(; nCount != 0; nCount--);
}
|
|