Encoder Timer switches between 0 and highest value.

OP #7669325
Rate this post
useful
not useful

Hello guys,

i want to work with an optical encoder. I only want to use one channel, which i already connected to a timer and configured for the encoder. Now i have a weird problem. When i spin the encoder it only switches between 0 and the highest value. Do any of you have an idea where the problem could be. You probably need more information, im happy to give them to you.

I work with a STM32H745XIHx.

Thank you

Moderator (Company: Titel) #7669348
Rate this post
useful
not useful

Harald K. schrieb:

or use embdev.net

I switched the thread over there.

Max schrieb:

use one channel, which i already connected to a timer and configured for the encoder.

In which way did you do this configuration? What you need is a simple counter that counts events at the pin, but no encoder.

You probably need more information

Indeed: show your schematic (as simple it may be) and your code.

OP #7669360
Rate this post
useful
not useful

Thank you.

First of all, im doing a project for university, thats why i only have a microcontroller in a "starter kit". So i cant change the connections. For the schematics: The Encoder Cannel A is connected to STmod 3 (PD6) and CN6 4 (PA6). Encoder COM2 is connected to STMod 8 (PI3) and CN7 2 (PF8). The only pin which can function as timer for the encoder is PA6 on TIM3_CH1. Therefore i only can use the one channel encoder. So i put the Timer 3 in combined channels encoder mode and the encode mode in TI1.

And here is my code:

1
void startEncoder(uint8_t u8iEncoder) {
2
  if(u8iEncoder == 1) {
3
    HAL_TIM_Encoder_Start(&htim3,TIM_CHANNEL_1);
4
  }
5

6
}
7

8
uint32_t getEncoder(uint8_t u8iEncoder) {
9
  uint32_t ui32Counter = 0;
10
  if(u8iEncoder == 1) {
11
    ui32Counter = __HAL_TIM_GET_COUNTER(&htim3);
12
  }
13
  return ui32Counter;
14
}
15
/* USER CODE END 0 */
16

17
TIM_HandleTypeDef htim2;
18
TIM_HandleTypeDef htim3;
19
TIM_HandleTypeDef htim5;
20
TIM_HandleTypeDef htim6;
21
TIM_HandleTypeDef htim7;
22

23
/* TIM2 init function */
24
void MX_TIM2_Init(void)
25
{
26

27
  /* USER CODE BEGIN TIM2_Init 0 */
28

29
  /* USER CODE END TIM2_Init 0 */
30

31
  TIM_MasterConfigTypeDef sMasterConfig = {0};
32
  TIM_OC_InitTypeDef sConfigOC = {0};
33

34
  /* USER CODE BEGIN TIM2_Init 1 */
35

36
  /* USER CODE END TIM2_Init 1 */
37
  htim2.Instance = TIM2;
38
  htim2.Init.Prescaler = 200;
39
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
40
  htim2.Init.Period = 999;
41
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
42
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
43
  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
44
  {
45
    Error_Handler();
46
  }
47
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
48
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
49
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
50
  {
51
    Error_Handler();
52
  }
53
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
54
  sConfigOC.Pulse = 0;
55
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
56
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
57
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
58
  {
59
    Error_Handler();
60
  }
61
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
62
  {
63
    Error_Handler();
64
  }
65
  /* USER CODE BEGIN TIM2_Init 2 */
66

67
  /* USER CODE END TIM2_Init 2 */
68
  HAL_TIM_MspPostInit(&htim2);
69

70
}
71
/* TIM3 init function */
72
void MX_TIM3_Init(void)
73
{
74

75
  /* USER CODE BEGIN TIM3_Init 0 */
76

77
  /* USER CODE END TIM3_Init 0 */
78

79
  TIM_Encoder_InitTypeDef sConfig = {0};
80
  TIM_MasterConfigTypeDef sMasterConfig = {0};
81

82
  /* USER CODE BEGIN TIM3_Init 1 */
83

84
  /* USER CODE END TIM3_Init 1 */
85
  htim3.Instance = TIM3;
86
  htim3.Init.Prescaler = 0;
87
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
88
  htim3.Init.Period = 65535;
89
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
90
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
91
  sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
92
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
93
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
94
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
95
  sConfig.IC1Filter = 0;
96
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
97
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
98
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
99
  sConfig.IC2Filter = 0;
100
  if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK)
101
  {
102
    Error_Handler();
103
  }
104
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
105
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
106
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
107
  {
108
    Error_Handler();
109
  }
110
  /* USER CODE BEGIN TIM3_Init 2 */
111

112
  /* USER CODE END TIM3_Init 2 */
113

114
}
115
/* TIM5 init function */
116
void MX_TIM5_Init(void)
117
{
118

119
  /* USER CODE BEGIN TIM5_Init 0 */
120

121
  /* USER CODE END TIM5_Init 0 */
122

123
  TIM_MasterConfigTypeDef sMasterConfig = {0};
124
  TIM_OC_InitTypeDef sConfigOC = {0};
125

126
  /* USER CODE BEGIN TIM5_Init 1 */
127

128
  /* USER CODE END TIM5_Init 1 */
129
  htim5.Instance = TIM5;
130
  htim5.Init.Prescaler = 200;
131
  htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
132
  htim5.Init.Period = 999;
133
  htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
134
  htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
135
  if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
136
  {
137
    Error_Handler();
138
  }
139
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
140
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
141
  if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
142
  {
143
    Error_Handler();
144
  }
145
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
146
  sConfigOC.Pulse = 0;
147
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
148
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
149
  if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
150
  {
151
    Error_Handler();
152
  }
153
  if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
154
  {
155
    Error_Handler();
156
  }
157
  /* USER CODE BEGIN TIM5_Init 2 */
158

159
  /* USER CODE END TIM5_Init 2 */
160
  HAL_TIM_MspPostInit(&htim5);
161

162
}
163
/* TIM6 init function */
164
void MX_TIM6_Init(void)
165
{
166

167
  /* USER CODE BEGIN TIM6_Init 0 */
168

169
  /* USER CODE END TIM6_Init 0 */
170

171
  TIM_MasterConfigTypeDef sMasterConfig = {0};
172

173
  /* USER CODE BEGIN TIM6_Init 1 */
174

175
  /* USER CODE END TIM6_Init 1 */
176
  htim6.Instance = TIM6;
177
  htim6.Init.Prescaler = 200;
178
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
179
  htim6.Init.Period = 999;
180
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
181
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
182
  {
183
    Error_Handler();
184
  }
185
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
186
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
187
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
188
  {
189
    Error_Handler();
190
  }
191
  /* USER CODE BEGIN TIM6_Init 2 */
192

193
  /* USER CODE END TIM6_Init 2 */
194

195
}
196
/* TIM7 init function */
197
void MX_TIM7_Init(void)
198
{
199

200
  /* USER CODE BEGIN TIM7_Init 0 */
201

202
  /* USER CODE END TIM7_Init 0 */
203

204
  TIM_MasterConfigTypeDef sMasterConfig = {0};
205

206
  /* USER CODE BEGIN TIM7_Init 1 */
207

208
  /* USER CODE END TIM7_Init 1 */
209
  htim7.Instance = TIM7;
210
  htim7.Init.Prescaler = 200;
211
  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
212
  htim7.Init.Period = 999;
213
  htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
214
  if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
215
  {
216
    Error_Handler();
217
  }
218
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
219
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
220
  if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
221
  {
222
    Error_Handler();
223
  }
224
  /* USER CODE BEGIN TIM7_Init 2 */
225

226
  /* USER CODE END TIM7_Init 2 */
227

228
}
229

230
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
231
{
232

233
  if(tim_pwmHandle->Instance==TIM2)
234
  {
235
  /* USER CODE BEGIN TIM2_MspInit 0 */
236

237
  /* USER CODE END TIM2_MspInit 0 */
238
    /* TIM2 clock enable */
239
    __HAL_RCC_TIM2_CLK_ENABLE();
240
  /* USER CODE BEGIN TIM2_MspInit 1 */
241

242
  /* USER CODE END TIM2_MspInit 1 */
243
  }
244
  else if(tim_pwmHandle->Instance==TIM5)
245
  {
246
  /* USER CODE BEGIN TIM5_MspInit 0 */
247

248
  /* USER CODE END TIM5_MspInit 0 */
249
    /* TIM5 clock enable */
250
    __HAL_RCC_TIM5_CLK_ENABLE();
251
  /* USER CODE BEGIN TIM5_MspInit 1 */
252

253
  /* USER CODE END TIM5_MspInit 1 */
254
  }
255
}
256

257
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* tim_encoderHandle)
258
{
259

260
  GPIO_InitTypeDef GPIO_InitStruct = {0};
261
  if(tim_encoderHandle->Instance==TIM3)
262
  {
263
  /* USER CODE BEGIN TIM3_MspInit 0 */
264

265
  /* USER CODE END TIM3_MspInit 0 */
266
    /* TIM3 clock enable */
267
    __HAL_RCC_TIM3_CLK_ENABLE();
268

269
    __HAL_RCC_GPIOB_CLK_ENABLE();
270
    __HAL_RCC_GPIOA_CLK_ENABLE();
271
    /**TIM3 GPIO Configuration
272
    PB5     ------> TIM3_CH2
273
    PA6     ------> TIM3_CH1
274
    */
275
    GPIO_InitStruct.Pin = GPIO_PIN_5;
276
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
277
    GPIO_InitStruct.Pull = GPIO_NOPULL;
278
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
279
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
280
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
281

282
    GPIO_InitStruct.Pin = GPIO_PIN_6;
283
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
284
    GPIO_InitStruct.Pull = GPIO_NOPULL;
285
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
286
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
287
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
288

289
  /* USER CODE BEGIN TIM3_MspInit 1 */
290

291
  /* USER CODE END TIM3_MspInit 1 */
292
  }
293
}
294

295
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
296
{
297

298
  if(tim_baseHandle->Instance==TIM6)
299
  {
300
  /* USER CODE BEGIN TIM6_MspInit 0 */
301

302
  /* USER CODE END TIM6_MspInit 0 */
303
    /* TIM6 clock enable */
304
    __HAL_RCC_TIM6_CLK_ENABLE();
305
  /* USER CODE BEGIN TIM6_MspInit 1 */
306

307
  /* USER CODE END TIM6_MspInit 1 */
308
  }
309
  else if(tim_baseHandle->Instance==TIM7)
310
  {
311
  /* USER CODE BEGIN TIM7_MspInit 0 */
312

313
  /* USER CODE END TIM7_MspInit 0 */
314
    /* TIM7 clock enable */
315
    __HAL_RCC_TIM7_CLK_ENABLE();
316

317
    /* TIM7 interrupt Init */
318
    HAL_NVIC_SetPriority(TIM7_IRQn, 14, 0);
319
    HAL_NVIC_EnableIRQ(TIM7_IRQn);
320
  /* USER CODE BEGIN TIM7_MspInit 1 */
321

322
  /* USER CODE END TIM7_MspInit 1 */
323
  }
324
}
325
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
326
{
327

328
  GPIO_InitTypeDef GPIO_InitStruct = {0};
329
  if(timHandle->Instance==TIM2)
330
  {
331
  /* USER CODE BEGIN TIM2_MspPostInit 0 */
332

333
  /* USER CODE END TIM2_MspPostInit 0 */
334
    __HAL_RCC_GPIOA_CLK_ENABLE();
335
    /**TIM2 GPIO Configuration
336
    PA15 (JTDI)     ------> TIM2_CH1
337
    PA3     ------> TIM2_CH4
338
    */
339
    GPIO_InitStruct.Pin = GPIO_PIN_15|GPIO_PIN_3;
340
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
341
    GPIO_InitStruct.Pull = GPIO_NOPULL;
342
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
343
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
344
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
345

346
  /* USER CODE BEGIN TIM2_MspPostInit 1 */
347

348
  /* USER CODE END TIM2_MspPostInit 1 */
349
  }
350
  else if(timHandle->Instance==TIM5)
351
  {
352
  /* USER CODE BEGIN TIM5_MspPostInit 0 */
353

354
  /* USER CODE END TIM5_MspPostInit 0 */
355

356
    __HAL_RCC_GPIOH_CLK_ENABLE();
357
    /**TIM5 GPIO Configuration
358
    PH10     ------> TIM5_CH1
359
    PH12     ------> TIM5_CH3
360
    */
361
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12;
362
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
363
    GPIO_InitStruct.Pull = GPIO_NOPULL;
364
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
365
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
366
    HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
367

368
  /* USER CODE BEGIN TIM5_MspPostInit 1 */
369

370
  /* USER CODE END TIM5_MspPostInit 1 */
371
  }
372

373
}
374

375
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
376
{
377

378
  if(tim_pwmHandle->Instance==TIM2)
379
  {
380
  /* USER CODE BEGIN TIM2_MspDeInit 0 */
381

382
  /* USER CODE END TIM2_MspDeInit 0 */
383
    /* Peripheral clock disable */
384
    __HAL_RCC_TIM2_CLK_DISABLE();
385
  /* USER CODE BEGIN TIM2_MspDeInit 1 */
386

387
  /* USER CODE END TIM2_MspDeInit 1 */
388
  }
389
  else if(tim_pwmHandle->Instance==TIM5)
390
  {
391
  /* USER CODE BEGIN TIM5_MspDeInit 0 */
392

393
  /* USER CODE END TIM5_MspDeInit 0 */
394
    /* Peripheral clock disable */
395
    __HAL_RCC_TIM5_CLK_DISABLE();
396
  /* USER CODE BEGIN TIM5_MspDeInit 1 */
397

398
  /* USER CODE END TIM5_MspDeInit 1 */
399
  }
400
}
401

402
void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* tim_encoderHandle)
403
{
404

405
  if(tim_encoderHandle->Instance==TIM3)
406
  {
407
  /* USER CODE BEGIN TIM3_MspDeInit 0 */
408

409
  /* USER CODE END TIM3_MspDeInit 0 */
410
    /* Peripheral clock disable */
411
    __HAL_RCC_TIM3_CLK_DISABLE();
412

413
    /**TIM3 GPIO Configuration
414
    PB5     ------> TIM3_CH2
415
    PA6     ------> TIM3_CH1
416
    */
417
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_5);
418

419
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);
420

421
  /* USER CODE BEGIN TIM3_MspDeInit 1 */
422

423
  /* USER CODE END TIM3_MspDeInit 1 */
424
  }
425
}
426

427
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
428
{
429

430
  if(tim_baseHandle->Instance==TIM6)
431
  {
432
  /* USER CODE BEGIN TIM6_MspDeInit 0 */
433

434
  /* USER CODE END TIM6_MspDeInit 0 */
435
    /* Peripheral clock disable */
436
    __HAL_RCC_TIM6_CLK_DISABLE();
437
  /* USER CODE BEGIN TIM6_MspDeInit 1 */
438

439
  /* USER CODE END TIM6_MspDeInit 1 */
440
  }
441
  else if(tim_baseHandle->Instance==TIM7)
442
  {
443
  /* USER CODE BEGIN TIM7_MspDeInit 0 */
444

445
  /* USER CODE END TIM7_MspDeInit 0 */
446
    /* Peripheral clock disable */
447
    __HAL_RCC_TIM7_CLK_DISABLE();
448

449
    /* TIM7 interrupt Deinit */
450
    HAL_NVIC_DisableIRQ(TIM7_IRQn);
451
  /* USER CODE BEGIN TIM7_MspDeInit 1 */
452

453
  /* USER CODE END TIM7_MspDeInit 1 */
454
  }
455
}
456

457
/* USER CODE BEGIN 1 */
458

459
/* USER CODE END 1 */
Moderator (Company: Titel) #7669467
Rate this post
useful
not useful

Max wrote:

thats why i only have a microcontroller in a "starter kit".

Which one? Is there already an encoder on it? If so: which one? If you connected an addidional encoder: wchich one and how?

The Encoder Cannel A is connected to STmod 3 (PD6) and CN6 4 (PA6). Encoder COM2 is connected to STMod 8 (PI3) and CN7 2 (PF8).

What is Channel A and what is COM2 of what encoder?

Therefore i only can use the one channel encoder.

I don't know a thing like a "one channel encoder". What does it mean?

For the schematics

No GND involved in this "schematic" anyhow?

A useful schematic is some kind of graphics with some symbols, names, values and wires on it. With such a schematic one can easily see what parts are intercconnected in what way. This kind of schematic can be understood in almost any langauge and it tells more than thousand words may do...

Pls read and obey the rules above every edit-box:

1
Reply
2
Rules — please read before posting
3
Post long source code as attachment, not in the text
(Company: IBM) #7682622
Rate this post
useful
not useful

Hello, our optical encoder is only toggling between 0 and the highest value when spun. Quick fixes:

Check connections for interference. Verify encoder and timer settings. Ensure stable power supply. Calibrate encoder settings. Inspect for mechanical issues. Apply software filtering. Review documentation for guidance. I hope this will help you. Thank you

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now