Hello World example for interfacing up to four rotary encoders to the STM32's timer/counter hardware, without interrupts.

Dependencies:   mbed-dev

Committer:
gregeric
Date:
Sun Oct 04 11:58:58 2015 +0000
Revision:
1:cd7b42c99ff8
Parent:
0:ee5cb967aa17
Added more targets, use HAL macros to read counter value & direction.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gregeric 0:ee5cb967aa17 1 #include "mbed.h"
gregeric 0:ee5cb967aa17 2
gregeric 1:cd7b42c99ff8 3 void EncoderInit(TIM_Encoder_InitTypeDef * encoder, TIM_HandleTypeDef * timer, TIM_TypeDef * TIMx, uint32_t maxcount, uint32_t encmode)
gregeric 0:ee5cb967aa17 4 {
gregeric 0:ee5cb967aa17 5
gregeric 1:cd7b42c99ff8 6 timer->Instance = TIMx;
gregeric 1:cd7b42c99ff8 7 timer->Init.Period = maxcount;
gregeric 1:cd7b42c99ff8 8 timer->Init.CounterMode = TIM_COUNTERMODE_UP;
gregeric 1:cd7b42c99ff8 9 timer->Init.Prescaler = 0;
gregeric 1:cd7b42c99ff8 10 timer->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
gregeric 0:ee5cb967aa17 11
gregeric 1:cd7b42c99ff8 12 encoder->EncoderMode = encmode;
gregeric 0:ee5cb967aa17 13
gregeric 1:cd7b42c99ff8 14 encoder->IC1Filter = 0x0F;
gregeric 1:cd7b42c99ff8 15 encoder->IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
gregeric 1:cd7b42c99ff8 16 encoder->IC1Prescaler = TIM_ICPSC_DIV4;
gregeric 1:cd7b42c99ff8 17 encoder->IC1Selection = TIM_ICSELECTION_DIRECTTI;
gregeric 1:cd7b42c99ff8 18
gregeric 1:cd7b42c99ff8 19 encoder->IC2Filter = 0x0F;
gregeric 1:cd7b42c99ff8 20 encoder->IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING;
gregeric 1:cd7b42c99ff8 21 encoder->IC2Prescaler = TIM_ICPSC_DIV4;
gregeric 1:cd7b42c99ff8 22 encoder->IC2Selection = TIM_ICSELECTION_DIRECTTI;
gregeric 0:ee5cb967aa17 23
gregeric 0:ee5cb967aa17 24
gregeric 1:cd7b42c99ff8 25 if (HAL_TIM_Encoder_Init(timer, encoder) != HAL_OK) {
gregeric 0:ee5cb967aa17 26 printf("Couldn't Init Encoder\r\n");
gregeric 0:ee5cb967aa17 27 while (1) {}
gregeric 0:ee5cb967aa17 28 }
gregeric 0:ee5cb967aa17 29
gregeric 1:cd7b42c99ff8 30 if(HAL_TIM_Encoder_Start(timer,TIM_CHANNEL_1)!=HAL_OK) {
gregeric 0:ee5cb967aa17 31 printf("Couldn't Start Encoder\r\n");
gregeric 0:ee5cb967aa17 32 while (1) {}
gregeric 0:ee5cb967aa17 33 }
gregeric 0:ee5cb967aa17 34 }
gregeric 0:ee5cb967aa17 35