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.

[General] Problems in reading ADC in PIC18

Status
Not open for further replies.

imran090

Newbie level 2
Joined
Dec 19, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
24
Hi all,


I am new to the pic programming...can anyone tell me how to calculate voltage in pic..?

I am using PIC18F85J11.. I want to read 3 channels ( AN0,AN1 & AN4 ). I have tried to read the values, but I am getting some junk values.

Thank You

View attachment adc.txt
 

wizpic

Advanced Member level 3
Joined
May 23, 2004
Messages
780
Helped
108
Reputation
216
Reaction score
72
Trophy points
1,308
Location
London
Activity points
6,080
You need to post your code as no one will be able to help as we don't know your setup and what language your using opps sorry did not see the attachment must have been one to many beers :grin:
 
Last edited:

imran090

Newbie level 2
Joined
Dec 19, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
24
Hi

Below is my code..

// Header Files
#include <p18cxxx.h>
#include <stdio.h>

// Configuration Bit Settings
#pragma config FOSC = HSPLL,DEBUG = ON, WDTEN = OFF, XINST = OFF


void adc_init();
void ADC_value();
unsigned int ADCRead_ch0();
unsigned int ADCRead_ch1();
unsigned int ADCRead_ch4();
void DelayFor18TCY();
void Read_ADC_All_Segments();

unsigned int adc_value,adc_value_1,adc_value_2;
unsigned int a0 = 0;
unsigned int a1 = 0;
unsigned int a4 = 0;
unsigned int val_0 ;
unsigned int val_1 ;
unsigned int val_4 ;

main
{
adc_init();

while(1)
{
Read_ADC_All_Segments();
}
}


// Delay Function
{
Delay10TCYx(20);
}

// ADC initialisation
void adc_init()
{
ADCON1bits.VCFG1 = 0;
ADCON1bits.VCFG0 = 0;
ADCON1bits.PCFG = 0b1010;
ADCON2bits.ADFM = 1;
ADCON2bits.ACQT = 0b010;
ADCON2bits.ADCS = 0b0010;
}

// Copying ADRESH and ADRESL
void ADC_value()
{
adc_value = ADRESH;
adc_value_1 = ADRESL;
adc_value = adc_value << 8;
adc_value_2 = adc_value | adc_value_1;
}

// Reading channel 0
unsigned int ADCRead_ch0()
{
ADCON0 = 0x04;
ADCON0bits.ADON = 1;
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE);
ADC_value();
a0 = adc_value_2;
return a0;
}

// Reading channel 1
unsigned int ADCRead_ch1( )
{
ADCON0 = 0x00;
ADCON0bits.ADON = 1;
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE);
ADC_value();
a1 = adc_value_2;
return a1;
}

// Reading channel 4
unsigned int ADCRead_ch4( )
{
ADCON0 = 0x10;
ADCON0bits.ADON = 1;
ADCON0bits.GO_DONE = 1;
while(ADCON0bits.GO_DONE);
ADC_value();
a4 = adc_value_2;
return a4;
}


void Read_ADC_All_Segments(void)
{
val_0 = ADCRead_ch0();
DelayFor18TCY();

val_1 = ADCRead_ch1( );
DelayFor18TCY();

val_4 = ADCRead_ch4();
DelayFor18TCY();
}


Thank you
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top