TA的每日心情 | 奋斗 2016-6-21 13:43 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
通过定时器1触发ADC扫描7通道,数据通过DMA存储在内存中。
void EvbADCConfig(void)
{
GPIO_InitPara GPIO_InitStructure;
DMA_InitPara DMA_InitParaStructure;
NVIC_InitPara NVIC_InitStructure;
// TIMER_BaseInitPara TIMER_BaseInitParaStructure;
/* Enable the ADC DMA Clock */
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOA|RCC_AHBPERIPH_DMA1|RCC_AHBPERIPH_GPIOB,ENABLE);
//ADC时钟选择 APB2 6分频 72M/6=12M
RCC_ADCCLKConfig(RCC_ADCCLK_APB2_DIV6);
/* Configure the ADC pin */
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_7|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AN;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AN;
GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//开ADC时钟 定时器1 时钟
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_ADC1|RCC_APB2CCR_TIMER1EN,ENABLE);
TIMER1->CTLR1|=TIMER_CTLR1_ARSE|TIMER_CTLR1_DIR;//自动重装影子寄存器使能
TIMER1->CHE|=TIMER_CHE_CH1E|TIMER_CHE_CH1P;//通道1使能 通道1低电平有效
TIMER1->CHCTLR2|=TIMER_CTLR2_MMC_2;//通道1作为触发输出
TIMER1->PSC=35;//预分频器//(48M/1M-1)
TIMER1->CARL=499;//自装载计数器//500us-1;
TIMER1->CNT=0;//计数器
TIMER1->CHCTLR1=TIMER_CHCTLR1_CH1OM_1|TIMER_CHCTLR1_CH1OM_2|TIMER_CHCTLR1_CH1OSE;//通道1输出模式 PWM1模式
TIMER1->CHCC1=250;//通道1
TIMER1->BKDT|=TIMER_BKDT_POE;
NVIC_InitStructure.NVIC_IRQ = DMA1_Channel1_IRQn; //
NVIC_InitStructure.NVIC_IRQPreemptPriority = 0; //主优先
NVIC_InitStructure.NVIC_IRQSubPriority = 0; //次优先
NVIC_InitStructure.NVIC_IRQEnable = ENABLE; //使能
NVIC_Init(&NVIC_InitStructure);
DMA_InitParaStructure.DMA_PeripheralBaseAddr=(uint32_t)&ADC1->RDTR;
DMA_InitParaStructure.DMA_MemoryBaseAddr=(uint32_t)&RegularConvData_Tab[0];
DMA_InitParaStructure.DMA_DIR=DMA_DIR_PERIPHERALSRC;
DMA_InitParaStructure.DMA_BufferSize=7;
DMA_InitParaStructure.DMA_PeripheralInc=DMA_DIR_PERIPHERALSRC;
DMA_InitParaStructure.DMA_MemoryInc=DMA_MEMORYINC_ENABLE;
DMA_InitParaStructure.DMA_PeripheralDataSize=DMA_PERIPHERALDATASIZE_HALFWORD;
DMA_InitParaStructure.DMA_MemoryDataSize=DMA_MEMORYDATASIZE_HALFWORD;
DMA_InitParaStructure.DMA_Mode=DMA_MODE_CIRCULAR;
DMA_InitParaStructure.DMA_Priority=DMA_PRIORITY_VERYHIGH;
DMA_InitParaStructure.DMA_MTOM=DMA_MEMTOMEM_DISABLE;
DMA_Init(DMA1_CHANNEL1,&DMA_InitParaStructure);
DMA_INTConfig(DMA1_CHANNEL1,DMA1_FLAG_TC1,ENABLE);
DMA_Enable(DMA1_CHANNEL1,ENABLE);
ADC1->CTLR2|=(uint32_t)ADC_CTLR2_DMA;//启用DMA功能
ADC1->RSQ1|=ADC_RSQ1_RL_1|ADC_RSQ1_RL_2;//配置规则组个数
ADC1->RSQ3|=0|(1<<5)|(7<<10)|(8<<15)|(4<<20)|(5<<25);//配置规则组
ADC1->RSQ2|=6;//配置规则组
ADC1->SPT2|=ADC_SPT2_SPT1|ADC_SPT2_SPT2|
ADC_SPT2_SPT3|ADC_SPT2_SPT4|ADC_SPT2_SPT5|
ADC_SPT2_SPT6|ADC_SPT2_SPT7;//设置通道扫描时间
ADC1->CTLR1|=ADC_CTLR1_SM; //开启扫描模式
ADC1->CTLR2 |= ADC_CTLR2_ETERC;//开启规则组外部触发使能
ADC1->CTLR2|=ADC_CTLR2_ADCON;//开启ADC
ADC1->CTLR2|=ADC_CTLR2_RSTCLB;//复位ADC校准功能
while((ADC1->CTLR2&ADC_CTLR2_RSTCLB)!=0);
ADC1->CTLR2|=ADC_CTLR2_CLB;//开始ADC校准功能
while((ADC1->CTLR2&ADC_CTLR2_CLB)!=0);
}
|
|