1 | void PWMSignalControl(void)
|
2 | {
|
3 |
|
4 | /* TIM1 channels Configuration in PWM mode */
|
5 | TIM_Config();
|
6 |
|
7 | /* COMP2 Configuration */
|
8 | COMP_Config();
|
9 |
|
10 | /* Infinite loop */
|
11 | while (1)
|
12 | {
|
13 | }
|
14 | }
|
15 | void TIM_Config(void)
|
16 | {
|
17 |
|
18 | TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
|
19 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
20 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
21 | GPIO_InitTypeDef GPIO_InitStructure;
|
22 |
|
23 | /* GPIOA clock enable */
|
24 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
25 |
|
26 | /* TIM1 channels pin configuration:
|
27 | TIM1_CH1 -> PA8
|
28 | */
|
29 | GPIO_StructInit(&GPIO_InitStructure);
|
30 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
31 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
32 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
33 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
34 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
35 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
36 |
|
37 | /* Enable Alternate function on PA8 to be controlled by TIM1 */
|
38 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
|
39 |
|
40 | /* TIM1 clock enable */
|
41 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
42 |
|
43 | /* Time Base configuration */
|
44 | TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
45 | TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
46 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
47 | TIM_TimeBaseStructure.TIM_Period = 480;
|
48 | TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
49 | TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
|
50 | TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
|
51 |
|
52 | /* Channel 1 Configuration in PWM mode */
|
53 | TIM_OCStructInit(&TIM_OCInitStructure);
|
54 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
|
55 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
56 | TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
|
57 | TIM_OCInitStructure.TIM_Pulse = 240;
|
58 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
|
59 | TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
|
60 | TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
|
61 | TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
|
62 | TIM_OC1Init(TIM1, &TIM_OCInitStructure);
|
63 |
|
64 | /* Automatic Output enable, Break, dead time and lock configuration*/
|
65 | TIM_BDTRStructInit(&TIM_BDTRInitStructure);
|
66 | TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
|
67 | TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
|
68 | TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
|
69 | TIM_BDTRInitStructure.TIM_DeadTime = 11;
|
70 | TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
|
71 | TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
|
72 | TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
|
73 | TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
|
74 |
|
75 | /* Main Output Enable */
|
76 | TIM_CtrlPWMOutputs(TIM1, ENABLE);
|
77 |
|
78 | /* TIM1 counter enable */
|
79 | TIM_Cmd(TIM1, ENABLE);
|
80 | }
|
81 |
|
82 | /**
|
83 | * @brief Configures COMP2: PA3 as COMP2 non inverting input
|
84 | * VREFINT as COMP2 inverting input
|
85 | * and COMP2 output to TIM2 BKIN.
|
86 | * @param None
|
87 | * @retval None
|
88 | */
|
89 | void COMP_Config(void)
|
90 | {
|
91 |
|
92 | COMP_InitTypeDef COMP_InitStructure;
|
93 | GPIO_InitTypeDef GPIO_InitStructure;
|
94 | /* GPIOA Peripheral clock enable */
|
95 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
96 |
|
97 | /* Configure PA3 in analog mode: PA3 is connected to COMP2 non inverting input */
|
98 | GPIO_StructInit(&GPIO_InitStructure);
|
99 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
100 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
|
101 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
102 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
103 |
|
104 | /* COMP Peripheral clock enable */
|
105 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
106 |
|
107 | /* COMP2 config */
|
108 | COMP_StructInit(&COMP_InitStructure);
|
109 | COMP_InitStructure.COMP_InvertingInput = COMP_InvertingInput_VREFINT;
|
110 | COMP_InitStructure.COMP_Output = COMP_Output_TIM1BKIN;
|
111 | COMP_InitStructure.COMP_Hysteresis = COMP_Hysteresis_No;
|
112 | COMP_InitStructure.COMP_Mode = COMP_Mode_HighSpeed;
|
113 | COMP_InitStructure.COMP_OutputPol = COMP_OutputPol_NonInverted;
|
114 | COMP_Init(COMP_Selection_COMP2, &COMP_InitStructure);
|
115 |
|
116 | /* Enable COMP2 */
|
117 | COMP_Cmd(COMP_Selection_COMP2, ENABLE);
|