commit cee206cd9a183e5a0a80d5c08489661b460f4fad Author: Samuel Date: Sun Jul 5 22:08:59 2026 +0200 Création du dépôt avec des fichiers Servomoteur.c & Servoteur.h éprouvés diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..7cd48ee --- /dev/null +++ b/Readme.md @@ -0,0 +1,7 @@ +Submodule Servomoteurs pour le RP2040 +===================================== + +Ceci est un submodule git pour intégrer facilement le support des servomoteurs à un projet. + + + diff --git a/Servomoteur.c b/Servomoteur.c new file mode 100644 index 0000000..faa76ad --- /dev/null +++ b/Servomoteur.c @@ -0,0 +1,37 @@ +#include "pico/stdlib.h" +#include "hardware/pwm.h" +#include "Servomoteur.h" + + + +void Servomoteur_Init(void){ + uint slice_num; + gpio_set_function(SERVO0, GPIO_FUNC_PWM); + + pwm_config pwm_servo = pwm_get_default_config(); + + // On veut un rebouclage au bout de 20 ms (50 Hz). + // Division de l'horloge système (133 MHz) par 254 : 523 kHz + pwm_config_set_clkdiv_int(&pwm_servo, 254); + // Valeur max du PXM pour avoir 50 Hz : 523 kHz / 50 Hz : 10460 + pwm_config_set_wrap(&pwm_servo, 10460); + // À la valeur finale, rebouclage à 0 (et non un compte à rebours) + pwm_config_set_phase_correct(&pwm_servo, false); + + slice_num = pwm_gpio_to_slice_num(SERVO0); + pwm_init(slice_num, &pwm_servo, true); + + slice_num = pwm_gpio_to_slice_num(SERVO1); + pwm_init(slice_num, &pwm_servo, true); + + Servomoteur_set(SERVO0, SERVO_VALEUR_0_5MS); + + Servomoteur_set(SERVO1, SERVO_VALEUR_0_5MS); +} + +void Servomoteur_set(uint8_t servomoteur, uint16_t valeur){ + uint slice_num = pwm_gpio_to_slice_num(servomoteur); + // Chan : A ou 0 si la GPIO est paire + // B ou 1 si la GPIO est impaire + pwm_set_chan_level(slice_num, servomoteur & 0x01, valeur); +} diff --git a/Servomoteur.h b/Servomoteur.h new file mode 100644 index 0000000..046e74f --- /dev/null +++ b/Servomoteur.h @@ -0,0 +1,19 @@ +#include "pico/stdlib.h" + +// Valeur des servomoteurs +#define SERVO_VALEUR_MAX 10459 +#define SERVO_VALEUR_2MS 1056 +#define SERVO_VALEUR_1_5MS 784 +#define SERVO_VALEUR_1MS 523 +#define SERVO_VALEUR_0_5MS 261 + + +// Servomoteurs +#define SERVO0 0 +#define SERVO1 1 +#define DOIGT_AVANCE 0, SERVO_VALEUR_1_5MS +#define DOIGT_RETRAIT 0, SERVO_VALEUR_0_5MS + + +void Servomoteur_Init(void); +void Servomoteur_set(uint8_t servomoteur, uint16_t valeur);