#include "dac.h" #include #include #include "ti/driverlib/dl_i2c.h" #include "ti_msp_dl_config.h" //The device updates all DAC analog output(vout) at the same time void DAC_UpdateOutput() { uint8_t general_call_command = 0x08; // General Call Update Command while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &general_call_command, 1); // Start I2C transaction DL_I2C_startControllerTransfer(I2C_controller_INST, 0x00, DL_I2C_CONTROLLER_DIRECTION_TX, 1); while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); printf("DAC Outputs Updated via General Call Software Update!\n"); } /**Function for FAST write command, sending values over I2c to every channel of DAC**/ bool DAC_fastWrite(uint16_t channel_a_value){ /*DAC has a 12 bit resolution that ranges from 0 to 4095. 0x00: sets the Power Mode to NORMAL for Channel A (channel_a_value >> 8): shifts the value to 8 places right and gives upper 4 bits */ uint8_t output_buffer[8]; output_buffer[0]= (0x00)|(channel_a_value >> 8)&0x0F; //output_buffer[0] = channel_a_value >> 8; output_buffer[1]= channel_a_value & 0xFF; output_buffer[2]= 0x40; // Power down for the other channels output_buffer[3]= 0x00; output_buffer[4]= 0x40; output_buffer[5]= 0x00; output_buffer[6]= 0x40; output_buffer[7]= 0x00; if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) { printf("I2C Write Error: Failed to write to DAC channels for FAST write mode!\n"); return false; // Return failure if there was an error } // **Wait for I2C Bus to be Free** while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); // **Start I2C Write Transaction** DL_I2C_startControllerTransfer(I2C_controller_INST, DAC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 8); // **Load Configuration Byte into TX FIFO** DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t*)&output_buffer, 8); // **Ensure STOP Condition is Sent** while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS); printf("DAC Fast write Successful!\n"); DAC_UpdateOutput(); return true; }