testTemp.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <Wire.h>
  2. //#include "MCP3428.h"
  3. #define MCP3428_ADDRESS 0x68 // Adresse I2C du MCP3428
  4. #define VREFMCP 0.0000625// Tension de référence en volt
  5. // Initialisation
  6. void setup() {
  7. Serial.begin(9600);
  8. Wire.begin();
  9. Wire.requestFrom(MCP3428_ADDRESS, (uint8_t)1);
  10. if (!Wire.available()) {
  11. Serial.print("No device found at address ");
  12. while (1)
  13. ;
  14. }
  15. }
  16. void loop() {
  17. int i = 2;
  18. float tension = MCP3428_Read(i);
  19. Serial.print(" ");
  20. Serial.print(i);
  21. Serial.print(" Tension: ");
  22. Serial.print(tension, 6); // Affiche la tension avec 3 décimales
  23. Serial.println(" V");
  24. delay(1000);
  25. }
  26. byte getChannelConfigByte(int channel) {
  27. byte configByte = 0x88; // Configuration par défaut pour le canal 1
  28. if (channel == 1) {
  29. configByte = 0xA8;
  30. } else if (channel == 2) {
  31. configByte = 0xC8;
  32. } else if (channel == 3) {
  33. configByte = 0xE8;
  34. }
  35. return configByte;
  36. }
  37. float MCP3428_Read(uint8_t channel) {
  38. Wire.beginTransmission(MCP3428_ADDRESS);
  39. Wire.write(getChannelConfigByte(channel));
  40. Wire.endTransmission();
  41. delay(200);
  42. byte config,byte1,byte2;
  43. while(1){
  44. Wire.requestFrom(MCP3428_ADDRESS, 3);
  45. byte2 = Wire.read();
  46. byte1 = Wire.read();
  47. config = Wire.read();
  48. if(config>>8==0) //tant qu'il a pas fini
  49. break;
  50. }
  51. int value = ((byte2) << 8) | byte1;
  52. return ((float)value * 62.5)/1000000;
  53. }