Other Parts Discussed in Thread: ENERGIA
Hello. I know, that this should better be posted on 43oh.com forum, but I got no answer from there.
I have wrote a simple Energia program for digital potentiometer control with incremental rotary encoder. They are connected to MSP430G2553 launchpad.
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 it.
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));
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);
}
Thank you in advance,
Superpanky