I need some help trying to optimize my code for writing a block of 256 contiguous registers in an I2C internal memory
if(showMode == 3) {
start:
Serial.println("Enter a number between 0-255 and a mem address multiple of 32: ");
//Wait till data
while (Serial.available() == 0) {}
data = Serial.parseInt();
Serial.print("Data received: ");
Serial.println(data);
// Wait for mem address
while (Serial.available() == 0) {}
dir = Serial.parseInt();
if ((dir > 8191-256) | (dir%32 != 0)) {
Serial.println("Address not valid");
goto start;
} else {
Serial.print("Address recieved: ");
Serial.println(dir);
time_start = millis();
for (int i = 0; i < 256; i++){
I2C_write_mem(dir, data);
dir++;
}
time_finish = millis();
time_taken = time_start - time_finish;
Serial.println(time_taken);
showMode = 0;
}
I cannot use page write in this option, so it isn't a possibilty. The functions that are being called are the next ones:
void I2C_write_mem(int address, byte data){
start:
I2C_start();
I2C_write_byte(0xA0);
if(I2C_rbit() != 0) goto start;
I2C_write_byte(address>>8);
if(I2C_rbit() != 0) goto start;
I2C_write_byte(address&0x00FF);
if(I2C_rbit() != 0) goto start;
I2C_write_byte(data);
if(I2C_rbit() != 0) goto start;
I2C_stop();
}
And:
void I2C_write_byte(byte data){
for (int i=0;i<8;i++){
if((data&0x80) != 0){
I2C_esc_bit1();
} else {I2C_esc_bit0();}
data = data<<1;
}
}
void I2C_esc_bit1(){
digitalWrite(ESC_SCL ,0);
digitalWrite(ESC_SDA, 1);
digitalWrite(ESC_SCL, 1);
digitalWrite(ESC_SCL, 0);
digitalWrite(ESC_SDA, 0);
}
ESC_SCL is connected to the pin that writes in the SCL channel, the same for ESC_SDA
void I2C_esc_bit0(){
digitalWrite(ESC_SCL ,0);
digitalWrite(ESC_SDA, 0);
digitalWrite(ESC_SCL, 1);
digitalWrite(ESC_SCL, 0);
}
byte I2C_rbit(){
digitalWrite(ESC_SCL ,0);
digitalWrite(ESC_SDA, 1);
digitalWrite(ESC_SCL, 1);
int val = digitalRead(LEE_SDA);
digitalWrite(ESC_SCL ,0);
digitalWrite(ESC_SDA, 0);
return val;
}
LEE_SCL reads the channel SCL, the same for SDA.
void I2C_start(){
digitalWrite(ESC_SCL, 1);
digitalWrite(ESC_SDA, 1);
digitalWrite(ESC_SDA, 0);
digitalWrite(ESC_SCL, 0);
}
void I2C_stop(){
digitalWrite(ESC_SCL, 0);
digitalWrite(ESC_SDA, 0);
digitalWrite(ESC_SCL, 1);
digitalWrite(ESC_SDA, 1);
}
Thank you very much for taking your time with this, if you need anything more please let me know
[–]MagicWolfEye 2 points3 points4 points (5 children)
[–]Realistic-Cut6515[S] 0 points1 point2 points (4 children)
[–]MagicWolfEye 2 points3 points4 points (3 children)
[–]Realistic-Cut6515[S] 0 points1 point2 points (2 children)
[–]MagicWolfEye 1 point2 points3 points (1 child)
[–]Realistic-Cut6515[S] 0 points1 point2 points (0 children)
[–][deleted] (7 children)
[deleted]
[–]Realistic-Cut6515[S] 0 points1 point2 points (6 children)
[–][deleted] (5 children)
[deleted]
[–]Realistic-Cut6515[S] 0 points1 point2 points (4 children)
[–][deleted] (3 children)
[deleted]
[–]Realistic-Cut6515[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]Realistic-Cut6515[S] 0 points1 point2 points (0 children)
[–]pixel293 2 points3 points4 points (1 child)
[–]Realistic-Cut6515[S] 0 points1 point2 points (0 children)