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 } }
#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
}
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.
#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 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.
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
#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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?