1 | #include "stm32f4xx_conf.h"
|
2 |
|
3 | #define MOTORPWMTIMER TIM4
|
4 | #define MOTORPWMTIMCLOCK RCC_APB1Periph_TIM4
|
5 | #define MOTORPWMPORTCLOCK RCC_AHB1Periph_GPIOD
|
6 | #define MOTORPWMAF GPIO_AF_TIM4
|
7 | #define MOTORPWMPORT GPIOD
|
8 | #define MOTORPWMBIT GPIO_Pin_12
|
9 | #define MOTORPWMTIMBIT GPIO_PinSource12
|
10 |
|
11 |
|
12 | // Private typedefs ----------------------------------------------------------
|
13 | GPIO_InitTypeDef GPIO_InitStructureLED;
|
14 | GPIO_InitTypeDef GPIO_InitStructureTimer;
|
15 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
16 | TIM_OCInitTypeDef TIM_OCInitStructure;
|
17 |
|
18 |
|
19 | // Private variables ---------------------------------------------------------
|
20 | uint32_t TimerCounterClock = 0;
|
21 | uint32_t TimerOutputClock = 0;
|
22 | uint16_t PrescalerValue = 0;
|
23 | uint32_t PulseDurationInMicroSeconds = 0;
|
24 |
|
25 | int i;
|
26 |
|
27 |
|
28 | // the prototypes ------------------------------------------------------------
|
29 | int main(void);
|
30 |
|
31 | // Timer init for PWM
|
32 | void TimerInit(void);
|
33 |
|
34 | void Delay(__IO uint32_t nCount);
|
35 |
|
36 |
|
37 | int main(void)
|
38 | {
|
39 | /*!< At this stage the microcontroller clock setting is already configured,
|
40 | this is done through SystemInit() function which is called from startup
|
41 | file (startup_stm32f4xx.s) before to branch to application main.
|
42 | To reconfigure the default setting of SystemInit() function, refer to
|
43 | system_stm32f4xx.c file
|
44 | */
|
45 |
|
46 | // /dev/tty.usbserial-A900J1T0
|
47 |
|
48 | // call my new USART init
|
49 | usartInit();
|
50 |
|
51 |
|
52 | // call timer init for PWM
|
53 | TimerInit();
|
54 |
|
55 |
|
56 | // Timer base configuration
|
57 | // Don't know why, but this code has to be here (not in a seperate method)
|
58 | TIM_TimeBaseStructure.TIM_Period = (uint16_t) (TimerCounterClock / TimerOutputClock);
|
59 | TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) ((SystemCoreClock /2) / TimerCounterClock) - 1;
|
60 | TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
61 | TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
62 |
|
63 | // basic timer init
|
64 | TIM_TimeBaseInit(MOTORPWMTIMER, &TIM_TimeBaseStructure);
|
65 |
|
66 | // configure PWM mode and duration
|
67 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
68 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
69 | TIM_OCInitStructure.TIM_Pulse = PulseDurationInMicroSeconds; // set the duty cycle / pulse here!
|
70 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
71 | TIM_OC1Init(MOTORPWMTIMER, &TIM_OCInitStructure);
|
72 | TIM_OC1PreloadConfig(MOTORPWMTIMER, TIM_OCPreload_Enable);
|
73 |
|
74 | // preload timer config
|
75 | TIM_ARRPreloadConfig(MOTORPWMTIMER, ENABLE);
|
76 |
|
77 | // enable timer / counter
|
78 | TIM_Cmd(MOTORPWMTIMER, ENABLE);
|
79 |
|
80 |
|
81 | //
|
82 | // motor pwm test ! ! !
|
83 | //
|
84 | while(1)
|
85 | {
|
86 | for (i=1; i<70; i++)
|
87 | {
|
88 | Delay(0x03FFFF);
|
89 | TIM_Cmd(MOTORPWMTIMER, DISABLE);
|
90 |
|
91 | PulseDurationInMicroSeconds = i;
|
92 |
|
93 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
94 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
95 | TIM_OCInitStructure.TIM_Pulse = PulseDurationInMicroSeconds; // set the duty cycle / pulse here!
|
96 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
97 | TIM_OC1Init(MOTORPWMTIMER, &TIM_OCInitStructure);
|
98 | TIM_OC1PreloadConfig(MOTORPWMTIMER, TIM_OCPreload_Enable);
|
99 |
|
100 | TIM_ARRPreloadConfig(MOTORPWMTIMER, ENABLE);
|
101 |
|
102 | TIM_Cmd(MOTORPWMTIMER, ENABLE);
|
103 | }
|
104 |
|
105 | for (i=70; i>2; i--)
|
106 | {
|
107 | Delay(0x03FFFF);
|
108 | TIM_Cmd(MOTORPWMTIMER, DISABLE);
|
109 |
|
110 | PulseDurationInMicroSeconds = i;
|
111 |
|
112 | TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
113 | TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
114 | TIM_OCInitStructure.TIM_Pulse = PulseDurationInMicroSeconds; // set the duty cycle / pulse here!
|
115 | TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
116 | TIM_OC1Init(MOTORPWMTIMER, &TIM_OCInitStructure);
|
117 | TIM_OC1PreloadConfig(MOTORPWMTIMER, TIM_OCPreload_Enable);
|
118 |
|
119 | TIM_ARRPreloadConfig(MOTORPWMTIMER, ENABLE);
|
120 |
|
121 | TIM_Cmd(MOTORPWMTIMER, ENABLE);
|
122 | }
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 | /**
|
128 | * @brief Configure the timer for PWM.
|
129 | * @param None
|
130 | * @retval None
|
131 | */
|
132 | void TimerInit(void)
|
133 | {
|
134 | // set timer frequencies
|
135 | TimerCounterClock = 1000000; // 1 MHz
|
136 | TimerOutputClock = 10000; // 10 kHz = 100 µs period
|
137 |
|
138 | // set pulse duration in mili seconds (HIGH time)
|
139 | // can be up to from 0 to 99 (due to a TimerOutputClock of 10 kHz)
|
140 | PulseDurationInMicroSeconds = 50;
|
141 |
|
142 | // Timer clock enable
|
143 | RCC_APB1PeriphClockCmd(MOTORPWMTIMCLOCK, ENABLE);
|
144 |
|
145 | // Port clock enable
|
146 | RCC_AHB1PeriphClockCmd(MOTORPWMPORTCLOCK, ENABLE);
|
147 |
|
148 | // Set PWM Port, Pin and method
|
149 | GPIO_InitStructureTimer.GPIO_Pin = MOTORPWMBIT;
|
150 | GPIO_InitStructureTimer.GPIO_Mode = GPIO_Mode_AF;
|
151 | GPIO_InitStructureTimer.GPIO_Speed = GPIO_Speed_100MHz;
|
152 | GPIO_InitStructureTimer.GPIO_OType = GPIO_OType_PP;
|
153 | GPIO_InitStructureTimer.GPIO_PuPd = GPIO_PuPd_UP ;
|
154 | GPIO_Init(MOTORPWMPORT, &GPIO_InitStructureTimer);
|
155 |
|
156 | // Connect TIM pin to AF
|
157 | GPIO_PinAFConfig(MOTORPWMPORT, MOTORPWMTIMBIT, MOTORPWMAF);
|
158 | }
|
159 |
|
160 |
|
161 | /**
|
162 | * @brief Delay Function.
|
163 | * @param nCount:specifies the Delay time length.
|
164 | * @retval None
|
165 | */
|
166 | void Delay(__IO uint32_t nCount)
|
167 | {
|
168 | while(nCount--)
|
169 | {
|
170 | }
|
171 | }
|