MCP3428.c 868 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "MCP3428.h"
  2. byte getChannelConfigByte(int channel) {
  3. byte configByte = 0x88; // Configuration par défaut pour le canal 1
  4. if (channel == 1) {
  5. configByte = 0xA8;
  6. } else if (channel == 2) {
  7. configByte = 0xC8;
  8. } else if (channel == 3) {
  9. configByte = 0xE8;
  10. }
  11. return configByte;
  12. }
  13. float MCP3428_Read(uint8_t channel) {
  14. Wire.beginTransmission(MCP3428_ADDRESS);
  15. Wire.write(getChannelConfigByte(channel));
  16. Wire.endTransmission();
  17. delay(200);
  18. byte config,byte1,byte2;
  19. while(1){
  20. Wire.requestFrom(MCP3428_ADDRESS, 3);
  21. byte2 = Wire.read();
  22. byte1 = Wire.read();
  23. config = Wire.read();
  24. if(config>>8==0) //tant qu'il a pas fini
  25. break;
  26. }
  27. int value = ((byte2) << 8) | byte1;
  28. return ((float)value * 62.5)/1000000;
  29. }