Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] ADC PIC18f4520 LM35 (ADCON 2 setting)

Status
Not open for further replies.

doucheful80

Junior Member level 1
Joined
Dec 14, 2011
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,414
;*** ADC
movlw b'00000001' ;ADC enable
movwf ADCON0
movlw b'00001110' ;vrefs=vdd,vss .AN0 used only
movwf ADCON1
movlw b'10010100' ;ADRESH read as zero, follow
movwf ADCON2

Im using a LM35 to obtain temperature then calculate the speed of sound. But the problem is that it seem to be unstable. (lm35 out volt is stable when measured with multimeter)
for example im measuring temperature 25-31 C. When at 28C, the adreshL can be either decimal 37, 42 sometime 16 for awhile at the beginning.

I looked into the datasheet of pic18f4520 and check the acquisition time and A/D conversion clock. ADCON2 is currently set at 10010100, or AD conversion clock, =Fosc/4 and 4TAD for acquisition time. What does conversion clock and acquisition time means exactly? so far i get that conversion clock is depending on your oscillator (im using 4MHz crystal). and the acquisition time is the time it take to sample the voltage before conversion begins.

I calculated the acquisition time by refering the 4520 datasheet, basically everything is the same except the temperature, i put in a temp of 30C. (the example gave 85C)

So follow it, (30-25C)x0.02us/C = 0.1u
Tamp given 0.2u
Tc ,in my circuit i assume its the same with the datasheet, so= 1.05

Tacq (aquisition time) = 1.35us

Does this means that the Tacq should be 2Tad since my clock cycle is 2us (4MHz crystal), and is ADCON2 configuration correct? Thanks for helping ..!
 

I looked into the datasheet of pic18f4520 and check the acquisition time and A/D conversion clock. ADCON2 is currently set at 10010100, or AD conversion clock, =Fosc/4 and 4TAD for acquisition time. What does conversion clock and acquisition time means exactly? so far i get that conversion clock is depending on your oscillator (im using 4MHz crystal). and the acquisition time is the time it take to sample the voltage before conversion begins.

The TAD is the ADC Clock period, which for a 18FXXXX device has a required minimum of 0.7us. The ADC Clock prescaler divides the Fosc to generate the appropriate ADC Clock and TAD.

The specified number of TADs to complete the conversion is dependent on the exact number of bits in the required conversion. For example a 10-bit ADC requires 11TAD to successfully carry out the 10-bit conversion.

Here is a fairly good tutorial on the ADC module of the PIC18F series:

Using Analog to Digital Converter (ADC) – PIC Microcontroller Tutorial

Although it provides an example in C, you should be able to glean the required information concerning the correct setup of the ADC Module for your application.

Also ensure you have shifted the result of the ADC correctly within the ADRESH and ADRESL register pair.

Another issue which can be the source of frustration when coding for a PIC18FXXXX device, is being aware of the actual system clock frequency versus the crystal frequency.

These devices have a PLL which by default multiplies the input crystal frequency by x12, producing an actual system clock of 48MHz, instead of the assumed 4MHz of the attached crystal.

Verify your configuration bit settings as to whether the PLL is enabled.

I just found this tutorial covering the ADC Module of the PIC18F which also has examples in PIC Assembly Language:





BigDog
 
Thx bigdog..i read the website..and i tried both 4Tosc and 2Tosc, which correspond to 4us and 2us when the minimum is 0.7us..and 2Tad since the site said the be above
2.45us. 2Tad, = 8us and 4us for Tacq (aquisition time).
But my areshL is still unstable and the coverted value does not make sense..it jump around 26 to 30 (ARESHL), ADRESH = 0. The current temperature is 28C. The vref i measure are 4.10v and -0.02v i used the Vdd and Vss.
Have you done an LM35 conversion before..would like a sample of the code...thx!!
 

Hi,

Have run a LM35 on the 4520 with no problems, though you will always get a little jitter on the 2 lsb's of the 10 bit result - though that should not equate to a 2 to 4 degree variance you are experiencing.

Apart from the software have you considered the hardware in terms of a stable psu , short connecting leads to the pic and lm35.
Are you building on a breadboad, have you tried using the 4meg internal oscillator as I always do ?

Perhaps posting your circuit might show some problem.
 

hi wp100, if it not too much too ask...can you kindly post ur program and circuit diagram here..if it still available..thanks!

i still have no idea what ADCON2 should be set as...i tested maybe unstableness cause by jumping +Vref, since my lm35 output a low voltage..so jumping of vref might cause it to jump as well, since the conversion take the proportion between the +vref and -vref (set to Vdd and Vss)..but it still jump when i connect 2.5v to the analogue input.
 
Last edited:

Hi,

Vref +&- , nooo.. forget them for now, they over complicate things.

Just connect the lm35 to VDD with its output and the 2k resistor to the adc port as the datasheet shows.

Cannot find the old LM35 code - probably deleted it as in the bench tests I did the LM35 was not accurate enough for my needs.

This is some old code showing a simple adc to leds example on the 18Fs

Just re specify the chip as 4520, its set for the 4meg internal oscillator so no need for your xtal.
The rest of the code should be obvious.
 

Attachments

  • adcled.rar
    1.3 KB · Views: 95
Thx wp100, I just made the adc stable.. in my circuit (breadboard) there is a blinking led and a 7seg displaying the content in adresL...the blinking somehow cause the current flow to be high low alternatively in each cycle. I off the blinking and the result was stable the aresl have value of 71, 72, 72..minor i bit change once in awhile. I also connect the output of lm35 to a 2.2k resistor before entering AN0, with resistor ARESL=71decimal, without 2.2k resistor ARESL=79decimal..

In my project im running an ultrasonic receiver while capturing the ambient temp (for calculation of the speed of sound at a temperature)..and also output a value to ports..so hopefully i can make it stable while PIC is giving different output through ports.
 

Hi,

Sounds like you still have 2 hardware problems, basically poor decoupling of the +5v rail and poor connections on the breadboard.

A circuit diagram would be helpful to see what caps you have fitted to your circuit and power supply.
Also sounds like your psu may be under rated or the leds incorrectly resistored , so drawing too much power and dropping the voltage.

The thin leads on the xtal and its caps can give all sorts of problems on a breadboard - suggest you use the internal osc as shown in that code example - its good for most general work like you are doing.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top