My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digipot_mcp4451.cpp 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "Configuration.h"
  2. #ifdef DIGIPOT_I2C
  3. #include "Stream.h"
  4. #include "utility/twi.h"
  5. #include "Wire.h"
  6. // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
  7. #define DIGIPOT_I2C_FACTOR 106.7
  8. #define DIGIPOT_I2C_MAX_CURRENT 2.5
  9. static byte current_to_wiper( float current ){
  10. return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
  11. }
  12. static void i2c_send(byte addr, byte a, byte b)
  13. {
  14. Wire.beginTransmission(addr);
  15. Wire.write(a);
  16. Wire.write(b);
  17. Wire.endTransmission();
  18. }
  19. // This is for the MCP4451 I2C based digipot
  20. void digipot_i2c_set_current( int channel, float current )
  21. {
  22. current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
  23. // these addresses are specific to Azteeg X3 Pro, can be set to others,
  24. // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
  25. byte addr= 0x2C; // channel 0-3
  26. if(channel >= 4) {
  27. addr= 0x2E; // channel 4-7
  28. channel-= 4;
  29. }
  30. // Initial setup
  31. i2c_send( addr, 0x40, 0xff );
  32. i2c_send( addr, 0xA0, 0xff );
  33. // Set actual wiper value
  34. byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
  35. i2c_send( addr, addresses[channel], current_to_wiper(current) );
  36. }
  37. void digipot_i2c_init()
  38. {
  39. const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
  40. Wire.begin();
  41. // setup initial currents as defined in Configuration_adv.h
  42. for(int i=0;i<=sizeof(digipot_motor_current)/sizeof(float);i++) {
  43. digipot_i2c_set_current(i, digipot_motor_current[i]);
  44. }
  45. }
  46. #endif