-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPWM.c
More file actions
52 lines (42 loc) · 1.48 KB
/
Copy pathPWM.c
File metadata and controls
52 lines (42 loc) · 1.48 KB
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
42
43
44
45
46
47
48
49
50
51
52
#include "stm32f10x.h" // Device header
#define LED_PIN GPIO_Pin_0
uint8_t CCR_Value=50;
void PWM_Init(void)
{
// 外設時鐘始能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// GPIO初始化
GPIO_InitTypeDef GPIO_InitTypeDefStructure;
GPIO_InitTypeDefStructure.GPIO_Mode=GPIO_Mode_AF_PP; // 復用推挽輸出
GPIO_InitTypeDefStructure.GPIO_Pin=LED_PIN;
GPIO_InitTypeDefStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitTypeDefStructure);
// 配置時鐘源
TIM_InternalClockConfig(TIM2);
// 時基單元配置
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure);
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=100-1; // ARR
TIM_TimeBaseInitStructure.TIM_Prescaler=720-1; // PSC
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
// 輸出比較單元配置
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR_Value; // CCR
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
// TIM使能
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare1(uint16_t Compare)
{
TIM_SetCompare1(TIM2,Compare);
}
uint16_t PWM_GetCapture(void)
{
return TIM_GetCapture1(TIM2);
}