Hi all I am trying to get a rather simple program cobbled together to run some tests on my run delay board. Since that thread is a bit long and was mostly dedicated to producing the board as well as a running commentary on the project, I will break this problem out as it can stand alone.
Can anyone spot any reasons as to why I am not getting any output as expected from my LED's?
Long and short - I am feeding in a 2.5vDC signal with a 2.5VAC signal riding on top of it into my ADC. After conversion, I am expecting ADRESL to be put into my variable and then the if statements to actuate from there.
My concerns are that perhaps I have incorrectly setup a SFR or that there is a timing mismatch between my internal clock at 4MHz and the ADC clock which I set to internal, but there is a max listed of 500KHz for the ADC.
I am not sure if that is the issue...
Any thoughts appreciated, thanks!
Link to PIC12F675 datasheet.
Can anyone spot any reasons as to why I am not getting any output as expected from my LED's?
Long and short - I am feeding in a 2.5vDC signal with a 2.5VAC signal riding on top of it into my ADC. After conversion, I am expecting ADRESL to be put into my variable and then the if statements to actuate from there.
My concerns are that perhaps I have incorrectly setup a SFR or that there is a timing mismatch between my internal clock at 4MHz and the ADC clock which I set to internal, but there is a max listed of 500KHz for the ADC.
I am not sure if that is the issue...
Any thoughts appreciated, thanks!
Link to PIC12F675 datasheet.
Code:
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // GP3/MCLR pin function select (GP3/MCLR pin function is MCLR)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#define _XTAL_FREQ 4000000 // Setting internal clock to 4MHz
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,
peak = 0, temp_peak = 0; // Hold temp values to compare ADC against
int count = 0;
//ADC Setup
ADCON0bits.ADFM = 0; // Left justified A/D results
ADCON0bits.VCFG = 0; // Voltage reference set to Vdd
ADCON0bits.CHS0 = 1; // Channel select 0 register set to 1 to select AN1
ADCON0bits.ADON = 1; // Turn A/D converter on
TRISIO = 0b000010; // AN1 is input :old comments //0b001100; // Only AN2 and AN3/MCLR are inputs all else outputs.
GPIO = 0b000000; // Turn off all outputs initially
//Analog Select Register Setup
ANSELbits.ADCS = 0b011; // Clock derived from internal osc - max of 500kHz - problem with 4MHz xtal freg??
ANSELbits.ANS1 = 1; // Sets AN1 as analog input (page 46 of pic675 manual states this and trisio need to be set)
// Start A/D conversion process
ADCON0bits.GO = 1;
while(1)
{
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
}
temp_peak = max - min; // temporary peak value established and used to compare later
if (count < 128) // set counter to prevent infinite loop
{
count = count + 1; // incrementing counter
}
if (count == 128)
{
__delay_ms(1000);
GPIO4 != GPIO4;
__delay_ms(1000);
// write a =! GPIO statement to turn led GP4 on and off every 128 cycles
}
if ((temp_peak >= 144) && (count == 128) // control statement - if peak value is above threshold and we iterated 129 times
&& (max >= 584) && (min <= 438)) // 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
GPIO = 0b100000; //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
count = 0; // reset count variable to zero for next go around
}
if ((temp_peak <= 130) && (max <= 560) && (min >= 490))
{
__delay_ms(5000); // statement checks to see if current has dropped below threshold
GPIO = 0b000000; //waits 5 seconds then shuts off GP5
}
// Start A/D conversion process
ADCON0bits.GO = 1;
}
}
Last edited: