1 | #include "stm32f0xx.h"
|
2 | #include "stm32f0xx_rcc.h"
|
3 | #include "stm32f0xx_gpio.h"
|
4 | #include "stm32f0xx_tim.h"
|
5 |
|
6 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
7 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
8 | uint16_t TimerPeriod = 0;
|
9 | uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
|
10 |
|
11 | void TIM_Config(void);
|
12 |
|
13 | void PWM_Output(void)
|
14 | {
|
15 | TIM_Config();
|
16 |
|
17 | TimerPeriod = (SystemCoreClock / 17570 ) - 1;
|
18 | /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
|
19 | Channel1Pulse = (uint16_t) (((uint32_t) 30 * (TimerPeriod - 1)) / 1000); //120=links 30=rechts
|
20 | /* Compute CCR2 value to generate a duty cycle at 37.5% for channel 2 */
|
21 | Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
|
22 | /* Compute CCR3 value to generate a duty cycle at 25% for channel 3 */
|
23 | Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
|
24 | /* Compute CCR4 value to generate a duty cycle at 12.5% for channel 4 */
|
25 | Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
|
26 |
|
27 | /* TIM1 clock enable */
|
28 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
|
29 |
|
30 | /* Time Base configuration */
|
31 | TIM_TimeBaseStructure.TIM_Prescaler = 351;
|
32 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
|
33 | TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
|
34 | TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
35 | TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
|
36 |
|
37 | TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
|
38 |
|
39 | /* Channel 1, 2, 3 and 4 Configuration in PWM mode */
|
40 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
41 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
42 | //TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
|
43 | //TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
44 | //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
|
45 | //TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
|
46 | //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
|
47 |
|
48 | TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
|
49 | TIM_OC1Init(TIM1, &TIM_OCInitStructure);
|
50 |
|
51 | TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
|
52 | TIM_OC2Init(TIM1, &TIM_OCInitStructure);
|
53 |
|
54 | TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
|
55 | TIM_OC3Init(TIM1, &TIM_OCInitStructure);
|
56 |
|
57 | TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
|
58 | TIM_OC4Init(TIM1, &TIM_OCInitStructure);
|
59 |
|
60 | /* TIM1 counter enable */
|
61 | TIM_Cmd(TIM1, ENABLE);
|
62 |
|
63 | /* TIM1 Main Output Enable */
|
64 | TIM_CtrlPWMOutputs(TIM1, ENABLE);
|
65 |
|
66 | /* Infinite loop */
|
67 | while (1)
|
68 | {}
|
69 | }
|
70 |
|
71 | /**
|
72 | * @brief Configure the TIM1 Pins.
|
73 | * @param None
|
74 | * @retval None
|
75 | */
|
76 | void TIM_Config(void)
|
77 | {
|
78 | GPIO_InitTypeDef GPIO_InitStructure;
|
79 |
|
80 | /* GPIOA Clocks enable */
|
81 | RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
|
82 |
|
83 | /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
|
84 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
|
85 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
86 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
87 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
88 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
|
89 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
90 |
|
91 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
|
92 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
|
93 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
|
94 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
|
95 |
|
96 | }
|