void Timer2_Config_Init(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2,ENABLE); //时钟配置
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_Prescaler_1, TIM2_CounterMode_Up,16*624); //1分频,向上计数,每625us定时中断一次
TIM2_ARRPreloadConfig(ENABLE);
// TIM2_SelectOutputTrigger(TIM2_TRGOSource_Update);
// TIM2_SelectOutputTrigger(TIM2_TRGOSource_Enable);
// TIM2_ITConfig(TIM2_IT_Update , ENABLE);
TIM2_ITConfig(TIM2_IT_Update , DISABLE);
}
void Ad_Config_Init(void)
{
ADC_DeInit(ADC1);
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE);//打开ADC1外设的时钟
GPIO_Init(GPIOA, GPIO_Pin_6, GPIO_Mode_In_FL_No_IT);//电源
GPIO_Init(GPIOB, GPIO_Pin_0, GPIO_Mode_In_FL_No_IT);//电流采样
GPIO_Init(GPIOB, GPIO_Pin_1, GPIO_Mode_In_FL_No_IT);//参考电压采样
delay_us(20);
ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_12Bit, ADC_Prescaler_1);//初始化ADC1,模式,12位分辨率,ADC时钟1分频
ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles);//配置ADC采样时间,384个时钟周期
ADC_VrefintCmd(DISABLE); //禁止内部电压基准源
ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1,ENABLE); //使能ADC1
ADC_ChannelCmd(ADC1, ADC_Channel_0, ENABLE);//打开ADC1的0通道
ADC_ChannelCmd(ADC1, ADC_Channel_17, ENABLE);//打开ADC1的17通道
ADC_ChannelCmd(ADC1, ADC_Channel_18, ENABLE);//打开ADC1的18通道
}
#define ADC1_DR_ADDRESS ((uint16_t)0x5344)
#define BUFFER_SIZE ((uint8_t) 0x03)
#define BUFFER_ADDRESS ( (uint32_t)(&Buffer))
uint16_t Buffer[3];
void DMA1_Config_Init(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel0);
DMA_GlobalDeInit();
SYSCFG_REMAPDMAChannelConfig(REMAP_DMA1Channel_ADC1ToChannel0);
DMA_Init(DMA1_Channel0,
BUFFER_ADDRESS,//缓冲区地址
ADC1_DR_ADDRESS,//ADC数据寄存器地址
BUFFER_SIZE,//缓冲区大小
DMA_DIR_PeripheralToMemory,//外设到内存
DMA_Mode_Circular,//循环转换
DMA_MemoryIncMode_Inc,//内存地址自增
DMA_Priority_High,//优先级为高
DMA_MemoryDataSize_HalfWord );//内存数据是半字位宽
DMA_SetCurrDataCounter(DMA1_Channel0,BUFFER_SIZE);//设置转速的数据个数
DMA_Cmd(DMA1_Channel0, ENABLE); //DMA Channel0 enable
DMA_ITConfig(DMA1_Channel0, DMA_ITx_TC, ENABLE); // Enable DMA1 channel0 Transfer complete interrupt
DMA_GlobalCmd(ENABLE); //DMA enable
}
INTERRUPT_HANDLER(DMA1_CHANNEL0_1_IRQHandler,2)
{
unsigned char i=0;
// ADC_Cmd(ADC1,DISABLE);
if(DMA_GetFlagStatus(DMA1_FLAG_TC0)!= RESET)
{
}
DMA_ClearITPendingBit(DMA1_IT_TC0);
}
INTERRUPT_HANDLER(TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler,19)
{
if(flg==0)
{
Vcc_Swich_On;
flg=1;
}
else
{
Vcc_Swich_Off;
flg=0;
}
TIM2_ClearITPendingBit(TIM2_IT_Update);
}
void Time_Adc_Dma_Init(void)
{
Timer2_Config_Init();
Ad_Config_Init();
DMA1_Config_Init();
ADC_ExternalTrigConfig(ADC1,ADC_ExtEventSelection_Trigger2,ADC_ExtTRGSensitivity_Falling);
TIM2_SelectOutputTrigger(TIM2_TRGOSource_Update);
TIM2_Cmd(ENABLE);
// ADC_SoftwareStartConv(ADC1);
}
|