I'm trying to use 2 ADC on this PIC to monitor 2 voltage, if either one of the voltage drop below 3, the LED will turn off. The problem i'm facing now is if I apply 4V on AN2, the LED will turn off, but if i apply voltage on AN0, the LED wun turn off.
What more weird is when i change the statement
if (result <3 || result2 <3)
PORTB =0b11111111; //LED ON
to
if (result <3 && result2 <3)
PORTB =0b11111111; //LED ON
then it would work. i apply 4V to either one of the pin, the LED will turn off. Is there any problem with my code? Any help is appreciated. thanks.
//program to turn on LED if voltage above the preset voltage-PIC18F1220
#include <p18cxxx.h>
#include <delays.h>
#pragma config WDT = OFF //watchdogtimer off
#pragma config LVP = OFF //lowvoltageprogramming off
#pragma config OSC = INTIO2 //select internal oscillator
#pragma config BOR = OFF // brownout reset off
#pragma config MCLRE = OFF //master clear off
#pragma code
float result;
float result2;
void getVoltage(void) //func to read ADC
{
ADCON0 =0x0; //select Vdd Vss as reference, select input AN0,
ADCON2=0x27; // left justified, use internal conversion clock
ADCON0bits.ADON = 1 ;
ADCON0bits.GO = 1; //start ADC
Delay100TCYx (2);
while(ADCON0bits.GO == 1); //wait for completion
result=(ADRESH*0.018); // x with 0.018 to convert back to actual value
Delay100TCYx (30);
ADRESH = 0x0;
ADCON0bits.ADON = 0 ;
Delay100TCYx (2);
ADCON0 =0x8; //select Vdd Vss as reference, select input AN2
ADCON2=0x27; // left justified, use internal conversion clock
ADCON0bits.ADON = 1 ; //enable ADC
ADCON0bits.GO = 1; //start ADC
Delay100TCYx (2);
while(ADCON0bits.GO == 1); //wait for completion
result2=(ADRESH*0.018); // x with 0.018 to convert back to actual value
}
void main (void)
{
OSCCON = 0x43; //set internal freq 1Mhz
ADCON1=0x70; //select AN0 and AN2 as analog
TRISA = 1; //set portA as input
TRISB = 0; //set port B as output
PORTB = 0b00000000; //reset port B
while (1)
{
getVoltage();
if (result <3 || result2 <3)
PORTB =0b11111111; //LED ON
else
PORTB = 0b00000000; //LED OFF
}
}
What more weird is when i change the statement
if (result <3 || result2 <3)
PORTB =0b11111111; //LED ON
to
if (result <3 && result2 <3)
PORTB =0b11111111; //LED ON
then it would work. i apply 4V to either one of the pin, the LED will turn off. Is there any problem with my code? Any help is appreciated. thanks.
//program to turn on LED if voltage above the preset voltage-PIC18F1220
#include <p18cxxx.h>
#include <delays.h>
#pragma config WDT = OFF //watchdogtimer off
#pragma config LVP = OFF //lowvoltageprogramming off
#pragma config OSC = INTIO2 //select internal oscillator
#pragma config BOR = OFF // brownout reset off
#pragma config MCLRE = OFF //master clear off
#pragma code
float result;
float result2;
void getVoltage(void) //func to read ADC
{
ADCON0 =0x0; //select Vdd Vss as reference, select input AN0,
ADCON2=0x27; // left justified, use internal conversion clock
ADCON0bits.ADON = 1 ;
ADCON0bits.GO = 1; //start ADC
Delay100TCYx (2);
while(ADCON0bits.GO == 1); //wait for completion
result=(ADRESH*0.018); // x with 0.018 to convert back to actual value
Delay100TCYx (30);
ADRESH = 0x0;
ADCON0bits.ADON = 0 ;
Delay100TCYx (2);
ADCON0 =0x8; //select Vdd Vss as reference, select input AN2
ADCON2=0x27; // left justified, use internal conversion clock
ADCON0bits.ADON = 1 ; //enable ADC
ADCON0bits.GO = 1; //start ADC
Delay100TCYx (2);
while(ADCON0bits.GO == 1); //wait for completion
result2=(ADRESH*0.018); // x with 0.018 to convert back to actual value
}
void main (void)
{
OSCCON = 0x43; //set internal freq 1Mhz
ADCON1=0x70; //select AN0 and AN2 as analog
TRISA = 1; //set portA as input
TRISB = 0; //set port B as output
PORTB = 0b00000000; //reset port B
while (1)
{
getVoltage();
if (result <3 || result2 <3)
PORTB =0b11111111; //LED ON
else
PORTB = 0b00000000; //LED OFF
}
}
Last edited: