PIC18452 Analog Compartor

Discussion in 'Embedded Systems and Microcontrollers' started by poison3000, Nov 24, 2010.

  1. poison3000

    Thread Starter New Member

    Oct 3, 2010
    22
    0
    hello people !!

    in my project, I have two voltages of range 0V - 160mV to compare .. if they match MCU will follow a specific procedure .. other wise it ignore it and continue its ordinary job ..

    the program is a normal loop with delay of half sec , after half sec MCU will check V1 and V2, if they match in value , it will start a procedure reside out of the loop then back to main loop .. if they do not match , nothing will happen, just continue the loop ...

    sounds easy job ,eh ?

    PIC18452 has analog comparator feature and I do not know how to make use of it since there is no enough resources talks about it ! I use MikroC and it doesnt tell much about it ..

    does anybody have any idea about this ? please help
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Do you mean the 18F452?
    It doesn't have a comparator.
    If you only need to check every half second, just read 2 ADC pins instead.
     
  3. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Actually, he needs to use the ADC to compare the values since the analog comparator (if exists) cannot be used to check for equality between two signals.
     
  4. poison3000

    Thread Starter New Member

    Oct 3, 2010
    22
    0
    alright , so i will activate the ADC to read analog voltage from PORTA.0 and PORTA.1 , do a conversion to binary vlaue for each (its 10 bit ADC so i will have 1024 resolution ) , then in my main program loop I will compare the two binary vlaues every half sec for a matching vlaue ... sounds great plan !!!

    any body have a nice well organized source or sample code to activate the ADC and assign vlaues to variable (x & y) .. and is it possible to perform logical operation on binary variable [ if ( x == y) goto P1; ] where x = 0b0001010 and y 0b0001011 in such way ?

    please acknowledge
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Instead of doing a compare you should subtract the two results, and look at the difference. Then doing ADC you will always have some bit flickering in the least significant bits. You should also amplify your signal so it fits better to the ADC range. That will give you better resolution
     
  6. poison3000

    Thread Starter New Member

    Oct 3, 2010
    22
    0
    well ... so far the problem is as follows:
    > I will have voltage A and voltage B , both in range of 0 to 160 mV
    > I will amplify both voltages (for better resolution results , PIC 18F452 can sense voltage from 0 to 5V )
    > in the main loop ; convert voltage A to 10 bit value and store it in a variable (a),
    > NEXT in the main loop ; convert voltage B to 10 bit value and store it in a variable (b),
    >NEXT in the main loop ; compare a & b , if they equal do somthing , otherwise continue the loop.

    i still dont know how to use the ADC ... this is the main issue !!
    secondary issue is , how to have variable like (a) and (b) that store 10 bits ?
     
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    If you are using C or basic, they handle 16 bit numbers for you, if you are using assembler have a look here:
    http://www.mcmanis.com/chuck/robotics/projects/pic-16bit.htm
    Using the ADC isn't too hard, it is all explained quite well in the datasheet. Have a go or find some sample code and modify it.
     
  8. poison3000

    Thread Starter New Member

    Oct 3, 2010
    22
    0
    I made this program ... MikroC compile it with no error ... yet I cant get it to work with the simulation ..

    ==============================

    void main()
    {
    unsigned long ARSSI, BRSSI;
    TRISA = 1; // PORTA is input
    TRISB = 0; // PORTB is output
    ADCON0 = 0x80; // Configure analog inputs and Vref
    for (; ; ) {
    PORTB = 0x01;
    Delay_ms(1000);
    PORTB = 0x00;
    ARSSI = Adc_Read (0);
    BRSSI = Adc_Read (1);
    if (ARSSI == BRSSI) PORTB = 0x02;
    Delay_ms(1000);
    PORTB = 0x00;
    Delay_ms(500);
    }
    }

    ================================================

    can somebody test the code for me, please ?
     
  9. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    You need to include a delay of about 10us between the ADC Reads:

    ARSSI = Adc_Read (0);
    delay_us(10);
    BRSSI = Adc_Read (1);

    This code may be never executed since the input reading may vary by one bit due to noise.

    if (ARSSI == BRSSI) PORTB = 0x02;

    It would be better to find RSSI=abs(ARSSI-BRSSI) and then
    if(RSSI<x) PORTB=0x02;

    x is a value which determines how close the values of the two inputs have to be as to be assumed as equal.
     
  10. poison3000

    Thread Starter New Member

    Oct 3, 2010
    22
    0
    alright , i think its a better idea to have the difference ... i belive its not possible to test the code using the debugger, so maybe a test on the simulation will do the job but i dont know how to setup the circuit properly ...

    how to setup the circuit on ISIS ? can u please help on this ?
     
  11. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    I am not the one who can help you on this. :p
     
  12. poison3000

    Thread Starter New Member

    Oct 3, 2010
    22
    0
    hey man , I appreciate ur help .. i did make a circuit , it WORKS !!!!! well , I was not aware of the leds voltage .. which almost drove me crazy .. then changed them to "forward voltage = 2.5" and "driving current = 50mV" .. finally it worked .. sigh , small things can ruin ur focus !

    I have couple of questions to ask regarding the code ..

    > What does the code ADCON0 = 0x80; really do ? why I had to assign that value to it ?

    > the ADC is 10 bit , hence I will expect 1024 resolution , how do I set the range to be divided into 1024 step ? in my case I want the range 0 - 160mV .. (I know its a small value , i will amplify it as Im working on it now) ..

    > what does the code RSSI = abs(ARSSI-BRSSI); do ? is RSSI an integer ?
     
  13. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    If your LEDs are standard 5mm LEDs, you should drop your driving current to 20mA or less.

    Less than 20mA means longer life.

    At 50mA, you may be replacing it by the end of the week.

    Your LEDs should have a datasheet. In that sheet the forward voltage and current will be listed.

    If you guessed at the forward voltage and current, you should definitely drop the current.

    If you can find the datasheet, do it. The proper forward voltage and current will keep from smoking your circuit.
     
Loading...