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.

[SOLVED] pic18f43k22 adc wont work

Status
Not open for further replies.

bhan1992

Member level 1
Joined
Feb 25, 2012
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,511
Code:
#include <p18f43k22.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>

int result, increment;

void main (void)
{
	OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_2_TAD, ADC_REF_VDD_VSS, ADC_CH0 & ADC_INT_OFF );

	TRISB = 0;

	while(1)
	{
		SetChanADC(ADC_CH0);
		ConvertADC();
		while(BusyADC());
		result = ReadADC();

		if (result < 550)
		{
			PORTB = 1;	
		}
		else
		{
			PORTB = 0;
		}
	}
	CloseADC();
}
i tried to connect a potentiometer at the analog channel 0 (pin 2 of the pic) but the led in pin rb5 just wont light up.
does the code has something missing?
 

yeah, PORTB = 1 means RB0 is the output....

otherwise you have to write

Code C - [expand]
1
2
TRISB5 = 0;
RB5 = 1;

 

Hi,

PORTB=0xFF;

How much voltage are you feeding through POT.

may be ADC values are greater than 550. try below calculation for converting adc reading to voltage.

const float maxAdcBits = 4095.0f; // Using Float for clarity if it 12 bit(2^12=4095)
const float maxVolts = 5.0f; // Using Float for clarity
const float voltsPerBit = (maxVolts / maxAdcBits);

float yourVoltage = ADCReading * voltsPerBit;


try these and update me.

Best regards,
 
Last edited:

im sorry but i don't get you.
yeah, PORTB = 1 means RB0 is the output....

otherwise you have to write

Code C - [expand]
1
2
TRISB5 = 0;
RB5 = 1;


- - - Updated - - -

im feeding the pot with 5v. i try to tune the pot but led doesnt light up
Hi,

TRISB=0xFF;

How much voltage are you feeding through POT.

may be ADC values are greater than 550. try below calculation for converting adc reading to voltage.

const float maxAdcBits = 4095.0f; // Using Float for clarity if it 12 bit(2^12=4095)
const float maxVolts = 5.0f; // Using Float for clarity
const float voltsPerBit = (maxVolts / maxAdcBits);

float yourVoltage = ADCReading * voltsPerBit;


try these and update me.

Best regards,
 

Hello again,

im sorry but i don't get you.
PORT B is also an 8 bit bi-directional PORT. Its direction controlled and maintained by TRIS B data direction register. Setting the TRIS B into logic ‘1’ makes the corresponding “PORT B” pin as an input. Clearing the TRIS B bit make PORT B as an output.

im feeding the pot with 5v. i try to tune the pot but led doesnt light up
Try these too..
Code:
if (result >= 550)
		{
			PORTB = 1;	
		}
		else
		{
			PORTB = 0;
		}

Update me.

Best regards,

- - - Updated - - -

Code:
#include <p18f43k22.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>

int result, increment;

void main (void)
{
	OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_2_TAD, ADC_REF_VDD_VSS, ADC_CH0 & ADC_INT_OFF );

	TRISB = 0;

	while(1)
	{
		SetChanADC(ADC_CH0);
		ConvertADC();
		while(BusyADC());
		result = ReadADC();

		if (result < 550)
		{
			PORTB = 1;	
		}
		else
		{
			PORTB = 0;
		}
	}
	CloseADC();
}
i tried to connect a potentiometer at the analog channel 0 (pin 2 of the pic) but the led in pin rb5 just wont light up.
does the code has something missing?

Try these code and update me.
Code:
#include <p18f43k22.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>

float result, increment;
const unsigned long maxAdcBits = 4095; //  if it 12 bit(2^12=4095)
const unsigned long  maxVolts = 5000; // 
const float voltsPerBit = (maxVolts / maxAdcBits);

void main (void)
{
	OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_2_TAD, ADC_REF_VDD_VSS, ADC_CH0 & ADC_INT_OFF );

	TRISB = 0x00;
	TRISA=0x01;// RA0 for analog channel input.
	ANSELA=0x01; // analog pin select
        PIR1bits.ADIF = 0; //make sure A/D Int not set

	while(1)
	{
		SetChanADC(ADC_CH0);
		ConvertADC();
		while(BusyADC());
  		result = ReadADC();
              result  = result * voltsPerBit;// analog to Voltage conversion.
		if (result < 550)// if less than 550mV
		{
			PORTB = 1;	
		}
		else
		{
			PORTB = 0;
		}
	}
	CloseADC();
}
 

it works but there is a delay. eg: when i turn the pot to the max, i have to wait for about 15 sec before the LED turns on. and turn i turn the pot the other way, i have to wait another 15 sec before the LED is turned off.
my modified code below
Code:
#include <p18f43k22.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>

int result, increment;

void main (void)
{
	OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_2_TAD, ADC_REF_VDD_VSS, ADC_INT_OFF );

	TRISB = 0;
	while(1)
	{
		SetChanADC(ADC_CH0);
		ConvertADC();
		while(BusyADC());
		result = ReadADC();

		if (result >= 550)
		{
			PORTBbits.RB5 = 1;	
		}
		else
		{
			PORTBbits.RB5 = 0;
		}
	}
	CloseADC();
}
Hello again,


PORT B is also an 8 bit bi-directional PORT. Its direction controlled and maintained by TRIS B data direction register. Setting the TRIS B into logic ‘1’ makes the corresponding “PORT B” pin as an input. Clearing the TRIS B bit make PORT B as an output.


Try these too..
Code:
if (result >= 550)
		{
			PORTB = 1;	
		}
		else
		{
			PORTB = 0;
		}

Update me.

Best regards,

- - - Updated - - -



Try these code and update me.
Code:
#include <p18f43k22.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>

float result, increment;
const unsigned long maxAdcBits = 4095; //  if it 12 bit(2^12=4095)
const unsigned long  maxVolts = 5000; // 
const float voltsPerBit = (maxVolts / maxAdcBits);

void main (void)
{
	OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_2_TAD, ADC_REF_VDD_VSS, ADC_CH0 & ADC_INT_OFF );

	TRISB = 0x00;
	TRISA=0x01;// RA0 for analog channel input.
	ANSELA=0x01; // analog pin select
        PIR1bits.ADIF = 0; //make sure A/D Int not set

	while(1)
	{
		SetChanADC(ADC_CH0);
		ConvertADC();
		while(BusyADC());
  		result = ReadADC();
              result  = result * voltsPerBit;// analog to Voltage conversion.
		if (result < 550)// if less than 550mV
		{
			PORTB = 1;	
		}
		else
		{
			PORTB = 0;
		}
	}
	CloseADC();
}

- - - Updated - - -

[update]
Problem solved, it was the configuration words. i forget to set to XT which im using @ 4MHz. after setting the conf words, it works perfect now. thnx guys!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top