Fast encoder read - don't understand asm produced


i'm trying code fast quadrature encoder read, divides input (luckily) multiple of 2 , preserves quadrature timing.

quote

[font=courier new][size=11]/*rotary encoder*/

/*fast read of quadrature encoder no interrupts
 thanks to oleg at circuits@home for original code
 http://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino */

#define enc_a 14 //encoder a-pulse
#define enc_b 15 //encoder b-pulse
#define tied_to_ground1 16 //these pins have low time. set them outputs
#define tied_to_ground2 17 //and send low. maybe tie ground via 1k resistor.
#define enc_port pinc //use portc

void setup()
{
  nointerrupts(); //interupts off
  pinmode(enc_a, input); //set a-input pin
  digitalwrite(enc_a, high); //enable pull-up
  pinmode(enc_b, input); //set b-input pin
  digitalwrite(enc_b, high); //enable pull-up
  pinmode(tied_to_ground1, output); //set unused pin output
  digitalwrite(tied_to_ground1, low); //send low
  pinmode(tied_to_ground2, output); //set unused pin output
  digitalwrite(tied_to_ground2, low); //send low
}

void loop()
{
  const static int8_t enc_states[] = {
    0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0  }; //const static initialised once only
  static int8_t counter = 0; //static initialised once only
  static uint8_t old_ab = 0; //static initialised once only
  old_ab <<= 2; //store previous state of encoder channels in bits 2 , 3
  old_ab += enc_port; //store current state.
  counter+=enc_states[(old_ab & 0x0f)]; //increment counter value returned encoder read
}
[/size][/font]


the encoder have 1024 ppr, running @ 3200rpm max. means have 75 instructions @ 16mhz in work.

disassembling code gives following:-

code: [select]
; using 8 bit counter

 const static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
 static int8_t counter = 0;
 static uint8_t old_ab = 0;
 old_ab <<= 2;
 old_ab += enc_port;

100:      86 b1             in      r24, 0x06      ; 6  ;[1] load r24 pinc (at memory 0x06)
102:      e0 91 10 01       lds      r30, 0x0110          ;[2] load r30 old_ab (at memory 0x0110)
106:      ee 0f             add      r30, r30             ;[1] left shift
108:      ee 0f             add      r30, r30             ;[1] left shift again
10a:      e8 0f             add      r30, r24             ;[1] add pinc old_ab
10c:      e0 93 10 01       sts      0x0110, r30          ;[2] store in old_ab

 counter+=enc_states[(old_ab & 0x0f)];

110:      f0 e0             ldi      r31, 0x00      ; 0  ;[1] load r31 0x00
112:      ef 70             andi      r30, 0x0f      ; 15 ;[1] , r30 (old_ab) 0xff


114:      f0 70             andi      r31, 0x00      ; 0  ;[1]
116:      e0 50             subi      r30, 0x00      ; 0  ;[1]
118:      ff 4f             sbci      r31, 0xff      ; 255;[1]  


11a:      80 91 11 01       lds      r24, 0x0111          ;[2] load r24 counter (at memory 0x0111)
11e:      90 81             ld      r25, z               ;[1] load r18 contents of data address pointed z
120:      89 0f             add      r24, r25             ;[1] add r25 r24 store in r24.
122:      80 93 11 01       sts      0x0111, r24          ;[2] store r24 in 0x0111
}
126:      08 95             ret                          ;[4]

; 23 instructions


which @ 23 instructions quite good, wonder if it's possible better.

i not understand 114, 116 & 118 doing...

thanks.


looks me code "sign extension".  i suspect compiler promoting 8 bit value ("old_ab & 0x0f" perhaps) 16 bit value (probably "int").

i suggest intermediate expression may force compiler's hand...

 old_ab += enc_port; //store current state.
 uint8_t index = (old_ab & 0x0f);
 counter+=enc_states[index]; //increment counter value returned encoder read



Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Fast encoder read - don't understand asm produced


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums