Hello all, I added in the code for monitoring both ADC inputs (pic12f675) to my ongoing project, but its not working. I need some help getting the code to function properly. I am not sure of how to alternate between ADC inputs.
When I set the ADCON0.bits for voltage and right justification outside of the loop at the start of the program, is that assumed by the program to be the universal setting? (see first ADC setup)
I have highlighted the new sections of code with *** - the highlight feature does not work inside of code brackets.
Thanks!
When I set the ADCON0.bits for voltage and right justification outside of the loop at the start of the program, is that assumed by the program to be the universal setting? (see first ADC setup)
I have highlighted the new sections of code with *** - the highlight feature does not work inside of code brackets.
Thanks!
Code:
int main(void)
{
// 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_value = 0, temp_value1= 0,
temp_peak = 0, temp_peak1 = 0; // Hold temp values to compare ADC against
int count = 0;
//ADC Setup
ADCON0bits.ADFM = 1; // Right justified A/D results
ADCON0bits.VCFG = 0; // Voltage reference set to Vdd
TRISIO = 0b101000; // AN2 and AN3/MCLR are inputs all else outputs.
GPIO = 0b000000; // Turn off all outputs 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
{
// ADC Setup
ADCON0bits.CHS = 11; // Channel select register set to 11 to select AN3
ADCON0bits.ADON = 1; // Turn A/D converter on
__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
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
}
// ***
// ADC Setup
ADCON0bits.CHS = 10; // Channel select register set to 11 to select AN2
ADCON0bits.ADON = 1; // Turn A/D converter on
__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 (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)
{
GPIO4 = 1; // write a =! GPIO statement to turn led GP4 on and off every 128 cycles
__delay_ms(50);
GPIO4 = 0;
}
}
temp_peak = max - min; // temporary peak value established and used to compare later
min = 511; // reset variable
max = 511;
// ***
temp_peak1 = max1 - min1; // same as above on other channel
min1 = 511; // reset variable
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
//ADCON0bits.GO_DONE = 1; // do A/D measurement
}
if ((temp_peak <= 60)||(temp_peak1 >= 60))
{
__delay_ms(3000);
GPIO5 = 0;
}
count = 0; // reset count variable to zero for next go around
}
}