RCC_PCLK2Config(RCC_HCLK_Div1);
//设置低速AHB时钟(APB1)为HCLK的2分频
RCC_PCLK1Config(RCC_HCLK_Div2);
//设置FLASH代码延时
FLASH_SetLatency(FLASH_Latency_2);
//使能预取指缓存
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//设置PLL时钟,为HSE的9倍频8MHz * 9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//使能PLL
RCC_PLLCmd(ENABLE);
//等待PLL准备就绪
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//设置PLL为系统时钟源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//判断PLL是否是系统时钟
while(RCC_GetSYSCLKSource() != 0x08);
}
//开启TIM3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//开启GPIOB的时钟和复用功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_cfg()
{
GPIO_InitTypeDef GPIO_InitStructure;
//部分映射,将TIM3_CH2映射到PB5
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
//选择引脚5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
//输出频率最大50MHz
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//复用推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void TIMER_cfg()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//重新将Timer设置为缺省值
TIM_DeInit(TIM3);
//采用内部时钟给TIM3提供时钟源
TIM_InternalClockConfig(TIM3);
//预分频系数为0,即不进行预分频,此时TIMER的频率为72MHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
//设置时钟分割
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
//设置计数器模式为向上计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//设置计数溢出大小,每计7200个数就产生一个更新事件,即PWM的输出频率为10kHz
TIM_TimeBaseStructure.TIM_Period = 7200 - 1;
//将配置应用到TIM3中
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
}
void PWM_cfg()
{
TIM_OCInitTypeDef TimOCInitStructure;
//设置缺省值
TIM_OCStructInit(&TimOCInitStructure);
//PWM模式1输出
TimOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//设置占空比,占空比=(CCRx/ARR)*100%或(TIM_Pulse/TIM_Period)*100%
TimOCInitStructure.TIM_Pulse = dutyfactor * 7200 / 100;
//TIM输出比较极性高
TimOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//使能输出状态
TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//TIM3的CH2输出
TIM_OC2Init(TIM3, &TimOCInitStructure);
//设置TIM3的PWM输出为使能
TIM_CtrlPWMOutputs(TIM3,ENABLE);
}