Show Posts
Pages: [1]
1  Development / Suggestions for the Arduino Project / Re: % Division in USART interrupt code... on: January 05, 2012, 09:37:13 pm
If divider is power of 2, why not use & ?
Only worth this kind of optimization in cases where serial performance was the bottleneck.

I try to do simple audio recorder from the arduino uno (just for fun and training).
I programmed Timer2 for generate interrupts at 16KHz (every 62.5uS) and set ADC prescaler to 64
(so, ADC conversion must take 52uS). But sometymes, ADC don't have to complete previous conversion, when the next interrupt occurs.

I think, it is because other interrupts brings a time jitter to time, when Timer2 interrupt handler raise...

Thank you, for your quick response!
2  Development / Suggestions for the Arduino Project / Re: % Division in USART interrupt code... on: January 05, 2012, 09:16:33 pm
Hm... Very strange.
Yes, your code do not generate any divisions.

But when a compile simple sketch:
void setup() {
  Serial.begin(9600);
}
void loop() {}

and do (on Windows):
c:\BIN\Arduino\arduino-1.0\hardware\tools\avr\bin\avr-objdump.exe -d test123.cpp.elf

i get:
 .....
00000162 <__vector_19>
 162:   1f 92          push   r1
 164:   0f 92          push   r0
 .......
 1bc:   01 96          adiw   r24, 0x01   ; 1
 1be:   60 e4          ldi   r22, 0x40   ; 64
 1c0:   70 e0          ldi   r23, 0x00   ; 0
 1c2:   0e 94 34 03    call   0x668   ; 0x668 <__divmodhi4>
 ....

Maybe, this code is from another place... I don't know assembler wery well...
3  Development / Suggestions for the Arduino Project / % Division in USART interrupt code... on: January 05, 2012, 04:28:07 pm
Function store_char() (in HardwareSerial.cpp), called from USART interrupt, use % division, which is mutch slower (208 ticks, as i measure), than & operation. If divider is power of 2, why not use & ?

For example:

#define SERIAL_BUFFER_SIZE  64
.....

int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
are equal to:
int i = (unsigned int)(buffer->head + 1) & (SERIAL_BUFFER_SIZE - 1);
Pages: [1]