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.

[Moved]: PIC 16F676 C CODE for PHASE DETECTOR

Status
Not open for further replies.

saptagiriicafe

Newbie level 5
Joined
Jul 3, 2015
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
93
Sir, i am trying to detect a three phases lines i.e which phase is available or not by using pic16f676 .(three phases like R, Y & B Phases individually connected to pic16f676 micro controller pins RA0,RA1 & RA2 respectively) using series of 2.2M ohm 1/2 watt+5M ohm 1/4 watt to PIC pin RA0 and 10K ohm connected to ground & PIC RA0.

if R,Y & B phases (Three phase present) all are available RC0 pin connected(Three_phase) LED will Glow ON
if only R phase is available RC1 pin connected(Single_phase) LED will Glow ON
if R & B phases are available & Y phase failure RC2 pin connected (Two_phase)LED will Glow ON

i don't want digital conversion only phase is available OR not so how to detect these phases are available or not for C code ...

i am initialize like this

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define R_phase RA0
#define Y_phase RA1
#define B_phase RA2
 
#define Three_phase_LED RC0
#define Two_phase_LED RC1
#define Single_phase_LED RC2
#define NO_phase_LED RC3
void main()
{
CMCON=0x07;
ANSEL=0x00; 
TRISA =0b001111;
PORTA=0b001111;
TRISC=0b000000;
PORTC=0b00000;
 
while(R_phase&&Y_phase&&B_phase)
{
Three_phase_LED = 1; ///RC0 pin connected(Three_phase) LED will Glow ON
}
 
while(R_phase&&B_phase)
{
Two_phase_LED = 1; ///RC1 pin connected(Two_phase) LED will Glow ON
}
while(R_phase||Y_phase||B_phase)
{
Single_phase_LED = 1; ///RC2 pin connected(Single_phase) LED will Glow ON
}
while(!R_phase&&!Y_phase&&!B_phase)
{
NO_phase_LED = 1; ///RC3 pin connected(No_phase) LED will Glow ON
}
}



I have tried this concept but only NO_phase_LED = 1; LED Glow ON

finally i want detect AC mains either three/two/single phase available please let me know the program concept

thanking with u...
 
Last edited by a moderator:

It would seem like if-else statements would work better than while loops to test the phases. Then the LED's could be turned off. All three phases should be tested for each of the conditions, When testing for a single phase, each condition needs to be tested individually.

Here is some sample code to look at:

Code:
#define R_phase RA0
#define Y_phase RA1
#define B_phase RA2
 
#define Three_phase_LED RC0
#define Two_phase_LED RC1
#define Single_phase_LED RC2
#define NO_phase_LED RC3
void main()
{
  CMCON=0x07;
  ANSEL=0x00; 
  TRISA =0b001111;
  PORTA=0b001111;
  TRISC=0b000000;
  PORTC=0b00000;

  while(1) // while loop to test phases continously
    {
    // test for all phases on
  
    if(R_phase && Y_phase && B_phase) 
       {
       Three_phase_LED = 1;  // RC0 pin connected(Three_phase) LED will Glow ON
       }
    else
       Three_phase_LED = 0;

    // test for phase R and B on ------------------------------------------------

    if(R_phase && B_phase && !Y_phase)
       {
       Two_phase_LED = 1;    // RC1 pin connected(Two_phase) LED will Glow ON
       }
    else
       Two_phase_LED = 0; 

   // test for a single phase on ------------------------------------------------- 

    if(R_phase || !Y_phase || !B_phase) // only R_phase is on
       {
       Single_phase_LED = 1; // RC2 pin connected(Single_phase) LED will Glow ON
       }
    else
       Single_phase_LED = 0; 

    if(!R_phase || Y_phase || !B_phase) // only Y_phase is on
       {
       Single_phase_LED = 1; // RC2 pin connected(Single_phase) LED will Glow ON
       }
    else
       Single_phase_LED = 0; 

   if(!R_phase || !Y_phase || B_phase)  / only B_phase is on
       {
       Single_phase_LED = 1; // RC2 pin connected(Single_phase) LED will Glow ON
       }
    else
       Single_phase_LED = 0; 

    // test for no phase on ------------------------------------------------------
   
    if(!R_phase && !Y_phase && !B_phase)
       {
       NO_phase_LED = 1;    //RC3 pin connected(No_phase) LED will Glow ON
       }
    else
       NO_phase_LED = 0;

   }   // end of while 1
}

- - - Updated - - -

From the circuit description you give , there is not enough information about the voltage characteristics of the phases you are trying to measure to know if you can get a meaningfull reading at RA0. With a total of 7.2 meg ohms between the phase and RA0, and 10k between RA0 and ground, it would seem that there would only be a fraction of a volt at RA0. The 10k resistor would pull RA0 to near ground.

If you are detecting alternating current, a circuit like the one below might be needed to get a positive direct current voltage to detect at RA0.

phase_test.jpg
 

Thanks for reply...
Sir, what have u suggested the if else statement is correct but if these pins like R_phase,Y_phase,B_phase are always considered as zero all the time will always these pins zero ithink its not considered one..i.e is problem.....
 

Yes, the input voltage always being zero is a problem.

To get the input voltage needed, the voltage divider resistors need to be adjusted to the proper values.
I still do not know what the voltage value of the phases you are trying to measure is, or whether it is A/C or D/C, so, I can't advise further on the circuit needed.
The input to RA0 etc. needs to be a stable, positive D/C voltage sufficient to indicate a logic "1" , when the phase is present, and a logic "0" when the phase is absent.
 

thank u sir. for giving advice...
Yes, the coming input voltage AC supply, so i have measure digital DC mode i.e 3.5 dc volts. its not work in these position but i have connected DC 3.0 volts its working good...

thanking with u.....

- - - Updated - - -

Yes, the input analog voltage AC supply, i did not convert DC volts so i have measure digital multi meter in DC mode i.e one point for RA0 and another point circuit GND is 3.5 dc volts. its not work in these position but i have connected DC 3.0 volts its working good... otherwise give me an ADC programe

thanking with u.....
 

The circuit I posted above will convert the A/C to +D/C so it can be read at the input. The diode only lets the positive portion of the A/C wave through. This charges the capacitor to an average voltage of the positive pulses coming through the diode. The capacitor needs to be large enough to filter out the voltage ripple. This way, whenever the input is read, there will be a positive voltage present if the phase is present. The voltage divider resistors will have to be adjusted to get the proper voltage for a logic 1 at the input. The capacitor will charge to a D/C voltage that is lower than the RMS A/C reading would be for the voltage divider without the diode and capacitor.

- - - Updated - - -

A software approach to reading the phases without converting the A/C to D/C would be to set up a for loop to sample the PORTA inputs for 20 milliseconds or so, and test each sample for a logic 1 on a phase, setting a flag variable for each phase if a 1 was detected. After the loop is done, you could turn the led's on or off according to the flag's settings, then clear the flags and start running the 20 millisecond sample loop again.

Using this approach a diode should still be used, as in the circuit above, to keep the input voltage from going below 0 volts, but the capacitor would not be used.
 
Last edited:

The circuit I posted above will convert the A/C to +D/C so it can be read at the input. The diode only lets the positive portion of the A/C wave through. This charges the capacitor to an average voltage of the positive pulses coming through the diode. The capacitor needs to be large enough to filter out the voltage ripple. This way, whenever the input is read, there will be a positive voltage present if the phase is present. The voltage divider resistors will have to be adjusted to get the proper voltage for a logic 1 at the input. The capacitor will charge to a D/C voltage that is lower than the RMS A/C reading would be for the voltage divider without the diode and capacitor.

- - - Updated - - -

A software approach to reading the phases without converting the A/C to D/C would be to set up a for loop to sample the PORTA inputs for 20 milliseconds or so, and test each sample for a logic 1 on a phase, setting a flag variable for each phase if a 1 was detected. After the loop is done, you could turn the led's on or off according to the flag's settings, then clear the flags and start running the 20 millisecond sample loop again.

Using this approach a diode should still be used, as in the circuit above, to keep the input voltage from going below 0 volts, but the capacitor would not be used.


Thanks for giving reply...sir

please suggest for how to setup a for loop to PORTA i/ps for 20 miliseconds weather phase is coming or not for logic 1 on phase. and also setting flag variable for each phase i dont know the which flag u suggested please send me some sample program c code...
 

Below is an idea for code to test 3.5 volts A/C at the inputs. The code is untested, so it may have bugs and it may or may not work, it just represents an idea for an algorythm. The clock speed of your 16F676 is unknown, so the duration of the "for loop" in the example to test the phases is unknown. It is set by the value given to "sample". It just needs to run for at least 20 milliseconds to be able to sample a complete (50 Hz??) wave of the 3.5 A/C voltage at the input. (1 divided by 50 Hz = 20 milliseconds) If any sample sees a positive voltage, the flag variable for that phase is set to one. Since the A/C voltage is continuously varying between +3.5 volts to zero volts to - 3.5 volts, only a sample taken when the voltage happens to be high enough to represent a logic 1 will set the flag to 1. After the loop is done, the program tests the flags to control the phase indicator LED's.

Code:
#define R_phase RA0
#define Y_phase RA1
#define B_phase RA2
 
#define Three_phase_LED RC0
#define Two_phase_LED RC1
#define Single_phase_LED RC2
#define NO_phase_LED RC3

char phaseRflag = 0;  // flag variables for each phase
char phaseYflag = 0;
char phaseBflag = 0;

char single_phase_flag = 0;  // used for single phase detection test

unsigned int sample = 0; // used to set "for loop" duration

void main()
{
  CMCON=0x07;
  ANSEL=0x00; 
  TRISA =0b001111;
  PORTA=0b001111;
  TRISC=0b000000;
  PORTC=0b00000;

  while(1) // while loop to test phases continously
    {
    phaseRflag = 0;  // phase flags are set to zero
    phaseYflag = 0;
    phaseBflag = 0;
   
   // a "for loop" tests the inputs for 20 milliseconds, setting a flag if a logic 1 is read

    for(sample = 65000; sample > 0; sample--)  // test all phases for at least 20 milliseconds
        {
        if(R_phase)        // if a 1 is detected on a phase set a flag to 1;
           phaseRflag = 1;
        if(Y_phase)
           phaseYflag = 1;   
        if(B_phase)
           phaseBflag = 1; 
        }                          // end of the  for loop   

    // phase tests running as before, except now looking at the flag settings
    // test for all phases on ------------------------------------------------------
  
    if(phaseRflag && phaseYflag && phaseBflag) 
       Three_phase_LED = 1;  // RC0 pin connected(Three_phase) LED will Glow ON
    else
       Three_phase_LED = 0;

    // test for phase R and B on ------------------------------------------------

    if(phaseRflag && phaseBflag && !phaseYflag)
        Two_phase_LED = 1;    // RC1 pin connected(Two_phase) LED will Glow ON
    else
        Two_phase_LED = 0; 

    // test for a single phase on ------------------------------------------------- 

    single_phase_flag = 0;  // set if only a single phase was detected
 
    if(phaseRflag || !phaseYflag || !phaseBflag)  // only R_phase is on
         single_phase_flag = 1;  
   
    if(!phaseRflag || phaseYflag || !phaseBflag)  // only Y_phase is on
         single_phase_flag = 1; 
   
    if(!phaseRflag || !phaseYflag || phaseBflag)  // only B_phase is on
         single_phase_flag = 1; 
   
    if(single_phase_flag == 1)   // if  a single phase was detected
         Single_phase_LED = 1;   // RC2 pin connected(Single_phase) LED will Glow ON
    else
         Single_phase_LED = 0;   // single phase LED off
    
    // test for no phases on ------------------------------------------------------
   
    if(!phaseRflag && !phaseYflag && !phaseBflag)
       NO_phase_LED = 1;    // RC3 pin connected(No_phase) LED will Glow On    
    else
       NO_phase_LED = 0;
   }   // end of while loop
}      // end of main

If A/C is applied to an input, a diode should be used to protect it from negative voltage.

diode.jpg
 

thank for instant reply ..sir,

i will try this concept but i am using internal oscillator 4Mhz clock speed. and also you have sent circuit using diode but some one used without diode its phase detecting without using diode only for resistor divider circuits they detecting only for software alternation so any way thank u so much..
 

If your clock is 4 Mhz, you can reduce the value of "sample" from 65000 in the example code to 16000 or so. The value given was large in case the clock was 20Mhz.
 

If your clock is 4 Mhz, you can reduce the value of "sample" from 65000 in the example code to 16000 or so. The value given was large in case the clock was 20Mhz.

thanking wit u...ir
i was tried but still i cant able detect to phase is present /absent (logic 1/0) i am using /defined like :
#define _XTAL_FREQ 4000000
__CONFIG(0x3FF4)
shall i change this configuration for detecting AC analog input volt...

if you have any idea for zero crossing find AC analog i/p phase
 

Three phase ac analog inputs sequence detect/ phase failure finding

Dear Sir/Madam please give any idea/concept for AC Phase detecting either Present/Absent (Logic 1/0)without converting DC volt to detect TTL PIC16F676 microcontroller someone already implemented this products but know i am also implementing this concept on same circuits but I have dont know the logic/ C code for this know I am tried as follows but still i cant able to detecting phase

three phase (RY & B) AC analog inputs directly connected microcontroller pin without converting DC volts / reduced the voltage by using resistor divider circuit series with of 2.2Mohm 0.5watt+5Mohm 0.25watt connected to microcontroller(PIC16F676 pins RA0,RA1 & RA2). and internal oscillator like


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define _XTAl_FREQ 4000000
__CONFIG(0x3FF4)
#define R_phase RA0
#define Y_phase RA1
#define B_phase RA2
 
#define Three_phase_LED RC0
#define Two_phase_LED RC1
#define Single_phase_LED RC2
#define NO_phase_LED RC3
 
char phaseRflag = 0;  // flag variables for each phase
char phaseYflag = 0;
char phaseBflag = 0;
 
char single_phase_flag = 0;  // used for single phase detection test
 
unsigned int sample = 0; // used to set "for loop" duration
 
void main()
{
  CMCON=0x07;
  ANSEL=0x00; 
  TRISA =0b001111;
  PORTA=0b001111;
  TRISC=0b000000;
  PORTC=0b00000;
 
  while(1) // while loop to test phases continously
    {
    phaseRflag = 0;  // phase flags are set to zero
    phaseYflag = 0;
    phaseBflag = 0;
   
   // a "for loop" tests the inputs for 20 milliseconds, setting a flag if a logic 1 is read
 
    for(sample = 16000; sample > 0; sample--)  // test all phases for at least 20 milliseconds
        {
        if(R_phase)        // if a 1 is detected on a phase set a flag to 1;
           phaseRflag = 1;
        if(Y_phase)
           phaseYflag = 1;   
        if(B_phase)
           phaseBflag = 1; 
        }                          // end of the  for loop   
 
    // phase tests running as before, except now looking at the flag settings
    // test for all phases on ------------------------------------------------------
  
    if(phaseRflag && phaseYflag && phaseBflag) 
       Three_phase_LED = 1;  // RC0 pin connected(Three_phase) LED will Glow ON
    else
       Three_phase_LED = 0;
 
    // test for phase R and B on ------------------------------------------------
 
    if(phaseRflag && phaseBflag && !phaseYflag)
        Two_phase_LED = 1;    // RC1 pin connected(Two_phase) LED will Glow ON
    else
        Two_phase_LED = 0; 
 
    // test for a single phase on ------------------------------------------------- 
 
    single_phase_flag = 0;  // set if only a single phase was detected
 
    if(phaseRflag || !phaseYflag || !phaseBflag)  // only R_phase is on
         single_phase_flag = 1;  
   
    if(!phaseRflag || phaseYflag || !phaseBflag)  // only Y_phase is on
         single_phase_flag = 1; 
   
    if(!phaseRflag || !phaseYflag || phaseBflag)  // only B_phase is on
         single_phase_flag = 1; 
   
    if(single_phase_flag == 1)   // if  a single phase was detected
         Single_phase_LED = 1;   // RC2 pin connected(Single_phase) LED will Glow ON
    else
         Single_phase_LED = 0;   // single phase LED off
    
    // test for no phases on ------------------------------------------------------
   
    if(!phaseRflag && !phaseYflag && !phaseBflag)
       NO_phase_LED = 1;    // RC3 pin connected(No_phase) LED will Glow On    
    else
       NO_phase_LED = 0;
   }   // end of while loop
}      // end of main


In this pregame only
NO_phase_LED = 1; LED is On always....


thanks for advances
 
Last edited by a moderator:

Re: Three phase ac analog inputs sequence detect/ phase failure finding

The code below was tested on a PIC16f876a. I don't have a PIC16F676 to test the code with.

You said that others were working ok with a/c at the inputs, so, I though you had answered that question to your satisfaction.

The code below was tested with 3.3 volts a/c, as measured with a meter, at the input, and it seemed to work OK.

Code:
#define _XTAl_FREQ 4000000
__CONFIG(0x3FF4)
#define R_phase RA0
#define Y_phase RA1
#define B_phase RA2
 
#define Three_phase_LED RC0
#define Two_phase_LED RC1
#define Single_phase_LED RC2
#define NO_phase_LED RC3
 
char phaseRflag = 0;  // flag variables for each phase
char phaseYflag = 0;
char phaseBflag = 0;
 
char single_phase_flag = 0;  // used for single phase detection test
 
unsigned int sample = 0; // used to set "for loop" duration
 
void main()
{
  CMCON=0x07;
  ANSEL=0x00; 
  TRISA =0b001111;
  PORTA=0b001111;
  TRISC=0b000000;
  PORTC=0b00000;
 
  while(1) // while loop to test phases continously
    {
    phaseRflag = 0;  // phase flags are set to zero
    phaseYflag = 0;
    phaseBflag = 0;
   
   // a "for loop" tests the inputs for 20 milliseconds, setting a flag if a logic 1 is read
 
    for(sample = 16000; sample > 0; sample--)  // test all phases for at least 20 milliseconds
        {
        if(R_phase)        // if a 1 is detected on a phase set a flag to 1;
           phaseRflag = 1;
        if(Y_phase)
           phaseYflag = 1;   
        if(B_phase)
           phaseBflag = 1; 
        }                          // end of the  for loop   
 
    // phase tests running as before, except now looking at the flag settings
    // test for all phases on ------------------------------------------------------
  
    if( (phaseRflag == 1) && (phaseYflag == 1) && (phaseBflag == 1) ) 
       Three_phase_LED = 1;  // RC0 pin connected(Three_phase) LED will Glow ON
    else
       Three_phase_LED = 0;

    // test for phase R and B on --------------------------------------------------

    if( (phaseRflag == 1) && (phaseBflag == 1) && (phaseYflag == 0) )
        Two_phase_LED = 1;    // RC1 pin connected(Two_phase) LED will Glow ON
    else
        Two_phase_LED = 0; 

    // test for a single phase on ------------------------------------------------- 

    single_phase_flag = 0;  // set if only a single phase detected
 
    if(( phaseRflag == 1) && (phaseYflag == 0) && (phaseBflag == 0) )  // only R_phase is on
        single_phase_flag = 1;  
   
    if(( phaseRflag == 0) && (phaseYflag == 1) && (phaseBflag == 0) )  // only R_phase is on
        single_phase_flag = 1; 
   
    if(( phaseRflag == 0) && (phaseYflag == 0) && (phaseBflag == 1) )  // only R_phase is on
        single_phase_flag = 1; 
   
    if(single_phase_flag == 1)   // if  a single phase was detected
         Single_phase_LED = 1;   // RC2 pin connected(Single_phase) LED will Glow ON
    else
         Single_phase_LED = 0;   // single phase LED off
    
    // test for no phases on ------------------------------------------------------
   
    if((phaseRflag == 0) && (phaseYflag == 0) && (phaseBflag == 0) )
       NO_phase_LED = 1;    // RC3 pin connected(No_phase) LED will Glow On    
    else
       NO_phase_LED = 0;
   }   // end of while loop
}      // end of main
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top