Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Stuck with ADC and justification!

Status
Not open for further replies.

bigal_scorpio

Active Member
Hi to all,

I am presently involved in my project of making a power supply with an LCD readout for voltage.

The other thread I started was in general electronics and I am with the help of Eric Gibbs, just about getting things moving on the PSU side of things.

Now I am coming to the PIC micro part which I am using a 16F872 to read 3 sets of voltage and 1 current measurement. My problem is due to ADC being new to me. Most of my projects so far have been digital only. I program using PicBasic Pro.

Can anyone explain in simple terms what Left Justify or Right Justify means. I just don't get that when I have a 10bit ADC and justify it either way, if it reads 6bits as zero then surely won't it leave me with only 4 bits of data?

Also I am a little puzzled as to what to do with ADCON1? I am wanting 4 ADC readings so I figure I need to set the last four bits as 0011 (I am also using a 4096 presicion Ref on Vref) but WHERE in the program do I use it? Should I set the MSB to Left or right Justify? And finally do I need to set ADCON1 more than once or is it sufficient to set and leave it?

All help appreciated, Al

PS here is the part of the 16F872 datasheet showing the ADCON1 data.
 
As the result of the ADC is 10 bits you can have the result in the 8 bit registers ADRESH and ADRESL in two different ways...

Right justified..

.....ADRESH.................ADRESL........
|X|X|X|X|X|X|9|8| |7|6|5|4|3|2|1|0|

Left justified..

.....ADRESH.................ADRESL........
|9|8|7|6|5|4|3|2| |1|0|X|X|X|X|X|X|

If you want to run with just an 8 bit result. left justified then read ADRESH only

If you want 10 bit you need to add the two registers

EDIT!! Combine the two registers, ie.. multiply the ADRESH before adding the ADRESL....
 
Last edited:
Hi Al,

May I ask if PBP doesn't have built-in ADC functions for you to use? And if so, is there a chance that the functions may automatically take care of the justification settings?

Regards, Mike
 
Hi Ian,

Thanks mate! You explained that perfectly! I now understand it at last.

I have been looking for hours for the justification meanings.

Al
 
Hi Mike,

Well you would think that the manual would have all the info required but here is the COMPLETE part of the manual pertaining to ADC.

Then they expect you to be fluent? ;)

Al

Please note that though the justification is seemingly taken care of that the channel being read can be more than one on some chips and it seems they expect you to know about that beside knowing basic.

The info on ADCON1 is very limited and nowhere does it say where and when it needs to be applied.

5.2. ADCIN
ADCIN Channel,Var
Read the on-chip analog to digital converter Channel and store the
result in Var. While the ADC registers can be accessed directly, ADCIN
makes the process a little easier.
Before ADCIN can be used, the appropriate TRIS register must be set to
make the desired pins inputs. The ADCON, ANCON and/or ANSEL
registers must also be set to assign the desired pins to analog inputs and
in some cases to set the result format and clock source (set the clock
source the same as the DEFINE specified for it, below). See the
Microchip data sheets for more information on these registers and things
like the clock source and how to set them for the specific device. Note:
The PIC14000 ADC is not compatible with the ADCIN instruction.
Depending on the device, it may have an 8-, 10- or 12-bit ADC. For
many PIC MCUs, the high bit of ADCON0 or ADCON1 controls whether
the result is left or right justified. In most cases, 8-bit results should be
left justified (ADCON1.7 = 0) and 10- and 12-bit results should be right
justified (ADCON1.7 = 1).
Several DEFINEs may also be used. The defaults are shown below:
DEFINE ADC_BITS 8 ‘ Set number of bits in result (8, 10 or 12)
DEFINE ADC_CLOCK 3 ‘ Set clock source (rc =3)
DEFINE ADC_SAMPLEUS 50 ‘ Set sampling time inmicroseconds
ADC_SAMPLEUS is the number of microseconds the program waits
between setting the Channel and starting the analog to digital
conversion. This is the sampling time.
TRISA = 255 ‘ Set PORTA to all input
ADCON1 = 0 ‘ PORTA is analog
ADCIN 0, B0 ‘ Read channel 0 to B0
 
Last edited:
Oh my goodness, that's not really very helpful, is it? Did you look for examples over on the PBP forum, Al?
 
they expect you to be fluent?
Yes, you should at least have some cursory knowledge of the datasheet. Regardless of which MCU you use (Microchip, Atmel, etc), there are two main things that compose them: MCU core and Peripherals. Before using any peripherals you need to configure them - that is true no matter which device/vendor you use.

Before ADCIN can be used, the appropriate TRIS register must be set to
make the desired pins inputs. The ADCON, ANCON and/or ANSEL
registers must also be set to assign the desired pins to analog inputs and
in some cases to set the result format and clock source (set the clock
source the same as the DEFINE specified for it, below).
This states you must configure TRIS, ADCON (0 and/or 1), ANCON and/or ANSEL before you use the ADCIN command.

I haven't used PBP in years; but if I recall there were examples of how to use the built-in ADC on the Examples folder.
 
Hi Guys,

Thanks for all the help and info. I think I can sort it now.

Thanks again, Al
 
Status
Not open for further replies.

Latest threads

Back
Top