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 detect code

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



Joined: 09 Oct 2012
Posts: 13
Location: Tunisie

View user's profile Send private message AIM Address

zero crossing detect code
PostPosted: Tue Oct 09, 2012 6:09 am     Reply with quote

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);
}
}
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue Oct 09, 2012 7:04 am     Reply with quote

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
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Tue Oct 09, 2012 7:43 am     Reply with quote

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......
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Oct 09, 2012 8:55 am     Reply with quote

Microchip app. note AN521 shows you how to do zero crossing detect.

Mike
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Tue Oct 09, 2012 9:42 am     Reply with quote

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.
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