From c531decce4912f278fa51bba138bc1cb9403ce00 Mon Sep 17 00:00:00 2001 From: Cyril Vasseur Date: Fri, 7 Apr 2023 15:19:56 +0200 Subject: [PATCH] IT_Roulis --- Drivers/Inc/Rouli.h | 3 + Drivers/Inc/TIMER.h | 6 ++ Drivers/Src/TIMER.c | 121 ++++++++++++++++++------ Projet_DevDrivers/Sources/Principal.c | 31 +++++- Projet_DevDrivers/{ => Sources}/Rouli.c | 4 +- Projet_DevDrivers/TP1.uvoptx | 11 ++- Projet_DevDrivers/TP1.uvprojx | 4 +- 7 files changed, 141 insertions(+), 39 deletions(-) rename Projet_DevDrivers/{ => Sources}/Rouli.c (97%) diff --git a/Drivers/Inc/Rouli.h b/Drivers/Inc/Rouli.h index d2b0bf7..bbb0a68 100644 --- a/Drivers/Inc/Rouli.h +++ b/Drivers/Inc/Rouli.h @@ -1,4 +1,6 @@ #include "stm32f10x.h" +#include "Timer.h" + typedef struct { float gX; @@ -12,3 +14,4 @@ void rouli_InitAccel(void); void rouli_GetAccel (XYZ * axe); void rouli_IT_Bascule(void); + diff --git a/Drivers/Inc/TIMER.h b/Drivers/Inc/TIMER.h index 235b60b..57bfb3c 100644 --- a/Drivers/Inc/TIMER.h +++ b/Drivers/Inc/TIMER.h @@ -31,6 +31,12 @@ void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void) #define MyTimer_Base_Start(Timer) (Timer->CR1 |= 0x01) #define MyTimer_Base_Stop(Timer) (Timer->CR1 &= ~0x01) +#define PWM_output 0x00 +#define PWM_inputTI1 0x01 +#define PWM_inputTI2 0x10 +#define PWM_inputTRC 0x11 +void MyPWM_init ( TIM_TypeDef * Timer,char Channel); +void MyPWM_Duty (TIM_TypeDef * Timer,char Channel, unsigned short CRR ); #endif diff --git a/Drivers/Src/TIMER.c b/Drivers/Src/TIMER.c index 7b9f22b..39e4235 100644 --- a/Drivers/Src/TIMER.c +++ b/Drivers/Src/TIMER.c @@ -1,92 +1,157 @@ #include "TIMER.h" -/*periode_timer=peridoe_horloge*prescaler*resolution -debordement stocké dans registre UIF -fixer val prescaler+ autoreload(equivalent resolution) -demarrage timer => CEN=1*/ - -void (*ptr_func)(void); +void (*tim_ptr1_func)(void); +void (*tim_ptr2_func)(void); +void (*tim_ptr3_func)(void); +void (*tim_ptr4_func)(void); void MyTimer_Base_Init ( MyTimer_Struct_TypeDef * Timer) { if(Timer->Timer==TIM1) { - RCC->APB2ENR |= 0x01<<11; + RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; } else if (Timer->Timer==TIM2) { - RCC->APB1ENR |= 0x01; + RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; } else if (Timer->Timer==TIM3) { - RCC->APB1ENR |= 0x01<<1; + RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; } else if (Timer->Timer==TIM4) { - RCC->APB1ENR |= 0x01<<2; + RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; } - Timer->Timer->ARR=Timer->ARR; - Timer->Timer->PSC=Timer->PSC; + Timer->Timer->ARR=Timer->ARR; // On set la donnée d'autoreload + Timer->Timer->PSC=Timer->PSC; // On set la donnée de prescaler (ce qui va nous permettre de diviser la valeur d'autoreaload) } +void MyPWM_init ( TIM_TypeDef * Timer,char Channel) +{ + if(Channel==1) + { + Timer->CCMR1&=~0x00FF; + Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0; + Timer->CCMR1 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2; + Timer->CCER |= TIM_CCER_CC1E; + + } + if(Channel==2) + { + Timer->CCMR1&=~0xFF00; + Timer->CCMR1 &= ~TIM_CCMR1_OC2M_0; + Timer->CCMR1 |= TIM_CCMR1_OC2M_1| TIM_CCMR1_OC2M_2; + Timer->CCER |= TIM_CCER_CC2E; + + } + if(Channel==3) + { + Timer->CCMR2&=~0x00FF; + Timer->CCMR2 &= ~TIM_CCMR2_OC3M_0; + Timer->CCMR2 |= TIM_CCMR2_OC3M_1| TIM_CCMR2_OC3M_2; + Timer->CCER |= TIM_CCER_CC3E; + + } + if(Channel==4) + { + Timer->CCMR2&=~0xFF00; + Timer->CCMR2 &= ~TIM_CCMR2_OC4M_0; + Timer->CCMR2 |= TIM_CCMR2_OC4M_1| TIM_CCMR2_OC4M_2; + Timer->CCER |= TIM_CCER_CC4E; + + } +} +void MyPWM_Duty (TIM_TypeDef * Timer,char Channel, unsigned short CRR ) +{ + + + if(Channel==1) + { + Timer->CCR1=CRR; + } + if(Channel==2) + { + Timer->CCR2=CRR; + } + if(Channel==3) + { + Timer->CCR3=CRR; + + } + if(Channel==4) + { + Timer->CCR4=CRR; + } + +} void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void)) { int num_tim; - ptr_func=IT_function; - Timer->DIER |= 0x01; + Timer->DIER |= 0x01; // On autorise les interruptions sur timer if(Timer==TIM1) { - num_tim=TIM1_UP_IRQn; + num_tim=TIM1_UP_IRQn; + tim_ptr1_func=IT_function; } else if(Timer==TIM2) { num_tim=TIM2_IRQn; + tim_ptr2_func=IT_function; } else if(Timer==TIM3) { num_tim=TIM3_IRQn; + tim_ptr3_func=IT_function; } else if(Timer==TIM4) { num_tim=TIM4_IRQn; + tim_ptr4_func=IT_function; } - NVIC->ISER[0] |= 0x01<IP[num_tim] |= Prio << 4; + NVIC->ISER[0] |= 0x01<IP[num_tim] |= Prio << 4; // On précise la priorité qu'on souhaite lui donner + +} + +void init_encoder_timer(TIM_TypeDef * Timer) //voir page 391 +{ + Timer->SMCR = 0x1; } void TIM2_IRQHandler (void) { - if(ptr_func!=0) + if(tim_ptr2_func!=0) { - (*ptr_func)(); + (*tim_ptr2_func)(); } - TIM2->SR &= ~(1<<0) ; + TIM2->SR &= ~(1<<0) ; // Remet à 0 le flag de l'interruption } void TIM3_IRQHandler (void) { - if(ptr_func!=0) + if(tim_ptr3_func!=0) { - (*ptr_func)(); + (*tim_ptr3_func)(); } TIM3->SR &= ~(1<<0) ; } void TIM4_IRQHandler (void) { - if(ptr_func!=0) + if(tim_ptr4_func!=0) { - (*ptr_func)(); + (*tim_ptr4_func)(); } TIM4->SR &= ~(1<<0) ; } -void TIM1_UP_IRQHandler (void) // à vérifier +void TIM1_UP_IRQHandler (void) { - if(ptr_func!=0) + if(tim_ptr1_func!=0) { - (*ptr_func)(); + (*tim_ptr1_func)(); } TIM1->SR &= ~(1<<0) ; -} +} \ No newline at end of file diff --git a/Projet_DevDrivers/Sources/Principal.c b/Projet_DevDrivers/Sources/Principal.c index 94d2c2f..32998c1 100644 --- a/Projet_DevDrivers/Sources/Principal.c +++ b/Projet_DevDrivers/Sources/Principal.c @@ -10,29 +10,39 @@ void Mafonction_IT (void); MyGPIO_Struct_TypeDef PA5; MyGPIO_Struct_TypeDef PC13; -MyTimer_Struct_TypeDef timer2; +MyTimer_Struct_TypeDef timer2,timer3; //PA5 LED //PC13 Bouton char X0,X1,Y0,Y1,Z0,Z1; char read_DATA,read_BWR,read_PWRC; int16_t gX,gY,gZ; - +int test =0; XYZ mesures; int main ( void ) { + PA5.GPIO=GPIOA; PA5.GPIO_Conf=Out_Ppull; PA5.GPIO_Pin=5; MyGPIO_Init(&PA5); timer2.Timer=TIM2; - timer2.ARR=35999; //pour avoir 500ms + timer2.ARR=18000; //pour avoir 250ms timer2.PSC=1000; MyTimer_Base_Init(&timer2); MyTimer_ActiveIT(timer2.Timer,1, &Mafonction_IT); MyTimer_Base_Start(timer2.Timer); + timer3.Timer=TIM3; + timer3.ARR=7200; //pour avoir 100ms + timer3.PSC=1000; + MyTimer_Base_Init(&timer3); + //MyTimer_ActiveIT(timer3.Timer,2, &Mafonction_IT); + MyTimer_Base_Start(timer3.Timer); + MyPWM_init(timer3.Timer,3); + + MySPI_Init(SPI1); rouli_InitAccel(); MySPI_Send(READ|DATA_FORMAT); @@ -44,8 +54,19 @@ int main ( void ) while(1) { - rouli_GetAccel(&mesures); - //printf("%f, %f, %f\n",mesures.gX,mesures.gY,mesures.gZ); + rouli_GetAccel(&mesures); + if(timer2.Timer->SR&=0x04) + { + + if((mesures.gY <= (-0.7)) || (mesures.gY >= 0.7)) + { + MyPWM_Duty(timer3.Timer,3,71);//0.985ms + test = test+1; + } + } + + + } return 0; } diff --git a/Projet_DevDrivers/Rouli.c b/Projet_DevDrivers/Sources/Rouli.c similarity index 97% rename from Projet_DevDrivers/Rouli.c rename to Projet_DevDrivers/Sources/Rouli.c index 37a0371..6942c6a 100644 --- a/Projet_DevDrivers/Rouli.c +++ b/Projet_DevDrivers/Sources/Rouli.c @@ -43,4 +43,6 @@ void rouli_GetAccel (XYZ * axe) axe->gZ = ((short int)((Z1<<8)|Z0))*0.004; } -// axe y entre -0.5 et 1 \ No newline at end of file +// axe y entre -0.7 et 0.7 + + diff --git a/Projet_DevDrivers/TP1.uvoptx b/Projet_DevDrivers/TP1.uvoptx index 02bf644..4d8876f 100644 --- a/Projet_DevDrivers/TP1.uvoptx +++ b/Projet_DevDrivers/TP1.uvoptx @@ -312,7 +312,7 @@ 0 DLGTARM - (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=1190,328,1611,733,1)(121=1101,403,1522,808,0)(122=1281,393,1702,798,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=-1,-1,-1,-1,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) + (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=900,708,1321,1113,0)(121=1464,713,1885,1118,1)(122=1281,393,1702,798,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=783,105,1377,799,1)(132=1326,11,1920,705,1)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) 0 @@ -342,6 +342,11 @@ 1 mesures + + 1 + 1 + test + @@ -473,10 +478,10 @@ 1 6 1 - 1 + 0 0 0 - .\Rouli.c + .\Sources\Rouli.c Rouli.c 0 0 diff --git a/Projet_DevDrivers/TP1.uvprojx b/Projet_DevDrivers/TP1.uvprojx index 8e3b0d6..27ae51e 100644 --- a/Projet_DevDrivers/TP1.uvprojx +++ b/Projet_DevDrivers/TP1.uvprojx @@ -411,7 +411,7 @@ Rouli.c 1 - .\Rouli.c + .\Sources\Rouli.c @@ -828,7 +828,7 @@ Rouli.c 1 - .\Rouli.c + .\Sources\Rouli.c