Utiliser le pilote d'autrui

This commit is contained in:
Oskar Orvik 2025-11-25 21:21:57 +01:00
parent 7c51dd43cf
commit 1858fa0ef0
6 changed files with 103 additions and 79 deletions

View file

@ -4,4 +4,5 @@
extern void configEncoder(TIM_TypeDef * Timer); extern void configEncoder(TIM_TypeDef * Timer);
extern int angleVent (TIM_TypeDef * Timer); extern int angleVent (TIM_TypeDef * Timer);
extern int vent2voile(int angle); extern int vent2voile(int angle);
extern void LocaliserZero(void);
#endif #endif

View file

@ -1,7 +1,9 @@
#ifndef PWM_H_ #ifndef PWM_H_
#define PWM_H_ #define PWM_H_
#include "stm32f10x.h" #include "stm32f10x.h"
//Variables
#define POWERMODE 2 // 1 vaut powermode 1, 0 vaut powermode 2 (Powermode pour le config de dutycycle)
// Config // Config
extern void init_PWM(TIM_TypeDef *Timer, int Channel); extern void MyTimer_PWM(TIM_TypeDef * Timer , int Channel);
extern void PWM_Set_DutyCycle(TIM_TypeDef *Timer, int Channel, float DutyCycle_Percent); extern int Set_DutyCycle_PWM(TIM_TypeDef *Timer, int Channel, int DutyC);
#endif #endif

View file

@ -48,3 +48,14 @@ int vent2voile(int angle){ // Conversion angle vent
} }
} }
// Localisation de z
void LocaliserZero(void){
int Z_trouve = 0;
while (Z_trouve != 1){
if(MyGPIO_Read(GPIOA,8)){ // Index
TIM2 -> CNT = 0x0; // Remet angle à zero
Z_trouve = 1;
}
}
}

View file

@ -1,67 +1,85 @@
#include "stm32f10x.h" #include "stm32f10x.h"
#include "PWM.h" #include "PWM.h"
void MyTimer_PWM(TIM_TypeDef * Timer , int Channel){
int pwrmd;
#if POWERMODE //Powermode 1
pwrmd = 0b110;
#else
pwrmd = 0b111; //Powermode 2
#endif
void init_PWM(TIM_TypeDef *Timer, int Channel) { // Activer PWM sur un output if (Channel == 1){
// preload Timer->CCMR1 &= ~(0b111<<4); //On clear les trois bits qui sont de pwm
Timer->CR1 |= TIM_CR1_ARPE; Timer->CCMR1 |= (pwrmd<<4); //On affecte le powermode au bits de lecture pour le µ-controlleur
Timer->CCMR1 |= TIM_CCMR1_OC1PE; //Update preload, il n'affecte pas le valeur avant que la prochaine cycle
Timer->CCER = TIM_CCER_CC1E; //Enable le pin voulu basculer
}
else if (Channel == 2){
Timer->CCMR1 &= ~(0b111<<12); //Le TIMx_CCMR1 configure deux channels, de bit [6:4] CH1, [14:12] CH2 (OC2M = Output Channel 2 )
Timer->CCMR1 |= (pwrmd<<12);
Timer->CCMR1 |= TIM_CCMR1_OC2PE;
Timer->CCER |= TIM_CCER_CC2E;
}
else if (Channel == 3){
Timer->CCMR1 &= ~(0b111<<4);
Timer->CCMR2 |= (pwrmd<<4);
Timer->CCMR2 |= TIM_CCMR2_OC3PE;
Timer->CCER |= TIM_CCER_CC3E;
}
else if (Channel == 4){
Timer->CCMR1 &= ~(0b111<<12);
Timer->CCMR2 |= (pwrmd<<12);
Timer->CCMR2 |= TIM_CCMR2_OC4PE;
Timer->CCER |= TIM_CCER_CC4E;
}
switch (Channel) { //En dessous d'ici, on a l'aide du plus gentil chat que je connais
case 1: // Enable auto-reload preload -- //Ensures that your initial configuration — PWM mode, duty cycle, period — actually takes effect before the timer starts counting.
// Config o channel 1 in "PWM Mode 1" and enable preload of CCR1 Timer->CR1 |= TIM_CR1_ARPE;
Timer->CCMR1 &= ~TIM_CCMR1_OC1M; // clean bit modes // Force update event to load ARR and CCR values immediately
Timer->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; // Mode PWM 1 Timer->EGR |= TIM_EGR_UG;
Timer->CCMR1 |= TIM_CCMR1_OC1PE; // enable preload // Start the timer
Timer->CR1 |= TIM_CR1_CEN;
// enable exit 1 (Output enable) switch (Channel) {
Timer->CCER |= TIM_CCER_CC1E; case 1:
break; if (Timer == TIM1){GPIOA->CRH &= ~(0xF<<0*4); GPIOA->CRH |= (0xA<<0*4); TIM1->BDTR |= 1<<15; }
case 2: if (Timer == TIM2){GPIOA->CRL &= ~(0xF<<0*4); GPIOA->CRL |= (0xA<<0*4);}
Timer->CCMR1 &= ~TIM_CCMR1_OC2M; if (Timer == TIM3){GPIOA->CRL &= ~(0xF<<6*4); GPIOA->CRL |= (0xA<<6*4);}
Timer->CCMR1 |= TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1; if (Timer == TIM4){GPIOB->CRL &= ~(0xF<<5*4); GPIOB->CRL |= (0xA<<5*4);}
Timer->CCMR1 |= TIM_CCMR1_OC2PE; break;
Timer->CCER |= TIM_CCER_CC2E; case 2:
break; if (Timer == TIM1){GPIOA->CRH &= ~(0xF<<1*4); GPIOA->CRL |= (0xA<<1*4); TIM1->BDTR |= 1<<15;}
case 3: if (Timer == TIM2){GPIOA->CRL &= ~(0xF<<1*4); GPIOA->CRL |= (0xA<<1*4);}
Timer->CCMR2 &= ~TIM_CCMR2_OC3M; if (Timer == TIM3){GPIOA->CRL &= ~(0xF<<7*4); GPIOA->CRL |= (0xA<<7*4);}
Timer->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1; if (Timer == TIM4){GPIOB->CRL &= ~(0xF<<7*4); GPIOB->CRL |= (0xA<<7*4);}
Timer->CCMR2 |= TIM_CCMR2_OC3PE; break;
Timer->CCER |= TIM_CCER_CC3E; case 3:
break; if (Timer == TIM1){GPIOA->CRH &= ~(0xF<<2*4); GPIOA->CRH |= (0xA<<2*4); TIM1->BDTR |= 1<<15;}
case 4: if (Timer == TIM2){GPIOA->CRL &= ~(0xF<<2*4); GPIOA->CRL |= (0xA<<2*4);}
Timer->CCMR2 &= ~TIM_CCMR2_OC4M; if (Timer == TIM3){GPIOB->CRL &= ~(0xF<<0*4); GPIOB->CRL |= (0xA<<0*4);}
Timer->CCMR2 |= TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1; if (Timer == TIM4){GPIOB->CRH &= ~(0xF<<0*4); GPIOB->CRH |= (0xA<<0*4);}
Timer->CCMR2 |= TIM_CCMR2_OC4PE; break;
Timer->CCER |= TIM_CCER_CC4E; case 4:
break; if (Timer == TIM1){GPIOA->CRH &= ~(0xF<<3*4); GPIOA->CRH |= (0xA<<3*4); TIM1->BDTR |= 1<<15;}
} if (Timer == TIM2){GPIOA->CRL &= ~(0xF<<3*4); GPIOA->CRL |= (0xA<<3*4);}
if (Timer == TIM3){GPIOB->CRL &= ~(0xF<<1*4); GPIOB->CRL |= (0xA<<1*4);}
if (Timer == TIM4){GPIOB->CRH &= ~(0xF<<1*4); GPIOB->CRH |= (0xA<<1*4);}
// special case(specific timers) }
if (Timer == TIM1 || Timer == TIM8) {
Timer->BDTR |= TIM_BDTR_MOE;
}
} }
void PWM_Set_DutyCycle(TIM_TypeDef *Timer, int Channel, float DutyCycle_Percent) {
unsigned short ccr_value;
// Percentages between 0 and 100 //Une fonction qui met le bon PWM voulu
if (DutyCycle_Percent > 100.0) DutyCycle_Percent = 100.0; int Set_DutyCycle_PWM(TIM_TypeDef *Timer, int Channel, int DutyC){
if (DutyCycle_Percent < 0.0) DutyCycle_Percent = 0.0; int CCR_VAL = (Timer -> ARR + 1) * DutyC / 100;
// calcule of crr switch (Channel){
ccr_value = (unsigned short)((DutyCycle_Percent / 100.0) * (Timer->ARR)); case 1: Timer->CCR1 = CCR_VAL;
// Assigner le valaur pour le registre de comparison pour le channel qui est pertient case 2: Timer->CCR2 = CCR_VAL;
switch (Channel) { case 3: Timer->CCR3 = CCR_VAL;
case 1: case 4: Timer->CCR4 = CCR_VAL;
Timer->CCR1 = ccr_value; default: break;
break; }
case 2: return 0;
Timer->CCR2 = ccr_value; Timer->EGR |= TIM_EGR_UG;
break;
case 3:
Timer->CCR3 = ccr_value;
break;
case 4:
Timer->CCR4 = ccr_value;
break;
}
} }

View file

@ -2,18 +2,17 @@
#include "DriverGPIO.h" #include "DriverGPIO.h"
#include "PWM.h" #include "PWM.h"
#include "Timer.h" #include "Timer.h"
#include "Accelerometre.h"
#include "Horloge.h"
void Servo_Moteur(int angle, TIM_TypeDef * Timer, int Channel){ // Controle du moteur void Servo_Moteur(int angle, TIM_TypeDef * Timer, int Channel){ // Controle du moteur
int dutyCycle = (5* angle + 5*90)/90; int dutyCycle = (5* angle + 5*90)/90; // 5-10 % Duty Cycle
Set_DutyCycle_PWM(TIM4, 3, dutyCycle); //On met Duty cycle à 2% et il reste autour de 90 deg Set_DutyCycle_PWM(Timer, Channel, dutyCycle);
} }
void initServo(TIM_TypeDef * Timer, int Channel){ // Config du moteur servo void initServo(TIM_TypeDef * Timer, int Channel){ // Config du moteur servo
if (Timer == TIM4) { if (Timer == TIM4) {
EnableTimer(TIM4); EnableTimer(TIM4);
MyTimer_Base_Init(TIM4, 20000 - 1, 71); //MyTimer_Base_Init(TIM4, 20000 - 1, 71);
MyTimer_Base_Init(TIM4, 0xFFFF, 22); // Pour obtenir un période de 20 ms
if (Channel == 3){ if (Channel == 3){
MyGPIO_Init(GPIOB, 8, AltOut_Ppull); // Outut push pull alternate MyGPIO_Init(GPIOB, 8, AltOut_Ppull); // Outut push pull alternate

View file

@ -1,12 +1,13 @@
#include <stm32f10x.h> #include <stm32f10x.h>
#include <stdio.h> // Pour print #include <stdio.h> // Pour print
#include "Girouette.h" #include "Girouette.h"
#include "Servo.h" #include "Servo.h"
#include "DriverGPIO.h"
//Variables //Variables
volatile int angleVentVar; int angleVentVar;
volatile int angleVoileVar; int angleVoileVar;
int main ( void ){ int main ( void ){
// ---- Setup ------ // ---- Setup ------
@ -15,19 +16,11 @@ int main ( void ){
// Giroutte.c // Giroutte.c
configEncoder(TIM2); configEncoder(TIM2);
// Localisation de z LocaliserZero();
int Z_trouve = 0;
while (Z_trouve != 1){
if(MyGPIO_Read(GPIOA,8)){ // Index
TIM2 -> CNT = 0x0; // Remet angle à zero
Z_trouve = 1;
}
}
// ----- Opération ----- // ----- Opération -----
while (1){ while (1){
angleVentVar = angleVent(TIM2); // Récupérer l'angle de girouette angleVentVar = angleVent(TIM2); // Récupérer l'angle de girouette
angleVoileVar = vent2voile(angleVentVar); // Transformer l'angle de girouette au l'angle des voiles souhaités angleVoileVar = vent2voile(angleVentVar); // Transformer l'angle de girouette au l'angle des voiles souhaités
Servo_Moteur(angleVoileVar, TIM4, 3); // Faire bouger le moteur servo Servo_Moteur(angleVoileVar, TIM4, 3); // Faire bouger le moteur servo
} }
}; };