| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include <Wire.h>
- //#include "MCP3428.h"
- #define MCP3428_ADDRESS 0x68 // Adresse I2C du MCP3428
- #define VREFMCP 0.0000625// Tension de référence en volt
- // Initialisation
- void setup() {
- Serial.begin(9600);
- Wire.begin();
- Wire.requestFrom(MCP3428_ADDRESS, (uint8_t)1);
- if (!Wire.available()) {
- Serial.print("No device found at address ");
- while (1)
- ;
- }
- }
- void loop() {
- int i = 2;
- float tension = MCP3428_Read(i);
- Serial.print(" ");
- Serial.print(i);
- Serial.print(" Tension: ");
- Serial.print(tension, 6); // Affiche la tension avec 3 décimales
- Serial.println(" V");
- delay(1000);
- }
- byte getChannelConfigByte(int channel) {
- byte configByte = 0x88; // Configuration par défaut pour le canal 1
- if (channel == 1) {
- configByte = 0xA8;
- } else if (channel == 2) {
- configByte = 0xC8;
- } else if (channel == 3) {
- configByte = 0xE8;
- }
- return configByte;
- }
- float MCP3428_Read(uint8_t channel) {
-
- Wire.beginTransmission(MCP3428_ADDRESS);
- Wire.write(getChannelConfigByte(channel));
- Wire.endTransmission();
-
- delay(200);
- byte config,byte1,byte2;
- while(1){
- Wire.requestFrom(MCP3428_ADDRESS, 3);
- byte2 = Wire.read();
- byte1 = Wire.read();
- config = Wire.read();
- if(config>>8==0) //tant qu'il a pas fini
- break;
- }
- int value = ((byte2) << 8) | byte1;
- return ((float)value * 62.5)/1000000;
- }
|