nambah buat eksternal

Dependencies:   mbed encoderKRAI Motor_new

Committer:
Yolandataniaa
Date:
Mon Jun 21 07:48:27 2021 +0000
Revision:
1:bbe0769f00e9
Parent:
0:49e87dcad299
revisi

Who changed what in which revision?

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