View previous topic :: View next topic |
Author |
Message |
leaveme
Joined: 24 Nov 2010 Posts: 19
|
Is my comparator setup wrong? |
Posted: Wed Jul 31, 2013 11:49 pm |
|
|
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: 19510
|
|
Posted: Thu Aug 01, 2013 3:41 am |
|
|
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
|
|
Posted: Thu Aug 01, 2013 6:07 am |
|
|
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: 19510
|
|
Posted: Thu Aug 01, 2013 9:02 am |
|
|
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
|
|
Posted: Fri Aug 02, 2013 2:41 am |
|
|
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: 19510
|
|
Posted: Fri Aug 02, 2013 8:24 am |
|
|
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
|
|
Posted: Fri Aug 02, 2013 1:12 pm |
|
|
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
|
|
Posted: Fri Aug 02, 2013 4:07 pm |
|
|
PCM programmer wrote: | lol |
made my day |
|
|
|