diff --git a/firmware/dac.c b/firmware/dac.c index 5e7837f..4e35069 100644 --- a/firmware/dac.c +++ b/firmware/dac.c @@ -43,22 +43,35 @@ void dac_write(uint16_t val) { LATGbits.LATG9 = IO_LOW; if (IO_HIGH == 1) - SPI2BUF = __dac_actual(val); + SPI2BUF = __dac_to_spi_cmd(val); else - SPI2BUF = ~__dac_actual(val); + SPI2BUF = ~__dac_to_spi_cmd(val); while (!SPI2STATbits.SPIRBF) continue; (void) SPI2BUF; LATGbits.LATG9 = IO_HIGH; } -uint16_t dac_get_actual(uint16_t val) +/* Given a DAC command between DAC_LOW and DAC_HIGH, + get the actual expected output voltage as a 16-bit value, where: + 0xffff = 4.9998v + 0x8000 = 0v + 0x0000 = -5v +*/ +uint16_t dac_get_actual_16bit(uint16_t val) { - return __dac_actual(val); + return __dac_to_16bit_equiv(val); } -/* Get actual output voltage from DAC_LOW to DAC_HIGH, as a float */ +/* Given a DAC command between DAC_LOW and DAC_HIGH, + get the actual expected output voltage as a FLOAT, where: + DAC_HIGH = 4.9998v + DAC_MID = 0v + DAC_LOW = -5v + Example: for 10-bit DAC, convert integer command 123 into + the more accurate 123.45 using known lower bits. +*/ float dac_get_actual_float(uint16_t val) { - return __dac_actual(val) * (DAC_RANGE / 65536.0); + return __dac_to_16bit_equiv(val) * (DAC_RANGE / 65536.0); } diff --git a/firmware/dac.h b/firmware/dac.h index 8df6d58..92ef5f6 100644 --- a/firmware/dac.h +++ b/firmware/dac.h @@ -8,15 +8,18 @@ #if DAC_TYPE == 0 /* AD5542, normal 16-bit range */ #define DAC_BITS 16 - #define __dac_actual(x) (x) + #define __dac_to_spi_cmd(x) (x) + #define __dac_to_16bit_equiv(x) (x) #elif DAC_TYPE == 1 /* AD5542, fake 10-bit range using random lower bits */ #define DAC_BITS 10 - #define __dac_actual(x) (dac_lookup[(x)&1023]) + #define __dac_to_spi_cmd(x) (dac_lookup[(x)&1023]) + #define __dac_to_16bit_equiv(x) (dac_lookup[(x)&1023]) #elif DAC_TYPE == 2 /* MAX504, true 10-bit DAC */ #define DAC_BITS 10 - #define __dac_actual(x) ((x) << 2) + #define __dac_to_spi_cmd(x) ((x) << 2) + #define __dac_to_16bit_equiv(x) ((x) << 6) #else #error Unknown DAC type #endif @@ -36,14 +39,22 @@ void dac_init(void); */ void dac_write(uint16_t val); -/* From DAC value, get the expected actual output voltage: - 0xffff 4.9998v - 0x8000 0v - 0x0000 -5v +/* Given a DAC command between DAC_LOW and DAC_HIGH, + get the actual expected output voltage as a 16-bit value, where: + 0xffff = 4.9998v + 0x8000 = 0v + 0x0000 = -5v +*/ +uint16_t dac_get_actual_16bit(uint16_t val); + +/* Given a DAC command between DAC_LOW and DAC_HIGH, + get the actual expected output voltage as a FLOAT, where: + DAC_HIGH = 4.9998v + DAC_MID = 0v + DAC_LOW = -5v + Example: for 10-bit DAC, convert integer command 123 into + the more accurate 123.45 using known lower bits. */ -uint16_t dac_get_actual(uint16_t val); - -/* Get actual output voltage from DAC_LOW to DAC_HIGH, as a float */ float dac_get_actual_float(uint16_t val); #endif diff --git a/firmware/mode_debug.c b/firmware/mode_debug.c index 474b4a2..4e9dab5 100644 --- a/firmware/mode_debug.c +++ b/firmware/mode_debug.c @@ -58,7 +58,7 @@ void run_debug(void) dac_write(dac); uart1_put_hex16(dac); uart1_put(' '); - uart1_put_dec(dac_get_actual(dac)); + uart1_put_dec(dac_get_actual_16bit(dac)); uart1_put(' '); uart1_put(' '); adc = adc_get(); diff --git a/firmware/zoom.mcw b/firmware/zoom.mcw index 146cca8..0a57b50 100644 Binary files a/firmware/zoom.mcw and b/firmware/zoom.mcw differ