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

Not getting 10 bit adc from pic18f452 !! Why ?

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



Joined: 02 Feb 2011
Posts: 5

View user's profile Send private message

Not getting 10 bit adc from pic18f452 !! Why ?
PostPosted: Wed Feb 02, 2011 1:44 am     Reply with quote

I'm using a PIC18F452 to make some adc measurement.

However, the values that I get through the serial port correspond only to 8 bit adc.

I'm using a 24 Mhz oscillator for this microcontroller (the datasheet specifies values from 4 Mhz to 10 Mhz ). Could that be the problem ?

Here is my code :
Code:

#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
//#device ADC=10
#use delay (clock=24000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

unsigned int16 citeste_adc ()
 {
      unsigned int16 value1;
      value1=read_adc();
      return value1;
 }
 
 void clipeste_led (unsigned int16 delay)
  {
      output_high(PIN_C2);
      delay_ms(delay);
      output_low(PIN_C2);
      delay_ms(delay);
  }

//#define LED PIN_C2

void main()
{
   unsigned int16 value;
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_64);
   set_adc_channel(7);
   
    while(true)
    {
        value=citeste_adc();
        clipeste_led(value);
        printf("\r\n Val:%ld",value);
       
      }

}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 2:10 am     Reply with quote

Uncomment this line
Code:
//#device ADC=10
Andreiutzu



Joined: 02 Feb 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 2:11 am     Reply with quote

Thanks for the quick reply !

It doesn't work even if I take your suggestion !

Any ideas ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 3:37 am     Reply with quote

OK.
With adc=10, it should work. This is needed.

The code is slightly over complex, and you have some odd comments. Where does the data sheet talk about 4 to 10MHz?. It only refers to 4 to 10MHz, _with PLL active_. The chip supports operation up to 40MHz, and internal oscillator with a crystal up to 25MHz. Hence 10Mhz, with *4PLL, or crystals up to 25MHz. If you had a clock problem, your serial wouldn't work. Table 22-4 1A. 4 to 25MHz using HS.

What compiler version?. If you have something like 4.020, then this is the problem. Anything below about 4.070, was not a working compiler...

What numbers do you actually see?.
Code:

#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10
#use delay (clock=24000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main(void) {
   unsigned int16 value;
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_DIV_64);
   set_adc_channel(7);
   setup_psp(PSP_DISABLED);
   delay_us(20); //must delay after selecting ADC channel

   while(true) {
      value=read_adc();
      printf("\r\n Val:%ld",value);
   }
}

What numbers do you get with 2.5v measured on pin 10?.

Best Wishes
Andreiutzu



Joined: 02 Feb 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 3:43 am     Reply with quote

Thank you Ttelmah !

I got it working now !

I'm kind of a newbie in working with microcontrollers.

That's why i misread the datasheet Very Happy You are right indeed. PIC18f452 works with up-to 40 MHz crystal.

Again, thank you for your answer and excuse my stupid mistake ! Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 4:05 am     Reply with quote

Just to correct your comment there.
No, the 452, _does not_ work with up to a 40MHz crystal.....

It works with:

Up to 10MHz crystal, and PLL.
Up to 25MHz crystal.
Up to 40MHz _external clock input_.

The oscillator is rated for 25Mhz max.
Horribly pedantic, but worth being aware of the limits.

It might be worth saying what the problem actually 'was', to possibly avoid another poster having a "wall, head, impact technology testing" moment.

Best Wishes
Andreiutzu



Joined: 02 Feb 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 4:09 am     Reply with quote

Thank you !

With your help, i've learned some new things today ! Wink
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 6:45 am     Reply with quote

Could you tell us what the solution finally was?
_________________
The search for better is endless. Instead simply find very good and get the job done.
Andreiutzu



Joined: 02 Feb 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 6:47 am     Reply with quote

Ttelmah was right !

Including the #device ADC 10 statement finally did the trick.
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Wed Feb 02, 2011 8:39 am     Reply with quote

Er. Fvm gave that answer, right at the start of the thread, and you said it didn't work.

Best Wishes
riz179



Joined: 06 Apr 2012
Posts: 21

View user's profile Send private message

PostPosted: Mon Apr 09, 2012 11:13 pm     Reply with quote

@Ttelmah..
I have mentioned #device adc=10 but still I get adc_read= 255
ver 4.057
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Tue Apr 10, 2012 1:22 am     Reply with quote

There are three things in all needed:
1) #device ADC=10
2) Using an int16, not an 'int' for the returned value
3) Using %l with the printf output format.

The original poster had two of these already done, hence they were not mentioned.

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