#include <p18f4525.h>
#include <adc.h>
#include <delays.h>
#pragma config WDT = OFF
void main(void)
{
int result=0;
float tension=0;
/**************** CONFIGURATION DE L'OSCILLATEUR DU PIC *********************/
OSCCONbits.SCS1=1; // Use of the internal oscillator at 8Mhz frequency
OSCCONbits.SCS0=1; // Utilisation de l'oscillateur interne du PIC
OSCCONbits.IRCF0=1; //
OSCCONbits.IRCF1=1; // Fréquence d'utilisation de l'oscillateur = 8Mhz.
OSCCONbits.IRCF2=1; //
/****************************************************************************/
TRISEbits.TRISE0=1; // Configure la patte RE0 en entrée pour le capteur de lumière (AN5), RE0 as input for the light sensor
TRISEbits.TRISE1=0; // Configure la patte RE1 en sortie pour la LED. (AN6), RE1 as output for the LED.
OpenADC(ADC_FOSC_2 & ADC_RIGHT_JUST & ADC_12_TAD,
ADC_CH5 & ADC_INT_OFF & ADC_REF_VDD_VSS,
ADC_6ANA); // Open the ADC in the Channel 5 (RE0, AN5) with +5V reference level (=Vdd) and 0V as Vss.
while(1)
{
Delay10TCYx( 5 );
ConvertADC();
while(BusyADC());
result=ReadADC();
tension = (result*5)/1023;
if(tension>=1) PORTEbits.RE1=1; // Allume la LED si activitée detectée sur CAN, LED is ON if Voltage supplied by the sensor is greater than 1Volt.
if(tension<1) PORTEbits.RE1=0; // Eteint la LED si tension inférieure à 1Volt, LED is OFF otherwise.
}
CloseADC(); // In reality, the ADC is never closed due to the while(1)...
}