PWM Example #50
-
Thanks for the PWM example, it helped me to get my first PWM port working, but now I am trying to get the second port working (I am using one processor to control two motors on my mill, drill, lathe machine) and will eventually add steppers for controlling the feeds. I have the following almost working but when I add back in the commented out line the sketch stops working correctly. I suspect I need to in the syntax shift the second port over to a different channel but have no idea how to do this and in fact would prefer to keep both ports on the same channel as they both need to run at 20 Hz. Perhaps I am not even using the easiest method but I can't find examples or explanations I can make sense of so far. Can someone help me to get to a working example of TWO PWM outputs, with maybe a simpler explanation of why this doesn't work? #include<Wire.h>
#include <LiquidCrystal_I2C.h>
#define pwm_lathe PB9
#define pwm_mill PB10
long int loop_cntr;
LiquidCrystal_I2C lcd(0x27, 20, 4);
TIM_TypeDef *InstanceMill = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pwm_mill), PinMap_PWM);
uint32_t channelMill = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pwm_mill), PinMap_PWM));
HardwareTimer *MyTimMill = new HardwareTimer(InstanceMill);
TIM_TypeDef *InstanceLathe = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pwm_lathe), PinMap_PWM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pwm_lathe), PinMap_PWM));
HardwareTimer *MyTimLathe = new HardwareTimer(InstanceLathe);
void pwm_Mill(int duty)
{
MyTimMill->setCaptureCompare(channel, duty, PERCENT_COMPARE_FORMAT);
delay(50);
}
void pwm_Lathe(int duty)
{
MyTimLathe->setCaptureCompare(channel, duty, PERCENT_COMPARE_FORMAT);
delay(50);
}
void setup(void) {
// pinMode(pwm_pin, OUTPUT);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
MyTimLathe->setPWM(channel, pwm_lathe, 20, 1);
// MyTimMill->setPWM(channel, pwm_mill, 20, 1); uncommmenting this kills abililty to change duty cycle
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("LCD OK");
lcd.setCursor(8,0);
loop_cntr=0;
}
void loop() {
++ loop_cntr;
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(loop_cntr);
lcd.setCursor(8,0);
delay(2000);
pwm_Lathe(1);
delay(2000);
pwm_Mill(30);
delay(1000);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This point has been discussed in the forum: |
Beta Was this translation helpful? Give feedback.
This point has been discussed in the forum:
https://www.stm32duino.com/viewtopic.php?f=7&t=496&start=10