Iam writing some code in C (WinAVR) for an M328 at 8MHz and I want to test the Analog Comparator output twice in a row and increment a counter if ACO is high each time.
Here is the code that WinAVR created.
260: if (ACSR & _BV(ACO)) acCounter++; //if ACO is high +0000059C: B780 IN R24,0x30 In from I/O location +0000059D: E090 LDI R25,0x00 Load immediate +0000059E: 9596 LSR R25 Logical shift right +0000059F: 9587 ROR R24 Rotate right through carry +000005A0: 9592 SWAP R25 Swap nibbles +000005A1: 9582 SWAP R24 Swap nibbles +000005A2: 708F ANDI R24,0x0F Logical AND with immediate +000005A3: 2789 EOR R24,R25 Exclusive OR +000005A4: 709F ANDI R25,0x0F Logical AND with immediate +000005A5: 2789 EOR R24,R25 Exclusive OR +000005A6: 2F28 MOV R18,R24 Copy register +000005A7: 7021 ANDI R18,0x01 Logical AND with immediate 261: if (ACSR & _BV(ACO)) acCounter++; //if ACO is high +000005A8: B600 IN R0,0x30 In from I/O location +000005A9: FC05 SBRC R0,5 Skip if bit in register cleared +000005AA: 5F2F SUBI R18,0xFF Subtract immediate
This seems to me to be rather complicated, and it takes something like 20 clock cycles in the simulator. Is this as simple as it can be? It strikes me as excessively complicated code! I don't know ASM in the AVR, and have not written ASM on anything in decades. I'd like to get as many tests of ACO done in a 5uS window worst case. Code is compiled "-o3" optimization level.