dac.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "dac.h"
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "ti/driverlib/dl_i2c.h"
  5. #include "ti_msp_dl_config.h"
  6. //The device updates all DAC analog output(vout) at the same time
  7. void DAC_UpdateOutput() {
  8. uint8_t general_call_command = 0x08; // General Call Update Command
  9. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  10. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &general_call_command, 1);
  11. // Start I2C transaction
  12. DL_I2C_startControllerTransfer(I2C_controller_INST, 0x00, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
  13. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  14. printf("DAC Outputs Updated via General Call Software Update!\n");
  15. }
  16. /**Function for FAST write command, sending values over I2c to every channel of DAC**/
  17. bool DAC_fastWrite(uint16_t channel_a_value){
  18. /*DAC has a 12 bit resolution that ranges from 0 to 4095.
  19. 0x00: sets the Power Mode to NORMAL for Channel A
  20. (channel_a_value >> 8): shifts the value to 8 places right and gives upper 4 bits
  21. */
  22. uint8_t output_buffer[8];
  23. output_buffer[0]= (0x00)|(channel_a_value >> 8)&0x0F;
  24. //output_buffer[0] = channel_a_value >> 8;
  25. output_buffer[1]= channel_a_value & 0xFF;
  26. output_buffer[2]= 0x40; // Power down for the other channels
  27. output_buffer[3]= 0x00;
  28. output_buffer[4]= 0x40;
  29. output_buffer[5]= 0x00;
  30. output_buffer[6]= 0x40;
  31. output_buffer[7]= 0x00;
  32. if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
  33. printf("I2C Write Error: Failed to write to DAC channels for FAST write mode!\n");
  34. return false; // Return failure if there was an error
  35. }
  36. // **Wait for I2C Bus to be Free**
  37. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  38. // **Start I2C Write Transaction**
  39. DL_I2C_startControllerTransfer(I2C_controller_INST, DAC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 8);
  40. // **Load Configuration Byte into TX FIFO**
  41. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t*)&output_buffer, 8);
  42. // **Ensure STOP Condition is Sent**
  43. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  44. printf("DAC Fast write Successful!\n");
  45. DAC_UpdateOutput();
  46. return true;
  47. }