From 88050db15f7c6aec8dea10b46a81c290606c6deb Mon Sep 17 00:00:00 2001
From: Neluji <38362829+Neluji@users.noreply.github.com>
Date: Sun, 22 Nov 2020 16:48:42 +0100
Subject: [PATCH] PWM input -> Duty_Cycle_Permilles
---
MDK-ARM/Project.uvoptx | 13 ++-----
MyDrivers/MyPWM.c | 8 ++++
MyDrivers/MyPWM.h | 7 ++++
Src/main.c | 87 ++++++++++++++++++++++++------------------
4 files changed, 68 insertions(+), 47 deletions(-)
diff --git a/MDK-ARM/Project.uvoptx b/MDK-ARM/Project.uvoptx
index fc20bbe..f817973 100644
--- a/MDK-ARM/Project.uvoptx
+++ b/MDK-ARM/Project.uvoptx
@@ -411,18 +411,13 @@
0
- ((porta & 0x00000080) >> 7 & 0x80) >> 7
- 00800000000000000000000000000000E0FFEF400100000000000000000000000000000028706F7274612026203078303030303030383029203E3E2037000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000CDCCCCCCCCCCD43F1800000000000000000000000000000000000000540E0008
+ ((portb & 0x00000040) >> 6 & 0x40) >> 6
+ 00008000000000000000000000000000E0FFEF400000000000000000000000000000000028706F7274622026203078303030303030343029203E3E2036000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000001000000000000000000E03F0D00000000000000000000000000000000000000440E0008
1
- (porta & 0x00000001)
- 00000000000000000000000000000000E0FFEF400100000000000000000000000000000028706F72746120262030783030303030303031290000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000002000000000000000000D43F1800000000000000000000000000000000000000E20E0008
-
-
- 2
- ((portb & 0x00000040) >> 6 & 0x40) >> 6
- 00008000000000000000000000000000E0FFEF400000000000000000000000000000000028706F7274622026203078303030303030343029203E3E2036000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000003000000333333333333D73F1800000000000000000000000000000000000000440E0008
+ ((portb & 0x00000080) >> 7 & 0x80) >> 7
+ 00000000000000000000000000000000E0FFEF400000000000000000000000000000000028706F7274622026203078303030303030383029203E3E2037000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000002000000000000000000E03F0D00000000000000000000000000000000000000040E0008
diff --git a/MyDrivers/MyPWM.c b/MyDrivers/MyPWM.c
index 1b7de35..a59d46a 100644
--- a/MyDrivers/MyPWM.c
+++ b/MyDrivers/MyPWM.c
@@ -55,3 +55,11 @@ void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, uint32_t CompareValue, int
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;
+ }
+}
diff --git a/MyDrivers/MyPWM.h b/MyDrivers/MyPWM.h
index 6702b92..b3237c2 100644
--- a/MyDrivers/MyPWM.h
+++ b/MyDrivers/MyPWM.h
@@ -1,3 +1,6 @@
+#ifndef MYPWM_H
+#define MYPWM_H
+
#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges
#include "stm32f1xx_ll_tim.h"
@@ -7,3 +10,7 @@ void MyPWM_Conf_Output(TIM_TypeDef * Timer, int channel);
void MyPWM_Conf_Input(TIM_TypeDef * Timer, int channel1, int channel2);
void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, uint32_t CompareValue, int channel);
+
+int MyPWM_Duty_Cycle_Permilles(TIM_TypeDef * Timer, int channel1, int channel2);
+
+#endif
diff --git a/Src/main.c b/Src/main.c
index a621f57..16ce13c 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -39,57 +39,68 @@ int main(void)
/* Configure the system clock to 72 MHz */
SystemClock_Config();
- /*===Test Output===*/
-
- TIM_TypeDef *Timer1 = TIM3;
- int Channel1 = LL_TIM_CHANNEL_CH2;
- TIM_TypeDef *Timer2 = TIM4;
- int Channel2 = LL_TIM_CHANNEL_CH4;
-
- MyTimer_Conf(Timer1,0xFFFE, 0x72);
- MyTimer_Conf(Timer2,0xFFFE, 0x72);
-
- MyPWM_Conf_Output(Timer1, Channel1);
- MyPWM_Conf_Output(Timer2, Channel2);
-
- MyPWM_Set_Impulse_Duration(Timer1, 25, Channel1);
- MyPWM_Set_Impulse_Duration(Timer2, 75, Channel2);
-
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
- LL_GPIO_InitTypeDef My_GPIO_Init_Struct;
- My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_7;
- My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_ALTERNATE;
- My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_2MHz;
- My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(GPIOA, &My_GPIO_Init_Struct);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
- My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_9;
- My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_ALTERNATE;
- My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_2MHz;
- My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(GPIOB, &My_GPIO_Init_Struct);
+ LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
+ LL_GPIO_InitTypeDef My_GPIO_Init_Struct;
- My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_0;
- My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_FLOATING;
- My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(GPIOA, &My_GPIO_Init_Struct);
+ /*===Test Output===*/
+
+// TIM_TypeDef *Timer1 = TIM3;
+// int Channel1 = LL_TIM_CHANNEL_CH2;
+// TIM_TypeDef *Timer2 = TIM4;
+// int Channel2 = LL_TIM_CHANNEL_CH4;
+//
+// MyTimer_Conf(Timer1,0xFFFE, 0x72);
+// MyTimer_Conf(Timer2,0xFFFE, 0x72);
+//
+// MyPWM_Conf_Output(Timer1, Channel1);
+// MyPWM_Conf_Output(Timer2, Channel2);
+//
+// MyPWM_Set_Impulse_Duration(Timer1, 25, Channel1);
+// MyPWM_Set_Impulse_Duration(Timer2, 75, Channel2);
+//
+// My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_7;
+// My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_ALTERNATE;
+// My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
+// My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_2MHz;
+// My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
+// LL_GPIO_Init(GPIOA, &My_GPIO_Init_Struct);
+//
+// My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_9;
+// My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_ALTERNATE;
+// My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
+// My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_2MHz;
+// My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
+// LL_GPIO_Init(GPIOB, &My_GPIO_Init_Struct);
+//
+// MyTimer_Start(Timer1);
+// MyTimer_Start(Timer2);
+
+ /*===Test Input===*/
+
+ TIM_TypeDef *Timer = TIM4;
+ int channel1 = LL_TIM_CHANNEL_CH1;
+ int channel2 = LL_TIM_CHANNEL_CH2;
My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_6;
My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_FLOATING;
My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
LL_GPIO_Init(GPIOB, &My_GPIO_Init_Struct);
+
+ My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_7;
+ LL_GPIO_Init(GPIOB, &My_GPIO_Init_Struct);
- MyTimer_Start(Timer1);
- MyTimer_Start(Timer2);
-
-
+ MyTimer_Conf(Timer,0xFFAD,0x15);
+
+ MyPWM_Conf_Input(Timer, channel1, channel2);
+
+ int duty_cycle = 0;
/* Infinite loop */
while (1)
{
+ duty_cycle = MyPWM_Duty_Cycle_Permilles(Timer, channel1, channel2);
}
}