Added MyTimer_PWM_Init generic function

This commit is contained in:
Yohan Boujon 2023-03-21 23:25:14 +01:00
parent f47e05482f
commit d3c453a14a
4 changed files with 43 additions and 13 deletions

View file

@ -29,9 +29,29 @@ void MyTimer_ActiveIT(TIM_TypeDef * TimerX, uint8_t Prio)
NVIC->ISER[0] |= (0x1<<positionTimerIT);
}
void MyTimer_PWM(TIM_TypeDef * TimerX, uint8_t channel)
void MyTimer_PWM_Init(MyPWM_Struct_TypeDef * PWM)
{
//TimerX->CCER
switch(PWM->channel)
{
case 2:
PWM->Timer->CCMR1 |= (PWMMode_1<<OC24M_START); //We activate the PWM Mode 1 for the given channel
PWM->Timer->CCR2 = PWM->CCR; //Compare Capture register count
break;
case 3:
PWM->Timer->CCMR2 |= (PWMMode_1<<OC13M_START);
PWM->Timer->CCR3 = PWM->CCR;
break;
case 4:
PWM->Timer->CCMR2 |= (PWMMode_1<<OC24M_START);
PWM->Timer->CCR4 = PWM->CCR;
break;
default:
PWM->Timer->CCMR1 |= (PWMMode_1<<OC13M_START);
PWM->Timer->CCR1 = PWM->CCR;
break;
}
PWM->Timer->BDTR |= (1<<MOE); //Main output enable
PWM->Timer->CCER |= (1<<4*(PWM->channel-1)); //enable capture/compare registers
}
int TimerX2Int(TIM_TypeDef * TimerX)

View file

@ -5,6 +5,10 @@
#define CEN 0x0
#define UIE 0x0
#define UIF 0x0
#define MOE 0xF
#define OC13M_START 0x4
#define OC24M_START 0xC
#define PWMMode_1 0x6
typedef struct {
TIM_TypeDef * Timer; //TIM1 -> TIM4
@ -12,12 +16,18 @@ typedef struct {
unsigned short PSC;
} MyTimer_Struct_TypeDef;
typedef struct {
TIM_TypeDef * Timer; //TIM1 -> TIM4
uint8_t channel;
uint16_t CCR;
} MyPWM_Struct_TypeDef;
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer);
int TimerX2Int(TIM_TypeDef * TimerX);
uint8_t TimerIT2UInt(TIM_TypeDef * TimerX);
void MyTimer_ActiveIT(TIM_TypeDef * TimerX, uint8_t Prio);
void Init_Periph (void (* ptrFonction)(void));
void MyTimer_PWM(TIM_TypeDef * TimerX, uint8_t channel);
void MyTimer_PWM_Init(MyPWM_Struct_TypeDef * PWM);
#define MyTimer_Base_Start(Timer) (Timer->CR1 |= (0x01<<CEN))
#define MyTimer_Base_Stop(Timer) (Timer->CR1 &= ~(0x01<<CEN))

View file

@ -2,8 +2,6 @@
#include "../../Drivers/gpiodriver.h"
#include "../../Drivers/timerdriver.h"
#define MOE 0x15
void ToggleLed(void)
{
MyGPIO_Toggle(GPIOA,5);
@ -26,10 +24,12 @@ int main (void)
MyTimer_ActiveIT(TIM2,2);
//PWM enable
TIM2->CCMR1 |= (0x6<<12);
TIM2->BDTR |= (1<<MOE);
TIM2->CCR2 = 10;
TIM2->CCER |= (1<<4); //0 pour CC1E, 4 pour CC2P
MyPWM_Struct_TypeDef timer2channel2 = {TIM2,1,400};
MyTimer_PWM_Init(&timer2channel2);
//TIM2->CCMR1 |= (0x6<<12);
//TIM2->BDTR |= (1<<MOE);
//TIM2->CCR2 = 10;
//TIM2->CCER |= (1<<4); //0 pour CC1E, 4 pour CC2E // 4*(channel-1)
do{
}while(1) ;

View file

@ -434,7 +434,7 @@
<GroupNumber>1</GroupNumber>
<FileNumber>3</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Drivers\timerdriver.c</PathWithFileName>