Code done! (needs a cleanup and perhaps writing of functions to make more compact, but working)
Problem occurred while writing code that had me chasing ghosts.... My test light GP5, to simulate activation of the relay on the board, stayed lit under certain situations. Turns out that U3 Allegro chip was outputting 5v instead of 1/2Vcc - 2.5vdc at no load. I am not quite sure why that is. If I have voltage present between U2's sensing pins 1-4, pin 7 on U3 goes high to 5v. If I move the voltage source to U3, U2 behaves as expected with 1/2Vcc as does U3 and the circuit works as expected. Of course, we need both Allegro chips to function properly with voltage present!!!
I am not sure how to troubleshoot from here - is it a bad Allegro chip? A tin whisker? Gremlins!!!!
Any thoughts?
Problem occurred while writing code that had me chasing ghosts.... My test light GP5, to simulate activation of the relay on the board, stayed lit under certain situations. Turns out that U3 Allegro chip was outputting 5v instead of 1/2Vcc - 2.5vdc at no load. I am not quite sure why that is. If I have voltage present between U2's sensing pins 1-4, pin 7 on U3 goes high to 5v. If I move the voltage source to U3, U2 behaves as expected with 1/2Vcc as does U3 and the circuit works as expected. Of course, we need both Allegro chips to function properly with voltage present!!!
I am not sure how to troubleshoot from here - is it a bad Allegro chip? A tin whisker? Gremlins!!!!
Any thoughts?
Code:
void main()
{
// Variable Declaration
int AN3_AD_IN_110 = 0; // variable to hold value of A/D input from U2 Allegro chip, 110v line.
int AN2_AD_IN_220 = 0; // " from U3 Allegro chip, 220v line.
// This signal is baseline of 2.5VDC with a 0-2.5VAC signal riding on top.
int max = 511, min = 511,
max1 = 511, min1 = 511; // variables for min/max comparisons on both channels 110/220
int temp_peak = 0, temp_peak1 = 0; // Hold temp values to compare ADC against
int count = 0;
CMCONbits.CM = 0b111; // Turn comparator off
//ADC Setup
ADCON0bits.ADFM = 1; // Right justified A/D results
ADCON0bits.VCFG = 0; // Voltage reference set to Vdd
ADCON0bits.ADON = 1; // Turn A/D converter on
__delay_us(15);
TRISIO = 0b010100; // Only AN2 and AN3/MCLR are inputs all else outputs.
GPIObits.GP1 = 0; // Turn off GP1 output initially
GPIObits.GP5 = 0; // Turn off GP5 output initially
//Analog Select Register Setup
ANSELbits.ADCS = 0b101; // Clock derived from internal osc - max of 500kHz - problem with 4MHz xtal freg??
ANSELbits.ANS2 = 1; // Sets AN2 as analog input (page 46 of pic675 manual states this and trisio need to be set)
ANSELbits.ANS3 = 1; // Sets AN3 as analog input
while(1)
{
while (count < 128) // set counter to prevent infinite loop
{
ADCON0bits.CHS = 0b11; // Channel select register set to 0b11 to select AN3
__delay_us(15);
// Start A/D conversion process
ADCON0bits.GO = 1;
__delay_us(15);
while (ADCON0bits.nDONE){} // Wait for conversion to be done
//
AN3_AD_IN_110 = ADRESL + (ADRESH << 8); // 1024 bit resolution 5v/1023 = 4.88mV per bit
// ADC Setup
ADCON0bits.CHS = 0b10; // Channel select register set to 0b10 to select AN2
__delay_us(15);
// Start A/D conversion process
ADCON0bits.GO = 1;
__delay_us(15);
while (ADCON0bits.nDONE){} // Wait for conversion to be done
//
AN2_AD_IN_220 = ADRESL + (ADRESH << 8); // 1024 bit resolution 5v/1023 = 4.88mV per bit
if (AN3_AD_IN_110 >= max) // check for 511 (1/2 Vcc) or greater
{
max = AN3_AD_IN_110; // set variable max to 511 or greater
}
if (AN3_AD_IN_110 <= min) // check variable min for 511 or less
{
min = AN3_AD_IN_110; // set var. min to 511 or less
}
if (AN2_AD_IN_220 >= max1) // check for 511 (1/2 Vcc) or greater
{
max1 = AN2_AD_IN_220; // set variable max to 511 or greater
}
if (AN2_AD_IN_220 <= min1) // check variable min for 511 or less
{
min1 = AN2_AD_IN_220; // set var. min to 511 or less
}
count = count + 1; // incrementing counter
if (count == 128)
{ // REMOVE from final - only for testing on dev.board
GPIO1 = 1; // write a =! GPIO statement to turn led GP1 on and off every 128 cycles
__delay_ms(50);
GPIO1 = 0;
}
}
temp_peak = max - min; // temporary peak value established and used to compare later
temp_peak1 = max1 - min1;
min = 511; // reset variable
max = 511; // reset variable
min1 = 511;
max1 = 511;
if ((temp_peak >= 80)||(temp_peak1 >= 80)) // control statement - if peak value is above threshold
// and if 0.354V above and below Vcc - (110v threshold) then proceed
{
__delay_ms(2000); // 2 second delay before turning on
//GPIO = 0b000001; // set GP0 high to turn on relay
GPIO5 = 1; //REMOVE ONLY FOR TESTING // this is to check code on dev. board - GP5 outermost LED to corner should light
}
if ((temp_peak <= 60)&&(temp_peak1 <= 60))
{
__delay_ms(3000);
GPIO5 = 0;
}
count = 0; // reset count variable to zero for next go around
}
}
Last edited: