From 11a7310a434a9ab8441ed555ab2f6fcb6d69348d Mon Sep 17 00:00:00 2001 From: Neluji <38362829+Neluji@users.noreply.github.com> Date: Wed, 4 Nov 2020 12:06:23 +0100 Subject: [PATCH] PWM input --- MDK-ARM/Project.uvoptx | 11 +++++++++-- MyDrivers/MyPWM.c | 36 ++++++++++++++++++++++++------------ MyDrivers/MyPWM.h | 4 ++-- MyDrivers/MyTimer.c | 5 +---- MyDrivers/MyTimer.h | 2 +- Src/main.c | 21 +++++++++++++++++++++ 6 files changed, 58 insertions(+), 21 deletions(-) diff --git a/MDK-ARM/Project.uvoptx b/MDK-ARM/Project.uvoptx index 7a44afb..463e23f 100644 --- a/MDK-ARM/Project.uvoptx +++ b/MDK-ARM/Project.uvoptx @@ -317,7 +317,7 @@ 0 DLGDARM - (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=98,107,519,534,0)(121=-1,-1,-1,-1,0)(122=674,103,1095,530,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=-1,-1,-1,-1,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) + (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=98,107,519,534,0)(121=-1,-1,-1,-1,0)(122=674,103,1095,530,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=273,213,867,964,0)(131=-1,-1,-1,-1,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) 0 @@ -378,7 +378,7 @@ 0 0 0 - 0 + 1 0 0 0 @@ -400,6 +400,13 @@ + + + 0 + ((porta & 0x00000100) >> 8 & 0x100) >> 8 + FF000000000000000000000000000000E0FFEF400100000000000000000000000000000028706F7274612026203078303030303031303029203E3E2038000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000F03F1A00000000000000000000000000000000000000040C0008 + + 1 0 diff --git a/MyDrivers/MyPWM.c b/MyDrivers/MyPWM.c index 2765216..ae9b27d 100644 --- a/MyDrivers/MyPWM.c +++ b/MyDrivers/MyPWM.c @@ -2,36 +2,48 @@ #include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges #include "stm32f1xx_ll_tim.h" -//Seul le channel CH1 des timers sera utilisé -int channel = LL_TIM_CHANNEL_CH1; - -void MyPWM_Conf_Output(TIM_TypeDef * Timer) +void MyPWM_Conf_Output(TIM_TypeDef * Timer, int channel) { - //Activation du channel (CH1) pour le timer considéré + //Activation du channel pour le timer considéré LL_TIM_CC_EnableChannel(Timer, 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); } - -void MyPWM_Conf_Input(TIM_TypeDef * Timer) +//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) { - //Activation du channel (CH1) pour le timer considéré - LL_TIM_CC_EnableChannel(Timer, channel); + //Activation des 2 channels pour le timer considéré + LL_TIM_CC_EnableChannel(Timer, channel1); + LL_TIM_CC_EnableChannel(Timer, channel2); LL_TIM_IC_InitTypeDef My_LL_Tim_IC_Init_Struct; - LL_TIM_IC_StructInit(&My_LL_Tim_IC_Init_Struct); - //Compléter ici?!... + //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); - LL_TIM_IC_Init(Timer,channel,&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); } diff --git a/MyDrivers/MyPWM.h b/MyDrivers/MyPWM.h index da2ed82..f96c348 100644 --- a/MyDrivers/MyPWM.h +++ b/MyDrivers/MyPWM.h @@ -2,8 +2,8 @@ #include "stm32f1xx_ll_tim.h" -void MyPWM_Conf_Output(TIM_TypeDef * Timer); +void MyPWM_Conf_Output(TIM_TypeDef * Timer, int channel); -void MyPWM_Conf_Input(TIM_TypeDef * Timer); +void MyPWM_Conf_Input(TIM_TypeDef * Timer, int channel1, int channel2); void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, int Percentage); diff --git a/MyDrivers/MyTimer.c b/MyDrivers/MyTimer.c index efd3cd5..6df39fe 100644 --- a/MyDrivers/MyTimer.c +++ b/MyDrivers/MyTimer.c @@ -32,7 +32,7 @@ void (*Ptr_ItFct_TIM4)(void); * int Psc : valeur à placer dans PSC * @retval None */ -void MyTimer_Conf(TIM_TypeDef * Timer, int Period) +void MyTimer_Conf(TIM_TypeDef * Timer, int Arr, int Psc) { LL_TIM_InitTypeDef My_LL_Tim_Init_Struct; @@ -42,9 +42,6 @@ void MyTimer_Conf(TIM_TypeDef * Timer, int Period) else if (Timer==TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4); - int Psc = (Period*72*1000000)/256 + 2; - int Arr = (Period*72*1000000)/Psc + 1; - // chargement structure Arr, Psc, Up Count My_LL_Tim_Init_Struct.Autoreload=Arr; My_LL_Tim_Init_Struct.Prescaler=Psc; diff --git a/MyDrivers/MyTimer.h b/MyDrivers/MyTimer.h index 6daa6fc..4be9606 100644 --- a/MyDrivers/MyTimer.h +++ b/MyDrivers/MyTimer.h @@ -18,7 +18,7 @@ Driver pour Timer 1 * int Psc : valeur à placer dans PSC * @retval None */ -void MyTimer_Conf(TIM_TypeDef * Timer,int Period); +void MyTimer_Conf(TIM_TypeDef * Timer, int Arr, int Psc); /** diff --git a/Src/main.c b/Src/main.c index 362e729..7fd899f 100644 --- a/Src/main.c +++ b/Src/main.c @@ -19,6 +19,8 @@ #include "stm32f1xx_ll_rcc.h" // utile dans la fonction SystemClock_Config #include "stm32f1xx_ll_utils.h" // utile dans la fonction SystemClock_Config #include "stm32f1xx_ll_system.h" // utile dans la fonction SystemClock_Config +#include "stm32f1xx_ll_gpio.h" +#include "MyTimer.h" #include "MyPWM.h" void SystemClock_Config(void); @@ -36,6 +38,25 @@ int main(void) { /* Configure the system clock to 72 MHz */ SystemClock_Config(); + + MyTimer_Conf(TIM1,999, 719); + + MyPWM_Conf_Output(TIM1, LL_TIM_CHANNEL_CH1); + + MyPWM_Set_Impulse_Duration(TIM1, 25); + +// LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); +// GPIO_TypeDef * GPIO; +// LL_GPIO_InitTypeDef My_GPIO_Init_Struct; +// GPIO = GPIOA; +// My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_8; +// My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_OUTPUT; +// My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; +// My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_10MHz; +// My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN; +// LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct); + + MyTimer_Start(TIM1); /* Infinite loop */ while (1)