中级会员
- 积分
- 235
- 金钱
- 235
- 注册时间
- 2015-1-22
- 在线时间
- 71 小时
|
1金钱
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_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();
DMA_Init(DMA1_Channel0,BUFFER_ADDRESS,
ADC1_DR_ADDRESS,BUFFER_SIZE,
DMA_DIR_PeripheralToMemory,
DMA_Mode_Circular,
DMA_MemoryIncMode_Inc,
DMA_Priority_High,
DMA_MemoryDataSize_HalfWord );
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_OC1);
TIM2_Cmd(ENABLE);
}
void main(void)
{
sim();//关中断
CLK_DeInit();
CLK_HSICmd(ENABLE);//开始内部高频RC 16M
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);//HSI为系统时钟
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); //1分频,8M
GPIO_Init(Vcc_PORT, Vcc_Swich_PIN, GPIO_Mode_Out_OD_Low_Fast);
Vcc_Swich_On;
Time_Adc_Dma_Init();
enableInterrupts();
while (1)
{
}
}
|
|