From e8ce9fb91d41e11edd2c392225defd2163c9de0e Mon Sep 17 00:00:00 2001 From: Noel Jumin Date: Tue, 11 Apr 2023 15:32:11 +0200 Subject: [PATCH] Girouette qui marche --- Drivers/Inc/GPIO.h | 2 +- Drivers/Inc/TIMER.h | 5 + Drivers/Src/ADC.c | 4 +- Drivers/Src/GPIO.c | 47 ++++---- Drivers/Src/TIMER.c | 53 ++++++++- Projet_voile/Includes/Girouette.h | 4 +- Projet_voile/Includes/Pwm_voile.h | 9 ++ Projet_voile/Sources/Girouette.c | 33 ++++-- Projet_voile/Sources/Principal.c | 19 ++-- Projet_voile/Sources/Pwm_voile.c | 44 ++++++++ Projet_voile/TP1.uvoptx | 176 ++++++++++++++++++++++++------ Projet_voile/TP1.uvprojx | 22 ++-- 12 files changed, 330 insertions(+), 88 deletions(-) create mode 100644 Projet_voile/Includes/Pwm_voile.h create mode 100644 Projet_voile/Sources/Pwm_voile.c diff --git a/Drivers/Inc/GPIO.h b/Drivers/Inc/GPIO.h index 75faf06..d8e848e 100644 --- a/Drivers/Inc/GPIO.h +++ b/Drivers/Inc/GPIO.h @@ -25,6 +25,6 @@ int MyGPIO_Read ( GPIO_TypeDef * GPIO , char GPIO_Pin ) ; // renvoie 0 ou 1 void MyGPIO_Set ( GPIO_TypeDef * GPIO , char GPIO_Pin ) ; void MyGPIO_Reset ( GPIO_TypeDef * GPIO , char GPIO_Pin ) ; void MyGPIO_Toggle ( GPIO_TypeDef * GPIO , char GPIO_Pin ) ; -void MyGPIO_ActiveIT (GPIO_TypeDef * GPIO, char GPIO_Pin, char Prio, void (*IT_function)(void)) +void MyGPIO_ActiveIT (GPIO_TypeDef * GPIO, char GPIO_Pin, char Prio, void (*IT_function)(void)) ; #endif diff --git a/Drivers/Inc/TIMER.h b/Drivers/Inc/TIMER.h index 57bfb3c..9f009f0 100644 --- a/Drivers/Inc/TIMER.h +++ b/Drivers/Inc/TIMER.h @@ -1,7 +1,9 @@ #ifndef MYTIMER_H #define MYTIMER_H +#include "GPIO.h" #include "stm32f10x.h" + typedef struct { TIM_TypeDef * Timer ; // TIM1 à TIM4 @@ -38,5 +40,8 @@ void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void) void MyPWM_init ( TIM_TypeDef * Timer,char Channel); void MyPWM_Duty (TIM_TypeDef * Timer,char Channel, unsigned short CRR ); +void init_encoder_timer(void (*IT_function)(void)); +void Reset_degree (void); +int Read_CNT (void); #endif diff --git a/Drivers/Src/ADC.c b/Drivers/Src/ADC.c index 3b9000e..b137088 100644 --- a/Drivers/Src/ADC.c +++ b/Drivers/Src/ADC.c @@ -117,9 +117,9 @@ void MyADC_init(MyADC_Struct_TypeDef* myADC) } else if(myADC->ADC==ADC2) { - RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // validation horloge ADC1 + RCC->APB2ENR |= RCC_APB2ENR_ADC2EN; // validation horloge ADC2 RCC->CFGR |= RCC_CFGR_ADCPRE_DIV6; // passage de l'horloge ADC1 à 12MHz - ADC2->CR2|= ADC_CR2_ADON; // démarrage ADC1 + ADC2->CR2|= ADC_CR2_ADON; // démarrage ADC2 ADC2->SQR1&= ADC_SQR1_L; // fixe le nombre de conversion à 1 ADC2->SQR3|= myADC->channel; // indique la voie à convertir ADC2->CR2 |= ADC_CR2_CAL; // début de la calibration diff --git a/Drivers/Src/GPIO.c b/Drivers/Src/GPIO.c index e4e3334..de31d25 100644 --- a/Drivers/Src/GPIO.c +++ b/Drivers/Src/GPIO.c @@ -38,7 +38,7 @@ void MyGPIO_Init ( MyGPIO_Struct_TypeDef * GPIOStructPtr ) } else // Si le numéro de pin est inférieur à 8 alors on écrit dans le registre CRLow { - GPIOStructPtr->GPIO->CRL &= ~0xF<<(4*((GPIOStructPtr->GPIO_Pin)%8)); //on met a 0 les bit de config du pin + GPIOStructPtr->GPIO->CRL &= ~(0xF<<(4*((GPIOStructPtr->GPIO_Pin)%8))); //on met a 0 les bit de config du pin GPIOStructPtr->GPIO->CRL |= GPIOStructPtr->GPIO_Conf<<(4*((GPIOStructPtr->GPIO_Pin)%8)); // On met à 1 les bons bits pour obtenir la config souhaité sur le pin } } @@ -74,37 +74,32 @@ void MyGPIO_Toggle ( GPIO_TypeDef * GPIO , char GPIO_Pin ) void MyGPIO_ActiveIT (GPIO_TypeDef * GPIO, char GPIO_Pin, char Prio, void (*IT_function)(void)) //p210 { RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; // On active l'horloge pour AFIO - if (GPIO==GPIOA) + if (GPIO==GPIOC) { - AFIO->EXTICR[0] |= (AFIO_EXTICR1_EXTI0_PA << (4*GPIO_Pin)); + AFIO->EXTICR[GPIO_Pin] |= AFIO_EXTICR1_EXTI3_PC ; } - else if (GPIO==GPIOB) - { - AFIO->EXTICR[0] |= (AFIO_EXTICR1_EXTI0_PB << (4*GPIO_Pin)); - } - else if (GPIO==GPIOC) - { - AFIO->EXTICR[0] |= (AFIO_EXTICR1_EXTI0_PC << (4*GPIO_Pin)); - } - else if (GPIO==GPIOD) - { - AFIO->EXTICR[0] |= (AFIO_EXTICR1_EXTI0_PD << (4*GPIO_Pin)); - } - // manque ligne pour activer l'interruption - NVIC->ISER[0] |= 0x01<IP[EXTI0_IRQn] |= Prio << 4; // On précise la priorité qu'on souhaite lui donner + EXTI->RTSR |= GPIO_Pin; +// EXTI->FTSR |= ~(0x01<EMR |= GPIO_Pin; + NVIC->ISER[0] |= 0x01<IP[EXTI3_IRQn] |= Prio << 4; // On précise la priorité qu'on souhaite lui donner } +void EXTI3_IRQHandler(void) +{ + if(gpio_ptr_func!=0) + { + (*gpio_ptr_func)(); + } + EXTI->PR &= ~(AFIO_EXTICR1_EXTI3_PC << (4*4)); // car c'est le pin4 +} + + void EXTI0_IRQHandler(void) { - if (EXTI->RTSR) //on test si RTSR est différent de 0 + if(gpio_ptr_func!=0) { - if(gpio_ptr_func!=0) - { - (*gpio_ptr_func)(); - } + (*gpio_ptr_func)(); } - EXTI->RTSR &=0xFFFFF; // On reset le flag lmevé - - //reset flag + EXTI->PR &= ~(AFIO_EXTICR1_EXTI0_PC << (4*4)); // car c'est le pin4 } diff --git a/Drivers/Src/TIMER.c b/Drivers/Src/TIMER.c index 7ec2b8d..b32e923 100644 --- a/Drivers/Src/TIMER.c +++ b/Drivers/Src/TIMER.c @@ -114,10 +114,59 @@ void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void) } -void init_encoder_timer(TIM_TypeDef * Timer) //voir page 391 +void init_encoder_timer(void (*IT_function)(void)) //voir page 391 { - Timer->SMCR = 0x1; + MyGPIO_Struct_TypeDef GPIO_PB6; + MyGPIO_Struct_TypeDef GPIO_PB7; + MyTimer_Struct_TypeDef Timer; + GPIO_PB6.GPIO = GPIOB; + GPIO_PB6.GPIO_Conf = In_Floating; + GPIO_PB6.GPIO_Pin = 6; + + + GPIO_PB7.GPIO = GPIOB; + GPIO_PB7.GPIO_Conf = In_Floating; + GPIO_PB7.GPIO_Pin = 7; + + + /*GPIO_PC3.GPIO = GPIOC; + GPIO_PC3.GPIO_Conf = In_Floating; + GPIO_PC3.GPIO_Pin = 3;*/ + + Timer.Timer=TIM4; + Timer.ARR = 719; + Timer.PSC = 0; + + MyTimer_Base_Init(&Timer); + + MyGPIO_Init (&GPIO_PB6); + + MyGPIO_Init (&GPIO_PB7); + + //MyGPIO_Init (&GPIO_PC3); + + //MyGPIO_ActiveIT (GPIO_PC3.GPIO, GPIO_PC3.GPIO_Pin, 4, IT_function); + + TIM4-> SMCR &= ~0x0007; + TIM4-> SMCR |= TIM_SMCR_SMS_1; + TIM4-> CCMR1 &= ~0xF2F2; // Mise à 0 des CC1S, CC2S, IC1F et IC2F + TIM4-> CCMR1 |= TIM_CCMR1_CC1S_0; + TIM4-> CCMR1 |= TIM_CCMR1_CC2S_0; + TIM4-> CCER &= TIM_CCER_CC1P; + TIM4-> CCER &= TIM_CCER_CC2P; + TIM4-> CR1 |= TIM_CR1_CEN; + +} + +void Reset_degree (void) +{ + TIM4->CNT = 0x0000; +} + +int Read_CNT (void) +{ + return TIM4->CNT; } void TIM2_IRQHandler (void) diff --git a/Projet_voile/Includes/Girouette.h b/Projet_voile/Includes/Girouette.h index d3eb84e..33c07c2 100644 --- a/Projet_voile/Includes/Girouette.h +++ b/Projet_voile/Includes/Girouette.h @@ -2,7 +2,9 @@ #define MYGIROUETTE_H #include "stm32f10x.h" #include "GPIO.h" +#include "TIMER.h" - +void init_girouette (void); +void Test_tour_girouette(void); #endif diff --git a/Projet_voile/Includes/Pwm_voile.h b/Projet_voile/Includes/Pwm_voile.h new file mode 100644 index 0000000..9c11f51 --- /dev/null +++ b/Projet_voile/Includes/Pwm_voile.h @@ -0,0 +1,9 @@ +#ifndef MYPWMVOILE_H +#define MYPWMVOILE_H +#include "stm32f10x.h" +#include "GPIO.h" +#include "TIMER.h" + +void init_pwm_voile(void); + +#endif diff --git a/Projet_voile/Sources/Girouette.c b/Projet_voile/Sources/Girouette.c index 634c02e..26d2027 100644 --- a/Projet_voile/Sources/Girouette.c +++ b/Projet_voile/Sources/Girouette.c @@ -1,13 +1,26 @@ #include "Girouette.h" -int bordage(void){ - /*MyGPIO_Struct_TypeDef signalA = (GPIOC, 6, In_PullDown); - MyGPIO_Struct_TypeDef signalB = (GPIOC, 7, In_PullDown); - MyGPIO_Struct_TypeDef signalI = (GPIOC, 8, In_PullDown); +MyGPIO_Struct_TypeDef GPIOC3; + +void Test_Encoder (void) +{ + Reset_degree(); +} + +void init_girouette (void) +{ - MyGPIO_Init(&signalA); - MyGPIO_Init(&signalB); - MyGPIO_Init(&signalI);*/ - - -} \ No newline at end of file + GPIOC3.GPIO = GPIOC; + GPIOC3.GPIO_Conf = In_Floating; + GPIOC3.GPIO_Pin = 3; + MyGPIO_Init (&GPIOC3); + init_encoder_timer(&Test_Encoder); +} + +void Test_tour_girouette(void){ + if(MyGPIO_Read(GPIOC3.GPIO, GPIOC3.GPIO_Pin)) + { + Reset_degree(); + } +} + diff --git a/Projet_voile/Sources/Principal.c b/Projet_voile/Sources/Principal.c index 2b8afb2..a9d5778 100644 --- a/Projet_voile/Sources/Principal.c +++ b/Projet_voile/Sources/Principal.c @@ -3,6 +3,9 @@ #include #include "GPIO.h" #include "UART.h" +#include "TIMER.h" +#include "Girouette.h" +#include "Pwm_voile.h" char MyChar; @@ -16,19 +19,21 @@ void UART_RX_IT (void) int main ( void ) { - MyGPIO_Struct_TypeDef myGPIO; - myGPIO.GPIO = GPIOA; - myGPIO.GPIO_Conf = Out_Ppull; - myGPIO.GPIO_Pin = 5; + MyGPIO_Struct_TypeDef GPIOA5; + GPIOA5.GPIO = GPIOA; + GPIOA5.GPIO_Conf = Out_Ppull; + GPIOA5.GPIO_Pin = 5; + MyGPIO_Init (&GPIOA5); - MyGPIO_Init (&myGPIO); + init_girouette(); + init_pwm_voile(); MyUART_init(); MyUART_ActiveIT(1, &UART_RX_IT); //mode permet d'activer soit l'interruption sur TX - while(1) { - + Test_tour_girouette(); + //Orientation_voiles(); } } diff --git a/Projet_voile/Sources/Pwm_voile.c b/Projet_voile/Sources/Pwm_voile.c new file mode 100644 index 0000000..f64d48e --- /dev/null +++ b/Projet_voile/Sources/Pwm_voile.c @@ -0,0 +1,44 @@ +# include "Pwm_voile.h" + +MyTimer_Struct_TypeDef Timer2, Timer3; +MyGPIO_Struct_TypeDef PB0; + +void write_pwm(void) +{ + int val_pwm; + float cnt; + cnt=Read_CNT(); + if(cnt>=360) + { + cnt=cnt-360; + val_pwm=(Timer3.ARR/20)+((Timer3.ARR/20)*(cnt/360.0)); + } + else + { + val_pwm=2*(Timer3.ARR/20)-((Timer3.ARR/20)*(cnt/360.0)); + } + //Timer3.ARR/20 --> 1ms, pour controler le moteur c'est entre 1ms et 2ms + //calcul angle en pwm + MyPWM_Duty(Timer3.Timer,3,val_pwm); +} + +void init_pwm_voile(void) +{ + Timer2.Timer=TIM2; + Timer2.ARR=1800; + Timer2.PSC=1000; + MyTimer_Base_Init(&Timer2); + MyTimer_ActiveIT(Timer2.Timer,2,&write_pwm); + MyTimer_Base_Start(Timer2.Timer); + PB0.GPIO=GPIOB; + PB0.GPIO_Conf=AltOut_Ppull; + PB0.GPIO_Pin=0; + MyGPIO_Init(&PB0); + Timer3.Timer=TIM3; + Timer3.ARR=14400; + Timer3.PSC=100; + MyTimer_Base_Init(&Timer3); + MyPWM_init(Timer3.Timer,3); + MyTimer_Base_Start(Timer3.Timer); + +} diff --git a/Projet_voile/TP1.uvoptx b/Projet_voile/TP1.uvoptx index 79e4a30..1d9f429 100644 --- a/Projet_voile/TP1.uvoptx +++ b/Projet_voile/TP1.uvoptx @@ -75,12 +75,12 @@ 1 0 - 1 + 0 - 0 + 18 - 0 - 1 + 1 + 0 1 1 1 @@ -117,25 +117,73 @@ BIN\UL2CM3.DLL + + 0 + ARMRTXEVENTFLAGS + -L70 -Z18 -C0 -M0 -T1 + + + 0 + DLGDARM + (1010=710,304,1086,861,1)(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=1161,471,1582,898,1)(121=-1,-1,-1,-1,0)(122=1317,323,1738,750,0)(123=-1,-1,-1,-1,0)(140=1117,100,1805,440,1)(240=263,238,662,583,1)(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)(234=-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 + ARMDBGFLAGS + -T0 + 0 UL2CM3 UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM)) - + + + 0 + 0 + 90 + 1 +
134218190
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\DRIVERS\Src\GPIO.c + + \\TP1\../DRIVERS/Src/GPIO.c\90 +
+ + 1 + 0 + 100 + 1 +
134218154
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\DRIVERS\Src\GPIO.c + + \\TP1\../DRIVERS/Src/GPIO.c\100 +
+
0 0 - 0 - 0 + 1 + 1 0 - 0 + 1 0 0 - 0 + 1 0 0 0 @@ -144,7 +192,7 @@ 0 0 0 - 0 + 1 0 0 0 @@ -181,12 +229,12 @@ 0x4 ARM-ADS - 12000000 + 8000000 - 0 + 1 1 - 1 - 0 + 0 + 1 0 @@ -228,14 +276,14 @@ 0 - 0 + 1 0 - 0 + 1 - 0 + 18 - 1 - 0 + 0 + 1 1 1 1 @@ -244,7 +292,7 @@ 1 1 1 - 0 + 1 1 1 1 @@ -258,7 +306,7 @@ 1 0 0 - -1 + 6 @@ -269,21 +317,60 @@ - + STLink\ST-LINKIII-KEIL_SWO.dll + + + 0 + ARMRTXEVENTFLAGS + -L70 -Z18 -C0 -M0 -T1 + + + 0 + DLGTARM + (1010=457,337,833,894,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=1002,18,1423,423,0)(121=235,274,656,679,1)(122=353,663,774,1068,0)(123=-1,-1,-1,-1,0)(140=1031,78,1719,418,0)(240=1050,425,1449,770,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=377,147,971,841,1)(133=139,242,733,936,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 + ARMDBGFLAGS + + + + 0 + ST-LINKIII-KEIL_SWO + -U-O206 -O206 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM) + + + 0 + DLGUARM + (105=-1,-1,-1,-1,0) + + + 0 + UL2CM3 + UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM)) + + + + + 0 + 1 + cnt,0x0A + + 0 0 - 0 - 0 + 1 + 1 0 - 0 + 1 0 0 - 0 + 1 0 0 0 @@ -292,7 +379,7 @@ 0 0 0 - 0 + 1 0 0 0 @@ -314,6 +401,19 @@ + + + System Viewer\TIM4 + 35905 + + + + 1 + 0 + 0 + 2 + 10000000 + @@ -339,7 +439,7 @@ 1 2 1 - 0 + 1 0 0 .\Sources\Girouette.c @@ -351,7 +451,7 @@ 1 3 1 - 0 + 1 0 0 ..\DRIVERS\Src\GPIO.c @@ -363,7 +463,7 @@ 1 4 1 - 0 + 1 0 0 ..\Drivers\Src\TIMER.c @@ -387,7 +487,7 @@ 1 6 1 - 1 + 0 0 0 ..\Drivers\Src\UART.c @@ -395,6 +495,18 @@ 0 0 + + 1 + 7 + 1 + 1 + 0 + 0 + .\Sources\Pwm_voile.c + Pwm_voile.c + 0 + 0 + @@ -407,7 +519,7 @@ ::Device - 0 + 1 0 0 1 diff --git a/Projet_voile/TP1.uvprojx b/Projet_voile/TP1.uvprojx index 2d4f63a..8a285b8 100644 --- a/Projet_voile/TP1.uvprojx +++ b/Projet_voile/TP1.uvprojx @@ -10,13 +10,13 @@ Simulé 0x4 ARM-ADS - 6190000::V6.19::ARMCLANG + 6160000::V6.16::ARMCLANG 1 STM32F103RB STMicroelectronics - Keil.STM32F1xx_DFP.2.4.0 + Keil.STM32F1xx_DFP.2.3.0 http://www.keil.com/pack/ IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00020000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE @@ -186,7 +186,6 @@ 0 0 0 - 0 0 0 8 @@ -414,6 +413,11 @@ 1 ..\Drivers\Src\UART.c + + Pwm_voile.c + 1 + .\Sources\Pwm_voile.c + @@ -604,7 +608,6 @@ 0 0 0 - 0 0 0 8 @@ -832,6 +835,11 @@ 1 ..\Drivers\Src\UART.c + + Pwm_voile.c + 1 + .\Sources\Pwm_voile.c + @@ -866,7 +874,7 @@ RTE\Device\STM32F103RB\RTE_Device.h - + @@ -875,7 +883,7 @@ RTE\Device\STM32F103RB\startup_stm32f10x_md.s - + @@ -884,7 +892,7 @@ RTE\Device\STM32F103RB\system_stm32f10x.c - +