View previous topic :: View next topic |
Author |
Message |
Andreiutzu
Joined: 02 Feb 2011 Posts: 5
|
Not getting 10 bit adc from pic18f452 !! Why ? |
Posted: Wed Feb 02, 2011 1:44 am |
|
|
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
|
|
Posted: Wed Feb 02, 2011 2:10 am |
|
|
Uncomment this line
|
|
|
Andreiutzu
Joined: 02 Feb 2011 Posts: 5
|
|
Posted: Wed Feb 02, 2011 2:11 am |
|
|
Thanks for the quick reply !
It doesn't work even if I take your suggestion !
Any ideas ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Wed Feb 02, 2011 3:37 am |
|
|
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
|
|
Posted: Wed Feb 02, 2011 3:43 am |
|
|
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 You are right indeed. PIC18f452 works with up-to 40 MHz crystal.
Again, thank you for your answer and excuse my stupid mistake ! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Wed Feb 02, 2011 4:05 am |
|
|
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
|
|
Posted: Wed Feb 02, 2011 4:09 am |
|
|
Thank you !
With your help, i've learned some new things today ! |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Feb 02, 2011 6:45 am |
|
|
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
|
|
Posted: Wed Feb 02, 2011 6:47 am |
|
|
Ttelmah was right !
Including the #device ADC 10 statement finally did the trick. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Wed Feb 02, 2011 8:39 am |
|
|
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
|
|
Posted: Mon Apr 09, 2012 11:13 pm |
|
|
@Ttelmah..
I have mentioned #device adc=10 but still I get adc_read= 255
ver 4.057 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Tue Apr 10, 2012 1:22 am |
|
|
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 |
|
|
|