This program is for controlling the velocity of 2 motors.

Committer:
r08522622
Date:
Wed Jul 15 11:18:13 2020 +0000
Revision:
0:e3e886021c90
edit parameters

Who changed what in which revision?

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