Hi all,
Using PIC16F688. The idea of the program is to take two ADC inputs (AN6/AN7) and subtract the values. If AN6 > AN7 turn RA0 high, if not turn it low.
However my code does not perform as expected. It only sets RA0 high if the two voltages on AN6 & AN7 are very close to one another. When either is significantly higher than the other the pin is low.
I have checked and both conversions seem to working properly so I believe it is in my subtraction, end of code. If anyone has any ideas where I might be going wrong it would be greatly appreciated.
Thanks.
Code:
Using PIC16F688. The idea of the program is to take two ADC inputs (AN6/AN7) and subtract the values. If AN6 > AN7 turn RA0 high, if not turn it low.
However my code does not perform as expected. It only sets RA0 high if the two voltages on AN6 & AN7 are very close to one another. When either is significantly higher than the other the pin is low.
I have checked and both conversions seem to working properly so I believe it is in my subtraction, end of code. If anyone has any ideas where I might be going wrong it would be greatly appreciated.
Thanks.
Code:
Code:
#include <htc.h>
#include <math.h>
#include <stdio.h>
#define _XTAL_FREQ 4000000
//#define bitset(var,bitno) (var|=1<<bitno)
//#define bitclr(var,bitno) (var&=~(1<<bitno))
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS);
void main(void){
int x = 0;
int IRone = 0; //AN6/RC2
int IRtwo = 0; //AN7/RC3
int IRdiff = 0; //for difference in IR measurements
PORTA = 0x00; // PORTA low
PORTC = 0x00; // PORTC low
CMCON0 = 0x07; // turn off comparators
ADCON0 = 0b10011001; //turn on ADC AN6 selected - right justified
ADCON1 = 0b00010000; //ADC clock select FOSC/8 - 2us @ 4Mhz
TRISA = 0x00; //Port A outputs
TRISC = 0b00001100; //RC2 + RC3 input pins (AN6/AN7)
ANSEL = 0b11000000; //AN6/AN7 selected as analogue input pins
ADIE = 1; //enable ADC interrupt
ADIF = 0; //ensure ADC interrupt flag is cleared
while(1){
ADCON0 = 0b10011001; //ADC ON - AN6
ADCON0 = 0b10011011; //ADC GO - AN6
while(ADIF = 0){ //wait for ADC interrupt flag
_delay(1);
}
ADIF = 0; //clear ADC interrupt flag
IRone = (ADRESH*256) + ADRESL; //calc IRone - right justified
ADCON0 = 0b10011101; //ADC ON - AN7
ADCON0 = 0b10011111; //ADC GO - AN7
while(ADIF == 0){ //wait for ADC to finish
_delay(1);
}
ADIF = 0; //clear ADC interrupt flag
IRtwo = (ADRESH*256) + ADRESL; //calc IRtwo - right justified
IRdiff = IRone - IRtwo;
if(IRdiff >= 0){
PORTA = 0b00000001;
for(x = 0; x <=13; x++){
__delay_ms(20);
}
}
else{
PORTA = 0b00000000;
}
}
}