1 | int main(void)
|
2 | {
|
3 | /* TIM Configuration */
|
4 | TIM_Config();
|
5 |
|
6 | /* TIM4 configuration: PWM Input mode ------------------------
|
7 | The external signal is connected to TIM4 CH2 pin (PB.07),
|
8 | The Rising edge is used as active edge,
|
9 | The TIM4 CCR2 is used to compute the frequency value
|
10 | The TIM4 CCR1 is used to compute the duty cycle value
|
11 | ------------------------------------------------------------ */
|
12 |
|
13 | TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
|
14 | TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
|
15 | TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
|
16 | TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
|
17 | TIM_ICInitStructure.TIM_ICFilter = 0x0;
|
18 |
|
19 | TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);
|
20 |
|
21 | /* Select the TIM4 Input Trigger: TI2FP2 */
|
22 | TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
|
23 |
|
24 | /* Select the slave Mode: Reset Mode */
|
25 | TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
|
26 | TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
|
27 |
|
28 | /* TIM enable counter */
|
29 | TIM_Cmd(TIM4, ENABLE);
|
30 |
|
31 | /* Enable the CC2 Interrupt Request */
|
32 | TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
|
33 | }
|
34 |
|
35 |
|
36 | void TIM_Config(void)
|
37 | {
|
38 | GPIO_InitTypeDef GPIO_InitStructure;
|
39 | NVIC_InitTypeDef NVIC_InitStructure;
|
40 |
|
41 | /* TIM4 clock enable */
|
42 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
43 |
|
44 | /* GPIOB clock enable */
|
45 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
46 |
|
47 | /* TIM4 chennel2 configuration : PB.07 */
|
48 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
49 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
50 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
51 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
52 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
|
53 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
54 |
|
55 | /* Connect TIM pin to AF2 */
|
56 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
|
57 |
|
58 | /* Enable the TIM4 global Interrupt */
|
59 | NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
|
60 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
61 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
62 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
63 | NVIC_Init(&NVIC_InitStructure);
|
64 |
|
65 | }
|