Hi everyone and sorry for my poor English. I'm working on a prototype to measure very high currents (until 80kA) through a shunt. The pulse can be very short, so I've decided to use a 12bits ADC, faster than the Arduino.
I can get the ADC working, but I have a problem ;
byte i=0; //compteur
//stockage des données
int courant[250];
int tension[250];
//valeurs maximum
int i_courant_max;
int i_courant_min;
int i_tension_max;
int i_tension_min;
float t_courant_max;
//octets provisoires
byte courant_low;
byte courant_high;
byte tension_low;
byte tension_high;
void setup() {
//initialisation de la communication série
Serial.begin(115200);
//déclaration des entrées/sorties
//entrées du CAN
pinMode(0,INPUT); //poids faible
pinMode(1,INPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT); //poids fort
//pilotage du CAN
pinMode(10,OUTPUT); //Startconv, PORTB2
pinMode(9,OUTPUT); //CS, PORTB1
pinMode(8,OUTPUT); //RD, PORTB0
}
void loop() {
i = 0;
do{
//boucle d'acquisition
bitSet(PORTB,2); //génération d'un front montant sur startconv
///////////////////////////////////////////////////////////////
//opérations durant le temps de conversion (1,45µs)
bitClear(PORTB,2); ////réinitialisation de la demande d'acquisition
bitClear(PORTB,1); //changement d'adc
bitSet(PORTB,0); //réinitialisation de la demande lecture
tension[i] = word(tension_high,tension_low);
courant[i] = word(courant_high,courant_low);
////////////////////////////////////////////////////////////////
bitClear(PORTB,0); //demande de lecture
tension_low = PIND;
tension_high = PINC;
bitSet(PORTB,1); //changement d'adc
courant_low = PIND;
courant_high = PINC;
}while(i++ < 250);
Serial.print(tension[40]/2048.0*5.0);
Serial.print(" V\n");
Serial.print(tension[40]);
Serial.print("\n");
Serial.print(t_courant_max);
Serial.print(" µs\n\n");
delay(1000);
}
Here is my code. If I comment the 6 lines "Serial.print()", my WHILE loop is done in 2µs ; very good, that's the speed I need!
If I uncomment the 6 lines "Serial.print()", my WHILE loop slow down, about 2.8 or 3µs if I remember well.
And If I add a function to find the maximum value, it's worse. I've looked at that with my oscilloscope.
I don't understand because this code is not INSIDE my loop. I don't understand how it can slow down my loop (I need it to be FAST). I've tried to stop the interruptions during the loop, but nothing changes.
My purpose is to send the datas to a PLC with MODBUS TCP after that, so if my loop slow down each time I add a line ![]()
What's wrong with my work? Would you help me?
Thank you very much!
Tristan.




