sbit LED_1 at GP1_bit;
sbit LED_2 at GP2_bit;
sbit LED_3 at GP5_bit;
unsigned int AD_Average;
void Init(void){
GPIO = 0x0;
TRISIO = 0b00001001;
ANSEL = 0x51; // TAD = 16Tosc
CMCON = 0x1E; // Analogue comparator used with internal ref.
VRCON = 0xA1; // Reference set in lowest value.
}
void main() {
Init();
while(1)
{
Delay_ms(500);
ADC_Read(0);
if (AD_Average < 250) {
LED_1 = 0;
LED_2 = 0;
}
else if (AD_Average < 420) {
LED_1 = 0;
LED_2 = 1;
}
else if (AD_Average < 650){
LED_1 = 1;
LED_2= 0;
}
else {
LED_1 = 1;
LED_2 = 1;
}
}
}
sbit LED_1 at GP1_bit;
sbit LED_2 at GP2_bit;
sbit LED_3 at GP5_bit;
unsigned int AD_Average;
void Init(void){
GPIO = 0x0;
TRISIO = 0b00001001;
ANSEL = 0x51; // TAD = 16Tosc
CMCON = 0x1E; // Analogue comparator used with internal ref.
VRCON = 0xA1; // Reference set in lowest value.
}
void main() {
Init();
while(1)
{
Delay_ms(500);
AD_Average = ADC_Read(0);
if (AD_Average < 250) {
LED_1 = 0;
LED_2 = 0;
}
else if (AD_Average < 420) {
LED_1 = 0;
LED_2 = 1;
}
else if (AD_Average < 650){
LED_1 = 1;
LED_2= 0;
}
else {
LED_1 = 1;
LED_2 = 1;
}
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 if (AD_Average < 250) { LED_1 = 0; LED_2 = 0; } else if (AD_Average < 420) { LED_1 = 0; LED_2 = 1; } else if (AD_Average < 650){ LED_1 = 1; LED_2 = 0; } else { LED_1 = 1; LED_2 = 1; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 if (AD_Average < 250) { LED_1 = 0; LED_2 = 0; } else if (AD_Average >= 250) && (AD_Average < 420) { LED_1 = 0; LED_2 = 1; } else if (AD_Average >= 420) && (AD_Average < 650){ LED_1 = 1; LED_2 = 0; } else { LED_1 = 1; LED_2 = 1; }
If AD_Average is < 250 then it is also < 420 and 650 so 3 conditions except the last else gets executed one by one
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 AD_Average = 150; if (AD_Average < 650) { LED_1 = 0; LED_2 = 0; } else if (AD_Average < 420) { LED_1 = 0; LED_2 = 1; } else if (AD_Average < 250) { LED_1 = 1; LED_2 = 0; } else { LED_1 = 1; LED_2 = 1; }
GPIO1 is configured as Output in your code but it is Cin- for Comparator Circuit. Cin+ is internally connected to CVRef. So, Cin- (GPIO1) has to be provided the test voltage. In your case it is connected to LED and used as output ? When Cin+ (Internal CVRef) is provided how are you providing test voltage to Cin- ?
What is the value of CVRef you are providing ? (1 / 24) * 5 = 0.208 ? Try 0xAF for VRCON which gives CVRef of (15 / 24) * 5 = 3.125 V.
I don't know how you said that ADC works fine when you had not configured ADCON0
COUT is connected to GP2 and you see it as blinking when 5V (anything greater than 3.125V) is applied to GP0 which is Cin- as CIS bit = 1.
Code C - [expand] 1LED_3 = COUT
Code C - [expand] 1 CMCON = 0x07 to disable Coparator during ADC operation and CMCON = 0x1D for Comparator operation
As you opined LED blinks due to comparator, then only LED connected to GP2 will be effected leaving others static. But the result is not like that. All the LEDs act as per the algorithm.
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?