// Analog Output routine // void SetDAC(uint_16 setting, uint_8 channel) { // Local Variable uint_16 dacout = 0; // DAC Data, 16 bit. // Constant value for DAC Control nibble. // uint_16 dac1_Control = 0x1000; // Select DAC A, unbuffered, Gain = 2, Active. uint_16 dac1_Control = 0x3000; // Select DAC A, unbuffered, Gain = 1, Active. // uint_16 dac1_Control = 0x5000; // Select DAC A, buffered, Gain = 2, Active. // uint_16 dac1_Control = 0x7000; // Select DAC A, buffered, Gain = 1, Active. uint_16 dac1_Disable = 0x0000; // Select DAC A, Disable. // uint_16 dac2_Control = 0x9000; // Select DAC B, unbuffered, Gain = 2, Active. uint_16 dac2_Control = 0xB000; // Select DAC B, unbuffered, Gain = 1, Active. // uint_16 dac2_Control = 0x9000; // Select DAC B, buffered, Gain = 2, Active. // uint_16 dac2_Control = 0xF000; // Select DAC B, buffered, Gain = 1, Active. uint_16 dac2_Disable = 0x8000; // Select DAC B, Disable. // Insert the control nibble, override if setting is shutdown command. // Note: for shutdown command use setting value of 0xFFFF. if (channel == 1) dacout = setting | dac1_Control; if ((channel == 1) & (setting == 0xFFFF) ) dacout = dac1_Disable; if (channel == 2) dacout = setting | dac2_Control; if ((channel == 2) & (setting == 0xFFFF) ) dacout = dac2_Disable; // Open the port. Third parameter is speed setting (SPBRG clock div). // For fOSC = 40MHz. // 10 = 1MHz // 24 = 420KHz // 32 = 310KHz SpiChnOpen(SPI_CHANNEL1, SPI_OPEN_MSTEN | SPI_OPEN_CKE_REV | SPI_OPEN_MODE16, 10); DACCS = 0; // Select the DAC Chip. SpiChnWriteC(SPI_CHANNEL1, dacout); // Send dat to DAC. while( SPI1STATbits.SPIBUSY ); // Wait for busy flag to clear. // for(i=0; i<200; i++); // perform a delay, stretch CS @ 156KHz. // for(i=0; i<100; i++); // perform a delay, stretch CS @ 310KHz. for(i=0; i<20; i++); // perform a delay, stretch CS @ 1MHz. DACCS = 1; // Deselect the DAC Chip. // for(i=0; i<120; i++); // perform a delay, LD setup time @ 156KHz // for(i=0; i<60; i++); // perform a delay, LD setup time @ 310KHz. for(i=0; i<2; i++); // perform a delay, LD setup time @ 1MHz. DACLD = 0; // Load new DAC Data to Output. // for(i=0; i<120; i++); // perform a delay, strech LD @ 156KHz // for(i=0; i<60; i++); // perform a delay, strech LD @ 310KHz. for(i=0; i<6; i++); // perform a delay, strech LD @ 1MHz. DACLD = 1; // Reset the Load pin. // for(i=0; i<40; i++); // perform a delay. SpiChnClose(SPI_CHANNEL1); // Close the SPI. }