You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 lines
1.4 KiB

  1. #include "config.h"
  2. #include "calibrate.h"
  3. #include "util.h"
  4. #include "adc.h"
  5. #include "dac.h"
  6. static float scale; /* delta(DAC) / delta(ADC) */
  7. /* Initialize. Assume some relatively-safe scaling if no calibration is run */
  8. void calibrate_init(void)
  9. {
  10. scale = 0;
  11. }
  12. /* Seek with the DAC to reach a specific ADC value, adjusting
  13. * calibration if necessary */
  14. uint16_t seek(int16_t desired)
  15. {
  16. int32_t dac = 32768;
  17. int16_t actual;
  18. int32_t delta = 0;
  19. while (1)
  20. {
  21. /* get current location */
  22. dac_write(dac);
  23. msleep(1);
  24. actual = adc_get();
  25. /* adjust scaling to match the jump we just saw */
  26. if (delta) {
  27. }
  28. if (actual == desired)
  29. return dac;
  30. /* figure out how much to change the DAC */
  31. delta = (desired - actual) * scale;
  32. dac -= (desired - actual) * scale;
  33. }
  34. uint16_t dac = 0;
  35. dac_write(0x8000
  36. }
  37. /* Perform calibration */
  38. void do_calibrate(void)
  39. {
  40. uint16_t daczero, x;
  41. float x1, y1, x2, y2;
  42. /* Zero ADC */
  43. daczero = seek(1024);
  44. /* Go halfway down and take an accurate sample */
  45. x = seek(512);
  46. oversample(x, &x1, &y1);
  47. /* Go halfway up and take an accurate sample */
  48. x = seek(768);
  49. oversample(x, &x2, &y2);
  50. /* Scale factor is 1/slope */
  51. scale = (x2 - x1) / (y2 - y1);
  52. }
  53. /* convert from (counts at ADC) to (counts at DAC) */
  54. int32_t adc_toc(int16_t adc)
  55. {
  56. int32_t r = ((float) adc) * scale;
  57. return r;
  58. }