/******************************************************************************************
* 函数名称 : TIM3_Configuration
* 功能描述 : TIM3初始化函数
* 参数 : 无
* 返回值 : 无
* 全局变量 : 无
* 全局静态变量: 无
* 局部静态变量: 无
******************************************************************************************/
void TIM3_Configuration( )
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //打开APB2外设
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //开时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //PA6 PA7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //要改为 AF_PP
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_DeInit(TIM3); //将TIM3寄存器设为缺省值
TIM_TimeBaseStructure.TIM_Period=72-1; //ARR的值,计数周期
TIM_TimeBaseStructure.TIM_Prescaler=36-1; //分频 CK_INT=2MHz 36分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化定时器3
//通道1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能
TIM_OCInitStructure.TIM_Pulse = 1-1 ; //待捕获比较寄存器的值 CRR
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //通道初始化
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //预装载使能
//通道2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能
TIM_OCInitStructure.TIM_Pulse = 36-1 ; //待捕获比较寄存器的值 CRR
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平有效
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //通道初始化
TIM3->CNT = 0;
TIM_Cmd(TIM3, ENABLE);//打开定时器
}
|