Projet_voilier2/Drivers/Src/TIMER.c

104 lines
1.7 KiB
C

#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 (*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;
}
else if (Timer->Timer==TIM2)
{
RCC->APB1ENR |= 0x01;
}
else if (Timer->Timer==TIM3)
{
RCC->APB1ENR |= 0x01<<1;
}
else if (Timer->Timer==TIM4)
{
RCC->APB1ENR |= 0x01<<2;
}
Timer->Timer->ARR=Timer->ARR;
Timer->Timer->PSC=Timer->PSC;
}
void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void))
{
int num_tim;
Timer->DIER |= 0x01;
if(Timer==TIM1)
{
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<<num_tim;
NVIC->IP[num_tim] |= Prio << 4;
}
void init_encoder_timer(TIM_TypeDef * Timer) //voir page 391
{
Timer->SMCR = 0x1;
}
void TIM2_IRQHandler (void)
{
if(tim_ptr2_func!=0)
{
(*tim_ptr2_func)();
}
TIM2->SR &= ~(1<<0) ;
}
void TIM3_IRQHandler (void)
{
if(tim_ptr3_func!=0)
{
(*tim_ptr3_func)();
}
TIM3->SR &= ~(1<<0) ;
}
void TIM4_IRQHandler (void)
{
if(tim_ptr4_func!=0)
{
(*tim_ptr4_func)();
}
TIM4->SR &= ~(1<<0) ;
}
void TIM1_UP_IRQHandler (void)
{
if(tim_ptr1_func!=0)
{
(*tim_ptr1_func)();
}
TIM1->SR &= ~(1<<0) ;
}