lionkid
Newbie level 1
c18 adc
Hi all!
This is my first topic because i'm new in PIC programming. I have C langage knowledge and i want to handle a new device which is a PIC.
I've done the hardware programmer Schaer+ and i use Winpic for programming my code, but it seems that it doesn't work! I've tested my pic with a led blinking and it works. So, i'm sure that my problem is due to the ADC use...
Here is my code (mixed with french, because i'm french) :
So, what's wrong? In my test board i've put the MCLR (bar)pin on +Vdd, the Vdd pin to +5V (given by a 78M05), Vss pin to the ground (of the 78M05), then i've got my light sensor on the RE0 pin which is mounted like that :
+5V
|
|
[sensor]
|--------------> RE0
| |
| | 10k resistor
|
GND
for my LED (3mm, 2mA @ 1.9V), i've got :
RE1
|
|
| | 1.5k
| |
|
LED
|
GND.
Have i made any mistakes? I'm discouraged because the Schaer+ programmer have taken a lot of time for working (soft + hard configuration) and now the PIC doesn't want to make what i want... :'(
PS : I don't use any external oscillator (no quartz), should i? (more stability ??).
PS2 : Is my PIC dead??
Regards,
lionkid.
Hi all!
This is my first topic because i'm new in PIC programming. I have C langage knowledge and i want to handle a new device which is a PIC.
I've done the hardware programmer Schaer+ and i use Winpic for programming my code, but it seems that it doesn't work! I've tested my pic with a led blinking and it works. So, i'm sure that my problem is due to the ADC use...
Here is my code (mixed with french, because i'm french) :
Code:
#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)...
}
So, what's wrong? In my test board i've put the MCLR (bar)pin on +Vdd, the Vdd pin to +5V (given by a 78M05), Vss pin to the ground (of the 78M05), then i've got my light sensor on the RE0 pin which is mounted like that :
+5V
|
|
[sensor]
|--------------> RE0
| |
| | 10k resistor
|
GND
for my LED (3mm, 2mA @ 1.9V), i've got :
RE1
|
|
| | 1.5k
| |
|
LED
|
GND.
Have i made any mistakes? I'm discouraged because the Schaer+ programmer have taken a lot of time for working (soft + hard configuration) and now the PIC doesn't want to make what i want... :'(
PS : I don't use any external oscillator (no quartz), should i? (more stability ??).
PS2 : Is my PIC dead??
Regards,
lionkid.