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; |