本帖最后由 牧马人人 于 2019-4-11 08:08 编辑
本人参照定时器3的配置方法,将定时器2,4,5配置,定时器3,4能够正常输出4路PWM控制舵机,但是定时器5无PWM输出,其中定时器2为了避免与定时器5引脚冲突,定时器2采用完全重映射将引脚映射到其他空闲引脚,映射后的四个引脚只有PB10,PB11正常输出PWM;另外两个引脚不输出。两个高级定时器time1和time8配置后也无PWM输出。求大佬指点
定时器1,2,3,4,5,8配置程序如下:
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA , ENABLE); //??GPIO??????
//????????????,??TIM1 CH1 CH2 CH3 CH4?PWM????
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //Timer1ÍêÈ«ÖØÓ³Éä
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14; //TIM1 CH1 CH2 CH3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; //TIM1 CH1 CH2 CH3
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??????
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //???????????????????????????
TIM_TimeBaseStructure.TIM_Prescaler =psc; //??????TIMx??????????? ???
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM??????
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //??TIM_TimeBaseInitStruct?????????TIMx???????
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //???????:TIM????????1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //??????
// TIM_OCInitStructure.TIM_Pulse = 0; //????????????????
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //????:TIM???????
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE ?????
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1?????
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH2?????
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH3?????
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH3?????
TIM_ARRPreloadConfig(TIM1, ENABLE); //??TIMx?ARR????????
TIM_Cmd(TIM1, ENABLE); //??TIM1
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ CH1--PA8 CH2--PA9 CH3--PA10 CH4--PA11
// ÍêÈ«ÖØÓ³Éäºó CH1--PE9 CH2--PE11 CH3--PE13 CH4--PE14
}
void TIM2_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ʹÄܶ¨Ê±Æ÷2ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->
B5
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM2 CH1--4 µÄ
WMÂö³å²¨ÐÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10|GPIO_Pin_11; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
//³õʼ»¯TIM2
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ ###//¸Ä¶¯¼«ÐÔ //###
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM2, ENABLE); //ʹÄÜTIM2
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ CH1--PA0 CH2--PA1 CH3--PA2 CH4--PA3
}
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ʹÄܶ¨Ê±Æ÷3ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->
B5
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄ
WMÂö³å²¨ÐÎ GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
//³õʼ»¯TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
//³õʼ»¯TIM3 Channel2 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ ###//¸Ä¶¯¼«ÐÔ //###
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM3, ENABLE); //ʹÄÜTIM3
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ CH1--PA6 CH2--PA7 CH3--PB0 CH4--PB1
}
void TIM4_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ʹÄܶ¨Ê±Æ÷4ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //Timer1ÍêÈ«ÖØÓ³Éä TIM3_CH2->
B5
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->
B5
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM4 CH2µÄ
WMÂö³å²¨ÐÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIO
//³õʼ»¯TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
//³õʼ»¯TIM4 Channel2 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ ###//¸Ä¶¯¼«ÐÔ //###
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
TIM_OC4Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM4, ENABLE); //ʹÄÜTIM4
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ CH1--PB6 CH2--PB7 CH3--PB8 CH4--PB9
}
void TIM5_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //ʹÄܶ¨Ê±Æ÷2ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer3²¿·ÖÖØÓ³Éä TIM3_CH2->
B5
//ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM2 CH1--4 µÄ
WMÂö³å²¨ÐÎ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
//³õʼ»¯TIM2
TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
//³õʼ»¯TIM2 Channel2 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ ###//¸Ä¶¯¼«ÐÔ //###
TIM_OC1Init(TIM5, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
TIM_OC2Init(TIM5, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
TIM_OC3Init(TIM5, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
TIM_OC4Init(TIM5, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4
TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable); //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
TIM_Cmd(TIM5, ENABLE); //ʹÄÜTIM2
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ CH1--PA0 CH2--PA1 CH3--PA2 CH4--PA3
}
void TIM8_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //??GPIO??????
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //Timer1ÍêÈ«ÖØÓ³Éä TIM3_CH2->
B5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //TIM1 CH1 CH2 CH3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //???????????????????????????
TIM_TimeBaseStructure.TIM_Prescaler =psc; //??????TIMx??????????? ???
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM??????
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //??TIM_TimeBaseInitStruct?????????TIMx???????
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //???????:TIM????????1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //??????
TIM_OCInitStructure.TIM_Pulse = 0; //????????????????
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //????:TIM???????
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC3Init(TIM8, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_OC4Init(TIM8, &TIM_OCInitStructure); //??TIM_OCInitStruct???????????TIMx
TIM_CtrlPWMOutputs(TIM8,ENABLE); //MOE ?????
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //CH1?????
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //CH2?????
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //CH3?????
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //CH3?????
TIM_ARRPreloadConfig(TIM8, ENABLE); //??TIMx?ARR????????
TIM_Cmd(TIM8, ENABLE); //??TIM1
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ CH1--PA8 CH2--PA9 CH3--PA10 CH4--PA11
// ÍêÈ«ÖØÓ³Éäºó CH1--PE9 CH2--PE11 CH3--PE13 CH4--PE14
}
一周热门 更多>