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.

Why ADC is giving wrong results ?

Status
Not open for further replies.

milan.rajik

Banned
Advanced Member level 5
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Visit site
Activity points
0
Why ADC is giving wrong results ?

I am testing this code in Proteus and also on EasyPIC7. The problem in EasyPIC7 debugging is that when ADC_Read(x) is executed the debugging stucks and doesn't go further. In run mode the code is not working in EasyPIC7. So, I am also testing it in Proteus to see what is happening. I am using 4 MHz clock. ADC result should be 300 for 5V but the adc is varying like 0 to 5x then again 0 to 5x. Both channels are behaving like that. I have not tested other channels. Is it a bug of Proteus or mikroC ADC library ? I am using mikroC PEO PIC 6.4.0 for coding.

Just to simulate the error I am attaching an example adc project. It has to display 300 for 5V adc input on both channels.

Am I missing some register configuration ? ADC result is set for right justified. Comparators are disabled. TRIS is set for 0x03 for AN0 and AN1. ANSEL is set for AN0 and AN1.

Edit: Attachment changed. I had attached wrong attachment earlier.

Tested the project in EasyPIC7 in run mode not debug mode. The adc values vary from 0 to 62 and then again becomes 0 and then goes to 62 and repeats when adc input is varied from 0 to 5V.

- - - Updated - - -

It was a mistake to use unsigned int. The value when multiplied by 300 was exceeding the 65535 value but I changed to unsigned long int and used LongToStr() function but still get incorrect adc value. If I use float then it works fine.



Edit 2:


This works


Code C - [expand]
1
2
volts = ADC_Read(0);
volts = volts * 300 / 1023;



but this doesn't work. why? I am using unsigned long int and LongToStr() in mikroC


Code C - [expand]
1
volts = ADC_Read(0) * 300 / 1023;

 

Attachments

  • ADC Problem.rar
    49.4 KB · Views: 58
Last edited:

Hi

Thank you for replying. That doesn't cause a problem because ADC_Read(x) function will take care of the ADON and GO/DONE bits in mikroC. Just to configure ADCSx bits I configured ADCON0. My previous post is updated. I have mentioned about the strange behaviour. Please read it.
 

Provided this is your code
Code:
unsigned long volts;
volts = ADC_Read(0) * 300 / 1023;

Yes, it will give wrong results according to elementary C type conversion rules.

You'll better write
Code:
unsigned long volts;
volts = (unsigned long)ADC_Read(0) * 300 / 1023;
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top