Browse Source

add basic PWM output driver functions

Arnaud Vergnet 3 years ago
parent
commit
b73da08273
3 changed files with 62 additions and 26 deletions
  1. 34
    18
      MyDrivers/Timer.c
  2. 23
    7
      MyDrivers/Timer.h
  3. 5
    1
      Services/Chrono.c

+ 34
- 18
MyDrivers/Timer.c View File

@@ -51,7 +51,7 @@ void TIM4_IRQHandler(void)
51 51
 	* @param  TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
52 52
   * @retval None
53 53
   */
54
-void Timer_IT_Enable(TIM_TypeDef * timer)
54
+void Timer_IT_enable(TIM_TypeDef * timer)
55 55
 {
56 56
 	LL_TIM_EnableIT_UPDATE(timer);
57 57
 }
@@ -63,7 +63,7 @@ void Timer_IT_Enable(TIM_TypeDef * timer)
63 63
 	* @param  TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
64 64
   * @retval None
65 65
   */
66
-void Timer_IT_Disable(TIM_TypeDef * timer)
66
+void Timer_IT_disable(TIM_TypeDef * timer)
67 67
 {
68 68
 	LL_TIM_DisableIT_UPDATE(timer); 
69 69
 }
@@ -76,7 +76,7 @@ void Timer_IT_Disable(TIM_TypeDef * timer)
76 76
 	*         int Prio : priorité associée à l'interruption
77 77
   * @retval None
78 78
   */
79
-void Timer_IT_Conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
79
+void Timer_IT_conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
80 80
 {
81 81
 	// affectation de la fonction
82 82
 	if (timer == TIM1) it_callback_TIM1 = it_callback;
@@ -114,7 +114,6 @@ void Timer_IT_Conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
114 114
   */
115 115
 void Timer_start(TIM_TypeDef * timer)
116 116
 {
117
-	Timer_IT_Enable(timer);
118 117
 	LL_TIM_EnableCounter(timer);
119 118
 }
120 119
 
@@ -126,7 +125,6 @@ void Timer_start(TIM_TypeDef * timer)
126 125
   */
127 126
 void Timer_stop(TIM_TypeDef * timer)
128 127
 {
129
-	Timer_IT_Disable(timer);
130 128
 	LL_TIM_DisableCounter(timer);
131 129
 }
132 130
 
@@ -138,9 +136,9 @@ void Timer_stop(TIM_TypeDef * timer)
138 136
 	*					int Psc   : valeur à placer dans PSC
139 137
   * @retval None
140 138
   */
141
-void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void))
139
+void Timer_conf(TIM_TypeDef * timer, int arr, int psc)
142 140
 {
143
-	LL_TIM_InitTypeDef My_LL_Tim_Init_Struct;
141
+	LL_TIM_InitTypeDef init_struct;
144 142
 	
145 143
 	// Validation horloge locale
146 144
 	if (timer == TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
@@ -149,16 +147,17 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void
149 147
 	else  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
150 148
 	
151 149
 	// chargement structure Arr, Psc, Up Count
152
-	My_LL_Tim_Init_Struct.Autoreload = arr;
153
-	My_LL_Tim_Init_Struct.Prescaler = psc;
154
-	My_LL_Tim_Init_Struct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
155
-	My_LL_Tim_Init_Struct.CounterMode = LL_TIM_COUNTERMODE_UP;
156
-	My_LL_Tim_Init_Struct.RepetitionCounter = 0;
150
+	init_struct.Autoreload = arr;
151
+	init_struct.Prescaler = psc;
152
+	init_struct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
153
+	init_struct.CounterMode = LL_TIM_COUNTERMODE_UP;
154
+	init_struct.RepetitionCounter = 0;
157 155
 	
158
-	LL_TIM_Init(timer,&My_LL_Tim_Init_Struct);
159
-	
160
-	Timer_IT_Conf(timer, it_callback, 3);
156
+	LL_TIM_Init(timer,&init_struct);
161 157
 
158
+	// Blocage interruptions
159
+	Timer_IT_disable(timer);
160
+	
162 161
 	// Blocage Timer
163 162
 	Timer_stop(timer);
164 163
 }
@@ -172,14 +171,31 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void
172 171
   *																	PWM OUTPUT
173 172
 	***************************************************************************/
174 173
 
175
-void PWMo_conf(TIM_TypeDef * timer, int channel, int arr, int psc)
174
+int getArrFromFreq(float freq_khz)
176 175
 {
177
-	
176
+	return (72000 / freq_khz) - 1;
178 177
 }
179 178
 
180
-void PWMo_setDutyCycle(TIM_TypeDef * timer, int dutyCycle)
179
+void PWMo_conf(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle)
181 180
 {
181
+	Timer_conf(timer, getArrFromFreq(freq_khz), 0);
182
+	LL_TIM_OC_InitTypeDef init_struct;
183
+	LL_TIM_OC_StructInit(&init_struct);
184
+	
185
+	init_struct.OCMode = LL_TIM_OCMODE_PWM1;
186
+	init_struct.OCState = LL_TIM_OCSTATE_ENABLE;
187
+	init_struct.CompareValue= dutyCycle * getArrFromFreq(freq_khz);
182 188
 	
189
+	LL_TIM_OC_Init(timer, channel, &init_struct);
190
+}
191
+
192
+void PWMo_setDutyCycle(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle)
193
+{
194
+	int compare = dutyCycle * getArrFromFreq(freq_khz);
195
+	if (channel == LL_TIM_CHANNEL_CH1) LL_TIM_OC_SetCompareCH1(timer, compare);
196
+	else if (channel == LL_TIM_CHANNEL_CH2) LL_TIM_OC_SetCompareCH2(timer, compare);
197
+	else if (channel == LL_TIM_CHANNEL_CH3) LL_TIM_OC_SetCompareCH3(timer, compare);
198
+	else  LL_TIM_OC_SetCompareCH4(timer, compare);
183 199
 }
184 200
 
185 201
 /****************************************************************************

+ 23
- 7
MyDrivers/Timer.h View File

@@ -3,6 +3,16 @@
3 3
 
4 4
 #include "stm32f103xb.h" 
5 5
 
6
+/****************************************************************************
7
+  *																	INTERRUPTIONS
8
+	***************************************************************************/
9
+
10
+void Timer_IT_enable(TIM_TypeDef * timer);
11
+
12
+void Timer_IT_disable(TIM_TypeDef * timer);
13
+
14
+void Timer_IT_conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio);
15
+
6 16
 
7 17
 /****************************************************************************
8 18
   *																	TIMER
@@ -17,7 +27,7 @@
17 27
 	*					int Psc   : valeur à placer dans PSC
18 28
   * @retval None
19 29
   */
20
-void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void));
30
+void Timer_conf(TIM_TypeDef * timer, int arr, int psc);
21 31
 
22 32
 
23 33
 /**
@@ -42,19 +52,25 @@ void Timer_stop(TIM_TypeDef * timer);
42 52
   *																PWM INPUT
43 53
 	***************************************************************************/
44 54
 
45
-void PWMi_conf(TIM_TypeDef * timer, int channel, int arr, int psc);
46
-
47
-int PWMi_getDutyCycle(TIM_TypeDef * timer);
48
-
55
+void PWMi_conf(TIM_TypeDef * timer, int channel);
49 56
 
57
+int PWMi_getDutyCycle(TIM_TypeDef * timer, int channel);
50 58
 
51 59
 /****************************************************************************
52 60
   *																PWM OUTPUT
53 61
 	***************************************************************************/
54 62
 
55
-void PWMo_conf(TIM_TypeDef * timer, int channel, int arr, int psc);
63
+void PWMo_conf(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle);
56 64
 
57
-void PWMo_setDutyCycle(TIM_TypeDef * timer, int dutyCycle);
65
+/**
66
+	* @brief  Arrêt le timer considéré
67
+  * @note   
68
+	* @param  TIM_TypeDef Timer : indique le timer à utiliser : TIM1, TIM2, TIM3 ou TIM4
69
+	*					int channel 		: Le channel utilisé par la PWM
70
+	*					float dutyCycle : Valeur entre 0 et 1
71
+  * @retval None
72
+  */
73
+void PWMo_setDutyCycle(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle);
58 74
 
59 75
 /****************************************************************************
60 76
   *																ENCODER

+ 5
- 1
Services/Chrono.c View File

@@ -37,7 +37,11 @@ void Chrono_Conf(TIM_TypeDef * Timer)
37 37
 	Chrono_Timer = Timer;
38 38
 
39 39
 	// Réglage Timer pour un débordement à 10ms
40
-	Timer_conf(Chrono_Timer, 999, 719, Chrono_Task_10ms);
40
+	Timer_conf(Chrono_Timer, 999, 719);
41
+	
42
+	// Réglage des interruptions
43
+	Timer_IT_conf(Chrono_Timer, Chrono_Task_10ms, 3);
44
+	Timer_IT_enable(Chrono_Timer);
41 45
 }
42 46
 
43 47
 

Loading…
Cancel
Save