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

ADC inconsistencies

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



Joined: 17 Apr 2007
Posts: 20
Location: tijuana,bajacalifornia,mexico

View user's profile Send private message MSN Messenger

ADC inconsistencies
PostPosted: Mon Jul 02, 2007 12:33 pm     Reply with quote

I have a little problem, I connected an LCD with a 18f4420 , I just tried to emulate a digital voltmeter.

It works, but with some inconsistencies, I mean, it shows a measure, but it changes in a range of +-70mV, I don't know why, but this behavior is not adequate to my project.

I also measured the same voltage at same time with my digital Multimeter (in parallel), and the voltage never changes in the multimeter but in the LCD it changes.

I tried to put a capacitor in parallel with the ADC pin, but it still varies.

Any help will be well came.

this is may code:


Code:

#include <18F4420.h>
#device adc=10
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
//#FUSES XT                       //Crystal osc <= 4mhzUT
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#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 LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#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)
#use delay(clock=4000000)
#include <hplcd.C>
#include <stdio.h>
#include <stdlib.h>
#use fast_io(d)
#use standard_io(c)
int16 vi=0;
float va;

#int_EXT
EXT_isr()
{

}

#int_EXT1
EXT1_isr()
{

}



void main()
{
   set_tris_d(0);
   set_tris_c(1);
   output_c(0xff);
   delay_ms(1000);
   output_c(0x0);
   setup_adc_ports(AN0|VSS_VDD);
//   setup_adc_ports(AN0|VSS_VREF);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_2);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_EXT1);
   enable_interrupts(GLOBAL);
   setup_low_volt_detect(FALSE);
//   setup_oscillator(FALSE);
   setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF);
   //my code
   lcd_init();
   printf(lcd_putc,"\f");
   printf(lcd_putc,"VOLTAGE:");
   while (true)
   {
      lcd_gotoxy(1,2);
      vi=read_adc();
      va=(vi*5/1023.0);
      printf(lcd_putc,"%.6f",va);
   }
}

_________________
Every little thing gonna be alright
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 02, 2007 12:47 pm     Reply with quote

Quote:

#FUSES LVP

#use delay(clock=4000000)

setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF);

setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_2);

You need to fix the problems shown above.

Do you have a low voltage programmer ? 99% of all people have
a high-voltage programmer. In that case, the fuse must be NOLVP.

You have the #use delay() set for one frequency, but you have
the oscillator set to a different frequency.

Look in the A/D section of the 18F4420 data sheet. What clock divisor
does it say to use for 4 MHz operation ?

Where's the line of code to set the channel ? Example:
Code:
set_adc_channel(0);


At least two or three of the problems above could be the cause of
your problem.
elphi



Joined: 17 Apr 2007
Posts: 20
Location: tijuana,bajacalifornia,mexico

View user's profile Send private message MSN Messenger

sorry
PostPosted: Mon Jul 02, 2007 6:44 pm     Reply with quote

OK, I post a bad version of may software, sorry , the #use delay is 8000000 as in the setting of the internal oscillator.

The datasheet says that I must use an 8TOSC, but I don’t understand that. Does it means some like :

Quote:
setup_adc(ADC_CLOCK_DIV_8|ADC_TAD_MUL_8);


and sincerely I don’t know what is the channel in the adc.

It’s embarrassing but, i will need more support
_________________
Every little thing gonna be alright
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 02, 2007 7:27 pm     Reply with quote

Quote:
I don’t know what is the channel in the adc.


Here, in your code, you setup the A/D to use channel 0:
Quote:
setup_adc_ports(AN0|VSS_VDD);


That's why I said you must add this line of code:
Code:
set_adc_channel(0);
elphi



Joined: 17 Apr 2007
Posts: 20
Location: tijuana,bajacalifornia,mexico

View user's profile Send private message MSN Messenger

ok
PostPosted: Mon Jul 02, 2007 10:23 pm     Reply with quote

Ok, i get it, i made the change, but regarding the other seccion of your answer:
Quote:

Look in the A/D section of the 18F4420 data sheet. What clock divisor
does it say to use for 4 MHz operation ?


The datasheet says that I must use an 8TOSC, but I don’t understand that. Does it means some like :

Quote:
setup_adc(ADC_CLOCK_DIV_8|ADC_TAD_MUL_8);


but remember that i am using the 8mhz internal clock.

tnks it advance for your help.
_________________
Every little thing gonna be alright
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 03, 2007 1:39 am     Reply with quote

That would work. According to the data sheet, the minimum Tad is 0.7 us.
Using the 8 MHz internal oscillator and dividing it by 8 gives you a 1 MHz
Tad clock, which gives a period of 1.0 us. That satisfies the minimum
requirement for Tad.

According to the data sheet, the minimum acquisition time is 2.4 us.
That means you could use ADC_TAD_MUL_4 to set the acquisition time,
because with a Tad of 1.0 us, this would give 4.0 us and it would satisfy
the Tacq requirement. But the ADC_TAD_MUL_8 value would still work.
It's longer than you need, but I doubt if it would make much difference
in the result.
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