Counter using TIM2

Dependencies:   mbed

Fork of Nucleo_Hello_counter by paolo della vedova

Committer:
c128
Date:
Wed Sep 30 08:15:19 2015 +0000
Revision:
4:26948bebef6c
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
c128 4:26948bebef6c 1 #include "mbed.h"
c128 4:26948bebef6c 2
c128 4:26948bebef6c 3 void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_IC_InitTypeDef counterSet, TIM_TypeDef *TIMx, uint32_t maxcount, uint32_t encmode)
c128 4:26948bebef6c 4 {
c128 4:26948bebef6c 5 timer.Instance = TIMx;
c128 4:26948bebef6c 6 timer.Init.Period = maxcount;
c128 4:26948bebef6c 7 timer.Init.CounterMode = TIM_COUNTERMODE_UP;
c128 4:26948bebef6c 8 timer.Init.Prescaler = 0;
c128 4:26948bebef6c 9 timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
c128 4:26948bebef6c 10
c128 4:26948bebef6c 11 if (encmode == 0) {
c128 4:26948bebef6c 12 printf("counterset");
c128 4:26948bebef6c 13 counterSet.ICFilter = 0x0;
c128 4:26948bebef6c 14 counterSet.ICPolarity = TIM_ICPOLARITY_RISING;
c128 4:26948bebef6c 15 counterSet.ICPrescaler = TIM_ICPSC_DIV1;
c128 4:26948bebef6c 16 counterSet.ICSelection = TIM_ICSELECTION_DIRECTTI;
c128 4:26948bebef6c 17 } else {
c128 4:26948bebef6c 18 printf("encodermode");
c128 4:26948bebef6c 19 encoder.EncoderMode = encmode;
c128 4:26948bebef6c 20
c128 4:26948bebef6c 21 encoder.IC1Filter = 0x0;
c128 4:26948bebef6c 22 encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
c128 4:26948bebef6c 23 encoder.IC1Prescaler = TIM_ICPSC_DIV1;
c128 4:26948bebef6c 24 encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;
c128 4:26948bebef6c 25
c128 4:26948bebef6c 26 encoder.IC2Filter = 0x0;
c128 4:26948bebef6c 27 encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
c128 4:26948bebef6c 28 encoder.IC2Prescaler = TIM_ICPSC_DIV1;
c128 4:26948bebef6c 29 encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI;
c128 4:26948bebef6c 30 }
c128 4:26948bebef6c 31
c128 4:26948bebef6c 32
c128 4:26948bebef6c 33 if (HAL_TIM_Encoder_Init(&timer, &encoder) != HAL_OK) {
c128 4:26948bebef6c 34 printf("Couldn't Init Encoder\r\n");
c128 4:26948bebef6c 35 while (1) {}
c128 4:26948bebef6c 36 }
c128 4:26948bebef6c 37
c128 4:26948bebef6c 38 if(HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1)!=HAL_OK) {
c128 4:26948bebef6c 39 printf("Couldn't Start Encoder\r\n");
c128 4:26948bebef6c 40 while (1) {}
c128 4:26948bebef6c 41 }
c128 4:26948bebef6c 42 }
c128 4:26948bebef6c 43