hi

Dependents:   hello_enc_test

Fork of Nucleo_Hello_Encoder by David Lowe

Committer:
gregeric
Date:
Sat Jan 10 20:06:07 2015 +0000
Revision:
0:ee5cb967aa17
Child:
1:cd7b42c99ff8
Hello World example using the STM32's timer hardware interface, no interrupts.; ; Supporting Nucleo F030, F401, F411 (others can be added - L0xx included but untested).; ; Encoder with TIM4 not working on F411, OK on F401?

Who changed what in which revision?

UserRevisionLine numberNew 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 0:ee5cb967aa17 4 * Beware mbed uses TIM5 for system timer, others 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 0:ee5cb967aa17 24 TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4;
gregeric 0:ee5cb967aa17 25 TIM_HandleTypeDef timer1, timer2, timer3, timer4;
gregeric 0:ee5cb967aa17 26
gregeric 0:ee5cb967aa17 27 int main()
gregeric 0:ee5cb967aa17 28 {
gregeric 0:ee5cb967aa17 29 //examples
gregeric 0:ee5cb967aa17 30
gregeric 0:ee5cb967aa17 31 //counting on A-input only, 2 ticks per cycle, rolls over at 100
gregeric 0:ee5cb967aa17 32 EncoderInit(encoder1, timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
gregeric 0:ee5cb967aa17 33
gregeric 0:ee5cb967aa17 34 //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
gregeric 0:ee5cb967aa17 35 EncoderInit(encoder2, timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
gregeric 0:ee5cb967aa17 36
gregeric 0:ee5cb967aa17 37 //counting on B-input only, 2 ticks per cycle, full 16-bit count
gregeric 0:ee5cb967aa17 38 EncoderInit(encoder3, timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
gregeric 0:ee5cb967aa17 39
gregeric 0:ee5cb967aa17 40 //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
gregeric 0:ee5cb967aa17 41 EncoderInit(encoder4, timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
gregeric 0:ee5cb967aa17 42
gregeric 0:ee5cb967aa17 43 //TIM5 is used by mbed for systick
gregeric 0:ee5cb967aa17 44 //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
gregeric 0:ee5cb967aa17 45
gregeric 0:ee5cb967aa17 46 printf("STM HAL encoder demo\n\r");
gregeric 0:ee5cb967aa17 47
gregeric 0:ee5cb967aa17 48 while(1) {
gregeric 0:ee5cb967aa17 49 int16_t count1=0, count3=0, count4=0;
gregeric 0:ee5cb967aa17 50 int32_t count2=0, count5=0;
gregeric 0:ee5cb967aa17 51
gregeric 0:ee5cb967aa17 52 count1=TIM1->CNT; //OK 401 411 TICKER 030
gregeric 0:ee5cb967aa17 53 count2=TIM2->CNT; //OK 401 411 N/A 030
gregeric 0:ee5cb967aa17 54 count3=TIM3->CNT; //OK 401 411 030
gregeric 0:ee5cb967aa17 55 count4=TIM4->CNT; //OK 401 NOK 411 N/A 030
gregeric 0:ee5cb967aa17 56 count5=TIM5->CNT; //TICKER 401 411 N/A 030
gregeric 0:ee5cb967aa17 57 printf("%d %d %d %d %d\r\n", count1, count2, count3, count4, count5);
gregeric 0:ee5cb967aa17 58 wait(1.0);
gregeric 0:ee5cb967aa17 59 }
gregeric 0:ee5cb967aa17 60 }