#include <HardwareTimer.h>
HardwareTimer *MyTim = nullptr; // Pointeur vers l'objet Timer (vide pour le moment)
const uint32_t PWM_FREQ_HZ = 100000; // nombre de hertz
const uint32_t DUTY_PERCENT = 50; // rapport cyclique à 50%
void setup() {
Serial.begin(115200);
while (!Serial) {}
pinMode(D6, OUTPUT);
PinName pin = digitalPinToPinName(D6);
if (pin == NC) { // on vérifie si la pin existe
Serial.println("Pin doen't exist!!!");
while (1) {}
}
// Trouve quel Timer matériel est associé à cette Pin en mode PWM
TIM_TypeDef *instance = (TIM_TypeDef *)pinmap_peripheral(pin, PinMap_PWM);
if (instance == nullptr) { // on vérifie qu'un timer est disponible
Serial.println("No timer allocated!!!");
while (1) {} // sinon on bloque
}
// on récupère le canal du timer
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin, PinMap_PWM));
MyTim = new HardwareTimer(instance); // création d'un objet timer basé sur le matériel trouvé
// on configure le canal en mode PWM sur la PIN
MyTim->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, D6);
// on définit la fréquence du timer (100kHz)
MyTim->setOverflow(PWM_FREQ_HZ, HERTZ_FORMAT);
// on définit le rapport cyclique
MyTim->setCaptureCompare(channel, DUTY_PERCENT, PERCENT_COMPARE_FORMAT);
MyTim->resume();
Serial.println("System Ready");
}
void loop() {
}