CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Can't understand ADC output

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
srikrishna



Joined: 06 Sep 2017
Posts: 82

View user's profile Send private message

Can't understand ADC output
PostPosted: Fri Jun 08, 2018 2:51 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 08, 2018 3:11 pm     Reply with quote

#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

View user's profile Send private message

PostPosted: Fri Jun 08, 2018 3:59 pm     Reply with quote

Quote:
value = ADC_Read(1);

This is not CCS compiler code. This is MikroC compiler code.
https://download.mikroe.com/documents/compilers/mikroc/pic/help/adc_library.htm

You're on the wrong forum. Each compiler has its own forum.
The forum for MikroC is:
https://forum.mikroe.com/viewforum.php?f=88
Also, you can use EDAboard for MikroC questions.
https://www.edaboard.com/showthread.php?197974-MicroC-ADC-syntax-problem
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 09, 2018 5:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 09, 2018 2:30 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 10, 2018 1:56 pm     Reply with quote

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?.....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group