// PIC16F1503
// Config word
//__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_OFF & CPD_OFF);
// these should be before any '#include'
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled CLKOUT pin)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON
#include <xc.h> // edit MCB data at 3 places
// Define CPU Frequency
// This must be defined, if __delay_ms() or
// __delay_us() functions are used in the code
#define _XTAL_FREQ 4000000
#define AN0 0
#define AN1 1
#define AN2 2
#define AN3 3 // RA4
#define AN4 4
#define AN5 5
#define AN6 6
#define AN7 7 // RC3
unsigned int GetADCValue(unsigned char Channel)
{
if (Channel > AN7) return 0;
ADCON0 = (ADCON0 & 0x03) | (Channel<<2); // select channel
__delay_ms(1); // Time for Acqusition capacitor to charge up and show correct value
GO_nDONE = 1; // Enable Go/Done
while(GO_nDONE); // wait for conversion completion
return (ADRESH<<8) | ADRESL; // Return 10 bit ADC value (uses unsigned int operation)
}
void InitADC(unsigned char Channel)
{
// all pins default to analog mode with TRISx = 1, so this isn't
// required, but...
switch (Channel) {
case AN0: ANSELAbits.ANSA0 = 1; TRISAbits.TRISA0 = 1; break; // RA0
case AN1: ANSELAbits.ANSA1 = 1; TRISAbits.TRISA1 = 1; break; // RA1
case AN2: ANSELAbits.ANSA2 = 1; TRISAbits.TRISA2 = 1; break; // RA2
case AN3: ANSELAbits.ANSA4 = 1; TRISAbits.TRISA4 = 1; break; // RA4
case AN4: ANSELCbits.ANSC0 = 1; TRISCbits.TRISC0 = 1; break; // RC0
case AN5: ANSELCbits.ANSC1 = 1; TRISCbits.TRISC1 = 1; break; // RC1
case AN6: ANSELCbits.ANSC2 = 1; TRISCbits.TRISC2 = 1; break; // RC2
case AN7: ANSELCbits.ANSC3 = 1; TRISCbits.TRISC3 = 1; break; // RC3
}
ADCON1 = 0b11110000; // right-justified, FRC clock, VREF=VDD
ADCON0 = 0x01; // Turn on the A/D Converter
// these are not needed... comparator off by default
// CM1CON0 = 0x07; // NOT CORRECT FOR 16F1503
// FVRCON = 0x00; // Shut off the Voltage Reference for Comparator
}
void main() {
unsigned int ADC_Val;
OPTION_REGbits.nWPUEN = 1;
//APFCON=0x20;
TRISAbits.TRISA0 = 0;
RA0=0;
InitADC(AN7);
while(1)
{
ADC_Val = GetADCValue(AN7);
if(ADC_Val<500)
{
RA0=0;
}
else
{
RA0=1;
}
}
}