View previous topic :: View next topic |
Author |
Message |
diode_blade
Joined: 18 Aug 2014 Posts: 53 Location: Sheffield, South Yorkshire
|
Help with converting 10 bit adc to 12 bit dac output |
Posted: Tue Jan 20, 2015 7:40 am |
|
|
CCS Ver 5.020
PIC 18f4431
Hardware board: Pic Millenium prototype board (from the year 2000!)
Breadboard: with 2 low pass filter opamp circuits into an AD626 differential amplifier circuit-output, then into the 18f4431 a/d.
DAC USED is MAX538, Vref to dac set at 2 volts.
The inputs (they also drive a center zero 100-0-100 uA meter) to the low pass filters are differential inputs from a pulse inductor metal detector circuit (no common ground between detector circuit and my PIC and other circuitry)), I use the DAC o/p to feed back to the met detector board to re-zero it after metal has passed over the head, basically i am trying to compensate for drift on the detector board and in the coil head.
Algorithm
Code: |
Init ADC
SET Dac to mid span of it full output (ie set to 2047 bits)
GET MAX/MIN SPAN AND STORE
GET CENT POINT VAL AND STORE
IF (DRIFT)
GET VALUE
CALC DAC VAL TO COMPENSATE FOR DRIFT
O/P VAL TO DAC
|
Everything works fine, it's just the maths I am struggling with a bit,
lets say
max_span is 2.5 volts
min span is 1 volt
cent_point is 1.5 volt
overall span is 1 volt
say it drifts to 1.75 volt then Drift=0.25 volts
I now need to convert this drift value from its 10 bit a/d reading to output the correct value to the 12 bit dac, note by setting the DAC vref to 2 volts this does cover the range of the overall span (i can see it re-center so to speak by controlling the DAC O/P manually via the PIC SPI_XFER function)
Cheers People
Regards Dave |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Jan 26, 2015 4:21 pm |
|
|
You've had no response.
I assume it's because none of us can figure out what you are trying to do.
Have another go.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Tue Jan 27, 2015 12:15 am |
|
|
12bit_dac_val = 10bit_adc_val << 2;
will give you 12bit value with 10bit resolution. _________________ A person who never made a mistake never tried anything new. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19375
|
|
Posted: Tue Jan 27, 2015 1:52 am |
|
|
Very much agreed with Mike. He is wanting to do more than just convert from 12 to 10bit, but it is not at all plain 'what'. He talks about voltage ranges, and midpoints, but then doesn't explain whether these are source, or destination.
I think if he drew out his actual endpoints and midpoints for both source and destination on a bit of paper as a graph, with the ranges over which these can move, the transfer function would become visible, as well as what has to adjust to deal with the shift. |
|
![](templates/subSilver/images/spacer.gif) |
diode_blade
Joined: 18 Aug 2014 Posts: 53 Location: Sheffield, South Yorkshire
|
|
Posted: Fri Jan 30, 2015 10:07 am |
|
|
Thanks Ttlemah,Mike and Riko for the reply, keeping it as simple as possible,
take a value from the ADC (call it signal) and store it as center_value, if the signal drifts either way from the center value find the difference and calculate a value to output to the DAC to bring the signal back to the center value, sort of auto zero
ADC 18f4431 10 bit
Max538 dac 12 bit
what I have managed to do is this and it works reasonably well
Code: |
in main
spi_xfer(2047); // set dac at its center output voltage range
ad_conv=5.0/1023.0; //conversion factors
dac_conv=2.5/4095.0;
if(input(pin_d5)){
//button pressed?
if(sig_val>=cent){ //signal greater than center value int16's
diff=sig-cent_v; // calc difference (floats)
dac_bits=(int16)(diff/dac_conv);//find the equivalent
shift=2047-dac_bits; //offset voltage
} //for the DAC to re-zero
if(sig_val<=cent){ //signal less than center value
diff=cent_v-sig;
dac_bits=diff*8;//(int16)(diff/dac_conv);
shift=2047+dac_bits; //
}
spi_xfer(shift);
}
|
The rest of the code is just the setting up of the A/D etc and not worth showing, hope that explains what I have been trying to do.
edit: I use the conversion factors to display on LCD as voltages, as well for the DAC value calculations above.
Ta
Dave |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Jan 30, 2015 12:43 pm |
|
|
Assuming you know the OPA gain and ADC & DAC transfer functions you should be able calculate a new DAC value so as to bring the output back to its centre value.
I don't see why you need to deal with +ve and -ve errors separately.
You're calculating an error as a +ve value in both cases.
In one case you add an offset, in the other you subtract.
Surely its simpler to get the error as either a +ve or -ve value and deal with only one process. Like Ttelmah says, it's a pencil and paper job.
Mike |
|
![](templates/subSilver/images/spacer.gif) |
|