Friday, 30 September 2022

Storing the long number(16bits / 32bits) in external flash of arduino or STM32 .

 #include <Wire.h>

#define flashaddress 0x50

int liters = 10058 , add1=10 , add2,time1=55; // value of liters save in flash

byte data , data1;

String liters1 = String(liters); // convert int into string

int result ;    // final value read from flash 

String flashmessage = ""; // convert bytes into string from flash 

char flashincoming; // read each byte from flash

String temperatureString = "";


unsigned int len = liters1.length() + 1; // lenght of string

void setup() {

 Wire.begin();

 Serial.begin(9600);

 Flashwrite();

  

}


void loop() {

   Flashread();

    delay(500);

}





void Flashwrite(){

  byte buff[len];

  Wire.beginTransmission(flashaddress);

  Wire.write(0x00);      //First Word Address

  Wire.write(0x00);      //Second Word Address

  liters1.getBytes(buff, len);  // read total bytes of liters1


for (int i = 0; i < len; i++)

{  Wire.write(buff[i]);

    delay(20);//Write value byte by byte 

  Serial.println(buff[i]);

}



 delay(10);

 Wire.endTransmission();

 delay(10);

  

  

  

  }



void Flashread(){

  Wire.beginTransmission(flashaddress);

 Wire.write(0x00);

 Wire.write(0x00);

 Wire.endTransmission();

 delay(10);


 Wire.requestFrom(flashaddress, len+1);

 delay(10);

  flashmessage = "";

 for(byte i=0; i<len; i++)      //Read 26 data bytes

 {

    flashincoming= Wire.read();

    delay(10);

    flashmessage += String(flashincoming); //convert flash data into string

     

   

  

  

 }

 delay(100);

  Serial.println(flashmessage);

   result = flashmessage.toInt() ;// convert string into int

   if(result==liters){Serial.println("equal");} // if result equal to liters 

   flashmessage = "";

   while(1){

     result++;

     Serial.println(result);

     delay(300);

    }

  

  }  

No comments:

Post a Comment

Storing the long number(16bits / 32bits) in external flash of arduino or STM32 .

 #include <Wire.h> #define flashaddress 0x50 int liters = 10058 , add1=10 , add2,time1=55; // value of liters save in flash byte data ...