| 1 | #include <stm32f4xx_gpio.h>
 | 
| 2 | #include <stm32f4xx_rcc.h>
 | 
| 3 | #include <stm32f4xx_tim.h>
 | 
| 4 | 
 | 
| 5 | int pwm = 83.99;
 | 
| 6 | 
 | 
| 7 | void init_pwm_gpi()
 | 
| 8 | {
 | 
| 9 |     GPIO_InitTypeDef GPIO_InitStructure;
 | 
| 10 |     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
 | 
| 11 |     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
 | 
| 12 |     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; /* Use the alternative pin functions */
 | 
| 13 |     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* GPIO speed - has nothing to do with the timer timing */
 | 
| 14 |     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* Push-pull */
 | 
| 15 |     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; /* Setup pull-down resistors */
 | 
| 16 |     GPIO_Init(GPIOA, &GPIO_InitStructure);
 | 
| 17 | }
 | 
| 18 | 
 | 
| 19 | void init_pwm_gpo()
 | 
| 20 | {
 | 
| 21 |     GPIO_InitTypeDef GPIO_InitStructure;
 | 
| 22 |     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 | 
| 23 |     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
 | 
| 24 |     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; /* Use the alternative pin functions */
 | 
| 25 |     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* GPIO speed - has nothing to do with the timer timing */
 | 
| 26 |     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* Push-pull */
 | 
| 27 |     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; /* Setup pull-down resistors */
 | 
| 28 |     GPIO_Init(GPIOD, &GPIO_InitStructure);
 | 
| 29 |     GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
 | 
| 30 | }
 | 
| 31 | 
 | 
| 32 | int my_init_pwm(int pwm_freq)
 | 
| 33 | {
 | 
| 34 | 
 | 
| 35 |     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE );
 | 
| 36 | 
 | 
| 37 |     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 | 
| 38 |     TIM_TimeBaseStructInit(& TIM_TimeBaseStructure);
 | 
| 39 |     TIM_TimeBaseStructure.TIM_Prescaler = 0;
 | 
| 40 |     TIM_TimeBaseStructure.TIM_Period = 8399;
 | 
| 41 |     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
 | 
| 42 |     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
 | 
| 43 |     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
 | 
| 44 | 
 | 
| 45 |     TIM_OCInitTypeDef  TIM_OCInitStructure;
 | 
| 46 | 
 | 
| 47 |     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
 | 
| 48 |     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 | 
| 49 |     TIM_OCInitStructure.TIM_Pulse = pwm_freq;
 | 
| 50 |     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
 | 
| 51 | 
 | 
| 52 |     TIM_OC1Init(TIM4, &TIM_OCInitStructure);
 | 
| 53 |     TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
 | 
| 54 | 
 | 
| 55 |     TIM_Cmd(TIM4 , ENABLE);
 | 
| 56 | 
 | 
| 57 | }
 | 
| 58 | 
 | 
| 59 | int main(void)
 | 
| 60 | {
 | 
| 61 |   SystemInit();
 | 
| 62 |   init_pwm_gpi();
 | 
| 63 |   init_pwm_gpo();
 | 
| 64 | 
 | 
| 65 |     while(1)
 | 
| 66 |     {
 | 
| 67 |       static int i = 0;
 | 
| 68 |       int pressedlvl = 0;
 | 
| 69 |       int releasedlvl = 0;
 | 
| 70 |       int pressed = 0;
 | 
| 71 | 
 | 
| 72 |       while (1) {
 | 
| 73 |         if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)){
 | 
| 74 |           pressedlvl++;
 | 
| 75 |               if(pressedlvl>8000){
 | 
| 76 |                 if(pressed==0){
 | 
| 77 |                   switch(i){
 | 
| 78 |                               case 0: my_init_pwm(pwm*0);
 | 
| 79 |                               i = i + 1;
 | 
| 80 |                               break;
 | 
| 81 |                               case 1: my_init_pwm(pwm*25);
 | 
| 82 |                               i = i + 1;
 | 
| 83 |                               break;
 | 
| 84 |                               case 2: my_init_pwm(pwm*50);
 | 
| 85 |                               i = i + 1;
 | 
| 86 |                               break;
 | 
| 87 |                               case 3: my_init_pwm(pwm*75);
 | 
| 88 |                               i = i + 1;
 | 
| 89 |                               break;
 | 
| 90 |                               case 4: my_init_pwm(pwm*100);
 | 
| 91 |                               i = 0;
 | 
| 92 |                               break;
 | 
| 93 |                               }
 | 
| 94 |                   pressed = 1;
 | 
| 95 |                 }
 | 
| 96 |                 pressedlvl = 0;
 | 
| 97 |               }
 | 
| 98 |             } else {
 | 
| 99 |               releasedlvl++;
 | 
| 100 |               if(releasedlvl>8000){
 | 
| 101 |                 pressed = 0;
 | 
| 102 |                 releasedlvl = 0;
 | 
| 103 |               }
 | 
| 104 |             }
 | 
| 105 |           }
 | 
| 106 |     }
 | 
| 107 | }
 |