Analog comparator of AT89C2051

Discussion in 'Embedded Systems and Microcontrollers' started by shubham11, Feb 12, 2013.

  1. shubham11

    Thread Starter New Member

    Feb 12, 2013
    5
    0
    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..
     
  2. GetDeviceInfo

    Senior Member

    Jun 7, 2009
    1,571
    230
    how much money you got?
     
  3. KrisBlueNZ

    Member

    Oct 17, 2012
    111
    14
    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.
     
  4. shubham11

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1. #include<AT89x51.h>
    2.  
    3. sbit in1=P1^0; // analog i/p 1
    4. sbit in2=P1^1;  // analog i/p 2
    5. sbit out=P3^6;  // output
    6. sbit led=P3^7;  // led
    7.  
    8. main()
    9. {
    10.     in1=in2=out=1;
    11.     in1=3.2; //analog input value 1 (don't know it's right way or not)
    12.     in2=2.3; // analog input value 2
    13.  
    14.     while(1)
    15.     {
    16.     if(out==1)
    17.     {
    18.         led=1;  // led  on
    19.     }
    20.     else
    21.         led=0;  // led off
    22. }
    23.         return 0;
    24. }
    but nothing is working..
     
  5. KrisBlueNZ

    Member

    Oct 17, 2012
    111
    14
    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: Feb 14, 2013
  6. shubham11

    Thread Starter New Member

    Feb 12, 2013
    5
    0
  7. KrisBlueNZ

    Member

    Oct 17, 2012
    111
    14
    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?
     
  8. KrisBlueNZ

    Member

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

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

    Code ( (Unknown Language)):
    1. include <AT89x51.h>
    2.  
    3. sbit comp_in1 = 0x90;    // P1.0 = comparator input 1
    4. sbit comp_in2 = 0x91;    // P1.1 = comparator input 2
    5. sbit comp_out = 0xB6;   // P3.6 = comparator output (read-only)
    6. sbit led_drive = 0xB7;   // P3.7 = output that drives the LED; low to illuminate
    7. // The above values are bit-number addresses in the bit-addressable SFRs.
    8.  
    9. void main(void) {
    10.      comp_in1 = comp_in2 = comp_out = 1; // Set digital I/O registers for comparator connections high (high-impedance)
    11.  
    12.   while (1) {
    13.     if (comp_out)
    14.       led_drive = 0;   // Drive the LED ON
    15.     else
    16.       led_drive = 1;   // Drive the LED OFF
    17.     }//while(1)
    18.   }//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.
     
  9. shubham11

    Thread Starter New Member

    Feb 12, 2013
    5
    0
    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 :

    Code ( (Unknown Language)):
    1. in1=3.2; //analog input value 1 (don't know it's right way or not)
    2.     in2=2.3; // analog input value 2
    Thanks a lot for the help !!!:):)
     
Loading...