View previous topic :: View next topic |
Author |
Message |
david90
Joined: 25 Feb 2006 Posts: 23
|
Trying to get ADC to work |
Posted: Sat Feb 25, 2006 1:29 am |
|
|
Code: | #include <16F688.h>
#device adc=8;
#use delay(clock=8000000)
#fuses INTRC_IO,NOPROTECT,NOPUT,NOWDT
void main()
{
int a;
setup_oscillator(OSC_8MHZ);
setup_adc_ports(1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
set_tris_a (1);
set_tris_c (0);
while(true){
OUTPUT_LOW(PIN_A1);
delay_ms(1000);
OUTPUT_HIGH(PIN_A1);
delay_ms(1000);}
output_c(READ_ADC());
} |
Above is my test code for the ADC. I'm using 16F688. I want to input my analog voltage at pin AN0 and output the 8bit ADC result to Port C.
I put leds on my port c pins but they aren''t lighting up when I put AN0 (pin 13) to VCC. Is something wrong with my code? I've tried everything. I made sure that the PIC is cycling thru the instruction by making an LED blinks on PIN A1. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Re: Trying to get ADC to work |
Posted: Sat Feb 25, 2006 1:43 am |
|
|
david90 wrote: | Code: | setup_adc_ports(1); |
|
Why aren't you using the ADC constants provided by CCS (such as ALL_ANALOG) ? |
|
|
david90
Joined: 25 Feb 2006 Posts: 23
|
|
Posted: Sat Feb 25, 2006 2:19 am |
|
|
It wouldn't compile if I use ALL_ANALOG because it is not define in the 16F688.H.
Update: i got the adc to work. A crystal oscillator for another uC on my board seems to interfere with the ADC. If I remove the crystal, the readings are stable but if I connect it back, the readings jump. |
|
|
Ttelmah Guest
|
|
Posted: Sat Feb 25, 2006 4:19 am |
|
|
The fact that 'ALL_ANALOG' is not defined, implies you are on a pretty early version of the compiler for this chip. However 'sAN0', is defined, and will do what your current constant does, while on the older compiler the assumed format for 'ALL_ANALOG', was:
aAN0|sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7
You are not defining any Vref range, and you are not disabling the comparator, which shares with the same pins. So try:
Code: |
#include <16F688.h>
#device adc=8;
#use delay(clock=8000000)
//Have you got MCLR pulled up?. Otherwise add NOMCLR
#fuses INTRC_IO,NOPROTECT,NOPUT,NOWDT,NOBROWNOUT
void main() {
int a;
setup_oscillator(OSC_8MHZ);
//Disable the comparator
setup_comparators(NC_NC_NC_NC);
//Select ADC channel, and input range
setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16);
//Note that 'internal', is geerally not recommended on most
//chips above 4MHz
set_adc_channel(0);
//These TRIS ops do nothing, since you have not selected fast_IO
//set_tris_a (1);
//set_tris_c (0);
while(true){
OUTPUT_LOW(PIN_A1);
delay_ms(1000);
OUTPUT_HIGH(PIN_A1);
delay_ms(1000);}
output_c(READ_ADC());
}
}
|
Best Wishes |
|
|
|