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 int angleVent (TIM_TypeDef * Timer);
extern int vent2voile(int angle);
extern void LocaliserZero(void);
#endif

View file

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

View file

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

View file

@ -1,12 +1,13 @@
#include <stm32f10x.h>
#include <stdio.h> // Pour print
#include "Girouette.h"
#include "Servo.h"
#include "DriverGPIO.h"
//Variables
volatile int angleVentVar;
volatile int angleVoileVar;
int angleVentVar;
int angleVoileVar;
int main ( void ){
// ---- Setup ------
@ -15,19 +16,11 @@ int main ( void ){
// Giroutte.c
configEncoder(TIM2);
// Localisation de z
int Z_trouve = 0;
while (Z_trouve != 1){
if(MyGPIO_Read(GPIOA,8)){ // Index
TIM2 -> CNT = 0x0; // Remet angle à zero
Z_trouve = 1;
}
}
LocaliserZero();
// ----- Opération -----
while (1){
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
Servo_Moteur(angleVoileVar, TIM4, 3); // Faire bouger le moteur servo
}
};