本帖最后由 飞羽1918 于 2016-8-27 15:10 编辑
代码如下,还望大家帮帮看看。我参照F4的固件库提供的同步规则模式配置。使用TIM2外部触发,但不管怎么样,只有ADC1能用,ADC2采不到数据。
[mw_shl_code=c,true]#include "ADC_Config.h"
#include "usart.h"
#define ADC_CCR_ADDRESS ((uint32_t)0x40012308) //ADC通用规则数据寄存器的地址
#define ArrayLenth 10
__IO uint32_t aADCDualConvertedValue[ArrayLenth]; //由于数据寄存器的是32位的,在此定义一个32位数组。高16位为ADC2接收的数据,低16位位ADC1接收到的数据
int i;
char B='B';
void Tim2Config(u32 arr,u32 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开TIM时钟
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure); //初始化配置结构,不受其它配置干扰
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; //对APB1时钟不分频,APB1=AHB/4=SYSCLK/4=168M/4=42M,则TIM3=APB1*2=84M
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //分频值=84M/(psc+1)
TIM_TimeBaseInitStructure.TIM_Period=arr; //溢出时间确定,
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数方式
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure); //将以上配置应用于定时器
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update); //数据更新作为触发源
}
void ADC12_Config(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
/* Enable peripheral clocks *************************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
DMA_Config(); //DMA2 数据流0 通道0的配置,对应ADC1,ADC1为主,ADC2为从
GPIO_Config(); //配置ADC1的通道10(对应PC0)、ADC2的通道12(对应PC2)为模拟输入
/* ADC Common Init */
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; //ADC的同时规则模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //ADC时钟为APB2的2分频->84/4=21MHZ(F407ADC在2.4-3.6V供电电压下最大速率36M,稳定速度为30M)
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 regular channels 10 configuration */
ADC1_CH10_Config();
/* ADC2 regular channels 12 configuration */
ADC2_CH12_Config();
ADC_DMACmd(ADC1, ENABLE); //开启ADC1 DMA
/* Enable DMA request after last transfer (Multi-ADC mode) */
ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE);
}
/***********************
函数名:StartAdcOscSam
功 能:开启AD采样
输 入:无
输 出:无
***********************/
void StartAdcOscSam(void)
{
//ADC_SoftwareStartConv(ADC1);
TIM_Cmd(TIM2, ENABLE);
}
/**********************
函数名:StopAdcOscSam
功 能:关闭AD采样
输 入:无
输 出:无
**********************/
void StopAdcOscSam(void)
{
TIM_Cmd(TIM2, DISABLE);
}
/**********************
函数名:ADC1printf
功 能:打印采集的数据
输 入:无
输 出:无
**********************/
void ADCprintf(void)
{
for(i=0;i<ArrayLenth;i++)
{
printf("%f ",(float)(((uint16_t)aADCDualConvertedValue)*(3.3/4096))); //低16位为ADC1采集的数据
printf("%f
",(float)(((uint16_t)(aADCDualConvertedValue>>16))*(3.3/4096))); //高16位为ADC2采集的数据
}
}
/******************************
函数名:DMA2_Stream0_IRQHandler
功 能:DMA2的中断服务函数,一旦
输 入:无
输 出:无
******************************/
void DMA2_Stream0_IRQHandler(void)
{
if(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0)!=RESET)
{
StopAdcOscSam();
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
printf("%c
",B); //测试是否进入中断,如果进入了中断,在串口调试助手中汇打印一个‘B’
ADCprintf();
}
}
/**
* @brief DMA Configuration
* @param None
* @retval None
*/
static void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //选择通道0,对用ADC1
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&aADCDualConvertedValue; //数据存储的地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC->CDR; //规则同时模式下,数据寄存器地址,高16位存储ADC2的数据,低16位存储ADC1的数据
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //数据传输方向为外设到内存
DMA_InitStructure.DMA_BufferSize = ArrayLenth; //数据缓存长度
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据形式:半字(16位)
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //内存数据存储形式:半字(16位)
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级高
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //不使用FIFO模式
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);
/* DMA2_Stream0 enable */
DMA_Cmd(DMA2_Stream0, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn; //选择DMA2通道数据流0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //抢占式优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=4; //响应式优先级为12
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //通道使能
NVIC_Init(&NVIC_InitStructure); //将以上配置应用于NVIC
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE); //使能DMA传输完成中断
}
/**
* @brief Configure ADC Channels 10,12 pins as analog inputs
* @param None
* @retval None
*/
static void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* ADC Channel 10 -> PC0
ADC Channel 12 -> PC2
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/**
* @brief ADC1 regular channels 10 configuration
* @param None
* @retval None
*/
static void ADC1_CH10_Config(void)
{
ADC_InitTypeDef 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_T2_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channels 10, 11 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_3Cycles);
//ADC_DMACmd(ADC1, ENABLE);
}
/**
* @brief ADC2 regular channels 12 configuration
* @param None
* @retval None
*/
static void ADC2_CH12_Config(void)
{
ADC_InitTypeDef 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_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC2, &ADC_InitStructure);
/* ADC2 regular channels 11, 12 configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 1, ADC_SampleTime_3Cycles);
//ADC_DMACmd(ADC2, ENABLE);
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>