TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular); /*Source of update is only counter overflow/underflow*/
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular); /*Source of update is only counter overflow/underflow*/
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Master Mode selection. TIM2 sets Enable as trigger output, for gating TIM1 */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Enable);
/* Slave Mode selection */
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0); // from TIM1
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1); //clocked by update of TIM1
---------------------------------
看我的代码试试!
---------------------------------
#define PrescalerValue 57
#define PWM_Preload 36
/* rivate functions ---------------------------------------------------------*/
void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/*GPIOA Configuration: TIM1 channel 1,remap to A.8 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置定时器定时周期,计数模式为向上 */
TIM_TimeBaseStructure.TIM_Period = (u32)PrescalerValue;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular); /*Source of update is only counter overflow/underflow*/
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = WM_Preload;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* Master Mode selection. TIM1 sets update as trigger output, act as prescaler to count TIM2 */
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
/* Slave Mode selection */
TIM_SelectInputTrigger(TIM1, TIM_TS_ITR1); // from TIM2
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated); //gated by enable of TIM2
/* Update TIM1*/
TIM_GenerateEvent(TIM1,TIM_EventSource_Update);
/*enable TIM1*/
TIM_Cmd(TIM1, ENABLE);
}
/****------------------------------------------------------------------------------********/
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* 配置定时器定时周期,计数模式为向上 */
TIM_TimeBaseStructure.TIM_Period = 10000;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular); /*Source of update is only counter overflow/underflow*/
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Master Mode selection. TIM2 sets Enable as trigger output, for gating TIM1 */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Enable);
/* Slave Mode selection */
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0); // from TIM1
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1); //clocked by update of TIM1
/* One ulse Mode selection ,TIM2 stop when overflow ocuur*/
TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
/* Update TIM2*/
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
/* TIM2 IT enable */
TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);
}
启动PWM则是:
TIM_SetAutoreload(TIM2,N-1); //N为预设PWM个数
TIM_GenerateEvent(TIM2,TIM_EventSource_Update);
TIM_Cmd(TIM2, ENABLE);
---------------------------------
谢谢楼主
一周热门 更多>