msp430 flash write

Thread Starter

mihaita212

Joined Mar 26, 2017
10
Hello guys i'm working on an application where i need to save 2 float numbers in flash memory (i need them when the msp lose power) ,i have the hardware part done but i can't figure out the coding i'm very new to programing.

I need to do something like this.
Code:
int sensorPin = 23; 
int sensorValue = 0;
float volt;

// loop part
sensorValue = analogRead(sensorPin);
unsigned long currentMillis = millis();
timp=currentMillis/1000;

if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;  
         volt=sensorValue;
         volt=(volt*3.3)/4096;
         volt=volt*8.51;
        putere1=volt*amp1;
        total_putere1=putere1+total_putere1;
}
     media_puteri1=total_putere1/timp;
      energie1=media_puteri1*timp/3600;
I need to save "energie1" to flash and whe my msp lose power the value stays on and then when i power it back my "energie1" value goes something like this energie1=(media_puteri1*timp/3600)+flash_value;

PS: i read the ENERGIA flash write/read exemple but i don't understand how to save float values;
 

MrChips

Joined Oct 2, 2009
30,712
What is the size of float? ( 32 bits or 4 bytes is typical).

There are a number of ways to do this. But you need to tell us what programming platform and compiler you are using.

Here are some methods:

1. Use fixed absolute address so that you know where the bytes are stored.
2. Use a union so that you can access individual bytes.
3. Use a pointer

Here is an example using a pointer.

Code:
float volt;
unsigned char *baddress;
unsigned char b1, b2, b3, b4;

baddress = (unsigned char *)&volt;  // get the address of volt
// get the four bytes of volt
b1 = *baddress++;
b2 = *baddress++;
b3 = *baddress++;
b4 = *baddress;
 

MrChips

Joined Oct 2, 2009
30,712
Declaring the variable as a union is the most straight forward:
Code:
unsigned char b1, b2, b3, b4;

union
{
  float value;
  unsigned b[4];
}volt;

// volt.value and volt.b[ ] occupy the same memory locations

volt.value = 1.0;  //set value as floating point
b1 = volt.b[0] // access first byte of volt
b4 = volt.b[3] // access last byte of volt
 

Thread Starter

mihaita212

Joined Mar 26, 2017
10
What is the size of float? ( 32 bits or 4 bytes is typical).

There are a number of ways to do this. But you need to tell us what programming platform and compiler you are using.

Here are some methods:

1. Use fixed absolute address so that you know where the bytes are stored.
2. Use a union so that you can access individual bytes.
3. Use a pointer

Here is an example using a pointer.

Code:
float volt;
unsigned char *baddress;
unsigned char b1, b2, b3, b4;

baddress = (unsigned char *)&volt;  // get the address of volt
// get the four bytes of volt
b1 = *baddress++;
b2 = *baddress++;
b3 = *baddress++;
b4 = *baddress;

I'm using ENERGIA compiler , msp430 have 4 segments in flash A B C and D and we can write in 3 B C D here is an exemple:
Code:
#include "MspFlash.h"

#define flash SEGMENT_D

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
   doErase(); 
   delay(5000);
   doWrite();
   delay(5000);
    doRead();
   delay(5000);
}
void doRead()
{
  unsigned char p = 0;
}
void doErase()
{
Serial.println("Erase");
Flash.erase(flash);
Serial.println("Done.");
}

void doWrite()
{
Serial.println("Write");
Flash.write(flash, (unsigned char*) "Hello World!" ,13);
Serial.println("Done.");
}
I understand i need to Erase first the memory segment then to write it , but i dont know how to return the float value that is in my program i have the value is called VOLT i want to save it in flash the make some matematic operations with it.I'm new to programing
 
Top