[mw_shl_code=c,true]
http://www.openedv.com/posts/list/0/48514.htm?privmsg=33039&&sysid=4#316814 [/mw_shl_code]
[mw_shl_code=c,true]我以前曾经在上面的链接里分享过一个永级定时器产生pwm控制电机的方法。[/mw_shl_code]
[mw_shl_code=c,true]这次做硬件引脚改了一下,把原来帖子里的timer2 和timer3 倒着用,结果就不灵了,压根没有波形输出。执行发现根本没有产生定时器中断。[/mw_shl_code]
[mw_shl_code=c,true]那位朋友眼尖的帮忙看看哪里改得不对?[/mw_shl_code]
[mw_shl_code=c,true][/mw_shl_code]
[mw_shl_code=c,true]#define PWM_BASE 36000 //2MS CYCLE,SO That it can start up slower
#define PWM_BASE_EXT (54000) //1.5 CYCLE long for zero pulse
#define PIN_DIR GPIO_Pin_5
#define PIN_STEP GPIO_Pin_6
char hasInterrupt=0;
char prevdir=3; //preview motor dir
/*---------------------------------------------------------------------------------------------------------------------------------------*/
/* Just make parameter name shorter */
#define _GetInternalTrigger(S,M) \
(S == TIM1 ? (M == TIM5 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM3 ? TIM_TS_ITR2

M == TIM4 ? TIM_TS_ITR3 : (u16)-1)))): \
(S == TIM8 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM4 ? TIM_TS_ITR2

M == TIM5 ? TIM_TS_ITR3 : (u16)-1)))): \
(S == TIM2 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM8 ? TIM_TS_ITR1 : (M == TIM3 ? TIM_TS_ITR2

M == TIM4 ? TIM_TS_ITR3 : (u16)-1)))): \
(S == TIM3 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM5 ? TIM_TS_ITR2

M == TIM4 ? TIM_TS_ITR3 : (u16)-1)))): \
(S == TIM4 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM3 ? TIM_TS_ITR2

M == TIM8 ? TIM_TS_ITR3 : (u16)-1)))): \
(S == TIM5 ? (M == TIM2 ? TIM_TS_ITR0 : (M == TIM3 ? TIM_TS_ITR1 : (M == TIM4 ? TIM_TS_ITR2

M == TIM8 ? TIM_TS_ITR3 : (u16)-1)))): \
(u16)-1))))))
#define GetInternalTrigger(SlaveTimer,MasterTimer) _GetInternalTrigger(SlaveTimer,MasterTimer)
#define compile_assert(exp) {extern char lxyppc_at_163_dot_com[(exp) ? 1:-1];}
void pwm_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM2 clock enable */
//RCC_PCLK1Config(RCC_HCLK_Div16); // USB SETTIN IT AS PCLK1CFG DIV2 IN hw_config.c
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin = PIN_STEP; //TIM3 CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM2 Main Output Enable */
//-------------------------------------------------------------------
/* Setup timer3 channel 1, Timer3 is slave timer
This timer is used to output waveforms */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 7;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = PWM_BASE_EXT;//PWM_BASE*2;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
/* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = PWM_BASE_EXT;//PWM_BASE*2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
}
//-----------------------------------------------------------------------
/* Setup Timer2 channel 3, Timer2 is master timer
This timer is used to control the waveform count of timer1 */
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_TimeBaseStructure.TIM_Prescaler =7;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period =PWM_BASE ;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse =PWM_BASE; // 1 pulse shorter them 2 ms ;36 second has one more 2ms cycle
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn ; //选择定时器TIM3
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //选择抢先式优先级(与中断嵌套级别有关)
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //选择子优先级(同抢先式优先级的响应顺序)
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //选择使能中断源
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_CC3); //中断标志位清零
TIM_ITConfig(TIM2, TIM_IT_CC3,ENABLE); //允许更新中断
}
}
//--------------------------------------------------------------------------
/* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave
timer1 is work under gate control mode, and controled by timer3
timer3's channel 4 is used as the control signal
*/
/* Enable timer's master/slave work mode */
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
/* timer3's channel 4 is used as the control signal */
//TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC3Ref );
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Enable);
/* Check the master/slave is valid or not */
compile_assert((u16)GetInternalTrigger(TIM3,TIM2) != (U16)-1);
/* Config timer1's external clock */
TIM_ITRxExternalClockConfig(TIM3, GetInternalTrigger(TIM3,TIM2));
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Gated);
/* Enable the slave tiemr*/
//TIM_Cmd(TIM2,ENABLE);
//-----------------------------------------------------------------------------
TIM3->CR1|=1; //we should enable it after
TIM2->CR1|=1;
//---------------------------------------------------------------------------
//set ios set speed select and
}[/mw_shl_code]