Analog comparator of AT89C2051

Thread Starter

shubham11

Joined Feb 12, 2013
5
Hello,
I am a student and currently working with AT89C2051. I'm new to this microcontroller. I,m using it in a circuit in which I need to compare a variable analog input to reference voltage using analog comparator of AT89C2051. I need to perform some operations in accordance with the logical output of comaprator. But I've stuck on it and don't know how to use this analog comparator. Please provide me the complete details/ C-code to perform this ASAP..
 

KrisBlueNZ

Joined Oct 17, 2012
111
But I've stuck on it and don't know how to use this analog comparator. Please provide me the complete details/ C-code to perform this ASAP..
And your post was looking so good up until there.
The people who contribute to this forum are volunteers. Volunteers without magic wands and without crystal balls.
Why would anyone try to help you when you seem to have made very little effort yourself? You say you can't understand the comparator and you want someone to do all your work for you, without even defining exactly what that work should be.

Explain exactly what you're trying to do.
Try to figure it out yourself.
Post the code that you've created, and a good description of what doesn't work.
Post a schematic of the hardware.
Post a link to the data sheet for the device.

Don't expect everyone to do all your work for you. Make an effort to make it as easy as possible for us to help, and we will try.
 

Thread Starter

shubham11

Joined Feb 12, 2013
5
Here is something I've tried :

Rich (BB code):
#include<AT89x51.h>

sbit in1=P1^0; // analog i/p 1
sbit in2=P1^1;	// analog i/p 2
sbit out=P3^6;	// output
sbit led=P3^7;	// led

main()
{
	in1=in2=out=1;
	in1=3.2; //analog input value 1 (don't know it's right way or not)
	in2=2.3; // analog input value 2

	while(1)
	{
	if(out==1)
	{
		led=1;	// led 	on
	}
	else
		led=0;	// led off
}
		return 0;
}
but nothing is working..
 

KrisBlueNZ

Joined Oct 17, 2012
111
OK, that's a start. I was hoping you would follow up on all of my suggestions, not just one of them.

The analog comparator output is readable as bit 6 of port 3. So you just need a loop that tests the state of P3 bit 6 and sets the LED output to the same state.

What compiler are you using? That's not standard C syntax. In standard C, the "^" operator means bitwise XOR; it doesn't specify a bit number.

When you read the value of bit 6 of an I/O port, it will be either 0x00 or 0x40. It will never be 1.
And what's the meaning of assigning floating point values ("3.2" and "2.3") to in1 and in2?
 
Last edited:

KrisBlueNZ

Joined Oct 17, 2012
111
You can't put analogue voltages on those pins by assigning floating point numbers to them in software.

The only way to use the analogue comparator is to provide the two analogue voltages externally. You need to connect two analogue voltages to pins 12 and 13 of the device.

That's why I asked you to post a schematic diagram. That mistake would have been obvious.

You did say you're using it in a circuit, not using it with the Keil simulator, right?

Can you give me a link to something that describes the Keil compiler's special syntax features? Like "sbit" and the special use of the "^" operator?
 

KrisBlueNZ

Joined Oct 17, 2012
111
Never mind, I found the answer.

It looks like you're not using sbit properly. Try this.

Rich (BB code):
include <AT89x51.h>

sbit comp_in1 = 0x90;    // P1.0 = comparator input 1
sbit comp_in2 = 0x91;    // P1.1 = comparator input 2
sbit comp_out = 0xB6;   // P3.6 = comparator output (read-only)
sbit led_drive = 0xB7;   // P3.7 = output that drives the LED; low to illuminate
// The above values are bit-number addresses in the bit-addressable SFRs.

void main(void) {
     comp_in1 = comp_in2 = comp_out = 1; // Set digital I/O registers for comparator connections high (high-impedance)

  while (1) {
    if (comp_out)
      led_drive = 0;   // Drive the LED ON
    else
      led_drive = 1;   // Drive the LED OFF
    }//while(1)
  }//main
Now you need some hardware that provides two analogue voltages on pins 12 and 13 of the AT89C2051, and you need to connect an LED with a series resistor between VCC and pin 11.
 

Thread Starter

shubham11

Joined Feb 12, 2013
5
This is working perfectly and I have even simulated it in software..

Even, the code I posted has also started working after the removal of thses lines from that code :

Rich (BB code):
in1=3.2; //analog input value 1 (don't know it's right way or not)
	in2=2.3; // analog input value 2
Thanks a lot for the help !!!:):)
 
Top