Moving functions to Girouette/Servo

This commit is contained in:
Oskar Orvik 2025-11-24 00:08:49 +01:00
parent af0aedc34b
commit 1fbbaacfea
15 changed files with 54 additions and 227 deletions

View file

@ -1,3 +1,5 @@
#ifndef DRIVERGPIO_H_
#define DRIVERGPIO_H_
#include "stm32f10x.h"
#define In_Floating 0x4
#define In_PullDown 0x8
@ -12,3 +14,4 @@ extern int MyGPIO_Read(GPIO_TypeDef * GPIO, char GPIO_Pin); // renvoie 0 ou autr
extern void MyGPIO_Set(GPIO_TypeDef * GPIO, char GPIO_Pin);
extern void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin);
extern void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin);
#endif

View file

@ -1,6 +1,7 @@
#ifndef _GIROUETTE_H
#define _GIROUETTE_H
#include "stm32f10x.h"
extern void configEncoder(TIM_TypeDef * Timer);
extern void configChannel();
extern int returnAngle (TIM_TypeDef * Timer);
extern int angleVent (TIM_TypeDef * Timer);
extern int vent2voile(int angle);
extern void Servo_Moteur(int angle, TIM_TypeDef * Timer, int Channel);
#endif

View file

@ -1,12 +0,0 @@
#include "stm32f10x.h"
//TIMERS start
#define MyTimer_Base_Start(Timer) (Timer->CR1 |= TIM_CR1_CEN)
#define MyTimer_Base_Stop(Timer) (Timer -> CR1 =(0x0))
// IT
extern volatile int g_tick_count; // Declara que a variável existe em outro arquivo
void Test(void);
void ConfigureIT();
// PWM
void ConfigurePWM();
void ConfigureTimers();

View file

@ -1,8 +0,0 @@
#include "stm32f10x.h"
// Config
extern void ConfigHorloge(void);
extern void ConfigBroche(void);
// Gestion des IO Spesifiques
extern int BoutonAppuye(void);
extern void AllumerLED(void);
extern void EteindreLED(void);

View file

@ -1,4 +1,7 @@
#ifndef PWM_H_
#define PWM_H_
#include "stm32f10x.h"
// Config
extern void MyTimer_PWM(TIM_TypeDef *Timer, char Channel);
extern void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent);
extern void init_PWM(TIM_TypeDef *Timer, int Channel);
extern void PWM_Set_DutyCycle(TIM_TypeDef *Timer, int Channel, float DutyCycle_Percent);
#endif

View file

@ -1,12 +1,8 @@
#ifndef TIMER_H_
#define TIMER_H_
#include "stm32f10x.h"
// Config de timer
extern void MyTimer_Base_Init(TIM_TypeDef *Timer , unsigned short ValARR , unsigned short ValPSC );
extern void MyTimer_ActiveIT(TIM_TypeDef *Timer, char Prio,void(*IT_function)(void));
// Fonctions d'interruption
extern void TIM2_IRQHandler(void);
extern void TIM3_IRQHandler(void);
extern void TIM4_IRQHandler(void);
extern void TIM1_CC_IRQnHandler(void);
extern void TIM1_UP_IRQnHandler(void);
// Enable timers
void EnableTimer(TIM_TypeDef *Timer);
#endif

Binary file not shown.

Binary file not shown.

View file

@ -1,5 +1,5 @@
#include "stm32f10x.h"
#include "DriverGPIO.h"
#define In_Floating 0x4
#define In_PullDown 0x8
#define In_PullUp 0x8

View file

@ -1,6 +1,4 @@
#include "stm32f10x.h"
#include "MyTimer.h"
#include "Nucleo.h"
#include "Timer.h"
#include "DriverGPIO.h"
#include "Girouette.h"
@ -20,19 +18,18 @@ void configEncoder(TIM_TypeDef * Timer){
Timer -> SMCR &= ~TIM_SMCR_SMS; // Reset SMS-bits
Timer -> SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;// SMS = "011"
Timer -> CR1 |= TIM_CR1_CEN; // Enable counter
Timer -> ARR = 0xFD20; // Setting ARR as 1440*45
}
void configChannel(){
Timer -> ARR = 0x5A0; // Setting ARR as 1440
// GPIO
MyGPIO_Init(GPIOA,0,In_Floating ); // GPIOA pin 0 in mode floating TIM2_CH1
MyGPIO_Init(GPIOA,1,In_Floating ); // GPIOA pin 1 in mode floating TIM2_CH2
MyGPIO_Init(GPIOA,2,In_Floating);
MyGPIO_Init(GPIOA,8,In_PullDown ); // GPIOA pin 7 in mode floating Index
}
int returnAngle (TIM_TypeDef * Timer){
return((Timer -> CNT % POSITIONS)/POSITIONS * 360);
int angleVent (TIM_TypeDef * Timer){ // Returner l'angle du vent
return((Timer -> CNT)/POSITIONS * 360);
}
// Reste à coder une fonction de reset des degrés et une fonction qui retourne correctement l'angle
int vent2voile(int angle){ // Conversion angle vent à angle voile
if(abs(angle) < 90){
return 0;
@ -42,7 +39,3 @@ int vent2voile(int angle){ // Conversion angle vent
}
}
void Servo_Moteur(int angle, TIM_TypeDef * Timer, int Channel){ // Controle du moteur
int dutyCycle = (5* angle + 5*90)/90;
MyTimer_Set_DutyCycle(Timer, Channel, dutyCycle);
}

View file

@ -1,39 +0,0 @@
#include "stm32f10x.h"
#include "Timer.h"
#include "MyTimer.h"
#include "PWM.h"
#include "DriverGPIO.h"
// Variables
#define ARR_TIM1 0xFFAD
#define PSC_TIM1 0xFF
#define ARR_TIM2 0xFFAD
#define PSC_TIM2 0x0225
#define ARR_TIM3 0x2CF
#define PSC_TIM3 0x0
volatile int g_tick_count;
void Test(void){
// Signal
g_tick_count++;
MyGPIO_Toggle(GPIOA, 8);
}
void ConfigureTimers(){
// Config ARR & PSC
//MyTimer_Base_Init(TIM2, ARR_TIM2, PSC_TIM2);
MyTimer_Base_Init(TIM1, ARR_TIM1, PSC_TIM1);
MyTimer_Base_Init(TIM3, ARR_TIM2, PSC_TIM2);
// Enable timer clock
EnableTimer(TIM1);
EnableTimer(TIM2);
EnableTimer(TIM3);
}
void ConfigureIT(){ // Activate general interuption with a function and priority
//MyTimer_ActiveIT(TIM2, 4, Test); //start interruption with priority 4
//MyTimer_ActiveIT(TIM1, 4, Test); //start interruption with priority 4
MyTimer_ActiveIT(TIM3, 4, Test); //start interruption with priority 4
}
void ConfigurePWM(){ // Set dutycycle with timer
MyTimer_PWM(TIM1, 1); // Utiliser timer1 avec channel 1
MyTimer_Set_DutyCycle(TIM1, 1, 20.0);
}

View file

@ -1,25 +0,0 @@
#include "stm32f10x.h"
#include "../Include/Nucleo.h"
#include "DriverGPIO.h"
void ConfigHorloge(void) { // Peut-être redondant ??
RCC->APB2ENR |= (0x01 << 2) | (0x01 << 3) | (0x01 << 4) | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_TIM1EN;
};
void ConfigBroche(void){ //
//Mettre Broche 5 GPIOA à output push-pull
MyGPIO_Init(GPIOA, 5, Out_Ppull);
//Mettre broche 8 sur GPIOA à output open drain
MyGPIO_Init(GPIOA, 8, Out_OD);
};
int BoutonAppuye(void){ // Peut être modifié avec ChercherEtat
return(MyGPIO_Read(GPIOA, 9));
}
void AllumerLED(void){
MyGPIO_Set(GPIOA, 8);
}
void EteindreLED(void){
MyGPIO_Reset(GPIOA, 8);
}

View file

@ -1,8 +1,8 @@
#include "stm32f10x.h"
#include "../Include/PWM.h"
#include "PWM.h"
void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) { // Activer PWM sur un output
void init_PWM(TIM_TypeDef *Timer, int Channel) { // Activer PWM sur un output
// preload
Timer->CR1 |= TIM_CR1_ARPE;
@ -41,7 +41,7 @@ void MyTimer_PWM(TIM_TypeDef *Timer, char Channel) { // Activer PWM sur un outpu
Timer->BDTR |= TIM_BDTR_MOE;
}
}
void MyTimer_Set_DutyCycle(TIM_TypeDef *Timer, char Channel, float DutyCycle_Percent) {
void PWM_Set_DutyCycle(TIM_TypeDef *Timer, int Channel, float DutyCycle_Percent) {
unsigned short ccr_value;
// Percentages between 0 and 100

View file

@ -1,10 +1,5 @@
#include "stm32f10x.h"
#include "../Include/Timer.h"
//REMEMBER TO ENALBLE TIMERS
//EXAMPLES
//RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN; // Enable TIM2
//RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable TIM1
#include "Timer.h"
void MyTimer_Base_Init( TIM_TypeDef * Timer , unsigned short ValARR , unsigned short ValPSC ) { // Configuration du timer
Timer -> PSC=(ValPSC);
@ -13,77 +8,7 @@ Timer->EGR |= TIM_EGR_UG;
};
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);
};
void EnableTimer(TIM_TypeDef *Timer){
if(Timer == TIM2){
RCC -> APB1ENR |= RCC_APB1ENR_TIM2EN;

View file

@ -1,43 +1,33 @@
#include "stm32f10x.h"
#include <stm32f10x.h>
#include <stdio.h> // Pour print
#include "MyTimer.h"
#include "Nucleo.h"
#include "Girouette.h"
#include "Servo.h"
#include "DriverGPIO.h"
int test=0;
volatile int angleVent;
volatile int angleVoile;
int main ( void ){
//Variables
volatile int angleVentVar;
volatile int angleVoileVar;
int main ( void ){
// ---- Setup ------
//Nucleo.c
ConfigHorloge();
//ConfigBroche();
//MyTimer.c
ConfigureTimers();
//ConfigureIT();
//PWM.
ConfigurePWM();
//Servo.c
initServo(TIM4, 3);
// Giroutte.c
configEncoder(TIM2);
configChannel();
//MyTimer_Base_Start (TIM2);
//MyTimer_Base_Start (TIM1);
//MyTimer_Base_Start (TIM3);
// ----- Opération -----
while (1){
// 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;
}
angleVent = returnAngle(TIM2); // Récupérer l'angle de girouette
angleVoile = vent2voile(angleVent); // Transformer l'angle de girouette au l'angle des voiles souhaités
Servo_Moteur(angleVoile, TIM1, 1); // Faire bouger le moteur servo
//printf("L'angle est %d ", returnAngle(TIM2));
}
// ----- 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, TIM1, 1); // Faire bouger le moteur servo
}
};