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.
 
 
 
 

61 lines
1.5 KiB

  1. #ifndef DAC_H
  2. #define DAC_H
  3. #include "config.h"
  4. #define DAC_TYPE 2
  5. #if DAC_TYPE == 0
  6. /* AD5542, normal 16-bit range */
  7. #define DAC_BITS 16
  8. #define __dac_to_spi_cmd(x) (x)
  9. #define __dac_to_16bit_equiv(x) (x)
  10. #elif DAC_TYPE == 1
  11. /* AD5542, fake 10-bit range using random lower bits */
  12. #define DAC_BITS 10
  13. #define __dac_to_spi_cmd(x) (dac_lookup[(x)&1023])
  14. #define __dac_to_16bit_equiv(x) (dac_lookup[(x)&1023])
  15. #elif DAC_TYPE == 2
  16. /* MAX504, true 10-bit DAC */
  17. #define DAC_BITS 10
  18. #define __dac_to_spi_cmd(x) ((x) << 2)
  19. #define __dac_to_16bit_equiv(x) ((x) << 6)
  20. #else
  21. #error Unknown DAC type
  22. #endif
  23. #define DAC_LOW 0
  24. #define DAC_HIGH ((uint16_t)(((uint32_t)1 << DAC_BITS) - 1))
  25. #define DAC_MID ((uint16_t)((DAC_LOW + DAC_HIGH + (uint32_t)1) / 2))
  26. #define DAC_RANGE (DAC_HIGH - DAC_LOW + 1)
  27. /* Initialize DAC (AD5542) */
  28. void dac_init(void);
  29. /* Write raw value to DAC:
  30. DAC_HIGH 4.9998v
  31. DAC_MID 0v
  32. DAC_LOW -5v
  33. */
  34. void dac_write(uint16_t val);
  35. /* Given a DAC command between DAC_LOW and DAC_HIGH,
  36. get the actual expected output voltage as a 16-bit value, where:
  37. 0xffff = 4.9998v
  38. 0x8000 = 0v
  39. 0x0000 = -5v
  40. */
  41. uint16_t dac_get_actual_16bit(uint16_t val);
  42. /* Given a DAC command between DAC_LOW and DAC_HIGH,
  43. get the actual expected output voltage as a FLOAT, where:
  44. DAC_HIGH = 4.9998v
  45. DAC_MID = 0v
  46. DAC_LOW = -5v
  47. Example: for 10-bit DAC, convert integer command 123 into
  48. the more accurate 123.45 using known lower bits.
  49. */
  50. float dac_get_actual_float(uint16_t val);
  51. #endif