View previous topic :: View next topic |
Author |
Message |
Nicki Guest
|
Receive A/D bits |
Posted: Sun Mar 23, 2008 1:07 pm |
|
|
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
|
|
Posted: Sun Mar 23, 2008 3:59 pm |
|
|
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
|
|
Posted: Sun Mar 23, 2008 9:42 pm |
|
|
Because I am VERY new to using PIC's and do not know how to. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Nicki Guest
|
|
Posted: Tue Mar 25, 2008 5:44 pm |
|
|
Oh thank you PCM programmer!
that made my job a lot easier
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
|
|
Posted: Tue Mar 25, 2008 5:55 pm |
|
|
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(); |
|
|
|
|