hi

Dependents:   hello_enc_test

Fork of Nucleo_Hello_Encoder by David Lowe

main.cpp

Committer:
gregeric
Date:
2015-01-10
Revision:
0:ee5cb967aa17
Child:
1:cd7b42c99ff8

File content as of revision 0:ee5cb967aa17:

/*
 * Using STM32's counter peripherals to interface rotary encoders.
 * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit.
 * Beware mbed uses TIM5 for system timer, others for PWM.
 * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders.
 *
 * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs.
 *
 * Thanks to:
 * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/
 *
 * References:
 * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf
 * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf
 * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
 * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
 * 
 * David Lowe Jan 2015
 */

#include "mbed.h"
#include "Encoder.h"

TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4;
TIM_HandleTypeDef timer1, timer2, timer3, timer4;

int main()
{
    //examples
    
    //counting on A-input only, 2 ticks per cycle, rolls over at 100
    EncoderInit(encoder1, timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
    
    //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
    EncoderInit(encoder2, timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);

    //counting on B-input only, 2 ticks per cycle, full 16-bit count
    EncoderInit(encoder3, timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
    
    //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
    EncoderInit(encoder4, timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
    
    //TIM5 is used by mbed for systick
    //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
    
    printf("STM HAL encoder demo\n\r");
    
    while(1) {
        int16_t count1=0, count3=0, count4=0;
        int32_t count2=0, count5=0;
        
        count1=TIM1->CNT; //OK 401 411 TICKER 030
        count2=TIM2->CNT; //OK 401 411 N/A 030
        count3=TIM3->CNT; //OK 401 411 030
        count4=TIM4->CNT; //OK 401 NOK 411 N/A 030
        count5=TIM5->CNT; //TICKER 401 411 N/A 030
        printf("%d %d %d %d %d\r\n", count1, count2, count3, count4, count5);
        wait(1.0);
    }
}