hi
Fork of Nucleo_Hello_Encoder by
main.cpp@1:cd7b42c99ff8, 2015-10-04 (annotated)
- Committer:
- gregeric
- Date:
- Sun Oct 04 11:58:58 2015 +0000
- Revision:
- 1:cd7b42c99ff8
- Parent:
- 0:ee5cb967aa17
Added more targets, use HAL macros to read counter value & direction.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gregeric | 0:ee5cb967aa17 | 1 | /* |
gregeric | 0:ee5cb967aa17 | 2 | * Using STM32's counter peripherals to interface rotary encoders. |
gregeric | 0:ee5cb967aa17 | 3 | * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit. |
gregeric | 1:cd7b42c99ff8 | 4 | * Beware mbed uses TIM5 for system timer, SPI needs TIM1, others used for PWM. |
gregeric | 0:ee5cb967aa17 | 5 | * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders. |
gregeric | 0:ee5cb967aa17 | 6 | * |
gregeric | 0:ee5cb967aa17 | 7 | * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs. |
gregeric | 0:ee5cb967aa17 | 8 | * |
gregeric | 0:ee5cb967aa17 | 9 | * Thanks to: |
gregeric | 0:ee5cb967aa17 | 10 | * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/ |
gregeric | 0:ee5cb967aa17 | 11 | * |
gregeric | 0:ee5cb967aa17 | 12 | * References: |
gregeric | 0:ee5cb967aa17 | 13 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf |
gregeric | 0:ee5cb967aa17 | 14 | * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf |
gregeric | 0:ee5cb967aa17 | 15 | * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf |
gregeric | 0:ee5cb967aa17 | 16 | * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf |
gregeric | 0:ee5cb967aa17 | 17 | * |
gregeric | 0:ee5cb967aa17 | 18 | * David Lowe Jan 2015 |
gregeric | 0:ee5cb967aa17 | 19 | */ |
gregeric | 0:ee5cb967aa17 | 20 | |
gregeric | 0:ee5cb967aa17 | 21 | #include "mbed.h" |
gregeric | 0:ee5cb967aa17 | 22 | #include "Encoder.h" |
gregeric | 0:ee5cb967aa17 | 23 | |
gregeric | 1:cd7b42c99ff8 | 24 | //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h |
gregeric | 1:cd7b42c99ff8 | 25 | #ifdef TARGET_STM32F3 |
gregeric | 1:cd7b42c99ff8 | 26 | #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT) |
gregeric | 1:cd7b42c99ff8 | 27 | #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR)) |
gregeric | 1:cd7b42c99ff8 | 28 | #endif |
gregeric | 1:cd7b42c99ff8 | 29 | |
gregeric | 0:ee5cb967aa17 | 30 | TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4; |
gregeric | 1:cd7b42c99ff8 | 31 | TIM_HandleTypeDef timer1, timer2, timer3, timer4; |
gregeric | 0:ee5cb967aa17 | 32 | |
gregeric | 0:ee5cb967aa17 | 33 | int main() |
gregeric | 0:ee5cb967aa17 | 34 | { |
gregeric | 0:ee5cb967aa17 | 35 | //examples |
gregeric | 0:ee5cb967aa17 | 36 | |
gregeric | 0:ee5cb967aa17 | 37 | //counting on A-input only, 2 ticks per cycle, rolls over at 100 |
gregeric | 1:cd7b42c99ff8 | 38 | EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1); |
gregeric | 1:cd7b42c99ff8 | 39 | |
gregeric | 0:ee5cb967aa17 | 40 | //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count |
gregeric | 1:cd7b42c99ff8 | 41 | EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); |
gregeric | 0:ee5cb967aa17 | 42 | |
gregeric | 0:ee5cb967aa17 | 43 | //counting on B-input only, 2 ticks per cycle, full 16-bit count |
gregeric | 1:cd7b42c99ff8 | 44 | EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2); |
gregeric | 0:ee5cb967aa17 | 45 | |
gregeric | 0:ee5cb967aa17 | 46 | //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count |
gregeric | 1:cd7b42c99ff8 | 47 | EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12); |
gregeric | 0:ee5cb967aa17 | 48 | |
gregeric | 0:ee5cb967aa17 | 49 | //TIM5 is used by mbed for systick |
gregeric | 0:ee5cb967aa17 | 50 | //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12); |
gregeric | 0:ee5cb967aa17 | 51 | |
gregeric | 0:ee5cb967aa17 | 52 | printf("STM HAL encoder demo\n\r"); |
gregeric | 0:ee5cb967aa17 | 53 | |
gregeric | 0:ee5cb967aa17 | 54 | while(1) { |
gregeric | 1:cd7b42c99ff8 | 55 | uint16_t count1=0, count3=0, count4=0; |
gregeric | 1:cd7b42c99ff8 | 56 | uint32_t count2=0; |
gregeric | 1:cd7b42c99ff8 | 57 | int8_t dir1, dir2, dir3, dir4; |
gregeric | 1:cd7b42c99ff8 | 58 | |
gregeric | 0:ee5cb967aa17 | 59 | |
gregeric | 1:cd7b42c99ff8 | 60 | //OK 401 411 446 TICKER 030 |
gregeric | 1:cd7b42c99ff8 | 61 | //count1=TIM1->CNT; |
gregeric | 1:cd7b42c99ff8 | 62 | //dir1=TIM1->CR1&TIM_CR1_DIR; |
gregeric | 1:cd7b42c99ff8 | 63 | count1=__HAL_TIM_GET_COUNTER(&timer1); |
gregeric | 1:cd7b42c99ff8 | 64 | dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1); |
gregeric | 1:cd7b42c99ff8 | 65 | |
gregeric | 1:cd7b42c99ff8 | 66 | //OK 401 411 446 NOK 030 |
gregeric | 1:cd7b42c99ff8 | 67 | //count2=TIM2->CNT; |
gregeric | 1:cd7b42c99ff8 | 68 | //dir2=TIM2->CR1&TIM_CR1_DIR; |
gregeric | 1:cd7b42c99ff8 | 69 | count2=__HAL_TIM_GET_COUNTER(&timer2); |
gregeric | 1:cd7b42c99ff8 | 70 | dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); |
gregeric | 1:cd7b42c99ff8 | 71 | |
gregeric | 1:cd7b42c99ff8 | 72 | //OK 401 411 446 030 |
gregeric | 1:cd7b42c99ff8 | 73 | //count3=TIM3->CNT; |
gregeric | 1:cd7b42c99ff8 | 74 | //dir3=TIM3->CR1&TIM_CR1_DIR; |
gregeric | 1:cd7b42c99ff8 | 75 | count3=__HAL_TIM_GET_COUNTER(&timer3); |
gregeric | 1:cd7b42c99ff8 | 76 | dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3); |
gregeric | 1:cd7b42c99ff8 | 77 | |
gregeric | 1:cd7b42c99ff8 | 78 | //OK 401 411 446 N/A 030 |
gregeric | 1:cd7b42c99ff8 | 79 | //count4=TIM4->CNT; |
gregeric | 1:cd7b42c99ff8 | 80 | //dir4=TIM4->CR1&TIM_CR1_DIR; |
gregeric | 1:cd7b42c99ff8 | 81 | count4=__HAL_TIM_GET_COUNTER(&timer4); |
gregeric | 1:cd7b42c99ff8 | 82 | dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4); |
gregeric | 1:cd7b42c99ff8 | 83 | |
gregeric | 1:cd7b42c99ff8 | 84 | //TICKER 401 411 446 N/A 030 |
gregeric | 1:cd7b42c99ff8 | 85 | // count5=__HAL_TIM_GET_COUNTER(&timer5); |
gregeric | 1:cd7b42c99ff8 | 86 | // dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5); |
gregeric | 1:cd7b42c99ff8 | 87 | |
gregeric | 1:cd7b42c99ff8 | 88 | printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-", |
gregeric | 1:cd7b42c99ff8 | 89 | count2, dir2==0 ? "+":"-", |
gregeric | 1:cd7b42c99ff8 | 90 | count3, dir3==0 ? "+":"-", |
gregeric | 1:cd7b42c99ff8 | 91 | count4, dir4==0 ? "+":"-" ); |
gregeric | 1:cd7b42c99ff8 | 92 | wait(0.5); |
gregeric | 0:ee5cb967aa17 | 93 | } |
gregeric | 0:ee5cb967aa17 | 94 | } |