Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Double resolution of a rotary encoder

Status
Not open for further replies.

mdUJ

Newbie level 6
Joined
Nov 28, 2010
Messages
12
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Location
Germany
Activity points
1,376
Hello,

years ago I made a program to control an inverted pendulum. To increase the system resolution for better control, I doubled the encoder resolution by software. Here is the code. It is written for ATmega88 - may be someone can use it.

Code:
// Port and Pin definitions for Rotary Encoder
#define RE_SIGNAL_90_DDR (DDRD)
#define RE_SIGNAL_90_PRT (PORTD)
#define RE_SIGNAL_90_PIN (PIND)
#define RE_SIGNAL_90_BIT (PD3)

#define RE_GET_SIGNAL_90() (RE_SIGNAL_90_PIN & RE_SIGNAL_90_BIT)
//-----------------------------------------------------------------------------

S32 volatile re_count; // Signed 32-bit variable
//-----------------------------------------------------------------------------

void re_init(void)
{
  // Set INT0 as input and enable pullup
  DDRD &= ~PD2;
  PORTD |= PD2;

  // Set second Rotary Encoder signal as input and enable pullup
  RE_SIGNAL_90_DDR &= RE_SIGNAL_90_BIT;
  RE_SIGNAL_90_PRT |= RE_SIGNAL_90_BIT;

  // Falling edge of INT0 generates an interrupt
  EICRA |= ISC01;

  // Clear INT0 interrupt flag
  EIFR |= INTF0;

  // Enable INT0 interrupt
  EIMSK |= INT0;
}
//-----------------------------------------------------------------------------

interrupt[EXT_INT0] void int0_isr(void)
{
  // Interrupt triggered by rising edge
  if (PIND & PD2)
  {
    if (RE_GET_SIGNAL_90()) // If second signal is high
    {
      re_count++;
    }
    else // If second signal is low
    {
      re_count--;
    }

    // Set falling edge to trigger INT0 interrupt
    EICRA &= ~ISC00;
  }
  else // Interrupt triggered by falling edge
  {
    if (RE_GET_SIGNAL_90()) // If second signal is high
    {
      re_count--;
    }
    else // If second signal is low
    {
      re_count++;
    }

    // Set rising edge to trigger INT0 interrupt
    EICRA |= ISC00;
  }
}
 
Last edited:

iabdim

Newbie level 4
Joined
Jun 16, 2007
Messages
6
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,395
Thank you very much for this favor. kindly can you let me know the schmetic diagram of same .

BAiG
 

mdUJ

Newbie level 6
Joined
Nov 28, 2010
Messages
12
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Location
Germany
Activity points
1,376
Hi,

there is no need for any schematics. A rotary encoder has normally 2 outputs.
Let´s call it S0 and S90. S90 is 90 degree shifted in phase related to S0.

Connect S0 to PD2 and S90 to PD3. You can also connect S90 to PD2 and S0 to PD3,
but then the sense of rotation will change.

Many rotary encoders have open drain outputs, you need pull-up resistors to get valid signals.

Regards

Udo
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top