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.

ADFM Result Placing Problem

Status
Not open for further replies.

Suraj143

Active Member
I have an AD result placing problem.

When I right justify the LSB is in ADRESL & the MSB is in 1st bit of ADRESH. That’s ok

But when I left justify where is the LSB & the MSB in a 10 bit result?

Thanks
 

Attachments

  • ADFM.JPG
    ADFM.JPG
    26.7 KB · Views: 163
the bits are counted from right to left, so that for a byte:
bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
therefore, for left justify, the MSB is the bit7 of ADRESH. LSB is bit6 of ADRESL.
for right justify, MSB went to bit1 of ADRESH where LSB is bit0 of ADRESL.
Code:
Left Justify:
ADRESH  ADRESL
7654321076543210
9876543210             <<result

Right justify:
ADRESH  ADRESL
7654321076543210
      9876543210       <<result
 
Last edited:
Hi gualala excellent reply.Thats what I want to know.

The left justify makes hard to read from the software because the 2 LSB is in the ADRESL.

But right justify easy to read.

Thats fine.
 
Suraj143 said:
Hi gualala excellent reply.Thats what I want to know.

The left justify makes hard to read from the software because the 2 LSB is in the ADRESL.

But right justify easy to read.
The reason for left justify is not for ease or difficulty of reading. It is so that you can easily use the high 8 bits of the result in ADRESH as an 8-bit value, dropping (ignoring) the two least significant bits in ADRESL. In many applications they're probably just noise anyway.

So if you only need an 8-bit result, use left justify and read ADRESH.

If you need all 10 bits, right justify and read both ADRESH and ADRESL.
 
gualala said:
well, if you need 8-bit resolution, you can left-justify it and use ADRESH as the results.

How did you read my mind?:D thats what i was going to ask from you.
its very nice.

I need a preset connect to RA0 (AN0) when the preset turns from zero to maximum the result value must vary between 0 to 255.

The problem is when I turn due to 10 bit it will roll over 4 times.
I dont need to rolls over 4 times I need to vary between 0-255 one time.

Do you get what I mean?
 
futz said:
It is so that you can easily use the high 8 bits of the result in ADRESH as an 8-bit value, dropping (ignoring) the two least significant bits in ADRESL.

Oh thats the point:D ignoring the LSB is not a good idea.

I need a 8 bit result format with 2 LSB also.It must vary between 0-255.

Let say the AD result value is decimal 9 = B'00001001'

reading just ADRESH will show the value = d'2' that is wrong.

Whats your idea?
 
Suraj143 said:
Oh thats the point:D ignoring the LSB is not a good idea.

I need a 8 bit result format with 2 LSB also.It must vary between 0-255.

Let say the AD result value is decimal 9 = B'00001001'

reading just ADRESH will show the value = d'2' that is wrong.

Whats your idea?
If you need precision and your measurement won't exceed 8-bits, right justify it and ignore ADRESH, which should never exceed zero anyway if you've set up your circuit and VREF+ correctly.
 
futz said:
If you need precision and your measurement won't exceed 8-bits, right justify it and ignore ADRESH, which should never exceed zero anyway if you've set up your circuit and VREF+ correctly.

This is correct but need bank switching as well.ref are Vdd & Vss.

I need to connect a preset to analog input & when I turn the preset from min to max it must vary the result from 0 to 255.

The PIC doesn't have 8 bit conversion its doing from 10 bit.So when I read the result registers its from 10 bit format.

When I turn it its rolls over 4 times thats the problem.256 X 4 = 1024
 
Suraj143 said:
This is correct but need bank switching as well.ref are Vdd & Vss.

I need to connect a preset to analog input & when I turn the preset from min to max it must vary the result from 0 to 255.

The PIC doesn't have 8 bit conversion its doing from 10 bit.So when I read the result registers its from 10 bit format.

When I turn it its rolls over 4 times thats the problem.256 X 4 = 1024
OH!!! Finally I understand what you were saying earlier! Gotcha!

Hmm... I never had to worry about that in anything I've done that needed 8-bit A/D. I just left justified and read ADRESH. Good enough.

Anyway, if your circuit is built correctly (and if necessary, you provide a proper VREF+ to get your readings in the proper range) either way should work fine.

You may need to spend some time with a calculator figuring out what voltage range you need to feed to the A/D pin to get the readings you want, and modify the circuit to get the voltage you're measuring into that range. You may find that you need to give the chip an external VREF+ to get it to read the kind of range you require. I had to do this for my accelerometer.

To stay in the low 8-bits right-justified with 5V VREF+, you need to be measuring a voltage of 0 - 1.25V.
 
Last edited:
Oh no again you are telling to left justify then it will still missing 2 LSB :(

It must right justify.

My supply is 5V.Preset is connect to 5V rails & the middle pin to PIC pin.

The maximum voltage is 5V.
 
Suraj143 said:
Oh no again you are telling to left justify then it will still missing 2 LSB :(
They're insignificant! It works. Try it. An 8-bit measurement can't be that accurate anyway, or it would be a 10-bit measurement. :D:D

What are you measuring?
 
hmmm... not really quite understand what you want but...

LSB means least significant bit. They can be discarded if you don't need that much resolution.
For a 10-bit ADC, it outputs 10-bit result from 0 to 1023. If we drop the last 2 bits, (i.e. set them to 00), you get 0 to 1020 with step size of 4. divide it by 4 (i.e. right shift 2 bits) results in 0 to 255 with step size of 1, which is the LEFT 8 bits of the result.
Code:
H: ADRESH, L: ADRESL, R: 10-bit AD result

Left-justified:
H7H6H5H4H3H2H1H0L7L6L5L4L3L2L1L0
R9R8R7R6R5R4R3R2R1R0  << 0 ~ 1023

discard ADRESL:
H7H6H5H4H3H2H1H0L7L6L5L4L3L2L1L0
R9R8R7R6R5R4R3R2 0 0  << 0 ~ 1020, step 4

result divide 4:
R9R8R7R6R5R4R3R2  << 0 ~ 255, step 1
 
Ok I‘ll tell what I’m doing.

There is a preset (variable resister) connected to PIC analog channel & the presets other two pins connected to 5V supply.

I need 10 values which correspond to AD result.

Means

If the AD result is between 0-25 value must show 1
If the AD result is between 25-50 value must show 2
If the AD result is between 50-100 value must show 3
---
---
If the AD result is between 225-250 value must show 10

When I turn the preset it must give these outputs to a register.

Now both of you got it?
 
Suraj143 said:
Now both of you got it?
Yup. Left justify and read ADRESH. Gives a nice 8-bit result. It works fine. Those two least-significant bits are irrelevant - that's why they're called "least significant". :p If you need that kind of precision then do a 10-bit read.

Suraj143 said:
I need a 8 bit result format with 2 LSB also.It must vary between 0-255.
For an 8-bit result, bits 1 and 0 of ADRESH are your two LSB's. ADRESH will vary between 0-255. It really does work that way. Try it.
 
Last edited:
Hi for both of you thanks for the input. I see now I totally understand.

I’ll left justify & read the ADRESH value.

If the input is 3V the result is d’613’ = b’1001100101’ for 10 bit.

If the input is 3V the result is d’153’ = b’10011001’ for 8 bit.

How nice is that number system:D

Thanks guys.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top