diff --git a/ProjetsKEIL/Drivers/inc/timer.h b/ProjetsKEIL/Drivers/inc/timer.h new file mode 100644 index 0000000..1a3ff25 --- /dev/null +++ b/ProjetsKEIL/Drivers/inc/timer.h @@ -0,0 +1,19 @@ +#ifndef MYTIMER_H +#define MYTIMER_H +#include "stm32f10x.h" + +typedef struct { + TIM_TypeDef * Timer; + unsigned short ARR; + unsigned short PSC; +} MyTimer_Struct_Typedef; + +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); + +#define MyTimer_Base_Start(Tim) (Tim.Timer->CR1 |= TIM_CR1_CEN) +#define MyTimer_Base_Stop(Tim) (Tim.Timer->CR1 &= ~TIM_CR1_CEN) + +#endif diff --git a/ProjetsKEIL/Drivers/src/gpio.c b/ProjetsKEIL/Drivers/src/gpio.c index 90664e3..c4c047f 100644 --- a/ProjetsKEIL/Drivers/src/gpio.c +++ b/ProjetsKEIL/Drivers/src/gpio.c @@ -70,5 +70,10 @@ void MyGPIO_Reset(GPIO_TypeDef * GPIO, char GPIO_Pin) { } void MyGPIO_Toggle(GPIO_TypeDef * GPIO, char GPIO_Pin) { - + if (MyGPIO_Read(GPIO, GPIO_Pin) == 0x1) { + MyGPIO_Reset(GPIO, GPIO_Pin); + } + else { + MyGPIO_Set(GPIO, GPIO_Pin); + } } diff --git a/ProjetsKEIL/Drivers/src/timer.c b/ProjetsKEIL/Drivers/src/timer.c new file mode 100644 index 0000000..33a5fcc --- /dev/null +++ b/ProjetsKEIL/Drivers/src/timer.c @@ -0,0 +1,96 @@ +#include "timer.h" + +void plantage(void) { + while(1); +} + +void (*IT_Tim2) (void) = plantage; +void (*IT_Tim3) (void) = plantage; +void (*IT_Tim4) (void) = plantage; + +void MyTimer_Base_Init(MyTimer_Struct_Typedef * Timer) { + if ((Timer->Timer) == TIM2) + RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; + if ((Timer->Timer) == TIM3) + RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; + if ((Timer->Timer) == TIM4) + RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; + + Timer->Timer->PSC = Timer->PSC; + Timer->Timer->ARR = Timer->ARR; +} + +void MyTimer_ActiveIT(TIM_TypeDef * Timer, char Prio, void (*IT_function) (void)) { + Timer->DIER |= TIM_DIER_UIE; + + if (Timer == TIM2) { + NVIC_EnableIRQ(TIM2_IRQn); + NVIC_SetPriority(TIM2_IRQn, Prio); + IT_Tim2 = IT_function; + } + if (Timer == TIM3) { + NVIC_EnableIRQ(TIM3_IRQn); + NVIC_SetPriority(TIM3_IRQn, Prio); + IT_Tim3 = IT_function; + } + if (Timer == TIM4) { + NVIC_EnableIRQ(TIM4_IRQn); + NVIC_SetPriority(TIM4_IRQn, Prio); + IT_Tim4 = IT_function; + } +} + +void TIM2_IRQHandler(void) { + TIM2->SR &= ~TIM_SR_UIF; + (*IT_Tim2)(); +} + +void TIM3_IRQHandler(void) { + TIM3->SR &= ~TIM_SR_UIF; + (*IT_Tim3)(); +} + +void TIM4_IRQHandler(void) { + TIM4->SR &= ~TIM_SR_UIF; + (*IT_Tim4)(); +} + +void MyTimer_PWM(TIM_TypeDef * Timer ,char Channel) +{ + switch (Channel) { + case 1: + case 2: + Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0; + 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; + break; + } + + Timer->CCER |= (TIM_CCER_CC1E << (4*(Channel-1))); +} + +void MyTimer_DutyCycle(TIM_TypeDef * Timer, char Channel, unsigned short DutyCycle) +{ + switch (Channel) { + case 1: + Timer->CCR1 = DutyCycle; + break; + + case 2: + Timer->CCR2 = DutyCycle; + break; + + case 3: + Timer->CCR3 = DutyCycle; + break; + + case 4: + Timer->CCR4 = DutyCycle; + break; + } +} \ No newline at end of file diff --git a/ProjetsKEIL/Projet/src/Principal.c b/ProjetsKEIL/Projet/src/Principal.c index 8069f75..f98aa24 100644 --- a/ProjetsKEIL/Projet/src/Principal.c +++ b/ProjetsKEIL/Projet/src/Principal.c @@ -1,9 +1,13 @@ #include "stm32f10x.h" #include "gpio.h" +#include "timer.h" + +void changer_led(void); int main(void) { MyGPIO_Struct_TypeDef led2; MyGPIO_Struct_TypeDef b1; + MyTimer_Struct_Typedef t3; MyGPIO_InitClock(); @@ -13,17 +17,28 @@ int main(void) { b1.GPIO = GPIOC; b1.GPIO_Pin = 13; b1.GPIO_Conf = In_PullUp; + t3.Timer = TIM3; + t3.ARR = 7199; + t3.PSC = 4999; MyGPIO_Init(&led2); MyGPIO_Init(&b1); + MyTimer_Base_Init(&t3); + MyTimer_ActiveIT(TIM3, 0, &changer_led); + + MyTimer_Base_Start(t3); do { - if (MyGPIO_Read(GPIOC, 13) == 0x1) { + /*if (MyGPIO_Read(GPIOC, 13) == 0x1) { MyGPIO_Reset(GPIOA, 5); } else { MyGPIO_Set(GPIOA, 5); - } + }*/ } while (1); -} \ No newline at end of file +} + +void changer_led(void) { + MyGPIO_Toggle(GPIOA, 5); +} diff --git a/ProjetsKEIL/Projet/tp1.uvoptx b/ProjetsKEIL/Projet/tp1.uvoptx index a6d95d1..0513012 100644 --- a/ProjetsKEIL/Projet/tp1.uvoptx +++ b/ProjetsKEIL/Projet/tp1.uvoptx @@ -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=-1,-1,-1,-1,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=150,186,744,937,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) + (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) 0 @@ -414,6 +414,18 @@ 0 0 + + 2 + 3 + 1 + 1 + 0 + 0 + ..\Drivers\src\timer.c + timer.c + 0 + 0 + @@ -426,7 +438,7 @@ ::Device - 0 + 1 0 0 1 diff --git a/ProjetsKEIL/Projet/tp1.uvprojx b/ProjetsKEIL/Projet/tp1.uvprojx index f5fb201..3fe9bc3 100644 --- a/ProjetsKEIL/Projet/tp1.uvprojx +++ b/ProjetsKEIL/Projet/tp1.uvprojx @@ -339,7 +339,7 @@ - .\Include + ..\Drivers\inc;.\inc @@ -398,6 +398,11 @@ 1 ..\Drivers\src\gpio.c + + timer.c + 1 + ..\Drivers\src\timer.c + @@ -800,6 +805,11 @@ 1 ..\Drivers\src\gpio.c + + timer.c + 1 + ..\Drivers\src\timer.c +