A program to monitor some parameters for a motor
Dependencies: mbed-dev BufferSerial
Thanks to David Lowe for https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/ which I adapted for the use of TIM2 32bit timer as an encoder reader on the Nucleo L432KC board.
main.cpp@4:4f115819171f, 2017-05-13 (annotated)
- Committer:
- tonnyleonard
- Date:
- Sat May 13 19:04:08 2017 +0000
- Revision:
- 4:4f115819171f
- Parent:
- 2:958e9094b198
- Child:
- 5:29372f6cb533
Excluding the first 16 bit encoder, not needed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tonnyleonard | 0:789510d98ade | 1 | /* |
tonnyleonard | 0:789510d98ade | 2 | * Using STM32's counter peripherals to interface rotary encoders. |
tonnyleonard | 0:789510d98ade | 3 | * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit. |
tonnyleonard | 0:789510d98ade | 4 | * Beware mbed uses TIM5 for system timer, SPI needs TIM1, others used for PWM. |
tonnyleonard | 0:789510d98ade | 5 | * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders. |
tonnyleonard | 0:789510d98ade | 6 | * |
tonnyleonard | 0:789510d98ade | 7 | * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs. |
tonnyleonard | 0:789510d98ade | 8 | * |
tonnyleonard | 0:789510d98ade | 9 | * Thanks to: |
tonnyleonard | 0:789510d98ade | 10 | * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/ |
tonnyleonard | 0:789510d98ade | 11 | * |
tonnyleonard | 0:789510d98ade | 12 | * References: |
tonnyleonard | 0:789510d98ade | 13 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf |
tonnyleonard | 0:789510d98ade | 14 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf |
tonnyleonard | 0:789510d98ade | 15 | * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf |
tonnyleonard | 0:789510d98ade | 16 | * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf |
tonnyleonard | 0:789510d98ade | 17 | * |
tonnyleonard | 0:789510d98ade | 18 | * David Lowe Jan 2015 |
tonnyleonard | 0:789510d98ade | 19 | */ |
tonnyleonard | 0:789510d98ade | 20 | |
tonnyleonard | 0:789510d98ade | 21 | #include "mbed.h" |
tonnyleonard | 0:789510d98ade | 22 | #include "Encoder.h" |
tonnyleonard | 0:789510d98ade | 23 | |
tonnyleonard | 4:4f115819171f | 24 | TIM_Encoder_InitTypeDef encoder1; |
tonnyleonard | 2:958e9094b198 | 25 | TIM_HandleTypeDef timer1, timer2; |
tonnyleonard | 0:789510d98ade | 26 | |
tonnyleonard | 0:789510d98ade | 27 | int main() |
tonnyleonard | 0:789510d98ade | 28 | { |
tonnyleonard | 0:789510d98ade | 29 | |
tonnyleonard | 0:789510d98ade | 30 | //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count |
tonnyleonard | 2:958e9094b198 | 31 | //For L432KC to work with TIM2 one needs to reassign the system ticker |
tonnyleonard | 2:958e9094b198 | 32 | //from TIM2 to TIM7 in TARGET/../device/hal_tick.h |
tonnyleonard | 4:4f115819171f | 33 | EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); |
tonnyleonard | 0:789510d98ade | 34 | |
tonnyleonard | 4:4f115819171f | 35 | printf("\n\rSTM HAL encoder with ADC and DAC\n\r"); |
tonnyleonard | 0:789510d98ade | 36 | |
tonnyleonard | 0:789510d98ade | 37 | while(1) { |
tonnyleonard | 4:4f115819171f | 38 | uint16_t count2=0; |
tonnyleonard | 4:4f115819171f | 39 | uint32_t count1=0; |
tonnyleonard | 4:4f115819171f | 40 | int8_t dir1; |
tonnyleonard | 0:789510d98ade | 41 | |
tonnyleonard | 0:789510d98ade | 42 | |
tonnyleonard | 0:789510d98ade | 43 | //OK 401 411 446 NOK 030 |
tonnyleonard | 0:789510d98ade | 44 | //count2=TIM2->CNT; |
tonnyleonard | 0:789510d98ade | 45 | //dir2=TIM2->CR1&TIM_CR1_DIR; |
tonnyleonard | 4:4f115819171f | 46 | count1=__HAL_TIM_GET_COUNTER(&timer2); |
tonnyleonard | 4:4f115819171f | 47 | dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); |
tonnyleonard | 0:789510d98ade | 48 | |
tonnyleonard | 0:789510d98ade | 49 | |
tonnyleonard | 4:4f115819171f | 50 | printf("%d%s\r\n", count1, dir1==0 ? "+":"-"); |
tonnyleonard | 2:958e9094b198 | 51 | wait(0.1); |
tonnyleonard | 0:789510d98ade | 52 | } |
tonnyleonard | 0:789510d98ade | 53 | } |