搞这个东西搞了一个下午还没搞出来了 我好菜呀
上代码

TIM14的初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void TIM14_PWM_Init(u32 arr,u32 psc)
{
//´Ë²¿·ÖÐèÊÖ¶¯ÐÞ¸ÄIO¿ÚÉèÖÃ

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14ʱÖÓʹÄÜ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //ʹÄÜPORTFʱÖÓ

GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9¸´ÓÃΪ¶¨Ê±Æ÷14

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
GPIO_Init(GPIOF,&GPIO_InitStructure); //³õʼ»¯PF9

TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷14

//³õʼ»¯TIM14 Channel1 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ
TIM_OC1Init(TIM14, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM1 4OC1

TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //ʹÄÜTIM14ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷

TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEʹÄÜ

TIM_Cmd(TIM14, ENABLE); //ʹÄÜTIM14


}

那么 改变相应的PWM值就用 TIM_SetCompare1(TIM14,led0pwmval);

TIM13的初始化

orz一开始怎么也写不对 后来忽然就对了
两者几乎是一样的orz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void TIM13_PWM_Init(u32 arr,u32 psc)
{
//´Ë²¿·ÖÐèÊÖ¶¯ÐÞ¸ÄIO¿ÚÉèÖÃ

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE); //TIM14ʱÖÓʹÄÜ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //ʹÄÜPORTFʱÖÓ

GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM13); //GPIOF9¸´ÓÃΪ¶¨Ê±Æ÷14

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ­
GPIO_Init(GPIOF,&GPIO_InitStructure); //³õʼ»¯PF9

TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseInit(TIM13,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷14

//³õʼ»¯TIM14 Channel1 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ
TIM_OC1Init(TIM13, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM1 4OC1

TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable); //ʹÄÜTIM14ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷

TIM_ARRPreloadConfig(TIM13,ENABLE);//ARPEʹÄÜ

TIM_Cmd(TIM13, ENABLE); //ʹÄÜTIM14


}

补 居然发现复用引脚是特定的 能在这么多引脚里找到我也是真厉害orz

最后更新: 2019年05月01日 14:05

原始链接: http://yoursite.com/2019/04/06/PWM波的配置/

× 请我吃糖~
打赏二维码