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
Differential ADC
Differential ADC
- Attachments
-
- Test_diff_adc.zip
- (2.77 KiB) Downloaded 3895 times
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
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.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.
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)
-Tony
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.spamiam wrote:I am reasonably sure that always doing a quick right shift will be faster than deciding when to sometimes subtract 1024.
- Don Kinzer
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.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.
-Tony