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 "encoderHAL.h"
Yolandataniaa 0:49e87dcad299 2
Yolandataniaa 0:49e87dcad299 3 namespace mbed
Yolandataniaa 0:49e87dcad299 4 {
Yolandataniaa 0:49e87dcad299 5
Yolandataniaa 0:49e87dcad299 6 encoderHAL::encoderHAL(TIM_TypeDef * _TIM)
Yolandataniaa 0:49e87dcad299 7 {
Yolandataniaa 0:49e87dcad299 8 TIM = _TIM;
Yolandataniaa 0:49e87dcad299 9 // Initialisation of the TIM module as an encoder counter
Yolandataniaa 0:49e87dcad299 10 EncoderInit(&encoder, &timer, _TIM, 0xffff, TIM_ENCODERMODE_TI12);
Yolandataniaa 0:49e87dcad299 11
Yolandataniaa 0:49e87dcad299 12 // Update (aka over- and underflow) interrupt enabled
Yolandataniaa 0:49e87dcad299 13 TIM->DIER |= 0x0001;
Yolandataniaa 0:49e87dcad299 14 // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else
Yolandataniaa 0:49e87dcad299 15 TIM->SR &= 0xfffe;
Yolandataniaa 0:49e87dcad299 16 //generate update event
Yolandataniaa 0:49e87dcad299 17 TIM->EGR = 1;
Yolandataniaa 0:49e87dcad299 18 //enable counter
Yolandataniaa 0:49e87dcad299 19 TIM->CR1 = 1;
Yolandataniaa 0:49e87dcad299 20
Yolandataniaa 0:49e87dcad299 21 }
Yolandataniaa 0:49e87dcad299 22
Yolandataniaa 0:49e87dcad299 23 encoderHAL::encoderHAL(TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode)
Yolandataniaa 0:49e87dcad299 24 {
Yolandataniaa 0:49e87dcad299 25 TIM = _TIM;
Yolandataniaa 0:49e87dcad299 26 // Initialisation of the TIM module as an encoder counter
Yolandataniaa 0:49e87dcad299 27 EncoderInit(&encoder, &timer, _TIM, _maxcount, _encmode);
Yolandataniaa 0:49e87dcad299 28
Yolandataniaa 0:49e87dcad299 29 // Update (aka over- and underflow) interrupt enabled
Yolandataniaa 0:49e87dcad299 30 TIM->DIER |= 0x0001;
Yolandataniaa 0:49e87dcad299 31 // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else
Yolandataniaa 0:49e87dcad299 32 TIM->SR &= 0xfffe;
Yolandataniaa 0:49e87dcad299 33 }
Yolandataniaa 0:49e87dcad299 34
Yolandataniaa 0:49e87dcad299 35 encoderHAL::encoderHAL(TIM_Encoder_InitTypeDef * _encoder, TIM_HandleTypeDef * _timer, TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode)
Yolandataniaa 0:49e87dcad299 36 {
Yolandataniaa 0:49e87dcad299 37 timer = *_timer;
Yolandataniaa 0:49e87dcad299 38 encoder = *_encoder;
Yolandataniaa 0:49e87dcad299 39 TIM = _TIM;
Yolandataniaa 0:49e87dcad299 40 // Initialisation of the TIM module as an encoder counter
Yolandataniaa 0:49e87dcad299 41 EncoderInit(&encoder, &timer, _TIM, _maxcount, _encmode);
Yolandataniaa 0:49e87dcad299 42
Yolandataniaa 0:49e87dcad299 43 // Update (aka over- and underflow) interrupt enabled
Yolandataniaa 0:49e87dcad299 44 TIM->DIER |= 0x0001;
Yolandataniaa 0:49e87dcad299 45 // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else
Yolandataniaa 0:49e87dcad299 46 TIM->SR &= 0xfffe;
Yolandataniaa 0:49e87dcad299 47 }
Yolandataniaa 0:49e87dcad299 48
Yolandataniaa 0:49e87dcad299 49
Yolandataniaa 0:49e87dcad299 50 int32_t encoderHAL::getPulses(bool reset)
Yolandataniaa 0:49e87dcad299 51 {
Yolandataniaa 0:49e87dcad299 52 int16_t count = TIM->CNT;
Yolandataniaa 0:49e87dcad299 53 if(reset){
Yolandataniaa 0:49e87dcad299 54 switch((uint32_t)TIM){
Yolandataniaa 0:49e87dcad299 55 case TIM1_BASE :
Yolandataniaa 0:49e87dcad299 56 TIM1->CNT = 0;
Yolandataniaa 0:49e87dcad299 57 break;
Yolandataniaa 0:49e87dcad299 58
Yolandataniaa 0:49e87dcad299 59 case TIM2_BASE :
Yolandataniaa 0:49e87dcad299 60 TIM2->CNT = 0;
Yolandataniaa 0:49e87dcad299 61 break;
Yolandataniaa 0:49e87dcad299 62
Yolandataniaa 0:49e87dcad299 63 case TIM3_BASE :
Yolandataniaa 0:49e87dcad299 64 TIM3->CNT = 0;
Yolandataniaa 0:49e87dcad299 65 break;
Yolandataniaa 0:49e87dcad299 66
Yolandataniaa 0:49e87dcad299 67 case TIM4_BASE :
Yolandataniaa 0:49e87dcad299 68 TIM4->CNT = 0;
Yolandataniaa 0:49e87dcad299 69 break;
Yolandataniaa 0:49e87dcad299 70
Yolandataniaa 1:bbe0769f00e9 71 //case TIM5_BASE :
Yolandataniaa 1:bbe0769f00e9 72 // TIM5->CNT = 0;
Yolandataniaa 1:bbe0769f00e9 73 // break;
Yolandataniaa 0:49e87dcad299 74
Yolandataniaa 1:bbe0769f00e9 75 // case TIM8_BASE :
Yolandataniaa 1:bbe0769f00e9 76 // TIM8->CNT = 0;
Yolandataniaa 1:bbe0769f00e9 77 // break;
Yolandataniaa 0:49e87dcad299 78 }
Yolandataniaa 0:49e87dcad299 79 }
Yolandataniaa 0:49e87dcad299 80 else{
Yolandataniaa 0:49e87dcad299 81 switch((uint32_t)TIM)
Yolandataniaa 0:49e87dcad299 82 {
Yolandataniaa 0:49e87dcad299 83 case TIM1_BASE :
Yolandataniaa 0:49e87dcad299 84 return (int32_t)count;
Yolandataniaa 0:49e87dcad299 85
Yolandataniaa 0:49e87dcad299 86 case TIM2_BASE :
Yolandataniaa 0:49e87dcad299 87 return (int32_t)count;
Yolandataniaa 0:49e87dcad299 88
Yolandataniaa 0:49e87dcad299 89 case TIM3_BASE :
Yolandataniaa 0:49e87dcad299 90 return (int32_t)count;
Yolandataniaa 0:49e87dcad299 91
Yolandataniaa 0:49e87dcad299 92 case TIM4_BASE :
Yolandataniaa 0:49e87dcad299 93 return (int32_t)count;
Yolandataniaa 1:bbe0769f00e9 94 //
Yolandataniaa 1:bbe0769f00e9 95 // case TIM5_BASE :
Yolandataniaa 1:bbe0769f00e9 96 // return (int32_t)count;
Yolandataniaa 1:bbe0769f00e9 97 //
Yolandataniaa 1:bbe0769f00e9 98 // case TIM8_BASE :
Yolandataniaa 1:bbe0769f00e9 99 // return (int32_t)count;
Yolandataniaa 0:49e87dcad299 100 }
Yolandataniaa 0:49e87dcad299 101 }
Yolandataniaa 0:49e87dcad299 102
Yolandataniaa 0:49e87dcad299 103 return (int32_t)count;
Yolandataniaa 0:49e87dcad299 104 }
Yolandataniaa 0:49e87dcad299 105
Yolandataniaa 0:49e87dcad299 106
Yolandataniaa 0:49e87dcad299 107 TIM_HandleTypeDef* encoderHAL::GetTimer()
Yolandataniaa 0:49e87dcad299 108 {
Yolandataniaa 0:49e87dcad299 109 return &timer;
Yolandataniaa 0:49e87dcad299 110 }
Yolandataniaa 0:49e87dcad299 111
Yolandataniaa 0:49e87dcad299 112 }