20
20
#define _PWM_RES_MIN 1500
21
21
// max resolution
22
22
#define _PWM_RES_MAX 3000
23
+ // pwm frequency
24
+ #define _PWM_FREQUENCY 25000 // default
25
+ #define _PWM_FREQUENCY_MAX 50000 // mqx
23
26
24
27
// structure containing motor slot configuration
25
28
// this library supports up to 4 motors
@@ -102,9 +105,10 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
102
105
mcpwm_config_t pwm_config;
103
106
pwm_config.counter_mode = MCPWM_UP_DOWN_COUNTER; // Up-down counter (triangle wave)
104
107
pwm_config.duty_mode = MCPWM_DUTY_MODE_0; // Active HIGH
108
+ pwm_config.frequency = 2 *pwm_frequency; // set the desired freq - just a placeholder for now https://github.com/simplefoc/Arduino-FOC/issues/76
105
109
mcpwm_init (mcpwm_unit, MCPWM_TIMER_0, &pwm_config); // Configure PWM0A & PWM0B with above settings
106
- mcpwm_init (mcpwm_unit, MCPWM_TIMER_1, &pwm_config); // Configure PWM0A & PWM0B with above settings
107
- mcpwm_init (mcpwm_unit, MCPWM_TIMER_2, &pwm_config); // Configure PWM0A & PWM0B with above settings
110
+ mcpwm_init (mcpwm_unit, MCPWM_TIMER_1, &pwm_config); // Configure PWM1A & PWM1B with above settings
111
+ mcpwm_init (mcpwm_unit, MCPWM_TIMER_2, &pwm_config); // Configure PWM2A & PWM2B with above settings
108
112
109
113
if (_isset (dead_zone)){
110
114
// dead zone is configured
@@ -149,6 +153,7 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
149
153
mcpwm_num->timer [1 ].period .upmethod = 0 ;
150
154
mcpwm_num->timer [2 ].period .upmethod = 0 ;
151
155
_delay (1 );
156
+ // _delay(1);
152
157
// restart the timers
153
158
mcpwm_start (mcpwm_unit, MCPWM_TIMER_0);
154
159
mcpwm_start (mcpwm_unit, MCPWM_TIMER_1);
@@ -171,9 +176,8 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
171
176
// - hardware speciffic
172
177
// supports Arudino/ATmega328, STM32 and ESP32
173
178
void _configure2PWM (long pwm_frequency,const int pinA, const int pinB) {
174
-
175
- if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = 20000 ; // default frequency 20khz - centered pwm has twice lower frequency
176
- else pwm_frequency = _constrain (pwm_frequency, 0 , 40000 ); // constrain to 40kHz max - centered pwm has twice lower frequency
179
+ if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25hz
180
+ else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 40kHz max
177
181
178
182
stepper_2pwm_motor_slots_t m_slot = {};
179
183
@@ -217,9 +221,8 @@ void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
217
221
// - hardware speciffic
218
222
// supports Arudino/ATmega328, STM32 and ESP32
219
223
void _configure3PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC) {
220
-
221
- if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = 20000 ; // default frequency 20khz - centered pwm has twice lower frequency
222
- else pwm_frequency = _constrain (pwm_frequency, 0 , 40000 ); // constrain to 40kHz max - centered pwm has twice lower frequency
224
+ if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25hz
225
+ else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 40kHz max
223
226
224
227
bldc_3pwm_motor_slots_t m_slot = {};
225
228
@@ -262,9 +265,8 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
262
265
// - Stepper motor - 4PWM setting
263
266
// - hardware speciffic
264
267
void _configure4PWM (long pwm_frequency,const int pinA, const int pinB, const int pinC, const int pinD) {
265
-
266
- if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = 20000 ; // default frequency 20khz - centered pwm has twice lower frequency
267
- else pwm_frequency = _constrain (pwm_frequency, 0 , 40000 ); // constrain to 50kHz max - centered pwm has twice lower frequency
268
+ if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = _PWM_FREQUENCY; // default frequency 25hz
269
+ else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX); // constrain to 40kHz max
268
270
stepper_4pwm_motor_slots_t m_slot = {};
269
271
// determine which motor are we connecting
270
272
// and set the appropriate configuration parameters
@@ -367,7 +369,7 @@ void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, in
367
369
int _configure6PWM (long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l){
368
370
369
371
if (!pwm_frequency || !_isset (pwm_frequency) ) pwm_frequency = 20000 ; // default frequency 20khz - centered pwm has twice lower frequency
370
- else pwm_frequency = _constrain (pwm_frequency, 0 , 40000 ); // constrain to 40kHz max - centered pwm has twice lower frequency
372
+ else pwm_frequency = _constrain (pwm_frequency, 0 , _PWM_FREQUENCY_MAX ); // constrain to 40kHz max - centered pwm has twice lower frequency
371
373
bldc_6pwm_motor_slots_t m_slot = {};
372
374
// determine which motor are we connecting
373
375
// and set the appropriate configuration parameters
0 commit comments