1 | #include <stm32f4xx_gpio.h>
|
2 | #include <stm32f4xx_rcc.h>
|
3 | #include <stm32f4xx_tim.h>
|
4 |
|
5 | void init_pwm_gpi()
|
6 | {
|
7 | GPIO_InitTypeDef GPIO_InitStructure;
|
8 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
9 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
10 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
11 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
12 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
13 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; resistors */
|
14 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
15 | }
|
16 |
|
17 | void init_pwm_gpo()
|
18 | {
|
19 | GPIO_InitTypeDef GPIO_InitStructure;
|
20 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
|
21 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
22 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
23 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
24 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
25 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; resistors */
|
26 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
27 | GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
|
28 | }
|
29 |
|
30 | int my_init_pwm(int pwm_freq)
|
31 | {
|
32 |
|
33 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE );
|
34 |
|
35 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
36 | TIM_TimeBaseStructInit(& TIM_TimeBaseStructure);
|
37 | TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
38 | TIM_TimeBaseStructure.TIM_Period = 8399;
|
39 | TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
40 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
|
41 | TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
|
42 |
|
43 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
44 |
|
45 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
46 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
47 | TIM_OCInitStructure.TIM_Pulse = pwm_freq;
|
48 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
49 |
|
50 | TIM_OC1Init(TIM4, &TIM_OCInitStructure);
|
51 | TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
|
52 |
|
53 | TIM_Cmd(TIM4 , ENABLE);
|
54 |
|
55 | }
|
56 |
|
57 | int main(void)
|
58 | {
|
59 | SystemInit();
|
60 | init_pwm_gpi();
|
61 | init_pwm_gpo();
|
62 |
|
63 | while(1)
|
64 | {
|
65 | static int i = 0;
|
66 | if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
|
67 | switch(i){
|
68 | case 0: my_init_pwm(0);
|
69 | i = i + 1;
|
70 | break;
|
71 | case 1: my_init_pwm(2199);
|
72 | i = i + 1;
|
73 | break;
|
74 | case 2: my_init_pwm(4299);
|
75 | i = i + 1;
|
76 | break;
|
77 | case 3: my_init_pwm(6299);
|
78 | i = i + 1;
|
79 | break;
|
80 | case 4: my_init_pwm(8399);
|
81 | i = 0;
|
82 | break;
|
83 | }
|
84 | }
|
85 |
|
86 | }
|
87 | }
|