From 8b95eeb0a500352ecf235513f9f28c07bcd827c2 Mon Sep 17 00:00:00 2001 From: Oskar Date: Mon, 24 Nov 2025 00:13:49 +0100 Subject: [PATCH] Ajouter servo et IT --- Pilotes/Include/IT.h | 11 +++++++ Pilotes/Include/Servo.h | 7 ++++ Pilotes/Source/IT.c | 73 +++++++++++++++++++++++++++++++++++++++++ Pilotes/Source/Servo.c | 26 +++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 Pilotes/Include/IT.h create mode 100644 Pilotes/Include/Servo.h create mode 100644 Pilotes/Source/IT.c create mode 100644 Pilotes/Source/Servo.c diff --git a/Pilotes/Include/IT.h b/Pilotes/Include/IT.h new file mode 100644 index 0000000..8d9a076 --- /dev/null +++ b/Pilotes/Include/IT.h @@ -0,0 +1,11 @@ +#ifndef IT_H_ +#define IT_H_ +#include +static void (*p_IT_functions[4])(void); +void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void)); +void TIM1_CC_IRQHandler(void); +void TIM1_UP_IRQHandler(void); +void TIM2_IRQHandler(void); +void TIM3_IRQHandler(void); +void TIM4_IRQHandler(void); +#endif // IT_H_ diff --git a/Pilotes/Include/Servo.h b/Pilotes/Include/Servo.h new file mode 100644 index 0000000..81ace88 --- /dev/null +++ b/Pilotes/Include/Servo.h @@ -0,0 +1,7 @@ +#ifndef SERVO_H_ +#define SERVO_H_ +#include +void Servo_Moteur(int angle, TIM_TypeDef * Timer, int Channel); +extern void initServo(TIM_TypeDef * Timer, int Channel); + +#endif // SERVO_H_ diff --git a/Pilotes/Source/IT.c b/Pilotes/Source/IT.c new file mode 100644 index 0000000..6a26429 --- /dev/null +++ b/Pilotes/Source/IT.c @@ -0,0 +1,73 @@ +#include "IT.h" + +static void (*p_IT_functions[4])(void); // Pour créer l'array des fonctions + +void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void)) { + //Enable interruption requisition + Timer->DIER |= TIM_DIER_UIE; // Update interrupt enable + + //Id the interruption timer routine + IRQn_Type IRQn; + + int timer_index = -1; // Indice pour notre array des pointeurs + if (Timer == TIM2) { + IRQn = TIM2_IRQn; + timer_index = 0; + } else if (Timer == TIM3) { + IRQn = TIM3_IRQn; + timer_index = 1; + } else if (Timer == TIM4) { + IRQn = TIM4_IRQn; + timer_index = 2; + } + // Keep the pointer of the valid timer function + if (timer_index != -1) { + p_IT_functions[timer_index] = IT_function; // index the function + } else { + return; // Timer invalid + } + // set interruption priority + NVIC_SetPriority(IRQn, Prio); + // Enable routine + NVIC_EnableIRQ(IRQn); +} + + +void TIM2_IRQHandler(void) { + // Clean flag + TIM2->SR &= ~TIM_SR_UIF; // Drapeau d'interuption + //Call function + if (p_IT_functions[0] != 0) { + p_IT_functions[0](); // Execute fonction + } +}; +void TIM3_IRQHandler(void) { + // Clean flag + TIM3->SR &= ~TIM_SR_UIF; + //Call function + if (p_IT_functions[1] != 0) { + p_IT_functions[1](); // Execute function + } +}; +void TIM4_IRQHandler(void) { + // Clean flag + TIM4->SR &= ~TIM_SR_UIF; + //Call function + if (p_IT_functions[2] != 0) { + p_IT_functions[2](); // Execute function + } +}; +// IT PWM +void TIM1_CC_IRQHandler(void) { + // Clean flag + TIM1 -> DIER &= ~TIM_DIER_CC1IE; + //Set bit + GPIOA -> ODR |= (0x1 << 8); +}; + +void TIM1_UP_IRQHandler(void) { + // Clean flag + TIM1-> DIER &= ~TIM_DIER_TIE; + //Reset bit + GPIOA -> ODR &= ~(0x1 << 8); +}; diff --git a/Pilotes/Source/Servo.c b/Pilotes/Source/Servo.c new file mode 100644 index 0000000..d8d4b6c --- /dev/null +++ b/Pilotes/Source/Servo.c @@ -0,0 +1,26 @@ +#include "Servo.h" +#include "DriverGPIO.h" +#include "PWM.h" +#include "Timer.h" + +void Servo_Moteur(int angle, TIM_TypeDef * Timer, int Channel){ // Controle du moteur + int dutyCycle = (5* angle + 5*90)/90; + PWM_Set_DutyCycle(Timer, Channel, dutyCycle); +} + +void initServo(TIM_TypeDef * Timer, int Channel){ // Config du moteur servo + if (Timer == TIM4) { + EnableTimer(TIM4); + MyTimer_Base_Init(TIM4, 20000 - 1, 71); //Claire m'a + if (Channel == 3){ + MyGPIO_Init(GPIOB, 8, AltOut_Ppull); + } + else{ + //printf("Cet pilôte n'existe pas"); + } + } + else{ + //printf("Cet pilôte n'existe pas"); + } +} +