#include "TIMER.h" 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 |= RCC_APB2ENR_TIM1EN; } else if (Timer->Timer==TIM2) { RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; } else if (Timer->Timer==TIM3) { RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; } else if (Timer->Timer==TIM4) { RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; } Timer->Timer->ARR=Timer->ARR; // On set la donnée d'autoreload Timer->Timer->PSC=Timer->PSC; // On set la donnée de prescaler (ce qui va nous permettre de diviser la valeur d'autoreaload) } 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; Timer->DIER |= 0x01; // On autorise les interruptions sur timer 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<IP[num_tim] |= Prio << 4; // On précise la priorité qu'on souhaite lui donner } void init_encoder_timer(void (*IT_function)(void)) //voir page 391 { MyGPIO_Struct_TypeDef GPIO_PB6; MyGPIO_Struct_TypeDef GPIO_PB7; MyTimer_Struct_TypeDef Timer; GPIO_PB6.GPIO = GPIOB; GPIO_PB6.GPIO_Conf = In_Floating; GPIO_PB6.GPIO_Pin = 6; GPIO_PB7.GPIO = GPIOB; GPIO_PB7.GPIO_Conf = In_Floating; GPIO_PB7.GPIO_Pin = 7; Timer.Timer=TIM4; Timer.ARR = 719; Timer.PSC = 0; MyTimer_Base_Init(&Timer); MyGPIO_Init (&GPIO_PB6); MyGPIO_Init (&GPIO_PB7); TIM4-> SMCR &= ~0x0007; TIM4-> SMCR |= TIM_SMCR_SMS_1; TIM4-> CCMR1 &= ~0xF2F2; // Mise à 0 des CC1S, CC2S, IC1F et IC2F TIM4-> CCMR1 |= TIM_CCMR1_CC1S_0; TIM4-> CCMR1 |= TIM_CCMR1_CC2S_0; TIM4-> CCER &= TIM_CCER_CC1P; TIM4-> CCER &= TIM_CCER_CC2P; TIM4-> CR1 |= TIM_CR1_CEN; } void Reset_degree (void) { TIM4->CNT = 0x0000; } int Read_CNT (void) { return TIM4->CNT; } void TIM2_IRQHandler (void) { if(tim_ptr2_func!=0) { (*tim_ptr2_func)(); } TIM2->SR &= ~(1<<0) ; // Remet à 0 le flag de l'interruption } 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); }