testPH.ino 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <Wire.h>
  2. //#include "../MCP3428/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 = 0;
  18. //for (i =0; i<4;i++){
  19. float tension = MCP3428_Read(i);
  20. Serial.print(" ");
  21. Serial.print(i);
  22. Serial.print(" Tension: ");
  23. Serial.print(tension, 6); // Affiche la tension avec 3 décimales
  24. Serial.println(" V");
  25. delay(10000); // Attendre 1 seconde avant la prochaine lecture
  26. }
  27. byte getChannelConfigByte(int channel) {
  28. byte configByte = 0x88; // Configuration par défaut pour le canal 1
  29. if (channel == 1) {
  30. configByte = 0xA8;
  31. } else if (channel == 2) {
  32. configByte = 0xC8;
  33. } else if (channel == 3) {
  34. configByte = 0xE8;
  35. }
  36. return configByte;
  37. }
  38. float MCP3428_Read(uint8_t channel) {
  39. Wire.beginTransmission(MCP3428_ADDRESS);
  40. Wire.write(getChannelConfigByte(channel));
  41. Wire.endTransmission();
  42. delay(200);
  43. byte config,byte1,byte2;
  44. while(1){
  45. Wire.requestFrom(MCP3428_ADDRESS, 3);
  46. byte2 = Wire.read();
  47. byte1 = Wire.read();
  48. config = Wire.read();
  49. if(config>>8==0) //tant qu'il a pas fini
  50. break;
  51. }
  52. int value = ((byte2) << 8) | byte1;
  53. return ((float)value * 62.5)/1000000;
  54. }