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.

Committer:
tonnyleonard
Date:
Sat Mar 11 22:56:39 2017 +0000
Revision:
2:958e9094b198
Parent:
1:a611582bffa7
Child:
4:4f115819171f
System ticker on Nucleo L432KC board changed from TIM2 to TIM7, so that one may use TIM2 for a 32bit encoder counter.

Who changed what in which revision?

UserRevisionLine numberNew 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 2:958e9094b198 24 TIM_Encoder_InitTypeDef encoder1, encoder2;
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 A-input only, 2 ticks per cycle, rolls over at 100
tonnyleonard 1:a611582bffa7 31 EncoderInit(&encoder1, &timer1, TIM1, 0xffff, TIM_ENCODERMODE_TI12);
tonnyleonard 0:789510d98ade 32
tonnyleonard 0:789510d98ade 33 //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
tonnyleonard 2:958e9094b198 34 //For L432KC to work with TIM2 one needs to reassign the system ticker
tonnyleonard 2:958e9094b198 35 //from TIM2 to TIM7 in TARGET/../device/hal_tick.h
tonnyleonard 0:789510d98ade 36 EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
tonnyleonard 0:789510d98ade 37
tonnyleonard 2:958e9094b198 38 printf("\n\rSTM HAL encoder demo\n\r");
tonnyleonard 0:789510d98ade 39
tonnyleonard 0:789510d98ade 40 while(1) {
tonnyleonard 2:958e9094b198 41 uint16_t count1=0;
tonnyleonard 0:789510d98ade 42 uint32_t count2=0;
tonnyleonard 2:958e9094b198 43 int8_t dir1, dir2;
tonnyleonard 0:789510d98ade 44
tonnyleonard 0:789510d98ade 45
tonnyleonard 0:789510d98ade 46 //OK 401 411 446 TICKER 030
tonnyleonard 0:789510d98ade 47 //count1=TIM1->CNT;
tonnyleonard 0:789510d98ade 48 //dir1=TIM1->CR1&TIM_CR1_DIR;
tonnyleonard 1:a611582bffa7 49 count1= __HAL_TIM_GET_COUNTER(&timer1);
tonnyleonard 1:a611582bffa7 50 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
tonnyleonard 0:789510d98ade 51
tonnyleonard 0:789510d98ade 52 //OK 401 411 446 NOK 030
tonnyleonard 0:789510d98ade 53 //count2=TIM2->CNT;
tonnyleonard 0:789510d98ade 54 //dir2=TIM2->CR1&TIM_CR1_DIR;
tonnyleonard 0:789510d98ade 55 count2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 0:789510d98ade 56 dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 0:789510d98ade 57
tonnyleonard 0:789510d98ade 58
tonnyleonard 1:a611582bffa7 59 printf("%d%s %d%s\r\n", count1, dir1==0 ? "+":"-", count2, dir2==0 ? "+":"-" );
tonnyleonard 2:958e9094b198 60 wait(0.1);
tonnyleonard 0:789510d98ade 61 }
tonnyleonard 0:789510d98ade 62 }