Differential ADC

Here you can share completed projects or parts of projects that may be useful to others. You may post files relevant to ZBasic - source code, schematics, etc.
Post Reply
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Differential ADC

Post by spamiam »

Here is a little program demonstrating how to do differential ADC. It was written for the ZX24, but the technique is good for all processors

-Tony
Attachments
Test_diff_adc.zip
(2.77 KiB) Downloaded 3894 times
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

In differential mode, it is convenient to use the ADLAR bit to force the result into bits 15-6. The advantage of doing this is that the result can be directly used as a 16-bit signed value. Obviously, an adjustment needs to be made in the ADC-to-voltage conversion equation to compensate for the fact that the value is 64 times what it should be.
- Don Kinzer
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

dkinzer wrote:In differential mode, it is convenient to use the ADLAR bit to force the result into bits 15-6. The advantage of doing this is that the result can be directly used as a 16-bit signed value. Obviously, an adjustment needs to be made in the ADC-to-voltage conversion equation to compensate for the fact that the value is 64 times what it should be.
Good point. I was wondering about doing something with the ADLAR bit. It is an easy mod to make. I had to do a little extra math to check if the value is greater than 511, then subtract 1024 from the value to get the correct signed value.

The problem with having a result that is 64 times larger is that some people might think that there is 64 times greater resolution!

I suppose it would be simple enough to right shift the ADC reading by 6 bits to restore the correct magnitude. (Right shifting will preserve the sign of a signed integer, right?) I am reasonably sure that always doing a quick right shift will be faster than deciding when to sometimes subtract 1024.

Code: Select all

dim ADC_Reading as Integer

'NOTE: ADLAR bit is set

ADC_Reading = ShR(CInt(Register.ADC) ,6)
I will make this modification to the code and post a new version. Thanks for the input!

-Tony
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

spamiam wrote:I am reasonably sure that always doing a quick right shift will be faster than deciding when to sometimes subtract 1024.
If the value will ultimately be converted to some other units (e.g. voltage, pressure, etc.) the scaling can be combined into that conversion, often at no additional cost since those conversions generally involve multiplication and division anyway.
- Don Kinzer
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

dkinzer wrote:If the value will ultimately be converted to some other units (e.g. voltage, pressure, etc.) the scaling can be combined into that conversion, often at no additional cost since those conversions generally involve multiplication and division anyway.
Right. But for the little demo program, I wanted it to be as general-purpose as possible. I was thnking that some users might transplant the code directly into their progran as-is, then further translate the data as needed.

-Tony
Post Reply