cancel
Showing results for 
Search instead for 
Did you mean: 

Encoder Interface and Default_handler

ahmed2399
Associate II
Posted on November 29, 2012 at 13:48

I'm trying  to read the tow signals from the encoder quadrature . I work with COIDE of coocox. but I don't know how i should write in the Default_handler (IRQ_TIM3).

I want read the both edges of the encoder signals,and know the value of my counter timer.

1) I ask if my configuration are OK ?

2) how i write in the IRQ of ''startup_stm32f10x_md_vl.c''

in encoder.c I write :

&sharpinclude ''stm32f10x.h''

&sharpinclude ''encodeur.h''

void config(void){

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_ICInitTypeDef  TIM_ICInitStructure;

  GPIO_InitTypeDef GPIO_InitStructure_Encoder;

  /* Enable TIMER GPIO clocks */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

  GPIO_InitStructure_Encoder.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure_Encoder.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_InitStructure_Encoder.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB,&GPIO_InitStructure_Encoder);

      /* Timer configuration in Encoder mode */

  TIM_DeInit(ENCODER_TIM);

  TIM_TimeBaseStructure.TIM_Prescaler = 0;  // No prescaling

  TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD-1;

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(ENCODER_TIM, &TIM_TimeBaseStructure);

  TIM_EncoderInterfaceConfig(ENCODER_TIM, TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Falling);

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_ICFilter = ENC_FILTER;//ICx_FILTER;

  TIM_ICInit(ENCODER_TIM, &TIM_ICInitStructure);

  ENCODER_TIM->CNT = 0;

  TIM_Cmd(ENCODER_TIM, ENABLE);

}

int main(void){

    void config(void);

}

________________

in the ''startup_stm32f10x_md_vl.c'' i write

// configuration

static void Default_Handler(void)

{

  /* Go into an infinite loop. */

  while (1) {

   int curCount= ENCODER_TIM->CNT; // i juste added this line in this file

  }}

#enoder-interface-handler
16 REPLIES 16
Posted on November 30, 2012 at 21:15

He's using TIM_GetCounter(TIM4); aka TIM4->CNT

It's a single register, it should be pretty apparent if it's counting up or down, or not.

Sit in a loop and see if it changes and printf() the new value, and repeat.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ahmed2399
Associate II
Posted on November 30, 2012 at 22:39

 

Posted on November 30, 2012 at 23:39

On the F1 series, input pulled up is

GPIO_InitStructure_Encoder.GPIO_Mode = GPIO_Mode_IPU;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
M0NKA
Senior
Posted on December 01, 2012 at 12:46

Here the working reading routine:

// Fir Selection public

__IO FirSelection        fs;

static void UiDriverUpdateFirSelection(void)

{

    fs.value_new = TIM_GetCounter(TIM4);

    // Ignore lower value flickr

    if(fs.value_new < ENCODER_FLICKR_BAND)

        return;

    // Ignore higher value flickr

    if(fs.value_new > (FIR_ENCODER_RANGE/FIR_ENCODER_LOG_D) + ENCODER_FLICKR_BAND)

        return;

    // No change, return

    if(fs.value_old == fs.value_new)

        return;

    //printf(''fir pot: %d\n\r'',fs.value_new);

    // Update filter

    control_whatever(ag.value_new - ENCODER_FLICKR_BAND);

    // Updated

    fs.value_old = fs.value_new;

}

void main()

{

// Init FIR pot

    fs.value_old             = 0;

    fs.value_new            = FIR_ENCODER_RANGE;

while(1)

{

UiDriverUpdateFirSelection();

}

}

// Protective band on top and bottom scale

// dependent on encoder quality and debounce

// capacitors

#define ENCODER_FLICKR_BAND    3

// --------------------------------

// Maximum pot value

#define FIR_ENCODER_RANGE    10

// Divider to create non linearity

#define FIR_ENCODER_LOG_D    1

// FIR selector public structure

typedef struct FirSelection

{

    // pot values

    //

    ulong    value_old;            // previous value

    ulong    value_new;            // most current value

} FirSelection;

So your controlled function will get a value from 0 - 10 based on encoder position.

When max value is reached - no call to the function will be send, same for lowest position.

Hope this helps.

ahmed2399
Associate II
Posted on December 01, 2012 at 14:08

so i will change the mode ..i found IPU mode and i will test again

aadityadengle
Associate II
Posted on December 03, 2014 at 16:47

@wolf_man

Can you put your final code here, Please?

Posted on December 03, 2014 at 19:49

This is a two year old thread, start a new one, state your specific situation, and pin connectivity. I know I've posted several, working, encode mode examples here on the forum.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..