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 value reading and output problem

Status
Not open for further replies.

suvaraj

Member level 2
Joined
Dec 24, 2010
Messages
50
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Location
erode
Activity points
1,580
hi to all
in my project im using pic16f873a,hi-tech c compiler.im reading adc value data=ADRESH;.and
i
but the
adc i/p value 2v means 2000 mv/4.88mv=adc value is 409.
im use the 409 value means im not get o/p.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
if(data>=409)
{
led=0;
led1=1;
}
again i divide 4 
409/4=102 this value used means i get the out put in 2v.
f(data>=102)
{
led=0;
led1=1;
}



what i mistake in my program?
pls help me .
thanks in advance
 
Last edited by a moderator:

The data you provided is not enough for finding the exact mistake...

It depends on how you configured the ADC registers.

You need to post your adc initialization code.
 

Your micro contains 10bit ADC. If PCFG3:pCFG0: A/D Port Configuration Control bits are supplied with 0000(ADCON1 register) then Vref+ will be connected to VDD and Vref- to Vss. Suppose the supply voltage (Vdd) is 5V DC. Now for 5 volt analog input the output will be 1024. Resolution 4.883mV. For 2 V the output will be 410(409.5).

---------- Post added at 20:30 ---------- Previous post was at 20:22 ----------

ADC output 102 means (102*5000)/1024 mV=498mV=0.5V(approx)
 
Last edited:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
void adc_init()//initialize adc
{
ADCON0=0X01;
ADCON1=0X2E;
}
void adc_read()//read's adc value
{
GODONE=1;
while(ADGO==1);
data=ADRESH;
}


what i do a mistake
 
Last edited by a moderator:

Here is an example code:-
Set the analog input AN0 as input inside the main

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void main(void)
{
ADCON0=0b10000000; 
ADCON1=0b11000000; 
ADCON0.ADON=1; // turn on the A2D conversion module
Delay_ms(300);
LCD_init();
while(1){
Delay_ms(300);
read_adc();
-----
-------
}
 
void read_adc(void)
{
int result;
ADCON0.GO = 1; //ADC start
while(ADCON0.GO==1); 
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC
}



---------- Post added at 12:49 ---------- Previous post was at 12:42 ----------

The ADRESH:ADRSEL registers contain the 10 bit result of the A/D conversion.

---------- Post added at 13:32 ---------- Previous post was at 12:49 ----------

ADC 10 BIT AND READING THAT 10 BIT
https://www.microchip.com/forums/m483050-print.aspx
 

hi thanks to u
but i can't unterstaand theese lines
(result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC).pls explain
and how to use another one adc channel in this program.?
pls explain me.
thanks in advance
 

Code:
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC

result = 0;
ie result = 0b0000000000000000;

let ARRESH = 0b11 (2 bit)
result = ADRESH;
so result become 0b0000000000000011;

now result = result << 8 ;
then result become 0b0000001100000000;

let ADRESL = 0b10101010;
now
result = result|ADRESL;
ie result becomes 0b0000001110101010;

---------- Post added at 12:09 ---------- Previous post was at 11:58 ----------

To change channel.
ADCON0 bit 5-3 CHS2:CHS0: Analog Channel Select bits

Also, you need to configure
ADCON1 - > bit 3-0 PCFG3:pCFG0: A/D Port Configuration Control bits to configure the required ADC input bits as analog...

This is for PIC16F877A. Little changes may be there since ur pic is not 40pin... So just check the ADC part in your data sheet , the ADCON0 and ADCON1
 

giv me one example adc program for using two adc channel in one program.

thanks in advance
 

Suvaraj,
Hope you understand now. Vinod has given a very informative step by step description above. If you use PIC16F873A then it will be applicable to your case too. Because on the fist page of the datasheet, PIC16F87X was written.
 

Suvaraj,
Hope you understand now. Vinod has given a very informative step by step description above. If you use PIC16F873A then it will be applicable to your case too. Because on the fist page of the datasheet, PIC16F87X was written.

i'm unterstand but a little bit of doubt will occured for using two adc channel.so i need a aexample program for use two adc channel.
that"s way i ask
 

Yes it is possible to accept data from more than one channels one by one.
You can use CHS2:CHS0: Analog Channel Select bits of ADCON0 to select different values as per datasheet to select different pins as analog input (keeping in mind PCFG3:pCFG0: A/D Port Configuration Control bits, better set them as 0000) and accept input from that pin.

Remember :- The PIC16F873A/876A devices only implement A/D channels 0 through 4; the unimplemented selections are reserved. Do not select any unimplemented channels with these devices.

And remember, The analogue inputs have a sample and hold circuit built in, this requires an internal capacitor to charge to the incoming voltage. If the source impedance is high, this takes longer to do, so the capacitor isn't fully charged when you take your reading.
So longer time delay may be needed between to readings.

I dont have ready code but I can suggest you a pseudocode.

---------- Post added at 21:52 ---------- Previous post was at 21:42 ----------


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void main(void)
{
ADCON0=0b10000000; //Select Channel 0 (AN0)
ADCON1=0b11000000; 
ADCON0.ADON=1; // turn on the A2D conversion module
Delay_ms(300);
LCD_init();
while(1){
Delay_ms(300);
read_adc();   //Read from Channel 0 (AN0)
----------
-------
Delay_ms(300)// <- this is importent
ADCON0=0b10001000; //Select Channel 1 (AN1)
ADCON0.ADON=1; // turn on the A2D conversion module
read_adc();   //Read from read_adc();   //Read from
--------------
---------------
Delay_ms(300)// <- this is importent if you want to change channel again(say by going to the top of the loop
 
}
 
void read_adc(void)
{
int result;
ADCON0.GO = 1; //ADC start
while(ADCON0.GO==1); 
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC
}

 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top