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

ADC with Pic 16f887

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



Joined: 11 Sep 2010
Posts: 8

View user's profile Send private message

ADC with Pic 16f887
PostPosted: Wed Oct 27, 2010 2:37 am     Reply with quote

Hi I'm writing a program that needs to read the temperature from four temperature sensors, LM35DZ's. And it needs to read them from Pin B0, B1, B2 and B3. the problem is that I can't even get one working and I'm sure its the code because when I measure the output voltage from the Vout of the temperature sensor and its right. Here is the code:
Code:

#include <16F887.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS)

//==========================
void main()
{
      int8 val;
      SETUP_ADC_PORTS(sAN9 | VSS_VDD);
      SETUP_ADC(ADC_CLOCK_INTERNAL);
      SET_ADC_CHANNEL(0);
while(1){
      delay_ms(18);
      val = READ_ADC();
      printf("%u ",val);
      delay_ms(1000);
}
}

The output of this is just random numbers that keep getting smaller.

Now there are a few questions that I have to ask: What does "SET_ADC_CHANNEL(0);" do?
What is the right Clock to set "SETUP_ADC(ADC_CLOCK_INTERNAL);" too?

Any help would be greatly appreciated.

Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Wed Oct 27, 2010 4:11 am     Reply with quote

First, the setup_adc command selects which ports pins are going to be configured for use by the ADC. You have this selecting sAN9. Pin B3. OK. Then the set_adc_channel command, selects which channel is connected through the multiplexer to the actual ADC input. Currently you have this selecting channel 0, which connects to pin A0.....

So, start by selecting the channel you have got enabled for ADC operation. 9.

Then look at your ADC clock setting. Go to table 9-1 in the data sheet. Read Note 4 here. Is ADC_CLOCK_INTERNAL right?. Look at the table itself, and the value that is recommended for 4MHz operation.

For the multiple channels, you will need the setup_adc command to have all four pins you want 'enabled' (or them together in the command), then you select these one at a time, wait a moment (you only need a few uSec, not the 18mSec you are using - section 9-3 of the data sheet), and then take the reading, and move to the next channel.

Best Wishes
William



Joined: 11 Sep 2010
Posts: 8

View user's profile Send private message

PostPosted: Wed Oct 27, 2010 7:23 am     Reply with quote

Thanks! that really helped!
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