Jump to content

Recommended Posts

Hello.
I have wrote an Energia program for digital potentiometer control. I want to save variable "counter" (it is an integer number from 0-255) in non-volatile memory using mspflash.h library in case of reset or power interrupt. But it doesn't saves.
I am allmost sure there is something wrong with initialization of variables or pointers inside my code. Can anyone help? Any improvements in code?

My code:

#include <SPI.h>
#include "MspFlash.h"

#define outputA P2_1
#define outputB P2_2

#define flash SEGMENT_D

 int aState;
 int aLastState;  

 const int slaveSelectPin = SS;
 const int shutDownPin = P1_4;

int pos=0;
int counter=0;
int p=0;

void setup() {

    pinMode (outputA,INPUT_PULLUP);
    pinMode (outputB,INPUT_PULLUP);
    aLastState = digitalRead(outputA);
 
  pinMode (slaveSelectPin, OUTPUT);
  pinMode (shutDownPin, OUTPUT);
    SPI.begin();
  digitalWrite(shutDownPin, HIGH);
  digitalPotWrite(1, 0);
}

void loop() {
 
Flash.read(flash+(pos * sizeof(int)), (unsigned char*)&p, sizeof(int));

counter=p;

      aState = digitalRead(outputA);
   if (aState != aLastState){     
     if (digitalRead(outputB) != aState) {
        counter=++counter;
     } else {
        counter=--counter;
     }
   }
   if (counter <= 255) {
   if (counter < 0) {
   digitalPotWrite(1, 0);
   counter = 0;
   Flash.erase(flash);
   Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));
   }
   else {
   digitalPotWrite(1, counter);
   Flash.erase(flash);
   Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));
   }
   }
    if (counter > 255) {    
   digitalPotWrite(1, 255);
   counter = 255;
   Flash.erase(flash);
   Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));  
   }
       aLastState = aState;
}
int digitalPotWrite(int address, int value) {
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(address);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin,HIGH);
}

 

Regards, Superpanky

 

Share this post


Link to post
Share on other sites

Maybe someone can try to tell, what could be wrong in these few lines? I don't understand, how these constructions with pointers and "unsigned char/int" datatypes do work.


int pos=0;
int counter=0;
int p=0;

void loop() {

            Flash.read(flash+(pos * sizeof(int)), (unsigned char*)&p, sizeof(int));

counter=p;

                        if (.............) {
                          counter=++counter;
                          } else {
                           counter=--counter;

                         }

             Flash.erase(flash);

             Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));

}

 

Regards, Superpanky

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...