CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Is my comparator setup wrong?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
leaveme



Joined: 24 Nov 2010
Posts: 19

View user's profile Send private message

Is my comparator setup wrong?
PostPosted: Wed Jul 31, 2013 11:49 pm     Reply with quote

I need to use comparator 1 with internal Vref (connected to A3). Comparator should output 1 if the input voltage exceeds at pin A0. I used a pot at PIN A0 and an LED connected to C1 for testing purposes.
The problem I'm having is, LED starts blinking if A0 exceeds reference voltage. My expection is the LED should be stady ON or OFF. Kindly advice about what I'm doing wrong.

Code:
/*
Compiler: CCS C 5.010

---------|     |----+5v
       A0|----POT
         |     |----GND
         |
         |
       C1|---|>|---/\/\--GND
         |
---------
18F2420
*/

#include <18F2420.h>
#fuses HS, NOMCLR, NOWDT, NOPROTECT, NOBROWNOUT, NOPUT, NOLVP, NOPBADEN
#use delay(clock=8000000)


void main()
{
   output_float(PIN_A0);  //set as input
   output_low(PIN_C1);    //turn led off initially
   
   setup_comparator(A0_A3_NC_NC_OUT_ON_A4 | CP1_INVERT); //CM2:CM0 = 001, CMCON=0000 0001
   setup_vref(VREF_LOW | 10 | VREF_F5);                  //Vref=2.08V
   delay_us(20);

   while(TRUE) {
      if(C1OUT == 1) output_high(PIN_C1); 
      else output_low(PIN_C1);
      delay_ms(500);
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19512

View user's profile Send private message

PostPosted: Thu Aug 01, 2013 3:41 am     Reply with quote

What smoothing have you got adjacent to the PIC?.
With no hysteresis, the chip will be very sensitive to supply noise. I'd suspect you are getting a spike on the supply rail, when the LED switches.

Best Wishes
leaveme



Joined: 24 Nov 2010
Posts: 19

View user's profile Send private message

PostPosted: Thu Aug 01, 2013 6:07 am     Reply with quote

I never used PIC comparator before and due to the problem I'm a bit confused whether I did it correctly or not.

So the problem is not with the setup, it is from the power or somewhere else.
Ttelmah



Joined: 11 Mar 2010
Posts: 19512

View user's profile Send private message

PostPosted: Thu Aug 01, 2013 9:02 am     Reply with quote

There are some errors/oddities.
Read the data sheet again for what the output voltage will be with VREF_LOW selected.
Why route the Vref output to F5, or use A3?. Wasting pins.
However none would give the behaviour you are seeing. What would, would be if the Vref changes when the LED turns on. Since the Vref is based on the supply, this is the obvious place to look.

Best Wishes
leaveme



Joined: 24 Nov 2010
Posts: 19

View user's profile Send private message

PostPosted: Fri Aug 02, 2013 2:41 am     Reply with quote

Thanks Ttelmah. My lab PSU is very stable and well filtered. Don't really understand why my comparator behaves abnormal.

Quote:
Why route the Vref output to F5, or use A3?. Wasting pins.

Your point is valid. However, these are the prototypes I find in 18F2420.h file and there is no VREF_A3.

Code:
// Comparator Prototypes:
_bif void setup_comparator(int32 mode);
// Constants used in setup_comparator() are:
#define A0_A3_A1_A3  0xfff04
#define A0_A3_A1_A2_OUT_ON_A4_A5  0xfcf03
#define A0_A3_A1_A3_OUT_ON_A4_A5  0xbcf05
#define NC_NC_NC_NC  0x0ff07
#define A0_A3_A1_A2  0xfff02
#define A0_A3_NC_NC_OUT_ON_A4  0x9ef01
#define A0_VR_A1_VR 0x3ff06
#define A3_VR_A2_VR 0xcff0e
#define CP1_INVERT  0x0000010
#define CP2_INVERT  0x0000020

#bit C1OUT = 0xfb4.6     
#bit C2OUT = 0xfb4.7     

////////////////////////////////////////////////////////////////// VREF
// VREF Prototypes:
_bif void setup_vref(int8 mode);
// Constants used in setup_vref() are:
#define VREF_LOW  0xa0
#define VREF_HIGH 0x80
// Or (with |) the above with a number 0-15
#define VREF_F5   0x40
#define VREF_COMP 0x10
Ttelmah



Joined: 11 Mar 2010
Posts: 19512

View user's profile Send private message

PostPosted: Fri Aug 02, 2013 8:24 am     Reply with quote

Even with a super lab PSU, you need _local_ decoupling close to the CPU. The wires to the CPU, can easily have a fraction of an ohm resistance. When loads change at the CPU, the result is small voltage spikes.

Study a PC motherboard. Huge supply, rated at many tens of amps. Large capacitances round the switchers to deliver the rails, Thick PCB tracks, yet there are decoupling capacitors typically at about every second chip on the board.....

Also, there is no such setting as 'VREF_A3'....

The point is that the Vref can be fed directly to the comparator input, _or_ the comparator can use the A3 input.

The setting:
A0_VR_A1_VR

Says to just use A0, for comparator 1, and A1 for comparator 2, with the second input from each fed directly from the Vref, without using any other pins (F3, or A3)

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 02, 2013 1:12 pm     Reply with quote

Here's a program I did last night that works. I didn't post it because I
didn't want people to see how late I stay up sometimes, lol. I changed
it a bit from your code, because I don't have your exact PIC and I'm using
a PicDem2-Plus to test it. But it's basically the same.
Code:

#include <18F2620.h>
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP, NOPBADEN
#use delay(clock=20M)

#define LED_PIN  PIN_B0

//==============================
void main()
{
output_low(LED_PIN);    // Turn led off initially

setup_comparator(A0_VR_A1_VR | CP1_INVERT);
setup_vref(VREF_LOW | 10);  // Vref=2.08V
delay_us(20);

while(1)
  {
   if(C1OUT == 1)
      output_high(LED_PIN); 
   else
     output_low(LED_PIN);
 
   delay_ms(500);
  }
}
stinky



Joined: 05 Mar 2012
Posts: 99
Location: Central Illinois

View user's profile Send private message

PostPosted: Fri Aug 02, 2013 4:07 pm     Reply with quote

PCM programmer wrote:
lol


made my day
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group