I've been trying to make a comparator in AVR using Proteus 8 with an Arduino ATMega 328P. I've been told that the comparison is practically made by the Arduino, so I just need to get the value from the location, but I haven't been able to access it. I've tried with LDI, SBI and IN/OUT instructions, but haven't worked.
Here's the code I've right now
;When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin AIN1, the Analog Comparator output, ACO, is set.
;------Preps--------
LDI R16,0xFF ;16 inFF
LDI R17,0x00 ;17 in00
LDI R18,0x80
LDI R19,0x40
LDI R21,0x20
OUT DDRC,R16 ;C out
;CBR ACSR,0x40 ;Clean the 6th ACSR bit to get the AIN0 input
STS ADCSRA,R18 ;Set the 7th ADCSRA bit (ADEN) to get AIN1 input
STS ADCSRB,R19 ;Set the 6th ADCSRB bit (ACME) to get AIN1 input
;CBR ACSR,0x0D ;Clean 0,1 and 3 in ACSR to obtain toggle with ACO and disable the interruption
OUT ACSR,R21 ;Clean ACSR register
;----------------------------------------------------*
;----------------------------------------------------*
loop:
NOP
NOP
IN R20, ACSR
CPI R20,0x80 ;Compare registers r1 and r0
BREQ aco1 ;If equal
RJMP aco0 ;IFNE aco0
aco1:
OUT PORTC,R16 ;Turns on the led
RJMP loop ;Goes back to verify if ACO changed
aco0:
OUT PORTC,R17 ;Turns off the led
RJMP loop ;Goes back to verify is ACO changed
I can't configure the inputs because I can't store at ADCSRA/B