Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC on 18F4525 coded in C langage under MPLAB (C18)

Status
Not open for further replies.

lionkid

Newbie level 1
Joined
Jan 18, 2009
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,293
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) :

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.
 

18f4525

Hi,

I don't know if you are still waiting for an answer on this topic ....
I think the problem is in :
tension = (result*5)/1023; with result being an (int) and tension a (float) and the you check if tension is >=1 or <1.
I think you will get always <1 and so the LED OFF always.
Try to use result (int) only and then
if result is grater then or equal to 205 LED ON else LED OFF.

Regards,

mt
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top