testSalinite.ino 1.5 KB

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