I am brand new to this world so am struggling with the basics

I am trying to set up a PWM output from my STM32F401CC Black Pill which will have 2 encoders to vary the frequency and the duty cycle.
I have the PWM output working and I have 1 encoder working but cannot figure out how to set up an interrupt on the PWM code so that it will notice when I turn the encoder?
Any pointers would be appreciated!

EDIT: Ok it seems that I completely misunderstood interrupts, so the new question would be how to adjust the frequency and duty cycle of the HarwareTimer from within the loop? For that matterm I would also need to be able to enable and disable (pause/resume) the timer...

Code: Select all
#include <LiquidCrystal.h> // include the LCD library
const int rs = PB_10, en = PB_2, d4 = PB_1, d5 = PB_0, d6 = PA_7, d7 = PA_6; //STM32 Pins to which LCD is connected
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //Initialize the LCD
#define PWMOUT PA0 // Main Output (Tim2Ch1)
#define STM32DUINO_CORE
#define ENC_CLK PB8
#define ENC_DATA PB9
#define LEDPIN PC13
volatile uint32_t encoderCount;
void encoder1_read(void)
{
volatile static uint8_t ABs = 0;
ABs = (ABs << 2) & 0x0f; //left 2 bits now contain the previous AB key read-out;
ABs |= (digitalRead(ENC_CLK) << 1) | digitalRead(ENC_DATA);
switch (ABs)
{
case 0x0d:
encoderCount++;
break;
case 0x0e:
encoderCount--;
break;
}
}
void led_blink()
{
static uint8_t led_state = 0;
led_state = 1 - led_state;
digitalWrite(LEDPIN, led_state);
}
void setup() {
// LCD setup
lcd.begin(16, 2);//Defining 16*2 LCD
lcd.setCursor(0, 0); //LCD Row 0 and Column 0
lcd.print("Freaky ISSTC"); //Print this Line
lcd.setCursor(0, 1); //LCD Row 0 and Column 1
lcd.print("Interrupter V1.0"); //Print this Line
delay(4000); //wait for four secounds
lcd.clear(); //Clear the screen
// Encoder setup
Serial.begin(9600);
pinMode(ENC_CLK, INPUT);
pinMode(ENC_DATA, INPUT);
pinMode(LEDPIN, OUTPUT);
encoderCount = 10;
// PWM output setup
pinMode(PWMOUT, OUTPUT);
// Automatically retrieve TIM instance and channel associated to pin
// This is used to be compatible with all STM32 series automatically.
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(PWMOUT), PinMap_PWM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(PWMOUT), PinMap_PWM));
// Instantiate HardwareTimer object. Thanks to 'new' instantiation, HardwareTimer is not destructed when setup() function is finished.
HardwareTimer *MyTim = new HardwareTimer(Instance);
// Configure and start PWM
MyTim->attachInterrupt(encoder1_read);
MyTim->setPWM(channel, PWMOUT, encoderCount, 10); // encodercount Hertz, 10% dutycycle
MyTim->resume();
}
void loop() {
static uint32_t count;
static uint32_t prevCount;
count = encoderCount;
if (count != prevCount)
{
prevCount = count;
led_blink();
}
lcd.print("BPS: ");
lcd.print(encoderCount); // display the beats per second
lcd.setCursor(0,1); // move cursor to next line
lcd.print("PW: ");
//lcd.print(PW); // display the pulse width
delay(200);
lcd.clear();
}
#ifdef STM32DUINO_CORE
void HAL_SYSTICK_Callback()
{
encoder1_read();
}
#endif