From 125c801a01fd2c38a07f69f91f00efa5e991110d Mon Sep 17 00:00:00 2001 From: Alix Chagot Date: Tue, 21 Mar 2023 15:57:44 +0100 Subject: [PATCH] fin act2 --- ProjetsKEIL/Drivers/inc/timer.h | 2 +- ProjetsKEIL/Drivers/src/timer.c | 80 +++++++++++++++++++++++++++--- ProjetsKEIL/Projet/src/Principal.c | 4 +- ProjetsKEIL/Projet/tp1.uvoptx | 8 +-- 4 files changed, 81 insertions(+), 13 deletions(-) diff --git a/ProjetsKEIL/Drivers/inc/timer.h b/ProjetsKEIL/Drivers/inc/timer.h index 1a3ff25..281867e 100644 --- a/ProjetsKEIL/Drivers/inc/timer.h +++ b/ProjetsKEIL/Drivers/inc/timer.h @@ -11,7 +11,7 @@ typedef struct { void MyTimer_Base_Init(MyTimer_Struct_Typedef * Timer); void MyTimer_ActiveIT(TIM_TypeDef * Timer, char Prio, void (*IT_function) (void)); void MyTimer_PWM(TIM_TypeDef * Timer ,char Channel); -void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned short DutyCycle); +void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned char DutyCycle); #define MyTimer_Base_Start(Tim) (Tim.Timer->CR1 |= TIM_CR1_CEN) #define MyTimer_Base_Stop(Tim) (Tim.Timer->CR1 &= ~TIM_CR1_CEN) diff --git a/ProjetsKEIL/Drivers/src/timer.c b/ProjetsKEIL/Drivers/src/timer.c index 33a5fcc..88936ed 100644 --- a/ProjetsKEIL/Drivers/src/timer.c +++ b/ProjetsKEIL/Drivers/src/timer.c @@ -1,4 +1,5 @@ #include "timer.h" +#include "gpio.h" void plantage(void) { while(1); @@ -57,40 +58,105 @@ void TIM4_IRQHandler(void) { void MyTimer_PWM(TIM_TypeDef * Timer ,char Channel) { + MyGPIO_Struct_TypeDef PWM_OUT; + PWM_OUT.GPIO_Conf = AltOut_Ppull; switch (Channel) { case 1: case 2: Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0; - Timer->CCMR1 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2; + Timer->CCMR1 |= (TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2); break; case 3: case 4: Timer->CCMR2 &= ~TIM_CCMR1_OC1M_0; - Timer->CCMR2 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2; + Timer->CCMR2 |= (TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2); break; } Timer->CCER |= (TIM_CCER_CC1E << (4*(Channel-1))); + + if(Timer == TIM2) + { + PWM_OUT.GPIO = GPIOA; + switch (Channel) { + case 1: + PWM_OUT.GPIO_Pin = 0; + break; + case 2: + PWM_OUT.GPIO_Pin = 1; + break; + case 3: + PWM_OUT.GPIO_Pin = 2; + break; + case 4: + PWM_OUT.GPIO_Pin = 3; + break; + } + } + + if(Timer == TIM3) + { + switch (Channel) { + case 1: + PWM_OUT.GPIO = GPIOA; + PWM_OUT.GPIO_Pin = 6; + break; + case 2: + PWM_OUT.GPIO = GPIOA; + PWM_OUT.GPIO_Pin = 7; + break; + case 3: + PWM_OUT.GPIO = GPIOB; + PWM_OUT.GPIO_Pin = 0; + break; + case 4: + PWM_OUT.GPIO = GPIOB; + PWM_OUT.GPIO_Pin = 1; + break; + } + } + + if(Timer == TIM4) + { + PWM_OUT.GPIO = GPIOB; + switch (Channel) { + case 1: + PWM_OUT.GPIO_Pin = 6; + break; + case 2: + PWM_OUT.GPIO_Pin = 7; + break; + case 3: + PWM_OUT.GPIO_Pin = 8; + break; + case 4: + PWM_OUT.GPIO_Pin = 9; + break; + } + } + MyGPIO_Init(&PWM_OUT); } -void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned short DutyCycle) +void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned char DutyCycle) { + unsigned int RC; + RC = ((Timer->ARR)/100)*(DutyCycle); switch (Channel) { case 1: - Timer->CCR1 = DutyCycle; + Timer->CCR1 = RC; break; case 2: - Timer->CCR2 = DutyCycle; + Timer->CCR2 = RC; break; case 3: - Timer->CCR3 = DutyCycle; + Timer->CCR3 = RC; break; case 4: - Timer->CCR4 = DutyCycle; + Timer->CCR4 = RC; break; } } \ No newline at end of file diff --git a/ProjetsKEIL/Projet/src/Principal.c b/ProjetsKEIL/Projet/src/Principal.c index f98aa24..2ced2aa 100644 --- a/ProjetsKEIL/Projet/src/Principal.c +++ b/ProjetsKEIL/Projet/src/Principal.c @@ -25,9 +25,11 @@ int main(void) { MyGPIO_Init(&b1); MyTimer_Base_Init(&t3); MyTimer_ActiveIT(TIM3, 0, &changer_led); - + MyTimer_PWM(TIM3, 1); + MyTimer_DutyCycle(TIM3,1,10); MyTimer_Base_Start(t3); + do { /*if (MyGPIO_Read(GPIOC, 13) == 0x1) { diff --git a/ProjetsKEIL/Projet/tp1.uvoptx b/ProjetsKEIL/Projet/tp1.uvoptx index 0513012..faf3266 100644 --- a/ProjetsKEIL/Projet/tp1.uvoptx +++ b/ProjetsKEIL/Projet/tp1.uvoptx @@ -75,7 +75,7 @@ 1 0 - 0 + 1 18 @@ -125,7 +125,7 @@ 0 DLGDARM - (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=920,358,1341,785,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=120,153,405,449,0)(130=-1,-1,-1,-1,0)(131=931,121,1525,872,0)(132=492,166,1086,917,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) + (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=920,358,1341,785,0)(121=-1,-1,-1,-1,0)(122=-1,-1,-1,-1,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=120,153,405,449,0)(130=-1,-1,-1,-1,0)(131=931,121,1525,872,0)(132=973,161,1567,912,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)(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 @@ -245,7 +245,7 @@ 1 0 - 1 + 0 18 @@ -406,7 +406,7 @@ 2 2 1 - 0 + 1 0 0 ..\Drivers\src\gpio.c