void PWM_Configuration(void)
{
// (1)初始化结构体
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructInit; //ê±»ù3õê¼»ˉ
TIM_OCInitTypeDef TIM_OCStructInit; // PWM±è½Ï¼Ä′æÆ÷
GPIO_InitTypeDef GPIO_InitStructure; //GPIO
// (2)使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3,ENABLE);
//
//GPIO_AFIODeInit();
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE); //ÖØó3éä
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2 , ENABLE); //ÖØó3éä
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2 , ENABLE);
// (3)配置GPIO
/*GPIOA15*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_15;//Ñ¡ÔñòaóÃμÄGPIOòy½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //éèÖÃòy½ÅÄ£ê½ÎaíÆÃaêä3öÄ£ê½
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//éèÖÃòy½ÅËù¶èÎa50MHZ
GPIO_Init(GPIOA, &GPIO_InitStructure);//μ÷óÿaoˉêy£¬3õê¼»ˉGPIO
/*GPIOB3 4*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3 | GPIO_Pin_4;//Ñ¡ÔñòaóÃμÄGPIOòy½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //éèÖÃòy½ÅÄ£ê½ÎaíÆÃaêä3öÄ£ê½
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//éèÖÃòy½ÅËù¶èÎa50MHZ
GPIO_Init(GPIOB, &GPIO_InitStructure);//μ÷óÿaoˉêy£¬3õê¼»ˉGPIO
/*
GPIO_AFIODeInit(); //
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE); //ÖØó3éä
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2 , ENABLE); //ÖØó3éä
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2 , ENABLE);
*/
/*=========================================*/
// (4)定时器
/*=========================================*/
TIM_DeInit(TIM3);
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit.TIM_Period = 9999; //ÖØ×°Öμ,¼′ò»¸ö¶¨ê±ÖüÆú
TIM_TimeBaseStructInit.TIM_Prescaler = 0; //Ô¤·ÖÆμ
TIM_TimeBaseStructInit.TIM_ClockDivision = 0; //ê±Öó·ÖÆμÏμêy
TIM_TimeBaseStructInit.TIM_CounterMode = TIM_CounterMode_Up; //ÏòéÏ′¥·¢
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructInit);//3õê¼»ˉTIM3
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructInit);//3õê¼»ˉTIM2
// (5)PWM配置
TIM_OCStructInit.TIM_OCMode = TIM_OCMode_PWM1;//ÅäÖÃÎaÄ£ê½1
TIM_OCStructInit.TIM_OutputState = TIM_OutputState_Enable; //¿aÆôêä3öÄ£ê½
//TIM_OCStructInit.TIM_Pulse = 10000; //éèÖÃÕ¼¿Õ±è50%
TIM_OCStructInit.TIM_OCPolarity = TIM_OCPolarity_High;//éèÖÃ
WMóDD§μÄμçƽÎa¸ß
// (6)开启pem通道
TIM_OC1Init(TIM3,&TIM_OCStructInit);
TIM_OC1Init(TIM2,&TIM_OCStructInit);
TIM_OC2Init(TIM2,&TIM_OCStructInit);
// (7)使能预装载寄存器
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
// (8)使能重载寄存器
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_ARRPreloadConfig(TIM2,ENABLE);
// (9)开启TIM
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
我不太清楚这个芯片的复用引脚的部分映射和完全映射,查手册也只写了是复用还是默认,配置出来一直都有问题呢,占空比好像完全没作用
还有虽然有电流通过,但是这个芯片上是想用PA15 , PB3 , PB4 来作为pwm输出,但是PB3一直没有电流,这是怎么回事呢,弄 了几天了,看了很多例程好像都是
这么配置的啊,有没有大神帮我指点一下,急~~谢谢了
气哭~~我也觉得,请问你怎么知道jtag占用了它呢
嗯嗯,就是三楼说的那个问题
遇到过
忧桑会儿
一周热门 更多>