#include <system.h>
//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF
//Set clock frequency
#pragma CLOCK_FREQ 20000000
void main( void )
{
//Configure port A - E
trisa = 0x2F;
trisb = 0x00;
trisc = 0x00;
trisd = 0x00;
//Initialize port A - E
portd = 0x00;
porta = 0x00;
portc = 0x00;
portb = 0x00;
//Configure A/D pins
adcon0 = 10011001b; //Set clock Fosc/32, ch1(AN0), GO = 0, ADC Operating
adcon1 = 0x80; //Right Justified, All ADC pins on
delay_ms(100);
// intcon = 0x00;
// int check;
volatile unsigned int ADC_VALUE;
// ADIE = 0; /* Masking the interrupt */
// ADIF = 0; /* Resetting the ADC interupt bit */
ADC_VALUE = 0000000000b;
while( 1 ) //Endless loop
{
adcon0 = 10001101b; //turn on ADC
while ((adcon0 & 0x04) == 0x04) //waiting until ADC conversion is finished
{
portd = 11111111b;
}
portd = 00000000b; //test that ADC exits since Go bit suppose to change to 0 when ADC finishes
delay_ms(1);
ADC_VALUE = ADRESL;
ADC_VALUE += (ADRESH << 8);
if (ADC_VALUE.9 == 1) //if equal or bigger than 2.5 Volts
{
portb = 11111111b;
}
else
{
portb = 00000000b;
}
portc = 11111111b;
// portb = ADRESH;
portc = 00000000b;
// delay_ms(1);
}
}