void GPIO_Ad_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(GPIO_gg_PORT, ENABLE);
GPIO_InitStructure.GPIO_Pin = gg ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(gg_PORT, &GPIO_InitStructure);
GPIO_SetBits(gg_PORT,gg);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = ad_0 | ad_1 | ad_2 |ad_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //通道扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //注意不要使用持续转换模式,否则只要触发一次,//后续的转换就会永不停歇(除非CONT清0),这样第一次以后的ADC,就不是由TIM2_CC2来触发了
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;//配置TIM2_CC2为触发源
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 4;
ADC_Init(ADC1, &ADC_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//配置时钟(12MHz),在RCC里面还应配置APB2=AHB时钟72MHz,
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5); //通道4,转换次序,转换时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_239Cycles5); //通道5,转换次序,转换时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_239Cycles5); //通道6,转换次序,转换时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_239Cycles5); //通道7,转换次序,转换时间
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1); //Start Calibration register
while(ADC_GetCalibrationStatus(ADC1)); //waiting for finishing the calibration
ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); //使能ADC中断
ADC_ExternalTrigConvCmd(ADC1, ENABLE); //设置外部触发模式使能(这个“外部“其实仅仅是相//对于ADC模块的外部,实际上还是在STM32内部)
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//先占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//通用定时器2中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器2!
void TIM2_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
TIM_DeInit(TIM2);//定时器TIM2初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //下面详细说明
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse = 0x7F; /////
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //如果是PWM1要为Low,PWM2则为High
TIM_OC2Init(TIM2, & TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_UpdateDisableConfig(TIM2, DISABLE);
// TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM2中断,允许更新中断
//中断优先级NVIC设置
// NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
// NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
// TIM_Cmd(TIM2, ENABLE); //使能TIMx
}
uint8_t h_flg=0;
uint8_t cout=0;
void ADC1_2_IRQHandler(void)
{
if(ADC_GetITStatus(ADC1,ADC_IT_EOC) != RESET)
{
ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
cout++;
if(cout>64)
{
if(h_flg==0)
{
GPIO_SetBits(gg_PORT,gg);
h_flg=1;
}
else
{
GPIO_ResetBits(gg_PORT,gg);
h_flg=0;
}
cout=0;
}
}
}
转载
|