150 lines
2.4 KiB
C
150 lines
2.4 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 (*ptr_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 MyPWM_init ( TIM_TypeDef * Timer,char Channel)
|
|
{
|
|
if(Channel==1)
|
|
{
|
|
Timer->CCMR1&=~0x00FF;
|
|
Timer->CCMR1 &= ~TIM_CCMR1_OC1M_0;
|
|
Timer->CCMR1 |= TIM_CCMR1_OC1M_1| TIM_CCMR1_OC1M_2;
|
|
Timer->CCER |= TIM_CCER_CC1E;
|
|
|
|
}
|
|
if(Channel==2)
|
|
{
|
|
Timer->CCMR1&=~0xFF00;
|
|
Timer->CCMR1 &= ~TIM_CCMR1_OC2M_0;
|
|
Timer->CCMR1 |= TIM_CCMR1_OC2M_1| TIM_CCMR1_OC2M_2;
|
|
Timer->CCER |= TIM_CCER_CC2E;
|
|
|
|
}
|
|
if(Channel==3)
|
|
{
|
|
Timer->CCMR2&=~0x00FF;
|
|
Timer->CCMR2 &= ~TIM_CCMR2_OC3M_0;
|
|
Timer->CCMR2 |= TIM_CCMR2_OC3M_1| TIM_CCMR2_OC3M_2;
|
|
Timer->CCER |= TIM_CCER_CC3E;
|
|
|
|
}
|
|
if(Channel==4)
|
|
{
|
|
Timer->CCMR2&=~0xFF00;
|
|
Timer->CCMR2 &= ~TIM_CCMR2_OC4M_0;
|
|
Timer->CCMR2 |= TIM_CCMR2_OC4M_1| TIM_CCMR2_OC4M_2;
|
|
Timer->CCER |= TIM_CCER_CC4E;
|
|
|
|
}
|
|
}
|
|
void MyPWM_Duty (TIM_TypeDef * Timer,char Channel, unsigned short CRR )
|
|
{
|
|
|
|
|
|
if(Channel==1)
|
|
{
|
|
Timer->CCR1=CRR;
|
|
}
|
|
if(Channel==2)
|
|
{
|
|
Timer->CCR2=CRR;
|
|
}
|
|
if(Channel==3)
|
|
{
|
|
Timer->CCR3=CRR;
|
|
|
|
}
|
|
if(Channel==4)
|
|
{
|
|
Timer->CCR4=CRR;
|
|
}
|
|
|
|
}
|
|
void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void))
|
|
{
|
|
int num_tim;
|
|
ptr_func=IT_function;
|
|
Timer->DIER |= 0x01;
|
|
if(Timer==TIM1)
|
|
{
|
|
num_tim=TIM1_UP_IRQn;
|
|
}
|
|
else if(Timer==TIM2)
|
|
{
|
|
num_tim=TIM2_IRQn;
|
|
}
|
|
else if(Timer==TIM3)
|
|
{
|
|
num_tim=TIM3_IRQn;
|
|
}
|
|
else if(Timer==TIM4)
|
|
{
|
|
num_tim=TIM4_IRQn;
|
|
}
|
|
NVIC->ISER[0] |= 0x01<<num_tim;
|
|
NVIC->IP[num_tim] |= Prio << 4;
|
|
|
|
}
|
|
|
|
void TIM2_IRQHandler (void)
|
|
{
|
|
if(ptr_func!=0)
|
|
{
|
|
(*ptr_func)();
|
|
}
|
|
TIM2->SR &= ~(1<<0) ;
|
|
}
|
|
|
|
void TIM3_IRQHandler (void)
|
|
{
|
|
if(ptr_func!=0)
|
|
{
|
|
(*ptr_func)();
|
|
}
|
|
TIM3->SR &= ~(1<<0) ;
|
|
}
|
|
|
|
void TIM4_IRQHandler (void)
|
|
{
|
|
if(ptr_func!=0)
|
|
{
|
|
(*ptr_func)();
|
|
}
|
|
TIM4->SR &= ~(1<<0) ;
|
|
}
|
|
|
|
void TIM1_UP_IRQHandler (void) // à vérifier
|
|
{
|
|
if(ptr_func!=0)
|
|
{
|
|
(*ptr_func)();
|
|
}
|
|
TIM1->SR &= ~(1<<0) ;
|
|
}
|