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

Zero Crossing detector

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







Zero Crossing detector
PostPosted: Tue Feb 27, 2007 10:36 am     Reply with quote

I'm trying to learn how to use the Pic's comparator. I want to do a zero crossing detector for a sine wave. Isn't that supose to work , I'm setting A0 and A3 as C1- and C1+ , and A1 and A2 as C2+ and C2-, And I connected the pins A0 and A1 to a 2 sines waves , and A3 and A2 to the ground. I though that when the sine cross the zero , pins A4 and A5 will change their states.

#include <18f4520.h>
#Use delay(clock=4000000)
#fuses NOWDT , XT , PUT ,NOPROTECT , NOBROWNOUT , NOLVP , NOCPD ,NOWRT, NODEBUG
main()
{
while(1)
{
setup_comparator(A0_A3_A1_A2);
enable_interrupts(INT_COMP);
enable_interrupts(GLOBAL);
}
}
Ttelmah
Guest







PostPosted: Tue Feb 27, 2007 11:00 am     Reply with quote

You need to generate an 'artificial' zero. Basically, you need an analog zero, between the two PIC supply rails. The comparator inputs (just like those on the rest of the PIC), have clamp diodes preventing them going more than a tiny fraction of a volt below the chip's Vss.
You must have the interrupt handler present, before you enable an interrupt (otherwise, where is the code to go...).
Look at ex_comp.c

Best Wishes
Phill
Guest







PostPosted: Wed Feb 28, 2007 2:11 pm     Reply with quote

setup_comparator(A1_VR_OUT_ON_A2); // sets two comparators(A1 and VR and A2 as the output)
setup_vref(VREF_HIGH|15);

when the ex put setup_comparator(A1_VR_OUT_ON_A2); that means that the A1 it's the input and the VR its the comparison set in the setup_vref(VREF_HIGH|15) , 3,6V?. So to compare with zero I need to put setup_vref(VREF_Low|0)?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 28, 2007 4:52 pm     Reply with quote

Add a DC offset to your sine wave and set the Comparator Vref at
that voltage.

See Figure 6 on page 14 of Microchip appnote AN823:
http://ww1.microchip.com/downloads/en/AppNotes/00823a.pdf
It shows how to add a positive DC offset to a signal, so that you
can shift the midpoint of the signal into a suitable voltage for
the comparator (such as +2.5v for a PIC running at +5v).
The diagram shows that you can do this with just two resistors.


You didn't give any specs on your input sine wave (amplitude, frequency,
existing DC offset), so I don't know if the above advice will work.
If you need more help, then give that information.
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