/***************************************************************************** * DAC ***************************************************************************** * FileName: DAC.c * Dependencies: system.h * Processor: PIC24 * Compiler: MPLAB C30 * Linker: MPLAB LINK30 * * Author Date Comment *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Michael Cecere 09/2006 *****************************************************************************/ #include "system.h" /***************************************************************************** * Module Constants Definitions *****************************************************************************/ //DAC Output Channel Definitions #define DAC_Toroid1 0 #define DAC_Toroid2 1 #define DAC_Toroid3 2 #define DAC_Toroid4 3 #define DAC_Spare1 4 #define DAC_Spare2 5 #define DAC_Spare3 6 #define DAC_CompExt 7 //full range of the DAC is 12bits/2*Reference Voltage, Ref = 2.048V // start valid signal output at DAC_FLOOR and stop at DAC_CEIL float DAC_REF =4096/(2*2.048); float DAC_FLOOR= 0.2; float DAC_CEIL= 3.8; extern SYS_Config_Struct SYS_Config; extern float pC_Adj[]; extern int PRINTON; extern unsigned long Data_Raw[]; int prt = 1; //DB to print only once /***************************************************************************** * Function: DAC_Init() * * Overview: initialize DAC settings * * Input: * * Output: * ******************************************************************************/ void DAC_Init() { int Chan; IOC_TRIG = 0; // trigger to IOC IP330 ADC to start conversion // signal is inverted before leaves board // IP330 trigger is negative going TTL DAC_CS = 1; // set in-active // 4volts/nC // set control reg 1 to enable internal ref voltage // 4 = internal ref of 1.024V // 6 = internal ref of 2.048V, the one we want // outputs are buffered with a gain of x2 // control reg #1 = address 8 DAC_Write(6,8); DAC_Write(0,9); // zero all outputs. 0 volts out indicates a falut to IOC since it's below DAC_FLOOR for (Chan=0;Chan<8;Chan++) { DAC_Write(0,Chan); } Chan=0; Chan=0; } /***************************************************************************** * Function: Write_pC_DAC() * * Overview: subtracts INT2 - INT1 then converts to DAC units and writes to DAC * * Input: * * Output: * ******************************************************************************/ void Write_pC_DAC() { int Chan; float BaseSubtract, DarkCurrent; int DAC_Counts; //char tstr[80]; // ============================================================================ // Commenting out Cecere's code DB: 5/9/08 for (Chan=0;Chan<4;Chan++) { // if (SYS_Config.Chan_ON[Chan]) { if (1) { BaseSubtract = pC_Adj[Chan]; //if (BaseSubtract4095)) data = 0; if ((Chan<0)|(Chan>15)) Chan = 0; Data_Out = Chan; Data_Out = Data_Out<<12; // Move the channel number to the upper 4 bits Data_Out = Data_Out + data; // add in 12-bit data DAC_CS = 0; // enable DAC chip SPI1MPolPut(Data_Out); SPI1MPolIsTransmitOver(); mSPI1MPolGet(); NumNops(10); DAC_CS = 1; // disable DAC chip } /***************************************************************************** * Function: Toroid_Output_Scale * * Preconditions: None. * * Overview: Converts pC (in units of pC) to DAC counts based on DAC channel # * * Input: Signal(value to convert), Chan(channel number of signal * * Output: 12-bit DAC value * * set 4V Full Scale DAC output to be a fault condition? * set 1.2nC max reading at 3.8 volts DAC_CEIL * with a minimum output voltage of DAC_FLOOR, 0.2volts ******************************************************************************/ int Toroid_DAC_Scale(float Signal, unsigned Chan) { int i; float Temp; if ((Chan>=0)&(Chan<8)) { // Gain Correction Signal = Signal * (3.2 / SYS_Config.DAC_Err_Gain[Chan]); // the perfect voltage desired Temp = (DAC_CEIL-DAC_FLOOR)*Signal/1200. + DAC_FLOOR; Temp = Temp - (SYS_Config.DAC_Err_Offset[Chan] - 0.2); Temp = DAC_REF * Temp; } if ((Temp<0)|(Temp>4095)) Temp = 0; i = (int)(Temp); return i; }