Counter using TIM2

Dependencies:   mbed

Fork of Nucleo_Hello_counter by paolo della vedova

Encoder/EncoderInit.cpp

Committer:
fadamo
Date:
2015-12-04
Revision:
5:e1783d6ee885
Parent:
4:26948bebef6c

File content as of revision 5:e1783d6ee885:

#include "mbed.h"

void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_IC_InitTypeDef counterSet, TIM_TypeDef *TIMx, uint32_t maxcount, uint32_t encmode)
{
    timer.Instance = TIMx;
    timer.Init.Period = maxcount;
    timer.Init.CounterMode = TIM_COUNTERMODE_UP;
    timer.Init.Prescaler = 0;
    timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

    if (encmode == 0) {
        printf("counterset");
    counterSet.ICFilter = 0x0;
    counterSet.ICPolarity = TIM_ICPOLARITY_RISING;
    counterSet.ICPrescaler = TIM_ICPSC_DIV1;
    counterSet.ICSelection = TIM_ICSELECTION_DIRECTTI;
    } else {
        printf("encodermode");
    encoder.EncoderMode = encmode;

    encoder.IC1Filter = 0x0;
    encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
    encoder.IC1Prescaler = TIM_ICPSC_DIV1;
    encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;

    encoder.IC2Filter = 0x0;
    encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
    encoder.IC2Prescaler = TIM_ICPSC_DIV1;
    encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI;
    }


    if (HAL_TIM_Encoder_Init(&timer, &encoder) != HAL_OK) {
        printf("Couldn't Init Encoder\r\n");
        while (1) {}
    }

    if(HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1)!=HAL_OK) {
        printf("Couldn't Start Encoder\r\n");
        while (1) {}
    }
}