Voilier/Drivers/Sources/Driver_Timer.c
2023-03-22 16:56:22 +01:00

46 lines
1.5 KiB
C

#include "Driver_Timer.h"
//-----------------------INITIALISATION TIMER---------------------//
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer){
if(Timer->Timer == TIM1){
//RCC->APB2ENR |= 0x0001<<11;
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
}
else if(Timer->Timer == TIM2){
//RCC->APB1ENR |= 0x0001;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
}
else if(Timer->Timer == TIM3){
//RCC->APB1ENR |= 0x0001 <<1;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
}
else if(Timer->Timer == TIM4){
//RCC->APB1ENR |= 0x0001 <<2;
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
}
Timer->Timer->ARR = Timer->ARR;
Timer->Timer->PSC = Timer->PSC;
}
//-----------------------START----------------------//
void MyTimer_Base_Start(TIM_TypeDef * Timer){
Timer->CR1 |= TIM_CR1_CEN; //Masque OU pour placer un 1 décalé avec des 0
}
//------------------------STOP----------------------//
void MyTimer_Base_Stop(TIM_TypeDef * Timer){
Timer->CR1 |= ~TIM_CR1_CEN; //Masque ET pour placer un 0 décalé avec des 1 (~)
}
void MyTimer_PWM( MyTimer_Struct_TypeDef * Timer, uint16_t cycle){
Timer->Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0; //Configuration du canal CH1
Timer->Timer->CCMR1 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2; // Ajouter 110 aux bits OC1M (registre CCMR1)
Timer->Timer->CCER |= TIM_CCER_CC1E; // Canal CH1 validé par bit CC1E (registre CCER)
Timer->Timer->CR1 |= TIM_CR1_CEN; // Lancement du timer
Timer->Timer->CCR1 = (cycle * Timer->ARR) / 100; // Fixer la durée à 20%
}