Hi all, please criticize my code. It is my first use of the ADC, and worked first go
The operation is to use an external voltage divided on AN0 and if the value is ~1/2 of Vdd turn high (LED connected) RA2. Approximately half since I only check the high register (justified left) of the ADC result.
Myu only issue is I am having problems denoting a single bit of a register to a variable eg.
ANSEL7 = ..
ANSEL.7 = ..
both produce errors.
The operation is to use an external voltage divided on AN0 and if the value is ~1/2 of Vdd turn high (LED connected) RA2. Approximately half since I only check the high register (justified left) of the ADC result.
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT);
void main(void){
int i;
PORTA = 0x00; // PORTA low
CMCON0 = 0x07; // turn off comparators
TRISA = 0x01; // RA2 output, RA0/AN0 input
TRISC = 0x00;
ADCON0 = 0b00000001; // turn on ADC
ADCON1 = 0b00000000; // conversion clock selected pins
ANSEL = 0b00000001; // AN0 analouge inout
while(1){
ADCON0 = 0b00000011; // start ADC
__delay_ms(10);
if(ADRESH >= 0b10000000){
RA2 = 1;
}
else{
RA2 = 0;
}
}
}
ANSEL7 = ..
ANSEL.7 = ..
both produce errors.