component serialdac "This component converts the lower 12 bits of an unsigned 32 bit word into serial output intended for a Linear Technology LTC1257 unipolar DAC."; //pin in u32 datain "This is the value (0 to 4095) to convert to serial binary."; pin in s32 datain "This is the value (0 to 4095) to convert to serial binary."; pin out bit dacclock "This clocks the n'th bit into DAC's shift register on rising edge"; pin out bit dacbit "This is the value of the n'th bit to output to the DAC's shift regester."; pin out bit dacload "This commands the shift regester to copy its contents to the DAC register, making the DAC ouput the new voltage."; variable u32 cycle; //"This holds the value of which bit is being sent."; variable u32 phase; //"This holds the value of which phase of the n'th bit being sent. There are four phases;get bit, shift bit, wait,reset clock."; function _ nofp; license "GPL"; ;; FUNCTION(_) { switch (phase) { case 0: dacbit=datain & (0x1000 >> cycle); dacclock=0; dacload=1; phase=1; break; case 1: dacclock=1; phase=2; break; case 2: if (cycle>=12) { dacload=0; } phase=3; break; case 3: dacclock=0; if (cycle>=12) { cycle=0; } phase=0; cycle++; break; default: cycle=0; dacclock=0; dacload=1; phase=0; break; } }