dac.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. VoutA channel, rest channels are ll powered down
  22. */
  23. uint8_t output_buffer[8];
  24. output_buffer[0]= (0x00)|(channel_a_value >> 8)&0x0F;
  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. DL_I2C_resetControllerTransfer(I2C_controller_INST); // Reset bus if stuck
  35. return false; // Return failure if there was an error
  36. }
  37. // **Wait for I2C Bus to be Free**
  38. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  39. // **Start I2C Write Transaction**
  40. DL_I2C_startControllerTransfer(I2C_controller_INST, DAC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 8);
  41. // **Load Configuration Byte into TX FIFO**
  42. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t*)&output_buffer, 8);
  43. // **Ensure STOP Condition is Sent**
  44. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  45. printf("DAC Fast write Successful!\n");
  46. DAC_UpdateOutput();
  47. return true;
  48. }