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

18F2525 ADC problem

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



Joined: 09 Oct 2009
Posts: 13
Location: SF,BG

View user's profile Send private message

18F2525 ADC problem
PostPosted: Mon Jun 07, 2010 5:02 am     Reply with quote

Hi guys,
I am trying to do ADC with this microcontroller: 18F2525 and my code:
Code:

#include <18F2525.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#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 NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include "D:\.......\Fuel\main.h"

void main()
{
   float adc_values;
   setup_adc(ADC_CLOCK_DIV_4);
   setup_adc_ports(AN0|VSS_VDD);
   set_adc_channel(AN0);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
 
  for(;;)
   {
   adc_values=read_adc();
   printf("\f%3.0f",adc_values);
   delay_ms(300);
   }
}

The problem is that "adc value" is not correct. I use AN0 (PIN 2). When I change voltage on AN0, adc value not change and adc value stay ~30. When I touch with my finger other ANALOG PORTS the value rise up at >700~800, but when I power others ANALOG PORTS value is again ~30. I made everything what I know.
Sorry for my bad english.
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 07, 2010 11:21 am     Reply with quote

Try a more simple program that will display ADC values from 0 to 1023
based on the input voltage. Also, make sure you have a ground
connection between the device that supplies the ADC input voltage and
the PIC board's ground. You can't just have the input signal wire to
pin A0. There must also be a ground wire between the two devices,
connected to the ground on each device (or board).
Code:

#include <18F2525.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//============================
void main()
{
int16 adc_value;
 
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_4);
set_adc_channel(0);
delay_us(20);

while(1)
  {
   adc_value = read_adc();
   printf("%lu", adc_value);
   delay_ms(500);
  }
}
peterl



Joined: 09 Oct 2009
Posts: 13
Location: SF,BG

View user's profile Send private message

re: PIC18F2525 ADC
PostPosted: Tue Jun 08, 2010 1:12 am     Reply with quote

The problem was here:
set_adc_channel(AN0); is not correct, correct is set_adc_channel(0);
10X Very Happy
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