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

A/D 16F916 Internal 4 Mhz Oscillator

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



Joined: 17 Feb 2006
Posts: 3

View user's profile Send private message

A/D 16F916 Internal 4 Mhz Oscillator
PostPosted: Fri Feb 17, 2006 11:13 am     Reply with quote

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







PostPosted: Fri Feb 17, 2006 11:22 am     Reply with quote

Setup_vref, only works on chips that have a programmable reference. I don't think the 819, does....

Best Wishes
Guest








PostPosted: Fri Feb 17, 2006 1:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 17, 2006 1:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 17, 2006 1:25 pm     Reply with quote

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:
Quote:
#device adc=8

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:
Code:
#device adc=10
williamspv



Joined: 17 Feb 2006
Posts: 3

View user's profile Send private message

PostPosted: Fri Feb 17, 2006 1:45 pm     Reply with quote

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
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