Wiblocks --- DAC

DAC/DAC.cpp

00001 #include "SPI.h"
00002 #include "DAC.h"
00003 
00004 extern "C" 
00005 {
00006 #include <inttypes.h>
00007 #include "WConstants.h"
00008 }
00009 
00017 
00018 #define dac_tlv5620_lsb (_ref * (_range[ch] ? 2 : 1) / _num_steps);
00019 #define dac_tlv5618_lsb (2 * _ref / _num_steps);
00020 
00030 
00031 DAC_TLV5620::DAC_TLV5620(unsigned char load_pin, unsigned char latch_pin) {
00032   unsigned char ch;
00033   _load_pin = load_pin;
00034   _latch_pin = latch_pin;
00035   for (ch = 0; ch < DAC_TLV5620_NUM_CHS; ch++) {
00036     _range[ch] = DAC_REFX2;
00037     _lsb[ch]   = dac_tlv5620_lsb;
00038   }
00039   digitalWrite(_load_pin,  1);
00040   digitalWrite(_latch_pin, 1);
00041   pinMode(_load_pin, OUTPUT);
00042   pinMode(_latch_pin, OUTPUT);
00043 };
00044 
00045 
00064 
00065 char DAC_TLV5620::write_data(dac_tlv5620_chs ch, dac_tlv5620_ranges range, 
00066                              unsigned char code) {
00067   unsigned char dac_data[2];
00068   dac_data[0] = ((ch & 0x03) << 1) | ((range & 0x01) << 0); 
00069   dac_data[1] = code;
00070   digitalWrite(_latch_pin, 1);
00071   digitalWrite(_load_pin,  1);
00072   spi.write(dac_data, 2);
00073   digitalWrite(_load_pin,  0);
00074   delayMicroseconds(10); 
00075   digitalWrite(_load_pin,  1);
00076   delayMicroseconds(10); 
00077   digitalWrite(_latch_pin, 0);
00078   delayMicroseconds(10); 
00079   digitalWrite(_latch_pin, 1);
00080   return(0);
00081 };
00082 
00091 char DAC_TLV5620::set_code(dac_tlv5620_chs ch, unsigned char code) { 
00092   return(write_data(ch, _range[ch], code));
00093 }
00094 
00104 void DAC_TLV5620::set_range(dac_tlv5620_chs ch, dac_tlv5620_ranges range) { 
00105   _range[ch] = range; 
00106   _lsb[ch] = dac_tlv5620_lsb;
00107 } 
00108 
00119 
00120 char DAC_TLV5620::set_voltage(dac_tlv5620_chs ch, double v) {
00121   if (v > _lsb[ch] * _fs_code - DBL_EPSILON) return(set_code(ch, _fs_code));
00122   if (v < DBL_EPSILON) return(set_code(ch, 0));
00123   return(set_code(ch, (unsigned char)(v / _lsb[ch])));
00124 }
00125 
00126 
00127 // ********************
00128 // 5618
00129 // ********************
00130 
00131 
00141 
00142 DAC_TLV5618::DAC_TLV5618(unsigned char cs_pin, dac_tlv5618_pwrs pwr, dac_tlv5618_spds spd) {
00143   unsigned char ch;
00144   _cs_pin = cs_pin;
00145   _lsb = dac_tlv5618_lsb;
00146   _control.control_byte = pwr | spd;
00147   digitalWrite(_cs_pin,  1);
00148   pinMode(_cs_pin, OUTPUT);
00149 };
00150 
00151 
00163 
00164 char DAC_TLV5618::write_data(dac_tlv5618_rs rs, unsigned int code) {
00165   unsigned char dac_data[2];
00166   _control.control_byte &= DAC_TLV5618_RS_MASK;
00167   _control.control_byte |= rs;
00168   dac_data[0] = (unsigned char)(((code & 0xF00)>>8) | _control.control_byte);
00169   dac_data[1] = (unsigned char)(code & 0xFF);
00170   digitalWrite(_cs_pin, 0);
00171   delayMicroseconds(10); 
00172   spi.write(dac_data, 2);
00173   delayMicroseconds(10); 
00174   digitalWrite(_cs_pin,  1);
00175   return(0);
00176 };
00177 
00178 
00215 
00216 char DAC_TLV5618::set_voltage(dac_tlv5618_rs rs, double v) {
00217   if (v > _lsb * _fs_code - DBL_EPSILON) return(set_code(rs, _fs_code));
00218   if (v < DBL_EPSILON) return(set_code(rs, 0));
00219   return(set_code(rs, (unsigned int)(v / _lsb)));
00220 }
00221 
00229 char DAC_TLV5618::set_code(dac_tlv5618_rs rs, unsigned int code) { 
00230   if (code > 4095) code = 4095;
00231   return(write_data(rs, code));
00232 }
00233 
00241 void DAC_TLV5618::set_speed(dac_tlv5618_spds spd) { 
00242   _control.control_bits.SPD = spd ? 1 : 0;
00243 }
00244 
00252 
00253 void DAC_TLV5618::set_power_control(dac_tlv5618_pwrs pwr) {
00254   _control.control_bits.PWR = pwr ? 1 : 0;
00255 }
00256