小学生
最后登录1970-1-1
在线时间 小时
注册时间2015-3-26
|
如题,本人目前需要配置stm32的timer3与timer8输出PWM,现在单独配置timer8时候有输出,但是加入timer3后,timer8就没有输出了。这时候timer3是有输出的。比较困惑。timer3用的pb0和pb1,timer8用的是PC6-9,查看手册这几个timer都是独立的,没有交集。请高手指点。谢谢!
附上代码:
/********************************TIMER 3**********************************/
static void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* éèÖÃTIM3CLK Îa 72MHZ */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
/*GPIOA Configuration: TIM3 channel 1 to 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMDÅoÅμçƽìø±äÖμ */
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 999; //챶¨ê±Æ÷′ó0¼Æêyμ½999£¬¼′Îa1000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
TIM_TimeBaseStructure.TIM_Prescaler = 0; //éèÖÃÔ¤·ÖÆμ£o2»Ô¤·ÖÆ죬¼′Îa72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃÎaPWMÄ£ê½1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //éèÖÃí¨μà3μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà3
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //éèÖÃí¨μà4μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //ê1Äüí¨μà4
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); // ê1ÄüTIM3ÖØÔؼÄ′æÆ÷ARR
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE); //ê1Äü¶¨ê±Æ÷3
}
void TIM3_PWM_Init(void)
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
/********************************TIMER 8**********************************/
static void TIM8_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM8, ENABLE);
/*GPIOA Configuration: TIM8 channel 1 to 4 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
static void TIM8_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* PWMDÅoÅμçƽìø±äÖμ */
u16 CCR1_Val = 2;
u16 CCR2_Val = 4;
u16 CCR3_Val = 6;
u16 CCR4_Val = 8;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 9; //챶¨ê±Æ÷′ó0¼Æêyμ½999£¬¼′Îa1000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
TIM_TimeBaseStructure.TIM_Prescaler = 72; //éèÖÃÔ¤·ÖÆμ£o2»Ô¤·ÖÆ죬¼′Îa72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //ÅäÖÃÎaPWMÄ£ê½1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖμê±£¬μçƽ·¢éúìø±ä
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //ê1Äüí¨μà1
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //éèÖÃí¨μà2μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //ê1Äüí¨μà2
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //éèÖÃí¨μà3μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC3Init(TIM8, &TIM_OCInitStructure); //ê1Äüí¨μà3
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //éèÖÃí¨μà4μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμÄWM
TIM_OC4Init(TIM8, &TIM_OCInitStructure); //ê1Äüí¨μà4
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM8, ENABLE); // ê1ÄüTIM8ÖØÔؼÄ′æÆ÷ARR
/* TIM8 enable counter */
TIM_Cmd(TIM8, ENABLE); //ê1Äü¶¨ê±Æ÷8
TIM_CtrlPWMOutputs(TIM8, ENABLE);
}
void TIM8_PWM_Init(void)
{
TIM8_GPIO_Config();
TIM8_Mode_Config();
}
|
|