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

PIC18F45K22 ADC Problem

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



Joined: 05 Feb 2014
Posts: 1

View user's profile Send private message

PIC18F45K22 ADC Problem
PostPosted: Wed Feb 05, 2014 9:29 am     Reply with quote

Hi everyone,

I've a small question, I only need a working code for ADC and it has to be like these type. I write these codes, but it doesn't work on circuit. So, where is the mistake in these codes given below. Maybe it is about "fuses" ?

Code:

#include <18F45K22.h>
#device ADC=8

#fuses XT,INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)

#define LED PIN_B2
#define LED1 PIN_B0
#define LED2 PIN_B4


unsigned long int g;
float fb;





//======================================
void main(void)
{

setup_adc(adc_clock_div_32);
setup_adc_ports(ALL_ANALOG);


   enable_interrupts(INT_AD);
   enable_interrupts(GLOBAL);   
     
while(1);
{
set_adc_channel(1);
delay_ms(20);
g=read_adc();
fb=1+(0.001953125*g);




  if (fb>0)
  {
    output_high(LED);
  } 
    else
  { 
  output_low(LED); 
  }
}
}

Ttelmah



Joined: 11 Mar 2010
Posts: 19458

View user's profile Send private message

PostPosted: Wed Feb 05, 2014 10:19 am     Reply with quote

First, you must only enable one oscillator.

Currently you have 'XT', which says to use an external crystal, and
'INTRC_IO' which says to use the internal oscillator, and enable I/O on external oscillator pins.

Then you must _never_ enable an interrupt without a handler. You don't want INT_AD enabled. This is only used when you are using a hardware event to enable the ADC (CCP for example), which you are not doing, and enabling it without a handler will crash the code when the ADC completes.

Then you have the ADC set to only return 8 bits. (ADC=8)....

Then don't use floats. Seriously, either use ADC counts, or convert to a scaled integer. Float maths should be avoided unless it is completely necessary. It is bulky, and slow.
As written, it is always going to be >0. ADC values are always positive, and you are adding '1', so the value is going to be 1, 1.00193 etc.. Never -ve.

Then you should only select the ADC channel once, outside the loop.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Wed Feb 05, 2014 10:20 am     Reply with quote

Well, without my morning coffee yet, the first thing that I see is how would "fb" ever be less than 1? You are adding some unsigned number times a positive coefficient to 1. That will always give you "1+something". Since you don't say exactly what "doesn't work on circuit" means, that is the first thing I notice. Since you are testing for "fb>0" that will always be true.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
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