本人刚学
STM32,想用stm32输出可调的PFM波,通过一个可调电阻来调节PFM波,但ADC不起作用,无论怎么调都还是输出300KHz的PFM波,即ADC为0,下面是程序,请大侠们指教。
int main(void)
{
u32 ad1=0;
u32 ad2=0;
RCC_Configura
tion();
GPIO_Configuration();
ADC_Configuration();
Tim1_Configuration();
while(1)
{
ad1=ADC_GetConversionValue(ADC1);
ad2=ad1*3.3/4095;
if(ad2>3)
{
TIM_SetAutoreload(TIM1,119);
TIM_SetCompare1(TIM1,60); //设定占空比
}
if((ad2>=2.5)&&(ad2<=3))
{
TIM_SetAutoreload(TIM1,99);
TIM_SetCompare1(TIM1,50); //设定占空比
}
if((ad2>=2)&&(ad2<=2.5))
{
TIM_SetAutoreload(TIM1,79);
TIM_SetCompare1(TIM1,40); //设定占空比
}
if((ad2>=1.5)&&(ad2<=2))
{
TIM_SetAutoreload(TIM1,59);
TIM_SetCompare1(TIM1,30); //设定占空比
}
if(ad2<1.5)
{
TIM_SetAutoreload(TIM1,39);
TIM_SetCompare1(TIM1,20); //设定占空比
}
}
}
void GPIO_Configuration(void) //端口初始程序,可初始化多组管脚,串口要用到PA9、10做输入输出
{
/*输出管脚初始化*/
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
/*初始化相应的GPIO*/
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 输入管脚初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// 初始化相应的GPIO
GPIO_Init(GPIOB, &GPIO_InitStructure);
//ADC输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void RCC_Configuration(void) //时钟初始程序
{ SystemInit(); //系统时钟频率72MHz /*打开GPIOX使用的GPIO的时钟使能*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//6分频为12M}
void Tim1_Configuration(void)
{ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //第三步,定时器基本配置
TIM_TimeBaseStructure.TIM_Period=150-1; // 自动重装载寄存器的值
TIM_TimeBaseStructure.TIM_Prescaler=6-1; // 时钟预分频数
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // 采样分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //第四步pwm输出配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //设置为pwm1输出模式
TIM_OCInitStructure.TIM_Pulse=75; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //设置输出极性 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出 //下面几个参数是高级定时器才会用到,通用定时器不用配置 TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //设置互补端输出极性
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//使能互补端输出
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //死区后输出状态
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;//死区后互补端输出状态
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //按照指定参数初始化 //第五步,死区和刹车功能配置,高级定时器才有的,通用定时器不用配置 TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;//运行模式下输出选择
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;//空闲模式下输出选择
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //锁定设置
TIM_BDTRInitStructure.TIM_DeadTime = 42; //死区时间设置
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;//刹车输入极性
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//自动输出使能
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure); //第六步,使能端的打开
TIM_Cmd(TIM1,ENABLE); //打开TIM2
TIM_CtrlPWMOutputs(TIM1, ENABLE); //pwm输出使能,一定要记得打开}
void ADC_Configuration(void) //配置数模转换
{ ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//多通道扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//循环采样
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//转换由软件而不是外部触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1; //转换通道数量
ADC_Init(ADC1, &ADC_InitStructure); //定义规则组
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);//ADC使能
ADC_ResetCalibration(ADC1); //ADC复位
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1); //ADC校准
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//启动ADC}