Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Poll zero-crossing point of AC

Status
Not open for further replies.

swapan

Full Member level 4
Joined
Feb 20, 2009
Messages
199
Helped
27
Reputation
54
Reaction score
24
Trophy points
1,298
Location
Kolkata
Activity points
2,806
Hi Guys,

I am not well conversant to C. Please see the code in assembly. I want to poll zero-crossing point of AC Main. Assume that positive-going zero-crossing pulse is polled by RA0 of PIC 16F676. What will be the code in C compiled by mikroC ?

swapan

Code:
# define  zero_pulse    PORTA, 0;

BTFSC     zero_pulse;
Goto         $-1;   
BTFSS     zero_pulse;
Goto         $-1
                       ;  
………….      ; Rest of code
 
Last edited by a moderator:

Re: Help to a newbie in mikroC.

Use INT0 to detect Zero Crossing.
 

Re: Help to a newbie in mikroC.

Assume that positive-going zero-crossing pulse is polled by RA0 of PIC 16F676. What will be the code in C compiled by mikroC ?

# define zero_pulse PORTA, 0;

BTFSC zero_pulse;
Goto $-1;
BTFSS zero_pulse;
Goto $-1
; ………….
; Rest of code

Code:
sbit zero_pulse at PORTA.B0; // or RA0_bit

while (  zero_pulse ); // wait for a falling edge
while ( !zero_pulse ); // wait for a raising edge
//....

But - in my opinion - this is a negative going zero-crossing pulse
 

Re: Help to a newbie in mikroC.

Code:
sbit zero_pulse at PORTA.B0; // or RA0_bit

while (  zero_pulse ); // wait for a falling edge
while ( !zero_pulse ); // wait for a raising edge
//....

But - in my opinion - this is a negative going zero-crossing pulse

Thanks zuisti for your instant reply. This piece of code I have seen in certain post in this forum. I tried this piece of code to test it. But the circuit did not work. Though I defined the zero_pulse as:

sbit zero_pulse at RA0_bit;

Is it the reason of its non-functioning?

swapan
 

Re: Help to a newbie in mikroC.

Do you switched off the analogue functions of RA0 pin?
Do you switched it to input? (TRISA.B0 = 1;)
I don't know the 16F676 but maybe this is the problem.
Study the datasheet.
 

Re: Help to a newbie in mikroC.

Yes, zuisti. RA0 has been selected as Digital I/O and set the bit to Input - setting RRISA.B0. The entire code is as under. Sorry the cod is not commented. Please see the coloured portion. without this piece of code, the circuit works as usual. when the portion is included, the circuit does not work.

Code:
sbit RLY_1 at RC4_bit;
 sbit RLY_2 at RC5_bit;
 sbit RLY_3 at RA4_bit;
 sbit RLY_4 at RA5_bit;
 sbit null at RA0_bit;
 volatile unsigned char testflag;
 sbit normal at testflag.B1;
 # define INTAP_LOW 0
 # define INTAP_MED 1
 # define INTAP_HIGH 2
 
 #define OUTTAP_LOW 0
 # define OUTTAP_HIGH 1
 unsigned char INPUT_TAPPING;
 unsigned char OUTPUT_TAPPING;
 
 void Init(void){
PORTA = 0x0;
TRISA = 0x1;
PORTC = 0x0;
TRISC = 0x2;
ANSEL = 0x20;
CMCON = 0x7;
}

 void StabilizeVoltage(void) {
unsigned int adv, new_adv,adv_old, hysteriasis,  count;
adv = ADC_Read (5);

if (adv < 300) {
normal = 0;
INPUT_TAPPING = INTAP_LOW;
OUTPUT_TAPPING = OUTTAP_LOW;
adv_old = adv;              
               }

else if (adv > 1020) {
normal = 0;
INPUT_TAPPING = INTAP_HIGH;
OUTPUT_TAPPING = OUTTAP_HIGH;
adv_old = adv;                       
                     }

else    {
normal  =1;


if  (adv < adv_old) {
hysteriasis = adv_old - adv;
                    }
else
hysteriasis = adv - adv_old;

if (hysteriasis >= 25){
if (adv < 360) {
INPUT_TAPPING = INTAP_LOW;
OUTPUT_TAPPING = OUTTAP_LOW;
adv_old = adv;
               }
else if (adv<490 ) {
INPUT_TAPPING = INTAP_LOW;
OUTPUT_TAPPING = OUTTAP_HIGH;
adv_old = adv;
              }
else if (adv<649 ) {
INPUT_TAPPING = INTAP_MED;
OUTPUT_TAPPING = OUTTAP_LOW;
adv_old = adv;
                    }
 else  if (adv<969 ) {
INPUT_TAPPING = INTAP_MED;
OUTPUT_TAPPING = OUTTAP_HIGH;
adv_old = adv;
                      }
else if (adv<1020 )  {
INPUT_TAPPING = INTAP_HIGH;
OUTPUT_TAPPING = OUTTAP_LOW;
adv_old = adv;
                     }
else   {
INPUT_TAPPING = INTAP_HIGH;
OUTPUT_TAPPING = OUTTAP_HIGH;
adv_old = adv;
       }
               }

         }
[COLOR="#FF0000"]while (null);
while (!null);
delay_us(5000);[/COLOR]
 if (INPUT_TAPPING == INTAP_LOW ){

 RLY_1 =0;
 RLY_2 =0;
                               }
 else if ( INPUT_TAPPING == INTAP_MED)  {
 RLY_1 =0;
 RLY_2 =1;
                                      }
 else    {
 RLY_1 =1;
 RLY_2 =1;
         }

if (OUTPUT_TAPPING == OUTTAP_HIGH) {
RLY_3  = 1;
                                  }
else     {
RLY_3  = 0;
         }

if (normal == 1) {
RLY_4 = 1;
                }
else      {

RLY_4 = 0;

          }

  }

void main() {
 Init();
 while(1){
 StabilizeVoltage();
         }

}
 

Re: Help to a newbie in mikroC.

The coloured code-snippet simply waits for a negative pulse on RA0, ie the default RA0 level is high, it waits (even forever !) for the low level, then waits for high (end of pulse).
If the pulse is missing or has bad (not enough high and/or low) level then the program will hang here.
When your pulse is the opposite (positive) in the reality then simply swap the coloured while ... lines.
Examine your hardware (especially the pulse) using a scope.
Or first write a much smaller 'debug' program to examine (and display, even with a LED) the state of the RA0 pin (controlled via a jumper with pullup).
Good luck...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top