Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Nucleo_Encoder_16_bits.cpp@3:d43c60d01569, 2018-12-19 (annotated)
- Committer:
- irfantitok
- Date:
- Wed Dec 19 04:02:46 2018 +0000
- Revision:
- 3:d43c60d01569
- Parent:
- 2:3fcf36c1b1af
- Child:
- 4:9ce0e451aeff
Overflow handler removed, uint16 count -> int16 count
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kkoichy | 0:ebd170807e11 | 1 | #include "Nucleo_Encoder_16_bits.h" |
| kkoichy | 0:ebd170807e11 | 2 | |
| kkoichy | 0:ebd170807e11 | 3 | namespace mbed |
| kkoichy | 0:ebd170807e11 | 4 | { |
| kkoichy | 1:e82009479b5c | 5 | |
| kkoichy | 1:e82009479b5c | 6 | Nucleo_Encoder_16_bits::Nucleo_Encoder_16_bits(TIM_TypeDef * _TIM) |
| kkoichy | 1:e82009479b5c | 7 | { |
| kkoichy | 1:e82009479b5c | 8 | TIM = _TIM; |
| kkoichy | 1:e82009479b5c | 9 | // Initialisation of the TIM module as an encoder counter |
| kkoichy | 1:e82009479b5c | 10 | EncoderInit(&encoder, &timer, _TIM, 0xffff, TIM_ENCODERMODE_TI12); |
| kkoichy | 1:e82009479b5c | 11 | |
| kkoichy | 1:e82009479b5c | 12 | // Update (aka over- and underflow) interrupt enabled |
| kkoichy | 1:e82009479b5c | 13 | TIM->DIER |= 0x0001; |
| kkoichy | 1:e82009479b5c | 14 | // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else |
| kkoichy | 1:e82009479b5c | 15 | TIM->SR &= 0xfffe; |
| kkoichy | 1:e82009479b5c | 16 | } |
| irfantitok | 3:d43c60d01569 | 17 | |
| kkoichy | 0:ebd170807e11 | 18 | Nucleo_Encoder_16_bits::Nucleo_Encoder_16_bits(TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode) |
| kkoichy | 0:ebd170807e11 | 19 | { |
| kkoichy | 0:ebd170807e11 | 20 | TIM = _TIM; |
| kkoichy | 0:ebd170807e11 | 21 | // Initialisation of the TIM module as an encoder counter |
| kkoichy | 0:ebd170807e11 | 22 | EncoderInit(&encoder, &timer, _TIM, _maxcount, _encmode); |
| kkoichy | 0:ebd170807e11 | 23 | |
| kkoichy | 0:ebd170807e11 | 24 | // Update (aka over- and underflow) interrupt enabled |
| kkoichy | 0:ebd170807e11 | 25 | TIM->DIER |= 0x0001; |
| kkoichy | 0:ebd170807e11 | 26 | // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else |
| kkoichy | 0:ebd170807e11 | 27 | TIM->SR &= 0xfffe; |
| kkoichy | 0:ebd170807e11 | 28 | } |
| kkoichy | 0:ebd170807e11 | 29 | |
| kkoichy | 0:ebd170807e11 | 30 | Nucleo_Encoder_16_bits::Nucleo_Encoder_16_bits(TIM_Encoder_InitTypeDef * _encoder, TIM_HandleTypeDef * _timer, TIM_TypeDef * _TIM, uint32_t _maxcount, uint32_t _encmode) |
| kkoichy | 0:ebd170807e11 | 31 | { |
| kkoichy | 0:ebd170807e11 | 32 | timer = *_timer; |
| kkoichy | 0:ebd170807e11 | 33 | encoder = *_encoder; |
| kkoichy | 0:ebd170807e11 | 34 | TIM = _TIM; |
| kkoichy | 0:ebd170807e11 | 35 | // Initialisation of the TIM module as an encoder counter |
| kkoichy | 0:ebd170807e11 | 36 | EncoderInit(&encoder, &timer, _TIM, _maxcount, _encmode); |
| kkoichy | 0:ebd170807e11 | 37 | |
| kkoichy | 0:ebd170807e11 | 38 | // Update (aka over- and underflow) interrupt enabled |
| kkoichy | 0:ebd170807e11 | 39 | TIM->DIER |= 0x0001; |
| kkoichy | 0:ebd170807e11 | 40 | // The initialisation process generates an update interrupt, so we'll have to clear the update flag before anything else |
| kkoichy | 0:ebd170807e11 | 41 | TIM->SR &= 0xfffe; |
| kkoichy | 0:ebd170807e11 | 42 | } |
| kkoichy | 0:ebd170807e11 | 43 | |
| kkoichy | 0:ebd170807e11 | 44 | |
| calmantara186 | 2:3fcf36c1b1af | 45 | int32_t Nucleo_Encoder_16_bits::GetCounter(bool reset) |
| kkoichy | 0:ebd170807e11 | 46 | { |
| irfantitok | 3:d43c60d01569 | 47 | int16_t count = TIM->CNT; |
| calmantara186 | 2:3fcf36c1b1af | 48 | if(reset){ |
| calmantara186 | 2:3fcf36c1b1af | 49 | switch((uint32_t)TIM){ |
| calmantara186 | 2:3fcf36c1b1af | 50 | case TIM1_BASE : |
| calmantara186 | 2:3fcf36c1b1af | 51 | TIM1->CNT = 0; |
| calmantara186 | 2:3fcf36c1b1af | 52 | break; |
| calmantara186 | 2:3fcf36c1b1af | 53 | |
| calmantara186 | 2:3fcf36c1b1af | 54 | case TIM2_BASE : |
| calmantara186 | 2:3fcf36c1b1af | 55 | TIM2->CNT = 0; |
| calmantara186 | 2:3fcf36c1b1af | 56 | break; |
| calmantara186 | 2:3fcf36c1b1af | 57 | |
| calmantara186 | 2:3fcf36c1b1af | 58 | case TIM3_BASE : |
| calmantara186 | 2:3fcf36c1b1af | 59 | TIM3->CNT = 0; |
| calmantara186 | 2:3fcf36c1b1af | 60 | break; |
| calmantara186 | 2:3fcf36c1b1af | 61 | |
| calmantara186 | 2:3fcf36c1b1af | 62 | case TIM4_BASE : |
| calmantara186 | 2:3fcf36c1b1af | 63 | TIM4->CNT = 0; |
| calmantara186 | 2:3fcf36c1b1af | 64 | break; |
| calmantara186 | 2:3fcf36c1b1af | 65 | |
| calmantara186 | 2:3fcf36c1b1af | 66 | case TIM5_BASE : |
| calmantara186 | 2:3fcf36c1b1af | 67 | TIM5->CNT = 0; |
| calmantara186 | 2:3fcf36c1b1af | 68 | break; |
| calmantara186 | 2:3fcf36c1b1af | 69 | } |
| calmantara186 | 2:3fcf36c1b1af | 70 | } |
| calmantara186 | 2:3fcf36c1b1af | 71 | else{ |
| calmantara186 | 2:3fcf36c1b1af | 72 | switch((uint32_t)TIM) |
| calmantara186 | 2:3fcf36c1b1af | 73 | { |
| calmantara186 | 2:3fcf36c1b1af | 74 | case TIM1_BASE : |
| irfantitok | 3:d43c60d01569 | 75 | return (int32_t)count; |
| calmantara186 | 2:3fcf36c1b1af | 76 | |
| calmantara186 | 2:3fcf36c1b1af | 77 | case TIM2_BASE : |
| irfantitok | 3:d43c60d01569 | 78 | return (int32_t)count; |
| calmantara186 | 2:3fcf36c1b1af | 79 | |
| calmantara186 | 2:3fcf36c1b1af | 80 | case TIM3_BASE : |
| irfantitok | 3:d43c60d01569 | 81 | return (int32_t)count; |
| calmantara186 | 2:3fcf36c1b1af | 82 | |
| calmantara186 | 2:3fcf36c1b1af | 83 | case TIM4_BASE : |
| irfantitok | 3:d43c60d01569 | 84 | return (int32_t)count; |
| calmantara186 | 2:3fcf36c1b1af | 85 | |
| calmantara186 | 2:3fcf36c1b1af | 86 | case TIM5_BASE : |
| irfantitok | 3:d43c60d01569 | 87 | return (int32_t)count; |
| calmantara186 | 2:3fcf36c1b1af | 88 | } |
| kkoichy | 0:ebd170807e11 | 89 | } |
| kkoichy | 0:ebd170807e11 | 90 | |
| kkoichy | 0:ebd170807e11 | 91 | return (int32_t)count; |
| calmantara186 | 2:3fcf36c1b1af | 92 | } |
| kkoichy | 0:ebd170807e11 | 93 | |
| irfantitok | 3:d43c60d01569 | 94 | |
| kkoichy | 0:ebd170807e11 | 95 | TIM_HandleTypeDef* Nucleo_Encoder_16_bits::GetTimer() |
| kkoichy | 0:ebd170807e11 | 96 | { |
| kkoichy | 0:ebd170807e11 | 97 | return &timer; |
| kkoichy | 0:ebd170807e11 | 98 | } |
| kkoichy | 0:ebd170807e11 | 99 | |
| kkoichy | 0:ebd170807e11 | 100 | } |