sjtitr 发表于 2014-4-23 15:51
这两个函数里面的内容还可以展开一下不?
附件里有。
void Timer_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure ; //声明结构体
NVIC_InitTypeDef NVIC_InitStructure; //声明中断结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE); //打开TIM2外设时钟
//**************************************************************************
// 定时器2设置: 720分频,2ms中断一次,向上计数
//**************************************************************************
TIM_TimeBaseStructure.TIM_Period = 199;
TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //初始化定时器
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //开定时器中断
TIM_Cmd(TIM2, ENABLE); //使能定时器
// 使能TIM2中断
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//**********************************************
//函数名称: void PWM_TIM1_init(void)
//函数作用:
//**********************************************
void PWM_TIM1_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure ; //声明结构体
TIM_OCInitTypeDef TIM_OCInitStructure; //声明通道结构体
GPIO_InitTypeDef GPIO_InitStructure; //声明IO结构体
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); //GPIOE时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); //TIM时钟
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); // 使能重定义
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //选择管脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //配置输出速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //管脚模式为推挽输出
GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化端口
TIM_TimeBaseStructure.TIM_Period = 499; //500us
TIM_TimeBaseStructure.TIM_Prescaler = 71; //主时钟72分频,1MHZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //TIM1初始化
TIM_ARRPreloadConfig(TIM1,ENABLE); //启动ARR影子寄存器
//通道1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //通道1 ,高有效
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //通道1 ,高有效
TIM_OCInitStructure.TIM_Pulse = 250; //占空比50%
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //初始化1通道
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//启动CCR1寄存器的影子寄存器
TIM_Cmd(TIM1, ENABLE); //开启TIM1
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}