Analog Comparator

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

hi guys,

i hoped that i would understand a bit more about AVR and C after 2weeks practicing with it, guess not :(.+

i'm working on a STK500 with Codevision programmed in a ATmega16L.

i have a voltage regulator connect to the AIN1 and change the voltage lower then the bandgap to higher, so i can see the change in leds.

here is my code:

#include 

// Declare your global variables here

void main(void)
{


    ACSR=0x40;
    SFIOR=0x00;

    DDRB = 0x00;   //  We put port A pins in output mode  
    PORTB = 0xFF;   // put all leds in off state 
    DDRD = 0xFF;    // We put port D pins in input mode  
    PORTD = 0xFF; //  we activate the internal pull-up 

while (1)
      {
      if(ACSR & (1 << 6)) // if ACO is set then
        {
        PORTD.0 = 1;
        PORTD.1 = 0;
        }
      else
        {
        PORTD.0 = 0;
        PORTD.1 = 1; 
        }
      

      };
}

i don't know what i'm doing wrong, hopefully we can work it out.

i measured the voltage on AIN0 and that is 5v, when i read the datasheet it should be 2.56. i'm getting confused a bit :s

thanks

Robbin

Last Edited: Mon. Oct 22, 2007 - 03:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It looks OK. What voltages are being applied to PB3/AIN1? [You >>are<< applying the internal pullup to the AIN1 pin so You have to over-drive that signal to get below the bandgap voltage.]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. Oct 22, 2007 - 03:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your comments are wrong, they don't match what you are trying to do.

Leon

Leon Heller G1HSM

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

owh sorry about the comments, they are old and from an other project, my mistake.

    DDRB = 0x00;   //  We put port B pins in output mode  
    PORTB = 0x00;   // no internal pull-up

i think that this is correct.

when i measure the bandgab again i get only 0.6v. led0 is burning, this means that ain1 is higher then ain0 cause ACO is set, but when i change the value nothing happens.

Robbin

Last Edited: Tue. Oct 23, 2007 - 11:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
DDRB = 0x00; // We put port A pins in output mode

DDRB is for port B, not port A

Klaus
********************************
Look at: www.megausb.de (German)
********************************

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

Your pins are configured as inputs on Port B, and you have your LEDs connected to Port D!

Try reading the data sheet.

Leon

Leon Heller G1HSM

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

i have re-writen the comments, hopefully it's oke now.


#include 

// Declare your global variables here

void main(void)
{


    ACSR=0x40;  // bandgab reference is set
    SFIOR=0x00;

    DDRB = 0x00;   //  We put port B pins in input mode  
    PORTB = 0x00;   // no internal pull-up
    DDRD = 0xFF;    // We put port D pins in output mode  
    PORTD = 0xFF; //  we activate the internal pull-up 

while (1)
      {
      if(ACSR & (1 << 6)) // if ACO is set then
        {
        PORTD.0 = 0;   //burn led
        PORTD.1 = 1;
        }
      else
        {
        PORTD.0 = 1;
        PORTD.1 = 0;   //burn led
        }
      

      };
}

if i look at the result of this code, then ACO is always 1, cause led0 is burning. when i increase the voltage on AIN1 nothing happens.

what can be the problem?

Thanks Robbin

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

I think you have to set the appropriate bits in the DIDR register to enable the comparator inputs, they default to digital inputs.

Leon

Leon Heller G1HSM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
      if(ACSR & (1 << 6)) // if ACO is set then 

Here's your problem. ACO is bit number 5, not 6.

Quote:

I think you have to set the appropriate bits in the DIDR register to enable the comparator inputs, they default to digital inputs.

You don't have to do that, though it is good practice to disable the digital portion if you're using the pin as comparator input.

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

mrono wrote:
Quote:

I think you have to set the appropriate bits in the DIDR register to enable the comparator inputs, they default to digital inputs.

You don't have to do that, though it is good practice to disable the digital portion if you're using the pin as comparator input.

Can you guys elaborate on how you setup the pin as a "comparator" input? The data sheet says

Quote:

• AIN1/OC0 – Port B, Bit 3
AIN1, Analog Comparator Negative Input. Configure the port pin as input with the internal pull-up
switched off to avoid the digital port function from interfering with the function of the analog
comparator.
• AIN0/INT2 – Port B, Bit 2
AIN0, Analog Comparator Positive input. Configure the port pin as input with the internal pull-up
switched off to avoid the digital port function from interfering with the function of the Analog
Comparator.

Are you referring "comparator" input as simply disabling the pull-up? (ie PORTB.2 & PORTB.3 = 0) The pins still have to be configured as "input" pins...yes?

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

Quote:

Can you guys elaborate on how you setup the pin as a "comparator" input?

There's no specific setup needed. As the datasheet says, you should just make sure that the digital side is not interfering. Configuring the pins as inputs and disabling the pullups are the obvious things to do, and usually that is also sufficient.

Many recent AVR models also have a DIDR register, from which you can completely disable the digital functionality of selected pins. So if you are using some pins as comparator inputs or adc inputs, you can disable the digital side of those pins. The benefit of doing this is that the digital inputs might switch rapidly if your adc input is close to vcc/2, creating noise and consuming power.

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

mrono wrote:
...So if you are using some pins as comparator inputs or adc inputs, you can disable the digital side of those pins. The benefit of doing this is that the digital inputs might switch rapidly if your adc input is close to vcc/2, creating noise and consuming power.

Ah, brilliant! The Atmega16 doesn't have a DIDR or the ability to "disable" digital inputs though which is what was confusing me. But that's good stuff to know for the other devices that are out that I might work with someday.

Thanks!

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

Hi...maybe it's too late to join this forum.This is my first post(as u can see).
I have a question 'bout analog comparator of atmega16.Is that okay if pin AIN0 wired to the output of sensor valued about 11.84 volt??And if i use external reference voltage about 11.80 volt wired to pin AIN1, is that okay too??

Thanks :D

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

Quote:
Is that okay if pin AIN0 wired to the output of sensor valued about 11.84 volt??

No.
Quote:
And if i use external reference voltage about 11.80 volt wired to pin AIN1, is that okay too??

No.

See the "Absolute Maximum Ratings" section of your datasheet.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

thanks for the information. I use voltage divider then..
it's work :D