The Arduino sketch contains the usual Arduino standard functions setup() and loop(). SW V1 sketch can be downloaded here: efi_davide_20160131_v1.
The function setup() is run when the microcontroller turns ON. In the code, the following instructions are executed:
- Timer1.initialize() initializes the timer object, which is used to handle the injection extra time. Timer1 is declared in the Tempo library, which can be downloaded here: tempo_library_v1.
- With pinMode() function, the status of the digital input (IN_INJ_PIN=2) and output (OUT_INJ_PIN=12) is set.
- Serial1.begin(9600) is used to initialize the status of the serial connection, which is used to communicate with the data logger/service tool, and with the PC. Arduino Micro Serial1 port is used for this feature.
- EEPROM_check() has the purpose of checking if the information written in the EEPROM is correct. In case of corruption, standard values are written in the EEPROM.
- attachInterrupt() is used to attach an interrupt on digital input pin 2, which is the one connected to the Honda CBR125R original ECU. This input is used to read the status of the signal which the original ECU wants to output to the fuel injector.
- Initialization of the global variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void setup() { Timer1.initialize(); pinMode(IN_INJ_PIN, INPUT); pinMode(OUT_INJ_PIN, OUTPUT); digitalWrite(OUT_INJ_PIN, LOW); Serial1.begin(9600); while (!Serial1) { // wait for serial port to connect. Needed for native USB } EEPROM_check(); attachInterrupt(1, signal_change, CHANGE); // interrupt sul pin 2 status_inj_in=0; // stato ingresso iniettore inj_start=0; // tempo inizio iniezione attuale inj_start_prev=0; // tempo inizio iniezione precedente delta_time=0; // differenza temporale tra due iniezioni (2rpm), attuale meno precedente delta_inj=0; extension_time=0; prev_time_val=0; // validita (non valido perche non ho ancora letto almeno un campione) perc_inc=0; // percentuale incremento rpm=0; //rpm calcolati motore throttle=0; //throttle voltage serial_byte_cnt=0; //conto caratteri ricevuti via seriale delta_time_buffer=0; // distanza tra due iniezioni (2rpm) buffer delta_inj_buffer=0; // tempo di iniezione input buffer throttle_buffer=0; // tensione del throttle pin 0-1023 buffer buffer_busy=0; } |
The function loop() is run continuously, until the Arduino power supply is cut or the microcontroller is reset. The function can be divided in the following parts:
- Serial communication buffer check, to verify if any message character has been received on Serial1.
- in case a request for sending info (rpm, injection time, throttle position, and so on) has been received via Serial1, the ECU replies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
void loop() { // SERIAL BYTES RECEIVED ARE MANAGED BY THE FOLLOWING LOGIC serial_byte_cnt=0; while (Serial1.available()) { // carattere ricevuto serial_byte_cnt++; if (serial_byte_cnt>=9){ serial_byte_cnt=9; //not valid number (9 bytes is not acceptable) }else{ // meno di 9 caratteri, quindi salvo nel buffer serial_inbyte[serial_byte_cnt-1]=Serial1.read(); //salvo nel buffer in posizione 0-7 (8 bytes in totale) } delay(10); // dai il tempo di arrivare al carattere successivo in seriale, qualora ci sia (per evitare comandi troncati a meta' tra un ciclo e successivo) } if (serial_byte_cnt==8){ // 8 bytes received by the service tool / logger if (serial_inbyte[0]=='w' || serial_inbyte[0]=='r' || serial_inbyte[0]=='d'){ evaluate_parameter_read_writing_request(); } } // IN CASE THE SERVICE TOOL / LOGGER REQUESTED A SNAPSHOT, THE ECU REPLIES if (data_send_req==1) { // in case the service tool / logger has sent a snapshot request (data "d") buffer_busy=1; // buffer busy flag activated, so it cannot be written during this time serialdataString[0]='d'; // 1 caratteri "d" = data serialdataString[1]=14; // 18 = 18 bytes consecutivi serialdataString[2]= (byte)(delta_time_buffer & 0xff); serialdataString[3]= (byte)((delta_time_buffer >> 8) & 0xff); serialdataString[4]= (byte)((delta_time_buffer >> 16) & 0xff); serialdataString[5]= (byte)((delta_time_buffer >> 24) & 0xff); // 4 caratteri serialdataString[6]= (byte)(delta_inj_buffer & 0xff); serialdataString[7]= (byte)((delta_inj_buffer >> 8) & 0xff); serialdataString[8]= (byte)((delta_inj_buffer >> 16) & 0xff); serialdataString[9]= (byte)((delta_inj_buffer >> 24) & 0xff); // 4 caratteri serialdataString[10]= (byte)(throttle_buffer & 0xff); serialdataString[11]= (byte)((throttle_buffer >> 8) & 0xff); // 2 caratteri unsigned long crc_somma; crc_somma=delta_time_buffer+delta_inj_buffer+throttle_buffer; serialdataString[12]= (byte )(crc_somma & 0xff); serialdataString[13]= (byte)((crc_somma >> 8) & 0xff); serialdataString[14]= (byte)((crc_somma >> 16) & 0xff); serialdataString[15]= (byte)((crc_somma >> 24) & 0xff); // 4 caratteri Serial1.write(serialdataString, 16); data_send_req=0; // resets the sending request buffer_busy=0; // reset the buffer busy flag, so that the it can be updated } delay(10); // delay ms } |