component serial_dac "This component converts the lower 12 bits of an unsigned 32 bit word into serial output intended for a Linear Technology LTC1257 unipolar DAC. (2007/12/29 kwallace@wallacecompany.com)"; pin in unsigned data_in "This is the value to convert to serial binary."; pin out bit dac_clock "This commands (on the rising edge) the DAC to clock the n'th bit into the DAC's shift register."; pin out bit dac_bit "This is the value of the n'th bit to output to the DAC's shift regester."; pin out bit dac_load "This commands (on the falling edge) the DAC to copy the contents of the shift register to the DAC register, resulting in the ouput of the new voltage."; variable unsigned cycle; //This holds the value of which bit is being sent. MSB bit 11 first to LSB bit 0 last variable unsigned phase; //This holds the value of which phase of the n'th bit being sent. There are four phases;get bit, shift bit, wait (also, test for last bit for load*),reset clock (also, test for last bit for cycle reset)."; variable unsigned data_adj; //This holds the adjusted data_in value. data_in will wrap (i.e. 5000 wraps to 904) if it's not clamped to 4095. function _ nofp; function in_adj nofp; license "GPL"; ;; FUNCTION(in_adj) { if (data_in < 0) { data_adj=0; } if (data_in > 4095) { data_adj=4095; } else { data_adj=data_in; } } FUNCTION(_) { switch (phase) { case 0: dac_bit=0x1 & (data_adj >> cycle); dac_clock=0; dac_load=1; phase=1; break; case 1: dac_clock=1; phase=2; break; case 2: if (cycle<=0) { dac_load=0; } phase=3; break; case 3: dac_clock=0; if (cycle<=0) { cycle=11; } else { cycle--; } phase=0; break; } }