View previous topic :: View next topic |
Author |
Message |
williamspv
Joined: 17 Feb 2006 Posts: 3
|
A/D 16F916 Internal 4 Mhz Oscillator |
Posted: Fri Feb 17, 2006 11:13 am |
|
|
My problem is that I continually get either 0 0r 0x3ff in the ADRESH and ADRESL as the result of the A/D reading.
I am looking at table 12-1 of the spec and it shows an A/D RC clock source. Am I required to use this source since I am using an internal oscillator?
Code:
#include <16F916.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //Reset when brownout detected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES DEBUG //Debug mode for use with ICD
#use delay(clock=4000000)
unsigned int16 value = 0;
void main()
{
setup_adc_ports(sAN0|sAN1|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8);
setup_spi(FALSE);
setup_lcd(LCD_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW|-2);
setup_oscillator(OSC_4MHZ);
set_adc_channel(0);
delay_us(20);
while(1)
{
value = Read_ADC();
delay_ms(500);
}
}
Thanks for any help you can provide,
Patrick |
|
|
Ttelmah Guest
|
|
Posted: Fri Feb 17, 2006 11:22 am |
|
|
Setup_vref, only works on chips that have a programmable reference. I don't think the 819, does....
Best Wishes |
|
|
Guest
|
|
Posted: Fri Feb 17, 2006 1:17 pm |
|
|
Ttelmah wrote: | Setup_vref, only works on chips that have a programmable reference. I don't think the 819, does....
|
I have removed that code along with some of the other setup code that doesn't apply and I get the same result. I am using the 16F916, not the 819.
Code:
#include <16F916.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //Reset when brownout detected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES DEBUG //Debug mode for use with ICD
#use delay(clock=4000000)
unsigned int16 value = 0;
void main()
{
setup_adc_ports(sAN0|sAN1|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8);
//setup_spi(FALSE);
//setup_lcd(LCD_DISABLED);
//setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
//setup_timer_1(T1_DISABLED);
//setup_timer_2(T2_DISABLED,0,1);
//setup_comparator(NC_NC_NC_NC);
//setup_vref(VREF_LOW|-2);
//setup_oscillator(OSC_4MHZ);
set_adc_channel(0);
delay_us(20);
while(1)
{
value = Read_ADC();
delay_ms(500);
}
} |
|
|
williamspv
Joined: 17 Feb 2006 Posts: 3
|
|
Posted: Fri Feb 17, 2006 1:20 pm |
|
|
Even with the other setup code commented I still get the same results either 0 or 0x3ff for voltages varying from 3 - 5 volts at the pin.
Patrick |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 17, 2006 1:25 pm |
|
|
He's actually using the 16F916, but there is a problem with the
setup_vref() statement.
You have got this statement:
Quote: | setup_vref(VREF_LOW|-2); |
But the 16F916.H file doesn't say that negative numbers are allowed.
It says you can bitwise OR the constants with a number from 0 to 15.
Quote: | // Constants used in setup_vref() are:
//
#define VREF_LOW 0xa0
#define VREF_HIGH 0x80
// Or (with |) the above with a number 0-15
#define VREF_A2 0x40
|
Another problem is that you have the compiler configured to return
an 8-bit A/D result, with this line:
But you appear to want a 10-bit result, because you put the result
into 'value', which is a 16-bit variable. I suggest that you change it to:
|
|
|
williamspv
Joined: 17 Feb 2006 Posts: 3
|
|
Posted: Fri Feb 17, 2006 1:45 pm |
|
|
Thank you for your help. VREF shouldn't have an effect on my A/D reading because I have VSS_VDD selected, right?
If I have ADC=8 I get 255. I I have ADC=10 I get 1023 or 0x3ff. Why does the A/D return only the lowest value(0) or the highest value(255 or 1023)? I have added a set_tris_a command as another user did in a different thread, but I am still getting this result.
I also forgot to mention that I am using the compiler version 3.242.
Revised Code:
#include <16F916.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES MCLR //Master Clear pin enabled
#FUSES NOCPD //No EE protection
#FUSES BROWNOUT //Reset when brownout detected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES DEBUG //Debug mode for use with ICD
#use delay(clock=4000000)
unsigned int16 value = 0;
void main()
{
set_tris_a(0b00000011);
setup_adc_ports(sAN0|sAN1|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_8);
// setup_spi(FALSE);
// setup_lcd(LCD_DISABLED);
// setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
// setup_timer_1(T1_DISABLED);
// setup_timer_2(T2_DISABLED,0,1);
// setup_comparator(NC_NC_NC_NC);
// setup_vref(VREF_LOW|-2);
// setup_oscillator(OSC_4MHZ);
set_adc_channel(1);
while(1)
{
value = Read_ADC();
delay_ms(500);
}
}
Patrick |
|
|
|