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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * adc_mcp3426.cpp - library for MicroChip MCP3426 I2C A/D converter
  24. *
  25. * For implementation details, please take a look at the datasheet:
  26. * https://www.microchip.com/en-us/product/MCP3426
  27. */
  28. #include "../../inc/MarlinConfig.h"
  29. #if ENABLED(HAS_MCP3426_ADC)
  30. #include "adc_mcp3426.h"
  31. // Read the ADC value from MCP342X on a specific channel
  32. int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain, uint8_t address) {
  33. Error = false;
  34. #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
  35. Wire.setSDA(pin_t(I2C_SDA_PIN));
  36. Wire.setSCL(pin_t(I2C_SCL_PIN));
  37. #endif
  38. Wire.begin(); // No address joins the BUS as the master
  39. Wire.beginTransmission(I2C_ADDRESS(address));
  40. // Continuous Conversion Mode, 16 bit, Channel 1, Gain x4
  41. // 26 = 0b00011000
  42. // RXXCSSGG
  43. // R = Ready Bit
  44. // XX = Channel (00=1, 01=2, 10=3 (MCP3428), 11=4 (MCP3428))
  45. // C = Conversion Mode Bit (1= Continuous Conversion Mode (Default))
  46. // SS = Sample rate, 10=15 samples per second @ 16 bits
  47. // GG = Gain 00 =x1
  48. uint8_t controlRegister = 0b00011000;
  49. if (channel == 2) controlRegister |= 0b00100000; // Select channel 2
  50. if (gain == 2)
  51. controlRegister |= 0b00000001;
  52. else if (gain == 4)
  53. controlRegister |= 0b00000010;
  54. else if (gain == 8)
  55. controlRegister |= 0b00000011;
  56. Wire.write(controlRegister);
  57. if (Wire.endTransmission() != 0) {
  58. Error = true;
  59. return 0;
  60. }
  61. const uint8_t len = 3;
  62. uint8_t buffer[len] = {};
  63. do {
  64. Wire.requestFrom(I2C_ADDRESS(address), len);
  65. if (Wire.available() != len) {
  66. Error = true;
  67. return 0;
  68. }
  69. for (uint8_t i = 0; i < len; ++i)
  70. buffer[i] = Wire.read();
  71. // Is conversion ready, if not loop around again
  72. } while ((buffer[2] & 0x80) != 0);
  73. union TwoBytesToInt16 {
  74. uint8_t bytes[2];
  75. int16_t integervalue;
  76. };
  77. TwoBytesToInt16 ConversionUnion;
  78. ConversionUnion.bytes[1] = buffer[0];
  79. ConversionUnion.bytes[0] = buffer[1];
  80. return ConversionUnion.integervalue;
  81. }
  82. MCP3426 mcp3426;
  83. #endif // HAS_MCP3426_ADC