View previous topic :: View next topic |
Author |
Message |
srikrishna
Joined: 06 Sep 2017 Posts: 82
|
Can't understand ADC output |
Posted: Fri Jun 08, 2018 2:51 pm |
|
|
Hello, i have used the following Code to convert an analog input to 10 bit digital number with low voltage reference 0v and high voltage reference 5V using a potentiometer. I used Yellow led to show the conversion result.
Code: | unsigned int value ;
void main()
{
ADCON1 = 0x80;
TRISA = 0xFF; // PORTA is input
TRISC = 0x3F; // Pins RC7, RC6 are outputs
TRISB = 0; // PORTB is output
do
{
value = ADC_Read(1); // Get 10-bit results of AD conversion
//of channel 1
PORTB = value ; // Send lower 8 bits to PORTB
PORTC = value >> 2; // Send 2 most significant //bits to RC7, RC6
} while(1);
} |
and after simulating in Proteus i have got the following result
Calculation
at 55% of 5V voltage
( 5V – 0V )× 55/100 = 2.75V
1024/(5 V) ×2.75V= 563.2
563.2 -> 1000110011.00110011001100110011(binary value)
ignoring the numbers after point , i get 1000110011
Its shows the correct answer.But it shows the number in the following order
But my question is Why always MSB started from RC7 pin?? Why not it starts from RB0 Pin(that is in the reverse order) ??
What is the reason behind this?
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Fri Jun 08, 2018 3:11 pm |
|
|
#1) consult the CCS manual to learn what values an unsigned int can have
#2) your adc conversion does not return a 10 bit result into 'value'. see #1 above
#3) you're best to think in BITS like a computer and not floating point numbers.
#4) this ... ADCON1 = 0x80; means nothing to me, you should at least comment what it's supposed to do. Better yet, use CCS C functions when using CCS C ! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Sat Jun 09, 2018 5:32 am |
|
|
What he really needs to do is buy the CCS compiler ! Whole lot easier to use/understand....also get rid of Proteus, since that schematic cannot work in the real world. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jun 09, 2018 2:30 pm |
|
|
I don't use either MicroC or your chip.
However the ADC output looks correct, and agrees with the comments in your code.
Like others have said, Proteus/ISIS is a waste of time for PIC simulation.
(See the sticky at the head of the General CCS C discussion.)
You need to either go to the MicroC site, or buy a CCS compiler AND use real hardware.
You've been on this site often enough to have got that message by now.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Sun Jun 10, 2018 1:56 pm |
|
|
Ignoring the wrong compiler, the answer to the question asked is that pin 0 of a port is it's least significant bit. The least significant bit of the result is put into the least significant bit of the port. Why on earth would you expect the most significant bit to be put here?..... |
|
|
|