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 support@ccsinfo.com

zero crossing detection code question

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



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

zero crossing detection code question
PostPosted: Tue Jun 30, 2015 5:23 am     Reply with quote

I would like to do zero crossing detection on a 50Hz signal (0.1V-3.1V) to fire triacs.
Would this code work?

Code:
#include <33EP512MU810.h>
#use delay(clock=4000000)
#byte PORTD=0xF81
#byte PORTB =0xF83

void init_all (void);
float32 adc;
int VCC=5;
             
void main() {
init_all();

WHILE (TRUE){
adc=(((float)read_adc()*VCC)/1023);
if (adc<1.63 && adc>1.57){
   delay_ms(1);
   output_high(PIN_D1);
   delay_us(60);
   output_low(PIN_D1);
   }
   }
}

void init_all(void){

set_tris_b(0xff);
set_tris_d(0x00);

setup_adc_ports( sAN0 | VSS_VDD );
setup_adc ( ADC_CLOCK_INTERNAL );
set_adc_channel ( 0 );
}


thanks
temtronic



Joined: 01 Jul 2010
Posts: 9196
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jun 30, 2015 7:04 am     Reply with quote

no.
some possible reasons

1) use of ADC_CLOCK_INTERNAL. Bad choice, read the adc section for more details.

2) use of float for ADC math result. HUGE delay in processing time. FP math should( must ?) be avoided especialyy in time critical events.

3) clock=4MHz. !! Way too slow

4) hardware. Normally you use a 'zero crossing opto coupler' to detect the zero cross.I think a MOC3011 is the part you should use.

5) zero cross should be an ISR, allowing PIC to do other stuff.

6) use of delay_ms() can easily miss the next zero cross.

That's just a quick answer....

Jay
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jun 30, 2015 8:59 am     Reply with quote

Your code is kind of a mess as is, but more important-
post your circuit schematic.

No way too know what you might be able to do without knowing
what you are connecting your code TO.
irmanao



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

PostPosted: Tue Jun 30, 2015 9:10 am     Reply with quote

I'm using a dspic33e usb starter kit

thanks for the replies
irmanao



Joined: 08 Apr 2015
Posts: 77

View user's profile Send private message

PostPosted: Tue Jun 30, 2015 10:24 am     Reply with quote

1. Isn't this enough for 50Hz? (ADC Internal RC Oscillator Period 250 ns)
3. How do i determine the correct one?
4. My goal is to use only software.
6. Period is 20ms, so the next zero cross could be missed because of clock=4000000?

Thank you for your time,
(i am new to programming)


Last edited by irmanao on Wed Jul 01, 2015 3:48 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19430

View user's profile Send private message

PostPosted: Tue Jun 30, 2015 12:50 pm     Reply with quote

First look at this thread:

<http://www.ccsinfo.com/forum/viewtopic.php?t=54087>

Then look at this one

<http://www.ccsinfo.com/forum/viewtopic.php?t=54077>

Within the last week, two important things already raised by Temtronic answered....

Realistically, to actually get a repeatable value using the ADC will be very hard. AC waveforms are not nice smooth sine waves. You will see spikes that will cross zero, that are nothing to do with the actual zero point of the waveform. To do this properly you would have to start working out where you are on the waveform, and work out when the smoothed curve crosses zero. A lot of quite complex maths. On any genuine AC waveform, a simple 'look for a particular value with the ADC', has no hope of working.....

This is why proper zero crossing detectors work by normally phase locking a synthetic waveform onto the incoming signal, and then using this to find the crossing point. Chips like the MOC3011 already mentioned will do a far better job, and far easier.

It is trying to use a screwdriver to drive a nail. Not the right tool for the job. It is possible to do, but only with much more complex code.
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