Analog Comparator no response

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0



I try to use Analog Comparator on ATTiny461A. The voltage of the AIN1 changes according to below table.

 

                       I/P         Normal       Left        Right

AIN0 (Pin 12)  NEG          2.5v         2.5V       2.5V

AIN1 (Pin 11)  POS           2.5V        2.1V       2.6V

AC0                                                  0           1

 

I wish to compare AIN1 to AIN0. If AIN1 voltage more than AIN0, AC0 to be set.  

 

I try to get below code work, but there isnt any response on the output pin. i attached the datasheet table. 

 

 

 


#include <avr/io.h>

void port_init()
{
	DDRA |= (1<<PINA6) | (1<<PINA7); //AIN0 AND AIN1 AS INPUT
	DDRB |= (1<<PINB4)| (1<<PINB6);//OUTPUT PINS
}

void AC_Init(){
	ACSRA = 0b00000000;
	ACSRB = 0b00000010; //ain0 as negative input, ain1 as positive

}


int main(void)
{
	port_init();
	AC_Init();
	while (1)
	{
		if ((ACSRA & 0X20) == 1)
		{
			
			PORTB |= (1 << PINB6);  //PINB6 HIGH
			PORTB &= ~(1 << PINB4);  //PINB4 LOW
		}
		else
		{
			PORTB &= ~(1 << PINB6);  //PINB6 LOW
			PORTB |=  (1 << PINB4);  //PINB4 HIGH
		}

	}
}

 

Is there anything else I need to set on the ACSRA, ACSRB register or ADCSRA register ?

 

I have try set ADEN to zero, it doesnt work too.  Any help is much appreciated. 

 

 

 

This topic has a solution.
Last Edited: Sat. May 21, 2022 - 07:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on.

		if ((ACSRA & 0X20) == 1)

can NEVER happen.

 

If you want to test (ACSRA & 0x20) you

either say      if ((ACSRA & 0x20) == 0x20)

or use this     if ((ACSRA & 0x20) != 0)

 

Personally I just say     if (ACSRA & 0x20)

because I am an old 6502 programmer.   But modern C compilers give a warning.

 

No I have not studied your actual code.   The Analog Comparator is pretty simple.   Just read the datasheet.

Oh,  and use proper bit-masks instead of a magic 0x20

 

David.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should use ACI and/or ACIE.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you mean have to use ISR and the test of the ACO bit have to be located inside the ISR vector ? i will try all and updates. 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You never need to use an ISR().

 

Set AIN0, AIN1 as inputs.   Enable ACME / ADEN and monitor the ACI flag.   Read the ACO output bit.

 

But you could ignore ACI and just act on ACO bit.

 

It should be pretty straightforward.

 

David.

Last Edited: Thu. May 19, 2022 - 02:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bits ACI, ACO and Interrupt are described in AVR128.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 

OK. I got it working. Thank you very much all.