View previous topic :: View next topic |
Author |
Message |
lindoudaM2
Joined: 09 Oct 2012 Posts: 13 Location: Tunisie
|
zero crossing detect code |
Posted: Tue Oct 09, 2012 6:09 am |
|
|
My code doesn't work well. I have a problem in synchronisation. Help me please.
here is my code
#include <16F877a.h>
#use delay(clock=1000000)
#fuses XT, NOWDT, NOPROTECT, PUT, NOLVP
#define zero_cross pin_B0;
byte x;
void init ()
{
setup_adc_ports(RA0_analog);
setup_adc(adc_clock_div_2);
set_adc_Channel(0);
setup_ccp1(ccp_pwm) ;
setup_timer_2 (T2_Div_by_1,255,1);
setup_timer_1(T1_internal);
enable_interrupts(int_timer1);
set_tris_b(0xff);
output_b(0xff);
}
#int_ext
void ext_isr()
{
if(input (pin_b0) )
ext_int_edge(H_to_L);
else
ext_int_edge(L_to_H);
}
void main()
{
init();
while (1)
{
delay_ms (10);
x=read_adc();
set_pwm1_Duty(x);
delay_ms (10);
}
} |
|
![](templates/subSilver/images/spacer.gif) |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Oct 09, 2012 7:04 am |
|
|
There are a lot of problems with this code.
First, let's put right the formatting:
Code: |
#include <16F877a.h>
// 16F877a eh? Probably a student or hobbyist project....
#use delay(clock=1000000)
// 1MHz is very slow. Are you sure you don't mean 10MHz?
#fuses XT, NOWDT, NOPROTECT, PUT, NOLVP
#define zero_cross pin_B0;
// Good: you define a meaningful name for this pin.
// Bad: you never use this define. If you did it wouldn't work
// because you've put a semicolon (;) at the end.
byte x;
void init ()
{
setup_adc_ports(RA0_analog);
// CCS is by default NOT case-sensitive: it doesn't care about the
// difference between RA0_ANALOG and RA0_analog. C normally IS
// case-sensitive and DOES care. The correct defines are all in upper
// case. RA0_ANALOG is an old define provided for backwards
// compatibility with old code. You should be using the newer
// version instead.
setup_adc(adc_clock_div_2);
// Again should be upper case.
set_adc_Channel(0);
setup_ccp1(ccp_pwm) ;
setup_timer_2 (T2_Div_by_1,255,1);
setup_timer_1(T1_internal);
// You don't use either of these timers. Why set them up?
enable_interrupts(int_timer1);
// NEVER enable interrupts on a device without defining a valid ISR
// to handle them. In this case it's not a big problem as you never
// enable global interrupts and therefore there will be no interrupts
// at all.
set_tris_b(0xff);
// You are not using fast_io, therefore this will not work and...
output_b(0xff);
// This will set all bits pf port b to be outputs, and set them to high. No inputs will work on this port.
}
// This ISR wont ever run as global interrupts are not enabled and
// the external interrupts, INT_EXT is not enabled.
#int_ext
void ext_isr()
{
// Why not use the define you did for this pin (which is wrong due to the ;)
if(input (pin_b0) )
ext_int_edge(H_to_L);
else
ext_int_edge(L_to_H);
}
void main()
{
init();
while (1)
{
delay_ms (10);
// Question: do interrupts interrupt delays? You need to
// find out.
x=read_adc();
// The ADC on this PIC is 10bit and needs a 16 bit int. x is
// a byte, i.e. an 8 bit unsigned int.
set_pwm1_Duty(x);
// Setting PWM duty is not simple. You need to know what
// you are doing. Will this code really do what you want,
// considering the ADC is 10 bit, not 8 bit.
delay_ms (10);
}
}
|
RF Developer |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Oct 09, 2012 7:43 am |
|
|
the latency and uncertainty of the entire approach may not be suitable - based on what you want to DO .
how little error - and how small a zero deviation can your design tolerate?
an external - latched hardware zero detector may be required .
if you want - say - a low jitter Triac trigger...... |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Oct 09, 2012 8:55 am |
|
|
Microchip app. note AN521 shows you how to do zero crossing detect.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Oct 09, 2012 9:42 am |
|
|
Be warned: the method shown in the APP note can be subject to asymmetric positive and negative half cycles - and is ONLY suitable for low frequencies - ie mains frequencies at best.
I tried and abandoned it for a design some time ago.
Second: selecting a pic with a comparator and using a negative clamped
input provides a less ambiguous way of zero cross detection ( than ADC operation ) and can have low overhead when using the 16f887, comparator #1 or #2 - which are both INTERRUPT capable.
I think you will find that the 887 will be pin for pin interchangeable
in your design and MUCH simpler than what you have been trying to do.
I first used this approach for stable, low jitter zero cross detection several years ago, in a design that works with 50 or 60 Hz line frequencies. |
|
![](templates/subSilver/images/spacer.gif) |
|