cancel
Showing results for 
Search instead for 
Did you mean: 

TIM_SetCounter(TIM4, 32000); problem

huydungshpy
Associate II
Posted on March 19, 2015 at 11:12

Hi all!

Anyone help me what mean of function “ voidTIM_SetCounter(TIM_TypeDef* TIMx, uint16_t Counter); For example

TIM_SetCounter(TIM4, 32000);

I don’t know how can calculate varieble uint16_tCounter.

If you have an example of Encoder Interface Mode, youcan send to me.

Sory because I am bad English!!

Thank a lot of!!!

4 REPLIES 4
Posted on March 19, 2015 at 15:37

Sets the internal counter in the timer, equivalent to TIM4->CNT = 32000;

Looks to be a fairly arbitrary value, probably half the period of the counter. You don't supply sufficient context.

I've posted several encoder/decoder examples, you'll need to be specific about which STM32 you're using, as ST makes several parts. Look also at the firmware library examples.

Toward the end of

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F3%20Discovery%20Counter&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=612

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
huydungshpy
Associate II
Posted on March 20, 2015 at 14:09

Thank for  reply!

I found a program on the Internet. But I don’t know function of it

(

TIM_SetCounter(TIM4, 32000); ) and what do it mean of  value 32000??

This is program. 

#include ''stm32f10x.h''

GPIO_InitTypeDef GPIO_InitStructure;

void RCC_Configuration(void)

{

    /* Setup the microcontroller system. Initialize the Embedded Flash Interface, 

    initialize the PLL and update the SystemFrequency variable. */

    //SystemInit();

    /* Enable GPIOB clocks */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

}

void Timer4_Configuration(void)

{

    TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge);

    TIM_ARRPreloadConfig(TIM4, ENABLE);

    TIM_SetCounter(TIM4, 32000);

    TIM_Cmd(TIM4,ENABLE);

}

void GPIO_Remap_Configuration(void)

{

    GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;

    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

}

int main(void)

{

    uint16_t temp;

    

    /* Configure the system clocks */

    RCC_Configuration();   

    Timer4_Configuration();

    GPIO_Remap_Configuration();

    /* Configure PB.8, PB.9, PB.10, PB11, PB12, PB.13, PB14 and PB.15 as output push-pull */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    while (1)

    {

        temp=TIM_GetCounter(TIM4);

        GPIO_Write(GPIOB, GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6)*64+GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)*128+temp);

    }

}

Thanks!

Posted on March 20, 2015 at 15:58

It sets the counter to the approximate mid-point of the count, moving the encoder back and forth will increment and decrement from that point.

min point in binary terms 32768

The positive number can make the math easier, as it doesn't wrap.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
huydungshpy
Associate II
Posted on March 21, 2015 at 13:54

You mean 32768  = 65536/2   ?

thank you so much !!