Counter using TIM2

Dependencies:   mbed

Fork of Nucleo_Hello_counter by paolo della vedova

Committer:
gregeric
Date:
Sat Jan 10 20:06:07 2015 +0000
Revision:
0:ee5cb967aa17
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 #include "mbed.h"
gregeric 0:ee5cb967aa17 2
gregeric 0:ee5cb967aa17 3 void EncoderInit(TIM_Encoder_InitTypeDef encoder, TIM_HandleTypeDef timer, TIM_TypeDef *TIMx, uint32_t maxcount, uint32_t encmode)
gregeric 0:ee5cb967aa17 4 {
gregeric 0:ee5cb967aa17 5 timer.Instance = TIMx;
gregeric 0:ee5cb967aa17 6 timer.Init.Period = maxcount;
gregeric 0:ee5cb967aa17 7 timer.Init.CounterMode = TIM_COUNTERMODE_UP;
gregeric 0:ee5cb967aa17 8 timer.Init.Prescaler = 0;
gregeric 0:ee5cb967aa17 9 timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
gregeric 0:ee5cb967aa17 10
gregeric 0:ee5cb967aa17 11 encoder.EncoderMode = encmode;
gregeric 0:ee5cb967aa17 12
gregeric 0:ee5cb967aa17 13 encoder.IC1Filter = 0x0F;
gregeric 0:ee5cb967aa17 14 encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
gregeric 0:ee5cb967aa17 15 encoder.IC1Prescaler = TIM_ICPSC_DIV4;
gregeric 0:ee5cb967aa17 16 encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;
gregeric 0:ee5cb967aa17 17
gregeric 0:ee5cb967aa17 18 encoder.IC2Filter = 0x0F;
gregeric 0:ee5cb967aa17 19 encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING;
gregeric 0:ee5cb967aa17 20 encoder.IC2Prescaler = TIM_ICPSC_DIV4;
gregeric 0:ee5cb967aa17 21 encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI;
gregeric 0:ee5cb967aa17 22
gregeric 0:ee5cb967aa17 23
gregeric 0:ee5cb967aa17 24 if (HAL_TIM_Encoder_Init(&timer, &encoder) != HAL_OK) {
gregeric 0:ee5cb967aa17 25 printf("Couldn't Init Encoder\r\n");
gregeric 0:ee5cb967aa17 26 while (1) {}
gregeric 0:ee5cb967aa17 27 }
gregeric 0:ee5cb967aa17 28
gregeric 0:ee5cb967aa17 29 if(HAL_TIM_Encoder_Start(&timer,TIM_CHANNEL_1)!=HAL_OK) {
gregeric 0:ee5cb967aa17 30 printf("Couldn't Start Encoder\r\n");
gregeric 0:ee5cb967aa17 31 while (1) {}
gregeric 0:ee5cb967aa17 32 }
gregeric 0:ee5cb967aa17 33 }
gregeric 0:ee5cb967aa17 34