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 CCS Technical Support

PIC18F8723 Analog to Digital Setup

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








PIC18F8723 Analog to Digital Setup
PostPosted: Fri Mar 27, 2009 11:45 am     Reply with quote

CCS version 4.068

Hello!! I am trying to setup my A2D conversion on my pic but the voltage I read with a meter and the voltage I read with the pic are off by about 120mv. My chip uses a 12 bit A2D. here is my code.

This is the function I call:
Code:

void Get_Altitude()
{
int i;
atemp=0;

for(i=0;i<10;++i)
  {
    set_adc_channel(1);
    delay_us(20);
    value=read_adc();
 
    avoltage=((value*(5.0367/(4095)))); 
    pressure=((avoltage)/(5.0367*.009)+.095/.009)*0.296133971;
 
    atemp=atemp+pressure;
  }

atemp=atemp/10;
height=((1-pwr((pressure/29.785),.19026))*288.15)/.00198122;
 

  // printf("height %ld pressure %2.3f voltage %5.3f avgpressure %5.3f \r\n",height, pressure,avoltage, temp);
   //delay_ms(500);
   

}


Here is my setup in the main:
Code:

void main()
{
   //Basic Chip Setup
   setup_adc_ports(AN0_TO_AN1|VREF_VREF );   
   setup_adc(ADC_CLOCK_DIV_8);
   setup_psp(PSP_DISABLED);
   //setup_spi2(SPI_MASTER|SPI_MODE_3|SPI_CLK_DIV_16);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(False);
   setup_oscillator(OSC_4MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);

And:
Code:

#include <18F8723.h>

//#device ICD=TRUE
#device adc=12

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                    //Internal Osc
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV25                   //Brownout reset at 2.5V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
//#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES MCU                      //Microcontroller Mode
#FUSES NOWAIT                   //Wait selections unavailable for Table Reads or Table Writes
#FUSES BW16                     //16-bit external bus mode
#FUSES ABW16                    //16-bit Address bus
#FUSES ECCPE                    //Enhanced CCP PWM outpts multiplexed with RE6 thorugh RE3

#use delay(clock=4000000)

I had everything working on a 4520 nicely but I needed more resolution. Thanks in advance.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 27, 2009 12:23 pm     Reply with quote

Get rid of the analysis code and display the raw ADC values every 500 ms.
Display them in hex (or decimal) and look for any problems.

See this recent discussion of Vref issues:
http://www.ccsinfo.com/forum/viewtopic.php?t=38276&highlight=vref
Guest








PostPosted: Fri Mar 27, 2009 12:45 pm     Reply with quote

My "Value" varies between 3418 and 3421. This along with the fact that that when I multiply that by 5/4095 I am still off 120mv which makes a huge difference for my application. But only varying a few steps I feel is pretty good. I don't know if there is a better way to setup the chip.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 27, 2009 12:57 pm     Reply with quote

Quote:
setup_adc_ports(AN0_TO_AN1|VREF_VREF );
setup_adc(ADC_CLOCK_DIV_8);

I don't know if there is a better way to setup the chip.

See this section in the 18F8723 data sheet:
Quote:
TABLE 2-1: TAD vs. DEVICE OPERATING FREQUENCIES

This clock divisor value is the correct one for your 4 MHz oscillator freq:
Quote:
ADC_CLOCK_DIV_4


18F8723 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/18F8723%20Data%20Sheet_39894A.pdf
Guest








PostPosted: Fri Mar 27, 2009 1:28 pm     Reply with quote

Yeah I went through and tried setting that differently. It had no effect on my output at all. Can I go and just subtract the difference in the voltage, it would be like a manual offset?
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