65 lines
2.3 KiB
C
65 lines
2.3 KiB
C
#include "MyPWM.h"
|
|
#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges
|
|
#include "stm32f1xx_ll_tim.h"
|
|
|
|
void MyPWM_Conf_Output(TIM_TypeDef * Timer, int channel)
|
|
{
|
|
LL_TIM_OC_InitTypeDef My_LL_Tim_OC_Init_Struct;
|
|
|
|
//Configuration du output channel en PWM
|
|
LL_TIM_OC_StructInit(&My_LL_Tim_OC_Init_Struct);
|
|
//Configure le mode de la PWM : PWM1 = 1 jusqu'à la CompareValue puis 0, PWM2 = l'inverse
|
|
My_LL_Tim_OC_Init_Struct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
|
LL_TIM_OC_Init(Timer,channel,&My_LL_Tim_OC_Init_Struct);
|
|
|
|
//Activation du channel pour le timer considéré
|
|
LL_TIM_CC_EnableChannel(Timer, channel);
|
|
}
|
|
|
|
|
|
//Configurer obligatoirement les channels 1 et 2 sur IC1 et IC2 ou IC3 et IC4
|
|
void MyPWM_Conf_Input(TIM_TypeDef * Timer, int channel1, int channel2)
|
|
{
|
|
LL_TIM_IC_InitTypeDef My_LL_Tim_IC_Init_Struct;
|
|
|
|
//Configuration du channel1 (front montant, mappé sur TI1 = valeurs par défaut)
|
|
LL_TIM_IC_StructInit(&My_LL_Tim_IC_Init_Struct);
|
|
LL_TIM_IC_Init(Timer,channel1,&My_LL_Tim_IC_Init_Struct);
|
|
|
|
//Configuration du channel2 (front descendant, mappé sur TI1 = valeurs modifiées)
|
|
LL_TIM_IC_StructInit(&My_LL_Tim_IC_Init_Struct);
|
|
//Détection sur front descendant
|
|
My_LL_Tim_IC_Init_Struct.ICPolarity = LL_TIM_IC_POLARITY_FALLING;
|
|
//Mappage sur TI1
|
|
My_LL_Tim_IC_Init_Struct.ICActiveInput = LL_TIM_ACTIVEINPUT_INDIRECTTI;
|
|
LL_TIM_IC_Init(Timer,channel2,&My_LL_Tim_IC_Init_Struct);
|
|
|
|
//Definition du trigger
|
|
LL_TIM_SetTriggerInput(Timer, LL_TIM_TS_TI1FP1);
|
|
|
|
//Configure le mode esclave en mode RESET
|
|
LL_TIM_SetSlaveMode(Timer, LL_TIM_SLAVEMODE_RESET);
|
|
|
|
//Activation des 2 channels pour le timer considéré
|
|
LL_TIM_CC_EnableChannel(Timer, channel1);
|
|
LL_TIM_CC_EnableChannel(Timer, channel2);
|
|
}
|
|
|
|
|
|
void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, uint32_t CompareValue, int channel)
|
|
{
|
|
if (channel==LL_TIM_CHANNEL_CH1) LL_TIM_OC_SetCompareCH1(Timer, CompareValue);
|
|
else if (channel==LL_TIM_CHANNEL_CH2) LL_TIM_OC_SetCompareCH2(Timer, CompareValue);
|
|
else if (channel==LL_TIM_CHANNEL_CH3) LL_TIM_OC_SetCompareCH3(Timer, CompareValue);
|
|
else LL_TIM_OC_SetCompareCH4(Timer, CompareValue);
|
|
}
|
|
|
|
int MyPWM_Duty_Cycle_Permilles(TIM_TypeDef * Timer, int channel1, int channel2) {
|
|
if(channel1 == LL_TIM_CHANNEL_CH1 && channel2 == LL_TIM_CHANNEL_CH2) {
|
|
return LL_TIM_IC_GetCaptureCH2(Timer) / LL_TIM_IC_GetCaptureCH1(Timer) * 1000;
|
|
}
|
|
else {
|
|
return -1;
|
|
}
|
|
}
|