|
- #include "config.h"
- #include "calibrate.h"
- #include "util.h"
- #include "adc.h"
- #include "dac.h"
-
- static float scale; /* delta(DAC) / delta(ADC) */
-
- /* Initialize. Assume some relatively-safe scaling if no calibration is run */
- void calibrate_init(void)
- {
- scale = 0;
- }
-
- /* Seek with the DAC to reach a specific ADC value, adjusting
- * calibration if necessary */
- uint16_t seek(int16_t desired)
- {
- int32_t dac = 32768;
- int16_t actual;
- int32_t delta = 0;
-
- while (1)
- {
- /* get current location */
- dac_write(dac);
- msleep(1);
- actual = adc_get();
-
- /* adjust scaling to match the jump we just saw */
- if (delta) {
-
- }
-
- if (actual == desired)
- return dac;
-
- /* figure out how much to change the DAC */
- delta = (desired - actual) * scale;
-
-
- dac -= (desired - actual) * scale;
-
-
- }
- uint16_t dac = 0;
- dac_write(0x8000
-
- }
-
- /* Perform calibration */
- void do_calibrate(void)
- {
- uint16_t daczero, x;
- float x1, y1, x2, y2;
-
- /* Zero ADC */
- daczero = seek(1024);
-
- /* Go halfway down and take an accurate sample */
- x = seek(512);
- oversample(x, &x1, &y1);
-
- /* Go halfway up and take an accurate sample */
- x = seek(768);
- oversample(x, &x2, &y2);
-
- /* Scale factor is 1/slope */
- scale = (x2 - x1) / (y2 - y1);
- }
-
- /* convert from (counts at ADC) to (counts at DAC) */
- int32_t adc_toc(int16_t adc)
- {
- int32_t r = ((float) adc) * scale;
- return r;
- }
|