Ejemplo n.º 1
0
static void Motor_TIM3_Config(void)
{
	uint16_t period, prescaler;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 

	Timer_16bit_Calc(MOTOR_STEP_FREQ, &period, &prescaler);
	/* Time base configuration */		 
	TIM_TimeBaseStructure.TIM_Period = period;
	TIM_TimeBaseStructure.TIM_Prescaler = prescaler;	        //设置预分频
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;                //设置时钟分频系数
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式

	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;       //配置为PWM模式1
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;   
    TIM_OCInitStructure.TIM_Pulse = (period+1)/2-1;      //设置跳变值,当计数器计数到这个值时,电平发生跳变
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //当定时器计数值小于CCR1_Val时为高电平

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);

    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

}
Ejemplo n.º 2
0
/*
 * 初始化PWM模块,设定脉冲频率(Hz)
 */
void PWM_Init(int freq)
{
	uint16_t period, prescaler;

	Timer_16bit_Calc(freq, &period, &prescaler);

	TIMx_Config(TIM2, period, prescaler);
	currentPeriod = period;

	for(int i=1; i<=4; i++)
		PWM_Channel(i, 50, false);

	TIMx_Enable(TIM2);

	// TIM2_SetInterrupt();
}