// CONFIG1
#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#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)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 8000000
#include <xc.h>
void Setup(void);
unsigned char ADCResult;
void main(void) {
Setup();
while(1){
__delay_us(100);
ADCON0bits.GO=1;
while(ADCON0bits.GO!=0); //Realizo una espera para captura de datos
ADCResult = (ADRESH<<8) + ADRESL ; //Sumo los registros para saber el nivel de tension
if(ADCResult<512) //Encender o apagar led
PORTD=0xff;
else
PORTD =0x00;
}
return;
}
void Setup(void){
INTCON = 0x00;
OSCCON = 0x00;
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;
ADRESH=0x00;
ADRESL=0x00;
ANSELH=0x00;
ANSEL =0xff;
ANSELbits.ANS0=1;
TRISD=0x00;
PORTD =0x00; // TOdo salida
ADCON1=0b10000000;
ADCON0=0b00000001;
__delay_us(100);
}