Changes to fix the real-10-bit case.
git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@9082 ddd99763-3ecb-0310-9145-efcb8ce7c51f
This commit is contained in:
parent
d59462ae25
commit
6b5a14b01f
firmware
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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(uint16_t val);
|
||||
uint16_t dac_get_actual_16bit(uint16_t 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);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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();
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user