Hello World example for interfacing up to four rotary encoders to the STM32's timer/counter hardware, without interrupts.

Dependencies:   mbed-dev

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Using STM32's counter peripherals to interface rotary encoders.
00003  * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit.
00004  * Beware mbed uses TIM5 for system timer, SPI needs TIM1, others used for PWM.
00005  * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders.
00006  *
00007  * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs.
00008  *
00009  * Thanks to:
00010  * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/
00011  *
00012  * References:
00013  * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf
00014  * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf
00015  * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
00016  * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
00017  * 
00018  * David Lowe Jan 2015
00019  */
00020 
00021 #include "mbed.h"
00022 #include "Encoder.h"
00023 
00024 //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
00025 #ifdef TARGET_STM32F3
00026 #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
00027 #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__)            (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
00028 #endif
00029 
00030 TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4;
00031 TIM_HandleTypeDef  timer1,  timer2,  timer3,  timer4;
00032 
00033 int main()
00034 {
00035     //examples
00036     
00037     //counting on A-input only, 2 ticks per cycle, rolls over at 100
00038     EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
00039 
00040     //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
00041     EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
00042 
00043     //counting on B-input only, 2 ticks per cycle, full 16-bit count
00044     EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
00045     
00046     //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
00047     EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
00048     
00049     //TIM5 is used by mbed for systick
00050     //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
00051     
00052     printf("STM HAL encoder demo\n\r");
00053     
00054     while(1) {
00055         uint16_t count1=0, count3=0, count4=0;
00056         uint32_t count2=0;
00057         int8_t dir1, dir2, dir3, dir4;
00058 
00059         
00060         //OK 401 411 446 TICKER 030
00061         //count1=TIM1->CNT;
00062         //dir1=TIM1->CR1&TIM_CR1_DIR;
00063         count1=__HAL_TIM_GET_COUNTER(&timer1);
00064         dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
00065 
00066         //OK 401 411 446 NOK 030
00067         //count2=TIM2->CNT;
00068         //dir2=TIM2->CR1&TIM_CR1_DIR;
00069         count2=__HAL_TIM_GET_COUNTER(&timer2);
00070         dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
00071 
00072         //OK 401 411 446 030
00073         //count3=TIM3->CNT;
00074         //dir3=TIM3->CR1&TIM_CR1_DIR;
00075         count3=__HAL_TIM_GET_COUNTER(&timer3);
00076         dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3);
00077 
00078         //OK 401 411 446 N/A 030
00079         //count4=TIM4->CNT;
00080         //dir4=TIM4->CR1&TIM_CR1_DIR;
00081         count4=__HAL_TIM_GET_COUNTER(&timer4);
00082         dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4);
00083 
00084         //TICKER 401 411 446 N/A 030
00085 //        count5=__HAL_TIM_GET_COUNTER(&timer5);
00086 //        dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5);
00087         
00088         printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-",
00089                                              count2, dir2==0 ? "+":"-",
00090                                              count3, dir3==0 ? "+":"-",
00091                                              count4, dir4==0 ? "+":"-" );
00092         wait(0.5);
00093     }
00094 }