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 support@ccsinfo.com

Receive A/D bits

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







Receive A/D bits
PostPosted: Sun Mar 23, 2008 1:07 pm     Reply with quote

I am very new with the PIC. The one I am using is the 18F4520. I have an A/D converter (ADC0804LNC) that is working and sending 8 bits to LED's. What I would like to know is how to receive those bits with the pic and output them via RS232.

Any help would be greatly appreciated.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Mar 23, 2008 3:59 pm     Reply with quote

The ADC0804 is an old type 8-bit converter. Why do you want to use this external converter when your PIC has an internal 10-bit converter which is more exact and easier to use?
Nicki
Guest







PostPosted: Sun Mar 23, 2008 9:42 pm     Reply with quote

Because I am VERY new to using PIC's and do not know how to.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 23, 2008 10:00 pm     Reply with quote

Here is an example program that uses the internal A/D converter in the PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
Nicki
Guest







PostPosted: Tue Mar 25, 2008 5:44 pm     Reply with quote

Oh thank you PCM programmer!

that made my job a lot easier Wink

Here's what I have:

Code:
#include <18F4520.h>
#device ICD=TRUE

#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, HS, NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

//============================
void main() {
   int16 adc_value;
   float volts;

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL);
   set_adc_channel(0);

   while(TRUE){
      adc_value = read_adc();
      volts = (float)(adc_value * 5)/255.0; //for 8 bit A/D

       printf("\r\nThe result is: %f ", volts);
      //printf("\r\nTesting");
      delay_ms(500);
   }
}


It sends the voltage to the serial port monitor the voltage based on what is dialed on the dial.

Now if I want to input my own external voltage, do I just assign a pin (say PIN_B5) as an input and read that?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 25, 2008 5:55 pm     Reply with quote

You have to use analog pins. Only certain pins on the 18F4520 can
be used as analog pins. This is in the data sheet. To use both AN0
and AN1 pins for analog, you need to specify it with this line:
Code:
setup_adc_ports(AN0_AN1);

AN1 is on pin RA1.

Then you have to change to the desired channel before you read the
analog voltage. You must also wait for a short time after you change
the channel, before you read the voltage. Example:
Code:
set_adc_channel(0);
delay_us(20);
value0 = read_adc();

set_adc_channel(1);
delay_us(20);
value1 = read_adc();
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