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 29, 2012 at 17:25

Default_Handler() is a dumping point for otherwise unhandled interrupts.

For TIM3 you'd need to build

void TIM3_IRQHandler(void)

{

  // Clear Interrupt

  // Do work

}

And enable an interrupt in the timer and NVIC.

But why an interrupt, and at what interval? In Encoder mode a timer should count up/down without needing an interrupt.
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 29, 2012 at 20:55

Clive really I'm a beginer with STM32. I made projcts with pic and arduino.I know that the signals of encoder are a extern interrupt..so i should make configuration of timer3 to read TI1 and TI2 and enable counting up or down .

should I configure the NVIC ? if you  have ran an exemple can you post it.and thanks

Posted on November 29, 2012 at 21:31

I'm not using encoders.

You'll have to configure the NVIC if you want the timer to interrupt.

The timer can interrupt when it under/over flows, it's not interrupting for each pulse from the encoder which seems to be what you're inferring.

Again why do you need the interrupt, you can read the position in TIM3->CNT any time you want.

If you want the pins to interrupt, look at the EXTI.

There is a lot of example code in the firmware libraries, you should review it.

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  /* Enable the TIM3 gloabal Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

..

  /* TIM Interrupts enable */

  TIM_ITConfig(TIM3, TIM_IT_Update , ENABLE);

..

void TIM3_IRQHandler(void)

{

  /* Checks whether the TIM interrupt has occurred */

  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);

    // Overflows

  }

}

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 29, 2012 at 22:07

thanks for you answer CLIVE ,

Do mean that i can read the position of the encoder any time i want without inturrupt..so it's very well..but i have other task to do in the same time i should read my encoder position .

then i don't have to configure the NVIC.

so where is the pb in my code .?

Posted on November 29, 2012 at 22:35

so where is the pb in my code .?

dk nmp

Aren't PB6 and PB7 attached to TIM4? TIM4_CH1, TIM4_CH2, So TIM3 won't encode them.

If you want to sample the position on a 10 Hz interrupt, you could use SysTick, read TIM4->CNT and compute velocity

Putting infinite while loops in interrupt service routines will seize the cpu on the first interrupt.

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 29, 2012 at 23:45

thank you Clive for your help but i used PA6 and PA7. I will try again ...

Posted on November 29, 2012 at 23:58

i used PA6 and PA7

 

GPIO_Init(GPIOB,&GPIO_InitStructure_Encoder);

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 November 30, 2012 at 11:36

Hello,

It seems there is some stuff missing from your init, here my code:

TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;

    GPIO_InitTypeDef             GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = (FIR_ENCODER_RANGE/FIR_ENCODER_LOG_D) + (ENCODER_FLICKR_BAND*2);    // range + 3 + 3

    TIM_TimeBaseStructure.TIM_Prescaler = 0;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;

    TIM_EncoderInterfaceConfig(TIM4,TIM_EncoderMode_TI12,TIM_ICPolarity_Falling,TIM_ICPolarity_Falling);

    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    TIM_Cmd(TIM4, ENABLE);

---------------------------------------------------------------------------

It assumes the GPIO clock is enabled earlier in the code. No external pullups

needed, but 10 nF capacitors to GND on each line is needed. In this mode

the encoder works like absolute potentiometer, but there is some flickr band

at the bottom and top of the range, you need to exclude it in your reading routine, eg:

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;

In my case this is called from main loop on regular intervals.

BR

ahmed2399
Associate II
Posted on November 30, 2012 at 21:01

Thanks MONKA..for this help.

It seems like my new code that i made. do you make encoder runing ? do you read the

counter ? if yes can you attach your headers files and sources of this project, to make sure

that my encoder works.

i try many times ..but i can't read my couter..i look for somone who makes run encoders and read pulses.